blob: d928cef9b4ec4330ce8ad3601ef85a71535b8c28 [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 */
Christopher Faulet666a0c42019-01-08 11:12:04 +010047#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
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 Fauletb8093cf2019-01-03 16:27:28 +010085 struct task *task; /* timeout management task */
86 int timeout; /* idle timeout duration in ticks */
87 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020088};
89
90/* H1 stream descriptor */
91struct h1s {
92 struct h1c *h1c;
93 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010094 struct cs_info csinfo; /* CS info, only used for client connections */
95 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020096
Christopher Faulet51dbc942018-09-13 09:05:15 +020097 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
98 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020099
Olivier Houchardf502aca2018-12-14 19:42:40 +0100100 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200101 struct h1m req;
102 struct h1m res;
103
104 enum http_meth_t meth; /* HTTP resquest method */
105 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200106};
107
108/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100109DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
110DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200111
Christopher Faulet51dbc942018-09-13 09:05:15 +0200112static int h1_recv(struct h1c *h1c);
113static int h1_send(struct h1c *h1c);
114static int h1_process(struct h1c *h1c);
115static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100116static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100117static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200118
119/*****************************************************/
120/* functions below are for dynamic buffer management */
121/*****************************************************/
122/*
123 * Indicates whether or not the we may call the h1_recv() function to
124 * attempt to receive data into the buffer and/or parse pending data. The
125 * condition is a bit complex due to some API limits for now. The rules are the
126 * following :
127 * - if an error or a shutdown was detected on the connection and the buffer
128 * is empty, we must not attempt to receive
129 * - if the input buffer failed to be allocated, we must not try to receive
130 * and we know there is nothing pending
131 * - if no flag indicates a blocking condition, we may attempt to receive,
132 * regardless of whether the input buffer is full or not, so that only de
133 * receiving part decides whether or not to block. This is needed because
134 * the connection API indeed prevents us from re-enabling receipt that is
135 * already enabled in a polled state, so we must always immediately stop as
136 * soon as the mux can't proceed so as never to hit an end of read with data
137 * pending in the buffers.
138 * - otherwise must may not attempt to receive
139 */
140static inline int h1_recv_allowed(const struct h1c *h1c)
141{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100142 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200143 return 0;
144
Christopher Fauletcf56b992018-12-11 16:12:31 +0100145 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
146 return 0;
147
Christopher Fauletcb55f482018-12-10 11:56:47 +0100148 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200149 return 1;
150
151 return 0;
152}
153
154/*
155 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
156 * flags are used to figure what buffer was requested. It returns 1 if the
157 * allocation succeeds, in which case the connection is woken up, or 0 if it's
158 * impossible to wake up and we prefer to be woken up later.
159 */
160static int h1_buf_available(void *target)
161{
162 struct h1c *h1c = target;
163
164 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
165 h1c->flags &= ~H1C_F_IN_ALLOC;
166 if (h1_recv_allowed(h1c))
167 tasklet_wakeup(h1c->wait_event.task);
168 return 1;
169 }
170
171 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
172 h1c->flags &= ~H1C_F_OUT_ALLOC;
173 tasklet_wakeup(h1c->wait_event.task);
174 return 1;
175 }
176
Christopher Faulet51dbc942018-09-13 09:05:15 +0200177 return 0;
178}
179
180/*
181 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
182 */
183static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
184{
185 struct buffer *buf = NULL;
186
187 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
188 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
189 h1c->buf_wait.target = h1c;
190 h1c->buf_wait.wakeup_cb = h1_buf_available;
191 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
192 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
193 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
194 __conn_xprt_stop_recv(h1c->conn);
195 }
196 return buf;
197}
198
199/*
200 * Release a buffer, if any, and try to wake up entities waiting in the buffer
201 * wait queue.
202 */
203static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
204{
205 if (bptr->size) {
206 b_free(bptr);
207 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
208 }
209}
210
211static int h1_avail_streams(struct connection *conn)
212{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100213 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200214
215 return h1c->h1s ? 0 : 1;
216}
217
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100218static int h1_max_streams(struct connection *conn)
219{
220 return 1;
221}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200222
223/*****************************************************************/
224/* functions below are dedicated to the mux setup and management */
225/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100226static struct conn_stream *h1s_new_cs(struct h1s *h1s)
227{
228 struct conn_stream *cs;
229
230 cs = cs_new(h1s->h1c->conn);
231 if (!cs)
232 goto err;
233 h1s->cs = cs;
234 cs->ctx = h1s;
235
236 if (h1s->flags & H1S_F_NOT_FIRST)
237 cs->flags |= CS_FL_NOT_FIRST;
238
239 if (stream_create_from_cs(cs) < 0)
240 goto err;
241 return cs;
242
243 err:
244 cs_free(cs);
245 h1s->cs = NULL;
246 return NULL;
247}
248
Olivier Houchardf502aca2018-12-14 19:42:40 +0100249static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200250{
251 struct h1s *h1s;
252
253 h1s = pool_alloc(pool_head_h1s);
254 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100255 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200256
257 h1s->h1c = h1c;
258 h1c->h1s = h1s;
259
Olivier Houchardf502aca2018-12-14 19:42:40 +0100260 h1s->sess = sess;
261
Christopher Faulet51dbc942018-09-13 09:05:15 +0200262 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200263 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200264
265 h1s->recv_wait = NULL;
266 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200267
268 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200269 h1s->req.flags |= H1_MF_NO_PHDR;
270
Christopher Faulet129817b2018-09-20 16:14:40 +0200271 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200272 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200273
274 h1s->status = 0;
275 h1s->meth = HTTP_METH_OTHER;
276
Christopher Faulet47365272018-10-31 17:40:50 +0100277 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
278 h1s->flags |= H1S_F_NOT_FIRST;
279 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
280
Christopher Faulet129817b2018-09-20 16:14:40 +0200281 if (!conn_is_back(h1c->conn)) {
282 if (h1c->px->options2 & PR_O2_REQBUG_OK)
283 h1s->req.err_pos = -1;
284 }
285 else {
286 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
287 h1s->res.err_pos = -1;
288 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200289
Christopher Faulet539e0292018-11-19 10:40:09 +0100290 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
291 * create a new one.
292 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200293 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100294 h1s->csinfo.create_date = date;
295 h1s->csinfo.tv_create = now;
296 h1s->csinfo.t_handshake = 0;
297 h1s->csinfo.t_idle = -1;
298
Christopher Fauletf2824e62018-10-01 12:12:37 +0200299 cs->ctx = h1s;
300 h1s->cs = cs;
301 }
Christopher Faulet47365272018-10-31 17:40:50 +0100302 else {
Olivier Houchardf502aca2018-12-14 19:42:40 +0100303 /* For frontend connections we should always have a session */
304 sess = h1c->conn->owner;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100305
306 h1s->csinfo.create_date = sess->accept_date;
307 h1s->csinfo.tv_create = sess->tv_accept;
308 h1s->csinfo.t_handshake = sess->t_handshake;
309 h1s->csinfo.t_idle = -1;
310
Christopher Faulet47365272018-10-31 17:40:50 +0100311 cs = h1s_new_cs(h1s);
312 if (!cs)
313 goto fail;
314 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200315 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100316
317 fail:
318 pool_free(pool_head_h1s, h1s);
319 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200320}
321
322static void h1s_destroy(struct h1s *h1s)
323{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200324 if (h1s) {
325 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200326
Christopher Fauletf2824e62018-10-01 12:12:37 +0200327 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200328
Christopher Fauletf2824e62018-10-01 12:12:37 +0200329 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100330 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200331 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100332 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200333
Christopher Fauletcb55f482018-12-10 11:56:47 +0100334 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100335 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
336 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
337 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200338
Christopher Fauletf2824e62018-10-01 12:12:37 +0200339 cs_free(h1s->cs);
340 pool_free(pool_head_h1s, h1s);
341 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200342}
343
Christopher Fauletfeb11742018-11-29 15:12:34 +0100344static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
345{
346 struct h1s *h1s = cs->ctx;
347
348 if (h1s && !conn_is_back(cs->conn))
349 return &h1s->csinfo;
350 return NULL;
351}
352
Christopher Faulet51dbc942018-09-13 09:05:15 +0200353/*
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100354 * Initialize the mux once it's attached. It is expected that conn->ctx
Christopher Faulet51dbc942018-09-13 09:05:15 +0200355 * points to the existing conn_stream (for outgoing connections) or NULL (for
356 * incoming ones). Returns < 0 on error.
357 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100358static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200359{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200360 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100361 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200362
363 h1c = pool_alloc(pool_head_h1c);
364 if (!h1c)
365 goto fail_h1c;
366 h1c->conn = conn;
367 h1c->px = proxy;
368
369 h1c->flags = H1C_F_NONE;
370 h1c->ibuf = BUF_NULL;
371 h1c->obuf = BUF_NULL;
372 h1c->h1s = NULL;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100373 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200374
Christopher Faulet51dbc942018-09-13 09:05:15 +0200375 LIST_INIT(&h1c->buf_wait.list);
376 h1c->wait_event.task = tasklet_new();
377 if (!h1c->wait_event.task)
378 goto fail;
379 h1c->wait_event.task->process = h1_io_cb;
380 h1c->wait_event.task->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100381 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200382
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100383 if (conn->ctx) {
384 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
385 if (tick_isset(proxy->timeout.serverfin))
386 h1c->shut_timeout = proxy->timeout.serverfin;
387 } else {
388 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
389 if (tick_isset(proxy->timeout.clientfin))
390 h1c->shut_timeout = proxy->timeout.clientfin;
391 }
392 if (tick_isset(h1c->timeout)) {
393 t = task_new(tid_bit);
394 if (!t)
395 goto fail;
396
397 h1c->task = t;
398 t->process = h1_timeout_task;
399 t->context = h1c;
400 t->expire = tick_add(now_ms, h1c->timeout);
401 }
402
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200403 if (!(conn->flags & CO_FL_CONNECTED))
404 h1c->flags |= H1C_F_CS_WAIT_CONN;
405
Christopher Fauletf2824e62018-10-01 12:12:37 +0200406 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100407 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200408 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200409
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100410 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200411
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100412
413 if (t)
414 task_queue(t);
415
Christopher Faulet51dbc942018-09-13 09:05:15 +0200416 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard9b960a82019-01-04 16:51:40 +0100417 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200418
419 /* mux->wake will be called soon to complete the operation */
420 return 0;
421
422 fail:
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100423 if (t)
424 task_free(t);
Christopher Faulet47365272018-10-31 17:40:50 +0100425 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200426 tasklet_free(h1c->wait_event.task);
427 pool_free(pool_head_h1c, h1c);
428 fail_h1c:
429 return -1;
430}
431
432
433/* release function for a connection. This one should be called to free all
434 * resources allocated to the mux.
435 */
436static void h1_release(struct connection *conn)
437{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100438 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200439
440 LIST_DEL(&conn->list);
441
442 if (h1c) {
443 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
444 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
445 LIST_DEL(&h1c->buf_wait.list);
446 LIST_INIT(&h1c->buf_wait.list);
447 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
448 }
449
450 h1_release_buf(h1c, &h1c->ibuf);
451 h1_release_buf(h1c, &h1c->obuf);
452
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100453 if (h1c->task) {
454 h1c->task->context = NULL;
455 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
456 h1c->task = NULL;
457 }
458
Christopher Faulet51dbc942018-09-13 09:05:15 +0200459 if (h1c->wait_event.task)
460 tasklet_free(h1c->wait_event.task);
461
Christopher Fauletf2824e62018-10-01 12:12:37 +0200462 h1s_destroy(h1c->h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100463 if (h1c->wait_event.events != 0)
464 conn->xprt->unsubscribe(conn, h1c->wait_event.events,
Christopher Faulet51dbc942018-09-13 09:05:15 +0200465 &h1c->wait_event);
466 pool_free(pool_head_h1c, h1c);
467 }
468
469 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100470 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200471
472 conn_stop_tracking(conn);
473 conn_full_close(conn);
474 if (conn->destroy_cb)
475 conn->destroy_cb(conn);
476 conn_free(conn);
477}
478
479/******************************************************/
480/* functions below are for the H1 protocol processing */
481/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200482/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
483 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200484 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100485static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200486{
Christopher Faulet570d1612018-11-26 11:13:57 +0100487 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200488
Christopher Faulet570d1612018-11-26 11:13:57 +0100489 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200490 (*(p + 5) > '1' ||
491 (*(p + 5) == '1' && *(p + 7) >= '1')))
492 h1m->flags |= H1_MF_VER_11;
493}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200494
Christopher Faulet9768c262018-10-22 09:34:31 +0200495/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
496 * greater or equal to 1.1
497 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100498static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200499{
Christopher Faulet570d1612018-11-26 11:13:57 +0100500 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200501
Christopher Faulet570d1612018-11-26 11:13:57 +0100502 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200503 (*(p + 5) > '1' ||
504 (*(p + 5) == '1' && *(p + 7) >= '1')))
505 h1m->flags |= H1_MF_VER_11;
506}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200507
Christopher Faulet9768c262018-10-22 09:34:31 +0200508/*
509 * Check the validity of the request version. If the version is valid, it
510 * returns 1. Otherwise, it returns 0.
511 */
512static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
513{
514 struct h1c *h1c = h1s->h1c;
515
516 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
517 * exactly one digit "." one digit. This check may be disabled using
518 * option accept-invalid-http-request.
519 */
520 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
521 if (sl.rq.v.len != 8)
522 return 0;
523
524 if (*(sl.rq.v.ptr + 4) != '/' ||
525 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
526 *(sl.rq.v.ptr + 6) != '.' ||
527 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
528 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200529 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200530 else if (!sl.rq.v.len) {
531 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200532
Christopher Faulet9768c262018-10-22 09:34:31 +0200533 /* RFC 1945 allows only GET for HTTP/0.9 requests */
534 if (sl.rq.meth != HTTP_METH_GET)
535 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200536
Christopher Faulet9768c262018-10-22 09:34:31 +0200537 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
538 if (!sl.rq.u.len)
539 return 0;
540
541 /* Add HTTP version */
542 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100543 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200544 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100545
546 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100547 ((*(sl.rq.v.ptr + 5) > '1') ||
548 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100549 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200550 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200551}
552
Christopher Faulet9768c262018-10-22 09:34:31 +0200553/*
554 * Check the validity of the response version. If the version is valid, it
555 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200556 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200557static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200558{
Christopher Faulet9768c262018-10-22 09:34:31 +0200559 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200560
Christopher Faulet9768c262018-10-22 09:34:31 +0200561 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
562 * exactly one digit "." one digit. This check may be disabled using
563 * option accept-invalid-http-request.
564 */
565 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
566 if (sl.st.v.len != 8)
567 return 0;
568
569 if (*(sl.st.v.ptr + 4) != '/' ||
570 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
571 *(sl.st.v.ptr + 6) != '.' ||
572 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
573 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200574 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100575
576 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100577 ((*(sl.st.v.ptr + 5) > '1') ||
578 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100579 h1m->flags |= H1_MF_VER_11;
580
Christopher Faulet9768c262018-10-22 09:34:31 +0200581 return 1;
582}
583/* Remove all "Connection:" headers from the HTX message <htx> */
584static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
585{
586 struct ist hdr = {.ptr = "Connection", .len = 10};
587 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200588
Christopher Faulet9768c262018-10-22 09:34:31 +0200589 while (http_find_header(htx, hdr, &ctx, 1))
590 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200591
Christopher Faulet9768c262018-10-22 09:34:31 +0200592 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
593}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200594
Christopher Faulet9768c262018-10-22 09:34:31 +0200595/* Add a "Connection:" header with the value <value> into the HTX message
596 * <htx>.
597 */
598static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
599{
600 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200601
Christopher Faulet9768c262018-10-22 09:34:31 +0200602 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200603}
604
605/* Deduce the connection mode of the client connection, depending on the
606 * configuration and the H1 message flags. This function is called twice, the
607 * first time when the request is parsed and the second time when the response
608 * is parsed.
609 */
610static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
611{
612 struct proxy *fe = h1s->h1c->px;
613 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
614
615 /* Tunnel mode can only by set on the frontend */
616 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
617 flag = H1S_F_WANT_TUN;
618 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
619 flag = H1S_F_WANT_CLO;
620
621 /* flags order: CLO > SCL > TUN > KAL */
622 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
623 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
624
625 if (h1m->flags & H1_MF_RESP) {
626 /* Either we've established an explicit tunnel, or we're
627 * switching the protocol. In both cases, we're very unlikely to
628 * understand the next protocols. We have to switch to tunnel
629 * mode, so that we transfer the request and responses then let
630 * this protocol pass unmodified. When we later implement
631 * specific parsers for such protocols, we'll want to check the
632 * Upgrade header which contains information about that protocol
633 * for responses with status 101 (eg: see RFC2817 about TLS).
634 */
635 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
636 h1s->status == 101)
637 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletbd44ca62018-11-29 09:55:22 +0100638 else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */
639 (h1m->flags & H1_MF_CONN_CLO && h1s->req.state != H1_MSG_DONE)) /*explicit close and unfinished request */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200640 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
641 }
642 else {
643 if (h1s->flags & H1S_F_WANT_KAL &&
644 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
645 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
646 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
647 }
648
649 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
650 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
651 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
652}
653
654/* Deduce the connection mode of the client connection, depending on the
655 * configuration and the H1 message flags. This function is called twice, the
656 * first time when the request is parsed and the second time when the response
657 * is parsed.
658 */
659static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
660{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100661 struct h1c *h1c = h1s->h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100662 struct session *sess = h1s->sess;
Christopher Fauleta1692f52018-11-22 10:19:50 +0100663 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200664 int flag = H1S_F_WANT_KAL;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100665 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200666
667 /* Tunnel mode can only by set on the frontend */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100668 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200669 flag = H1S_F_WANT_TUN;
670
671 /* For the server connection: server-close == httpclose */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100672 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200673 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Olivier Houchardf502aca2018-12-14 19:42:40 +0100674 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200675 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
676 flag = H1S_F_WANT_CLO;
677
678 /* flags order: CLO > SCL > TUN > KAL */
679 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
680 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
681
682 if (h1m->flags & H1_MF_RESP) {
683 /* Either we've established an explicit tunnel, or we're
684 * switching the protocol. In both cases, we're very unlikely to
685 * understand the next protocols. We have to switch to tunnel
686 * mode, so that we transfer the request and responses then let
687 * this protocol pass unmodified. When we later implement
688 * specific parsers for such protocols, we'll want to check the
689 * Upgrade header which contains information about that protocol
690 * for responses with status 101 (eg: see RFC2817 about TLS).
691 */
692 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
693 h1s->status == 101)
694 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
695 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
696 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
697 else if (h1s->flags & H1S_F_WANT_KAL &&
698 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
699 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
700 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
701 }
Christopher Faulet70033782018-12-05 13:50:11 +0100702 else {
703 if (h1s->flags & H1S_F_WANT_KAL &&
704 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
705 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
706 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
707 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200708
709 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
710 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
711 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200712}
713
Christopher Faulet9768c262018-10-22 09:34:31 +0200714static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
715 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200716{
717 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200718
719 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
720 * token is found
721 */
722 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200723 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200724
725 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200726 if (h1m->flags & H1_MF_CONN_CLO) {
727 if (conn_val)
728 *conn_val = ist("");
729 if (htx)
730 h1_remove_conn_hdrs(h1m, htx);
731 }
732 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
733 if (conn_val)
734 *conn_val = ist("keep-alive");
735 if (htx)
736 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
737 }
Christopher Fauletce851492018-12-07 18:06:59 +0100738 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
739 if (conn_val)
740 *conn_val = ist("");
741 if (htx)
742 h1_remove_conn_hdrs(h1m, htx);
743 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200744 }
745 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200746 if (h1m->flags & H1_MF_CONN_KAL) {
747 if (conn_val)
748 *conn_val = ist("");
749 if (htx)
750 h1_remove_conn_hdrs(h1m, htx);
751 }
752 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
753 if (conn_val)
754 *conn_val = ist("close");
755 if (htx)
756 h1_add_conn_hdr(h1m, htx, ist("close"));
757 }
Christopher Fauletce851492018-12-07 18:06:59 +0100758 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
759 if (conn_val)
760 *conn_val = ist("");
761 if (htx)
762 h1_remove_conn_hdrs(h1m, htx);
763 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200764 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200765}
766
Christopher Faulet9768c262018-10-22 09:34:31 +0200767static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
768 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200769{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200770 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
771 * token is found
772 */
773 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200774 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200775
776 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200777 if (h1m->flags & H1_MF_CONN_CLO) {
778 if (conn_val)
779 *conn_val = ist("");
780 if (htx)
781 h1_remove_conn_hdrs(h1m, htx);
782 }
783 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
784 if (conn_val)
785 *conn_val = ist("keep-alive");
786 if (htx)
787 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
788 }
Christopher Fauletce851492018-12-07 18:06:59 +0100789 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
790 if (conn_val)
791 *conn_val = ist("");
792 if (htx)
793 h1_remove_conn_hdrs(h1m, htx);
794 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200795 }
796 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200797 if (h1m->flags & H1_MF_CONN_KAL) {
798 if (conn_val)
799 *conn_val = ist("");
800 if (htx)
801 h1_remove_conn_hdrs(h1m, htx);
802 }
803 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
804 if (conn_val)
805 *conn_val = ist("close");
806 if (htx)
807 h1_add_conn_hdr(h1m, htx, ist("close"));
808 }
Christopher Fauletce851492018-12-07 18:06:59 +0100809 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
810 if (conn_val)
811 *conn_val = ist("");
812 if (htx)
813 h1_remove_conn_hdrs(h1m, htx);
814 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200815 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200816}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200817
Christopher Faulet9768c262018-10-22 09:34:31 +0200818/* Set the right connection mode and update "Connection:" header if
819 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
820 * message is updated accordingly. When <conn_val> is not NULL, it is set with
821 * the new header value.
822 */
823static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
824 struct htx *htx, struct ist *conn_val)
825{
826 if (!conn_is_back(h1s->h1c->conn)) {
827 h1_set_cli_conn_mode(h1s, h1m);
828 if (h1m->flags & H1_MF_RESP)
829 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
830 }
831 else {
832 h1_set_srv_conn_mode(h1s, h1m);
833 if (!(h1m->flags & H1_MF_RESP))
834 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
835 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200836}
837
Christopher Faulete44769b2018-11-29 23:01:45 +0100838
839/* Append the description of what is present in error snapshot <es> into <out>.
840 * The description must be small enough to always fit in a buffer. The output
841 * buffer may be the trash so the trash must not be used inside this function.
842 */
843static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
844{
845 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100846 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
847 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
848 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
849 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
850 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
851 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100852}
853/*
854 * Capture a bad request or response and archive it in the proxy's structure.
855 * By default it tries to report the error position as h1m->err_pos. However if
856 * this one is not set, it will then report h1m->next, which is the last known
857 * parsing point. The function is able to deal with wrapping buffers. It always
858 * displays buffers as a contiguous area starting at buf->p. The direction is
859 * determined thanks to the h1m's flags.
860 */
861static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
862 struct h1m *h1m, struct buffer *buf)
863{
864 struct session *sess = h1c->conn->owner;
865 struct proxy *proxy = h1c->px;
866 struct proxy *other_end = sess->fe;
867 union error_snapshot_ctx ctx;
868
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100869 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100870 other_end = si_strm(h1s->cs->data)->be;
871
872 /* http-specific part now */
873 ctx.h1.state = h1m->state;
874 ctx.h1.c_flags = h1c->flags;
875 ctx.h1.s_flags = h1s->flags;
876 ctx.h1.m_flags = h1m->flags;
877 ctx.h1.m_clen = h1m->curr_len;
878 ctx.h1.m_blen = h1m->body_len;
879
880 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
881 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100882 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
883 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100884}
885
Christopher Fauletadb22202018-12-12 10:32:09 +0100886/* Emit the chunksize followed by a CRLF in front of data of the buffer
887 * <buf>. It goes backwards and starts with the byte before the buffer's
888 * head. The caller is responsible for ensuring there is enough room left before
889 * the buffer's head for the string.
890 */
891static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
892{
893 char *beg, *end;
894
895 beg = end = b_head(buf);
896 *--beg = '\n';
897 *--beg = '\r';
898 do {
899 *--beg = hextab[chksz & 0xF];
900 } while (chksz >>= 4);
901 buf->head -= (end - beg);
902 b_add(buf, end - beg);
903}
904
905/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
906 * ensuring there is enough room left in the buffer for the string. */
907static void h1_emit_chunk_crlf(struct buffer *buf)
908{
909 *(b_peek(buf, b_data(buf))) = '\r';
910 *(b_peek(buf, b_data(buf) + 1)) = '\n';
911 b_add(buf, 2);
912}
913
Christopher Faulet129817b2018-09-20 16:14:40 +0200914/*
915 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200916 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
917 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800918 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200919 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200920static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200921 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200922{
923 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100924 union h1_sl h1sl;
925 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200926 int ret = 0;
927
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100928 if (!max)
929 goto end;
930
Christopher Faulet129817b2018-09-20 16:14:40 +0200931 /* Realing input buffer if necessary */
932 if (b_head(buf) + b_data(buf) > b_wrap(buf))
933 b_slow_realign(buf, trash.area, 0);
934
Christopher Fauletf2824e62018-10-01 12:12:37 +0200935 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100936 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200937 if (ret <= 0) {
938 /* Incomplete or invalid message. If the buffer is full, it's an
939 * error because headers are too large to be handled by the
940 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200941 if (ret < 0 || (!ret && b_full(buf)))
942 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200943 goto end;
944 }
945
946 /* messages headers fully parsed, do some checks to prepare the body
947 * parsing.
948 */
949
950 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200951 if (ret > (b_size(buf) - global.tune.maxrewrite))
952 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200953
Christopher Faulet9768c262018-10-22 09:34:31 +0200954 /* Save the request's method or the response's status, check if the body
955 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200956 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100957 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200958
Christopher Faulet129817b2018-09-20 16:14:40 +0200959 /* Request have always a known length */
960 h1m->flags |= H1_MF_XFER_LEN;
961 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
962 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200963
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100964 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
965 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200966 h1m->err_state = h1m->state;
967 goto vsn_error;
968 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200969 }
970 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100971 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200972
973 if ((h1s->meth == HTTP_METH_HEAD) ||
974 (h1s->status >= 100 && h1s->status < 200) ||
975 (h1s->status == 204) || (h1s->status == 304) ||
976 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
977 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
978 h1m->flags |= H1_MF_XFER_LEN;
979 h1m->curr_len = h1m->body_len = 0;
980 h1m->state = H1_MSG_DONE;
981 }
982 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
983 h1m->flags |= H1_MF_XFER_LEN;
984 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
985 h1m->state = H1_MSG_DONE;
986 }
987 else
988 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200989
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100990 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
991 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200992 h1m->err_state = h1m->state;
993 goto vsn_error;
994 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200995 }
996
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100997 /* Set HTX start-line flags */
998 if (h1m->flags & H1_MF_VER_11)
999 flags |= HTX_SL_F_VER_11;
1000 if (h1m->flags & H1_MF_XFER_ENC)
1001 flags |= HTX_SL_F_XFER_ENC;
1002 if (h1m->flags & H1_MF_XFER_LEN) {
1003 flags |= HTX_SL_F_XFER_LEN;
1004 if (h1m->flags & H1_MF_CHNK)
1005 flags |= HTX_SL_F_CHNK;
1006 else if (h1m->flags & H1_MF_CLEN)
1007 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001008 if (h1m->state == H1_MSG_DONE)
1009 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001010 }
1011
Christopher Faulet9768c262018-10-22 09:34:31 +02001012 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001013 struct htx_sl *sl;
1014
1015 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1016 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001017 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001018 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001019 }
1020 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001021 struct htx_sl *sl;
1022
1023 flags |= HTX_SL_F_IS_RESP;
1024 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1025 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001026 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001027 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001028 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001029
Christopher Faulet9768c262018-10-22 09:34:31 +02001030 if (h1m->state == H1_MSG_DONE)
1031 if (!htx_add_endof(htx, HTX_BLK_EOM))
1032 goto error;
1033
1034 h1_process_conn_mode(h1s, h1m, htx, NULL);
1035
1036 /* If body length cannot be determined, set htx->extra to
1037 * ULLONG_MAX. This value is impossible in other cases.
1038 */
1039 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
1040
1041 /* Recheck there is enough space to do headers rewritting */
1042 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
1043 goto error;
1044
1045 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001046 end:
1047 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001048
1049 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001050 h1m->err_state = h1m->state;
1051 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001052 vsn_error:
1053 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +01001054 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001055 ret = 0;
1056 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001057}
1058
1059/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001060 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1061 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001062 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001063 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001064 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001065static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001066 struct buffer *buf, size_t *ofs, size_t max,
Christopher Fauletafe57842019-01-21 11:55:02 +01001067 struct buffer *htxbuf, size_t reserve)
Christopher Faulet129817b2018-09-20 16:14:40 +02001068{
Christopher Fauletafe57842019-01-21 11:55:02 +01001069 uint32_t data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001070 size_t total = 0;
1071 int ret = 0;
1072
Christopher Fauletafe57842019-01-21 11:55:02 +01001073 data_space = htx_free_data_space(htx);
1074 if (data_space <= reserve)
1075 goto end;
1076 data_space -= reserve;
1077
Christopher Faulet129817b2018-09-20 16:14:40 +02001078 if (h1m->flags & H1_MF_XFER_LEN) {
1079 if (h1m->flags & H1_MF_CLEN) {
1080 /* content-length: read only h2m->body_len */
1081 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001082 if (ret > data_space)
1083 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001084 if ((uint64_t)ret > h1m->curr_len)
1085 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001086 if (ret > b_contig_data(buf, *ofs))
1087 ret = b_contig_data(buf, *ofs);
1088 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001089 /* very often with large files we'll face the following
1090 * situation :
1091 * - htx is empty and points to <htxbuf>
1092 * - ret == buf->data
1093 * - buf->head == sizeof(struct htx)
1094 * => we can swap the buffers and place an htx header into
1095 * the target buffer instead
1096 */
1097 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1098 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1099 void *raw_area = buf->area;
1100 void *htx_area = htxbuf->area;
1101 struct htx_blk *blk;
1102
1103 buf->area = htx_area;
1104 htxbuf->area = raw_area;
1105 htx = (struct htx *)htxbuf->area;
1106 htx->size = htxbuf->size - sizeof(*htx);
1107 htx_reset(htx);
1108 b_set_data(htxbuf, b_size(htxbuf));
1109
1110 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1111 blk->info += ret;
1112 /* nothing else to do, the old buffer now contains an
1113 * empty pre-initialized HTX header
1114 */
1115 }
1116 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001117 goto end;
1118 h1m->curr_len -= ret;
1119 *ofs += ret;
1120 total += ret;
1121 }
1122
1123 if (!h1m->curr_len) {
1124 if (!htx_add_endof(htx, HTX_BLK_EOM))
1125 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001126 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +02001127 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001128 }
1129 else if (h1m->flags & H1_MF_CHNK) {
1130 new_chunk:
1131 /* te:chunked : parse chunks */
1132 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001133 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001134 if (ret <= 0)
1135 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001136 h1m->state = H1_MSG_CHUNK_SIZE;
1137
Christopher Faulet129817b2018-09-20 16:14:40 +02001138 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001139 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001140 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001141 }
1142
1143 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1144 unsigned int chksz;
1145
Christopher Fauletf2824e62018-10-01 12:12:37 +02001146 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001147 if (ret <= 0)
1148 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001149 if (!chksz) {
1150 if (!htx_add_endof(htx, HTX_BLK_EOD))
1151 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001152 h1s->flags |= H1S_F_HAVE_I_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001153 h1m->state = H1_MSG_TRAILERS;
1154 }
1155 else
1156 h1m->state = H1_MSG_DATA;
1157
Christopher Faulet129817b2018-09-20 16:14:40 +02001158 h1m->curr_len = chksz;
1159 h1m->body_len += chksz;
1160 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001161 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001162 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001163 }
1164
1165 if (h1m->state == H1_MSG_DATA) {
1166 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001167 if (ret > data_space)
1168 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001169 if ((uint64_t)ret > h1m->curr_len)
1170 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001171 if (ret > b_contig_data(buf, *ofs))
1172 ret = b_contig_data(buf, *ofs);
1173 if (ret) {
1174 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1175 goto end;
1176 h1m->curr_len -= ret;
1177 max -= ret;
1178 *ofs += ret;
1179 total += ret;
1180 }
1181 if (!h1m->curr_len) {
1182 h1m->state = H1_MSG_CHUNK_CRLF;
Christopher Fauletafe57842019-01-21 11:55:02 +01001183 data_space = htx_free_data_space(htx);
1184 if (data_space <= reserve)
1185 goto end;
1186 data_space -= reserve;
Christopher Faulet9768c262018-10-22 09:34:31 +02001187 goto new_chunk;
1188 }
1189 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001190 }
1191
1192 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001193 /* Trailers were alread parsed, only the EOM
1194 * need to be added */
Willy Tarreau34d23482019-01-03 17:46:56 +01001195 if (h1s->flags & H1S_F_HAVE_I_TLR)
Christopher Faulet17276482018-11-22 11:44:35 +01001196 goto skip_tlr_parsing;
1197
Christopher Fauletf2824e62018-10-01 12:12:37 +02001198 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001199 if (ret > data_space)
1200 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001201 if (ret <= 0)
1202 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001203
1204 /* Realing input buffer if tailers wrap. For now
1205 * this is a workaroung. Because trailers are
1206 * not split on CRLF, like headers, there is no
1207 * way to know where to split it when trailers
1208 * wrap. This is a limitation of
1209 * h1_measure_trailers.
1210 */
1211 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1212 b_slow_realign(buf, trash.area, 0);
1213
1214 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1215 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001216 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001217 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001218 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001219 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001220
Christopher Faulet17276482018-11-22 11:44:35 +01001221 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001222 if (!htx_add_endof(htx, HTX_BLK_EOM))
1223 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001224 h1m->state = H1_MSG_DONE;
1225 }
1226 }
1227 else {
1228 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1229 * is no body. Switch the message in DONE state
1230 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001231 if (!htx_add_endof(htx, HTX_BLK_EOM))
1232 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001233 h1m->state = H1_MSG_DONE;
1234 }
1235 }
1236 else {
1237 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001238 ret = max;
1239 if (ret > data_space)
1240 ret = data_space;
1241 if (ret > b_contig_data(buf, *ofs))
1242 ret = b_contig_data(buf, *ofs);
1243 if (ret) {
1244 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1245 goto end;
1246
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001247 *ofs += ret;
1248 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001249 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001250 }
1251
1252 end:
1253 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001254 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001255 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001256 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001257 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001258 return 0;
1259 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 /* update htx->extra, only when the body length is known */
1261 if (h1m->flags & H1_MF_XFER_LEN)
1262 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001263 return total;
1264}
1265
1266/*
1267 * Synchronize the request and the response before reseting them. Except for 1xx
1268 * responses, we wait that the request and the response are in DONE state and
1269 * that all data are forwarded for both. For 1xx responses, only the response is
1270 * reset, waiting the final one. Many 1xx messages can be sent.
1271 */
1272static void h1_sync_messages(struct h1c *h1c)
1273{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001274 struct h1s *h1s = h1c->h1s;
1275
1276 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001277 return;
1278
Christopher Fauletf2824e62018-10-01 12:12:37 +02001279 if (h1s->res.state == H1_MSG_DONE &&
1280 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001281 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001282 /* For 100-Continue response or any other informational 1xx
1283 * response which is non-final, don't reset the request, the
1284 * transaction is not finished. We take care the response was
1285 * transferred before.
1286 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001287 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001288 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001289 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet129817b2018-09-20 16:14:40 +02001290 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001291 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001292 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1293 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001294 h1m_init_req(&h1s->req);
1295 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001296 h1s->req.state = H1_MSG_TUNNEL;
1297 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001298 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001299 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001300 }
1301}
1302
1303/*
1304 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001305 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1306 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001307 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001308static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001309{
Christopher Faulet539e0292018-11-19 10:40:09 +01001310 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001311 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001312 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001313 size_t total = 0;
1314 size_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001315 size_t count, rsv;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001316 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001317
Christopher Faulet539e0292018-11-19 10:40:09 +01001318 htx = htx_from_buf(buf);
1319 count = b_data(&h1c->ibuf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001320 if (!count)
1321 goto end;
Christopher Fauletafe57842019-01-21 11:55:02 +01001322 rsv = ((flags & CO_RFL_KEEP_RSV) ? global.tune.maxrewrite : 0);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001323
Christopher Fauletf2824e62018-10-01 12:12:37 +02001324 if (!conn_is_back(h1c->conn)) {
1325 h1m = &h1s->req;
1326 errflag = H1S_F_REQ_ERROR;
1327 }
1328 else {
1329 h1m = &h1s->res;
1330 errflag = H1S_F_RES_ERROR;
1331 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001332
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001333 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001334 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001335 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001336 if (!ret)
1337 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001338 }
1339 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Fauletafe57842019-01-21 11:55:02 +01001340 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001341 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001342 if (!ret)
1343 break;
1344 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001345 else if (h1m->state == H1_MSG_DONE) {
1346 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001347 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001348 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001349 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Fauletafe57842019-01-21 11:55:02 +01001350 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001351 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001352 if (!ret)
1353 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001354 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001355 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001356 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001357 break;
1358 }
1359
Christopher Faulet539e0292018-11-19 10:40:09 +01001360 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001361 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001362
Christopher Faulet47365272018-10-31 17:40:50 +01001363 if (h1s->flags & errflag)
1364 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001365
Christopher Faulet539e0292018-11-19 10:40:09 +01001366 b_del(&h1c->ibuf, total);
1367
1368 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001369 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001370
Willy Tarreau45f2b892018-12-05 07:59:27 +01001371 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001372 h1c->flags &= ~H1C_F_IN_FULL;
1373 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001374 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001375
Christopher Fauletcf56b992018-12-11 16:12:31 +01001376 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1377
1378 if (!b_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001379 h1_release_buf(h1c, &h1c->ibuf);
1380 h1_sync_messages(h1c);
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001381 }
Christopher Fauletcf56b992018-12-11 16:12:31 +01001382 else if (!htx_is_empty(htx))
1383 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001384
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001385 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1386 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet539e0292018-11-19 10:40:09 +01001387 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001388
Christopher Faulet539e0292018-11-19 10:40:09 +01001389 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001390
1391 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001392 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001393 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001394 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001395 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001396 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001397}
1398
Christopher Faulet129817b2018-09-20 16:14:40 +02001399/*
1400 * Process outgoing data. It parses data and transfer them from the channel buffer into
1401 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1402 * 0 if it couldn't proceed.
1403 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001404static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1405{
Christopher Faulet129817b2018-09-20 16:14:40 +02001406 struct h1s *h1s = h1c->h1s;
1407 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001408 struct htx *chn_htx;
1409 struct htx_blk *blk;
1410 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001411 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001412 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001413 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001414
Christopher Faulet47365272018-10-31 17:40:50 +01001415 if (!count)
1416 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001417
Christopher Faulet9768c262018-10-22 09:34:31 +02001418 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001419 if (htx_is_empty(chn_htx))
1420 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001421
Christopher Faulet51dbc942018-09-13 09:05:15 +02001422 if (!h1_get_buf(h1c, &h1c->obuf)) {
1423 h1c->flags |= H1C_F_OUT_ALLOC;
1424 goto end;
1425 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001426
Christopher Fauletf2824e62018-10-01 12:12:37 +02001427 if (!conn_is_back(h1c->conn)) {
1428 h1m = &h1s->res;
1429 errflag = H1S_F_RES_ERROR;
1430 }
1431 else {
1432 h1m = &h1s->req;
1433 errflag = H1S_F_REQ_ERROR;
1434 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001435
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001436 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001437 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001438
1439 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001440
Willy Tarreau3815b222018-12-11 19:50:43 +01001441 /* Perform some optimizations to reduce the number of buffer copies.
1442 * First, if the mux's buffer is empty and the htx area contains
1443 * exactly one data block of the same size as the requested count,
1444 * then it's possible to simply swap the caller's buffer with the
1445 * mux's output buffer and adjust offsets and length to match the
1446 * entire DATA HTX block in the middle. In this case we perform a
1447 * true zero-copy operation from end-to-end. This is the situation
1448 * that happens all the time with large files. Second, if this is not
1449 * possible, but the mux's output buffer is empty, we still have an
1450 * opportunity to avoid the copy to the intermediary buffer, by making
1451 * the intermediary buffer's area point to the output buffer's area.
1452 * In this case we want to skip the HTX header to make sure that copies
1453 * remain aligned and that this operation remains possible all the
1454 * time. This goes for headers, data blocks and any data extracted from
1455 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001456 */
1457 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001458 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001459
1460 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001461 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001462 htx_get_blk_value(chn_htx, blk).len == count) {
1463 void *old_area = h1c->obuf.area;
1464
1465 h1c->obuf.area = buf->area;
1466 h1c->obuf.data = count;
1467
1468 buf->area = old_area;
1469 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001470
1471 /* The message is chunked. We need to emit the chunk
1472 * size. We have at least the size of the struct htx to
1473 * write the chunk envelope. It should be enough.
1474 */
1475 if (h1m->flags & H1_MF_CHNK) {
1476 h1_emit_chunk_size(&h1c->obuf, count);
1477 h1_emit_chunk_crlf(&h1c->obuf);
1478 }
1479
Willy Tarreau3815b222018-12-11 19:50:43 +01001480 total += count;
1481 goto out;
1482 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001483 tmp->area = h1c->obuf.area + h1c->obuf.head;
1484 }
1485
Christopher Faulet9768c262018-10-22 09:34:31 +02001486 tmp->size = b_room(&h1c->obuf);
1487
Christopher Fauletb2e84162018-12-06 11:39:49 +01001488 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001489 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001490 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001491 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001492 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001493 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001494
Christopher Fauletb2e84162018-12-06 11:39:49 +01001495 vlen = sz;
1496 if (vlen > count) {
1497 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1498 goto copy;
1499 vlen = count;
1500 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001501
Christopher Fauletb2e84162018-12-06 11:39:49 +01001502 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001503 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001504 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001505
1506 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001507 h1m_init_req(h1m);
1508 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001509 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001510 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001511 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001512 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001513 goto copy;
1514 h1m->flags |= H1_MF_XFER_LEN;
1515 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001516 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001517
Christopher Faulet9768c262018-10-22 09:34:31 +02001518 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001519 h1m_init_res(h1m);
1520 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001521 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001522 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001523 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001524 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001525 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001526 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001527 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001528 if (sl->info.res.status < 200 &&
1529 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1530 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001531 h1m->state = H1_MSG_HDR_FIRST;
1532 break;
1533
1534 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001535 h1m->state = H1_MSG_HDR_NAME;
1536 n = htx_get_blk_name(chn_htx, blk);
1537 v = htx_get_blk_value(chn_htx, blk);
1538
1539 if (isteqi(n, ist("transfer-encoding")))
1540 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001541 else if (isteqi(n, ist("content-length"))) {
1542 if (h1_parse_cont_len_header(h1m, &v) <= 0)
1543 goto skip_hdr;
1544 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001545 else if (isteqi(n, ist("connection"))) {
1546 h1_parse_connection_header(h1m, v);
1547 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001548 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001549 if (!v.len)
1550 goto skip_hdr;
1551 }
1552
Christopher Fauletc59ff232018-12-03 13:58:44 +01001553 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 goto copy;
1555 skip_hdr:
1556 h1m->state = H1_MSG_HDR_L2_LWS;
1557 break;
1558
1559 case HTX_BLK_PHDR:
1560 /* not implemented yet */
1561 h1m->flags |= errflag;
1562 break;
1563
1564 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001565 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001566 /* There is no "Connection:" header and
1567 * it the conn_mode must be
1568 * processed. So do it */
1569 n = ist("Connection");
1570 v = ist("");
1571 h1_process_conn_mode(h1s, h1m, NULL, &v);
1572 process_conn_mode = 0;
1573 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001574 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001575 goto copy;
1576 }
1577 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001578
Willy Tarreau61952372019-01-03 21:27:19 +01001579 if ((h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1580 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN)) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001581 /* chunking needed but header not seen */
1582 if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28))
1583 goto copy;
1584 h1m->flags |= H1_MF_CHNK;
1585 }
1586
Christopher Faulet9768c262018-10-22 09:34:31 +02001587 h1m->state = H1_MSG_LAST_LF;
1588 if (!chunk_memcat(tmp, "\r\n", 2))
1589 goto copy;
1590
1591 h1m->state = H1_MSG_DATA;
1592 break;
1593
1594 case HTX_BLK_DATA:
1595 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001596 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001597 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001598 goto copy;
1599 break;
1600
1601 case HTX_BLK_EOD:
1602 if (!chunk_memcat(tmp, "0\r\n", 3))
1603 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001604 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001605 h1m->state = H1_MSG_TRAILERS;
1606 break;
1607
1608 case HTX_BLK_TLR:
Willy Tarreau34d23482019-01-03 17:46:56 +01001609 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001610 if (!chunk_memcat(tmp, "0\r\n", 3))
1611 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001612 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001613 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001614 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001615 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001616 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001617 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001618 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001619 break;
1620
1621 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001622 if ((h1m->flags & H1_MF_CHNK)) {
Willy Tarreau34d23482019-01-03 17:46:56 +01001623 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001624 if (!chunk_memcat(tmp, "0\r\n", 3))
1625 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001626 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001627 }
Willy Tarreau34d23482019-01-03 17:46:56 +01001628 if (!(h1s->flags & H1S_F_HAVE_O_TLR)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001629 if (!chunk_memcat(tmp, "\r\n", 2))
1630 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001631 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet32188212018-11-20 18:21:43 +01001632 }
1633 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001634 h1m->state = H1_MSG_DONE;
1635 break;
1636
1637 case HTX_BLK_OOB:
1638 v = htx_get_blk_value(chn_htx, blk);
1639 if (!chunk_memcat(tmp, v.ptr, v.len))
1640 goto copy;
1641 break;
1642
1643 default:
1644 h1m->flags |= errflag;
1645 break;
1646 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001647 total += vlen;
1648 count -= vlen;
1649 if (sz == vlen)
1650 blk = htx_remove_blk(chn_htx, blk);
1651 else {
1652 htx_cut_data_blk(chn_htx, blk, vlen);
1653 break;
1654 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001655 }
1656
Christopher Faulet9768c262018-10-22 09:34:31 +02001657 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001658 /* when the output buffer is empty, tmp shares the same area so that we
1659 * only have to update pointers and lengths.
1660 */
Willy Tarreaub57af612019-01-23 20:43:53 +01001661 if (tmp->area == h1c->obuf.area + h1c->obuf.head)
Willy Tarreauc5efa332018-12-05 11:19:27 +01001662 h1c->obuf.data = tmp->data;
1663 else
1664 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001665
Willy Tarreau3815b222018-12-11 19:50:43 +01001666 htx_to_buf(chn_htx, buf);
1667 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001668 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001669 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001670 end:
1671 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001672}
1673
Christopher Faulet51dbc942018-09-13 09:05:15 +02001674/*********************************************************/
1675/* functions below are I/O callbacks from the connection */
1676/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001677static void h1_wake_stream_for_recv(struct h1s *h1s)
1678{
1679 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001680 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001681 tasklet_wakeup(h1s->recv_wait->task);
1682 h1s->recv_wait = NULL;
1683 }
1684}
1685static void h1_wake_stream_for_send(struct h1s *h1s)
1686{
1687 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001688 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001689 tasklet_wakeup(h1s->send_wait->task);
1690 h1s->send_wait = NULL;
1691 }
1692}
1693
Christopher Faulet51dbc942018-09-13 09:05:15 +02001694/*
1695 * Attempt to read data, and subscribe if none available
1696 */
1697static int h1_recv(struct h1c *h1c)
1698{
1699 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001700 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001701 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001702 int rcvd = 0;
1703
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001704 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001705 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001706
Olivier Houchard75159a92018-12-03 18:46:09 +01001707 if (!h1_recv_allowed(h1c)) {
1708 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001709 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001710 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001711
Christopher Fauletfeb11742018-11-29 15:12:34 +01001712 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001713 rcvd = 1;
1714 goto end;
1715 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001716
Christopher Faulet51dbc942018-09-13 09:05:15 +02001717 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1718 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001719 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001720 }
1721
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001722 /*
1723 * If we only have a small amount of data, realign it,
1724 * it's probably cheaper than doing 2 recv() calls.
1725 */
1726 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1727 b_slow_realign(&h1c->ibuf, trash.area, 0);
1728
Willy Tarreau45f2b892018-12-05 07:59:27 +01001729 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001730 if (max) {
1731 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001732
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001733 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001734 if (!b_data(&h1c->ibuf)) {
1735 /* try to pre-align the buffer like the rxbufs will be
1736 * to optimize memory copies.
1737 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001738 h1c->ibuf.head = sizeof(struct htx);
1739 }
Willy Tarreauc0960d12018-12-14 10:59:15 +01001740 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001741 }
Christopher Faulet47365272018-10-31 17:40:50 +01001742 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001743 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001744 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001745 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001746 if (h1s->csinfo.t_idle == -1)
1747 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1748 }
Christopher Faulet47365272018-10-31 17:40:50 +01001749 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001750
Christopher Fauletcf56b992018-12-11 16:12:31 +01001751 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001752 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001753 goto end;
1754 }
1755
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001756 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001757
Christopher Faulet9768c262018-10-22 09:34:31 +02001758 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001759 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1760 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001761
Christopher Faulete6b39942018-12-07 09:42:49 +01001762 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Olivier Houchard6a2d3342018-12-06 17:41:26 +01001763 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001764 if (!b_data(&h1c->ibuf))
1765 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001766 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001767 h1c->flags |= H1C_F_IN_FULL;
1768 return rcvd;
1769}
1770
1771
1772/*
1773 * Try to send data if possible
1774 */
1775static int h1_send(struct h1c *h1c)
1776{
1777 struct connection *conn = h1c->conn;
1778 unsigned int flags = 0;
1779 size_t ret;
1780 int sent = 0;
1781
1782 if (conn->flags & CO_FL_ERROR)
1783 return 0;
1784
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001785 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001786 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1787 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001788 return 0;
1789 }
1790
Christopher Faulet51dbc942018-09-13 09:05:15 +02001791 if (!b_data(&h1c->obuf))
1792 goto end;
1793
1794 if (h1c->flags & H1C_F_OUT_FULL)
1795 flags |= CO_SFL_MSG_MORE;
1796
1797 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1798 if (ret > 0) {
1799 h1c->flags &= ~H1C_F_OUT_FULL;
1800 b_del(&h1c->obuf, ret);
1801 sent = 1;
1802 }
1803
Christopher Faulet145aa472018-12-06 10:56:20 +01001804 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1805 /* error or output closed, nothing to send, clear the buffer to release it */
1806 b_reset(&h1c->obuf);
1807 }
1808
Christopher Faulet51dbc942018-09-13 09:05:15 +02001809 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001810 if (!(h1c->flags & H1C_F_OUT_FULL))
1811 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001812
Christopher Faulet51dbc942018-09-13 09:05:15 +02001813 /* We're done, no more to send */
1814 if (!b_data(&h1c->obuf)) {
1815 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001816 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001817 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001818 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001819 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001820 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1821 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001822
1823 return sent;
1824}
1825
Christopher Faulet51dbc942018-09-13 09:05:15 +02001826
1827/* callback called on any event by the connection handler.
1828 * It applies changes and returns zero, or < 0 if it wants immediate
1829 * destruction of the connection.
1830 */
1831static int h1_process(struct h1c * h1c)
1832{
1833 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001834 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001835
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001836 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001837 return -1;
1838
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001839 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001840 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1841 goto end;
1842 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001843 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001844 }
1845
Christopher Fauletfeb11742018-11-29 15:12:34 +01001846 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001847 if (h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001848 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001849 conn_xprt_read0_pending(conn))
1850 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001851 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001852 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001853 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001854 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001855 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001856 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001857 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001858 }
1859
Christopher Fauletfeb11742018-11-29 15:12:34 +01001860 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1861 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1862
Olivier Houchard75159a92018-12-03 18:46:09 +01001863 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1864 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001865 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001866 int flags = 0;
1867
1868 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1869 flags |= CS_FL_ERROR;
1870 if (conn_xprt_read0_pending(conn))
Christopher Faulet5f50f5e2018-12-07 11:39:55 +01001871 flags |= CS_FL_EOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001872 h1s->cs->flags |= flags;
1873 h1s->cs->data_cb->wake(h1s->cs);
1874 }
Christopher Faulet47365272018-10-31 17:40:50 +01001875 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001876 if (h1c->task) {
1877 h1c->task->expire = TICK_ETERNITY;
1878 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001879 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001880 ? h1c->shut_timeout
1881 : h1c->timeout));
1882 task_queue(h1c->task);
1883 }
1884 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001885 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001886
1887 release:
1888 h1_release(conn);
1889 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001890}
1891
1892static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1893{
1894 struct h1c *h1c = ctx;
1895 int ret = 0;
1896
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001897 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001898 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001899 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001900 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001901 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001902 h1_process(h1c);
1903 return NULL;
1904}
1905
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001906static void h1_reset(struct connection *conn)
1907{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001908 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001909
1910 /* Reset the flags, and let the mux know we're waiting for a connection */
1911 h1c->flags = H1C_F_CS_WAIT_CONN;
1912}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001913
1914static int h1_wake(struct connection *conn)
1915{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001916 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001917 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001918
Christopher Faulet539e0292018-11-19 10:40:09 +01001919 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001920 ret = h1_process(h1c);
1921 if (ret == 0) {
1922 struct h1s *h1s = h1c->h1s;
1923
1924 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1925 ret = h1s->cs->data_cb->wake(h1s->cs);
1926 }
1927 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001928}
1929
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001930/* Connection timeout management. The principle is that if there's no receipt
1931 * nor sending for a certain amount of time, the connection is closed.
1932 */
1933static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
1934{
1935 struct h1c *h1c = context;
1936 int expired = tick_is_expired(t->expire, now_ms);
1937
1938 if (!expired && h1c)
1939 return t;
1940
1941 task_delete(t);
1942 task_free(t);
1943
1944 if (!h1c) {
1945 /* resources were already deleted */
1946 return NULL;
1947 }
1948
1949 h1c->task = NULL;
1950 /* If a stream is still attached to the mux, just set an error and wait
1951 * for the stream's timeout. Otherwise, release the mux. This is only ok
1952 * because same timeouts are used.
1953 */
1954 if (h1c->h1s && h1c->h1s->cs)
1955 h1c->flags |= H1C_F_CS_ERROR;
1956 else
1957 h1_release(h1c->conn);
1958 return NULL;
1959}
1960
Christopher Faulet51dbc942018-09-13 09:05:15 +02001961/*******************************************/
1962/* functions below are used by the streams */
1963/*******************************************/
1964/*
1965 * Attach a new stream to a connection
1966 * (Used for outgoing connections)
1967 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001968static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001969{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001970 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001971 struct conn_stream *cs = NULL;
1972 struct h1s *h1s;
1973
1974 if (h1c->flags & H1C_F_CS_ERROR)
1975 goto end;
1976
1977 cs = cs_new(h1c->conn);
1978 if (!cs)
1979 goto end;
1980
Olivier Houchardf502aca2018-12-14 19:42:40 +01001981 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001982 if (h1s == NULL)
1983 goto end;
1984
1985 return cs;
1986 end:
1987 cs_free(cs);
1988 return NULL;
1989}
1990
1991/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1992 * this mux, it's easy as we can only store a single conn_stream.
1993 */
1994static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1995{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001996 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001997 struct h1s *h1s = h1c->h1s;
1998
1999 if (h1s)
2000 return h1s->cs;
2001
2002 return NULL;
2003}
2004
2005static void h1_destroy(struct connection *conn)
2006{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002007 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002008
2009 if (!h1c->h1s)
2010 h1_release(conn);
2011}
2012
2013/*
2014 * Detach the stream from the connection and possibly release the connection.
2015 */
2016static void h1_detach(struct conn_stream *cs)
2017{
2018 struct h1s *h1s = cs->ctx;
2019 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002020 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002021 int has_keepalive;
2022 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002023
2024 cs->ctx = NULL;
2025 if (!h1s)
2026 return;
2027
Olivier Houchardf502aca2018-12-14 19:42:40 +01002028 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002029 h1c = h1s->h1c;
2030 h1s->cs = NULL;
2031
Olivier Houchard8a786902018-12-15 16:05:40 +01002032 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2033 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2034 h1s_destroy(h1s);
2035
2036 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002037 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002038 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002039 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002040 h1c->conn->flags |= CO_FL_PRIVATE;
2041
Olivier Houchard44d59142018-12-13 18:46:22 +01002042 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002043 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002044 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2045 h1c->conn->owner = NULL;
2046 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2047 /* The server doesn't want it, let's kill the connection right away */
2048 h1c->conn->mux->destroy(h1c->conn);
2049 else
2050 tasklet_wakeup(h1c->wait_event.task);
2051 return;
2052
2053 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002054 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002055 if (h1c->conn->owner == sess) {
2056 int ret = session_check_idle_conn(sess, h1c->conn);
2057 if (ret == -1)
2058 /* The connection got destroyed, let's leave */
2059 return;
2060 else if (ret == 1) {
2061 /* The connection was added to the server list,
2062 * wake the task so we can subscribe to events
2063 */
2064 tasklet_wakeup(h1c->wait_event.task);
2065 return;
2066 }
2067 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002068 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002069 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002070 struct server *srv = objt_server(h1c->conn->target);
2071
2072 if (srv) {
2073 if (h1c->conn->flags & CO_FL_PRIVATE)
2074 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002075 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002076 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2077 else
2078 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2079 }
2080 }
2081 }
2082
Christopher Faulet51dbc942018-09-13 09:05:15 +02002083 /* We don't want to close right now unless the connection is in error */
Christopher Faulet666a0c42019-01-08 11:12:04 +01002084 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01002085 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002086 h1_release(h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002087 else {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002088 tasklet_wakeup(h1c->wait_event.task);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002089 if (h1c->task) {
2090 h1c->task->expire = TICK_ETERNITY;
2091 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002092 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002093 ? h1c->shut_timeout
2094 : h1c->timeout));
2095 task_queue(h1c->task);
2096 }
2097 }
2098 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002099}
2100
2101
2102static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2103{
2104 struct h1s *h1s = cs->ctx;
2105
2106 if (!h1s)
2107 return;
2108
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002109 if ((h1s->flags & H1S_F_WANT_KAL) &&
2110 !(cs->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002111 return;
2112
Christopher Faulet51dbc942018-09-13 09:05:15 +02002113 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2114 if (cs->flags & CS_FL_SHR)
2115 return;
2116 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
2117 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002118 if ((cs->conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2119 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002120}
2121
2122static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2123{
2124 struct h1s *h1s = cs->ctx;
2125 struct h1c *h1c;
2126
2127 if (!h1s)
2128 return;
2129 h1c = h1s->h1c;
2130
Christopher Fauletf2824e62018-10-01 12:12:37 +02002131 if ((h1s->flags & H1S_F_WANT_KAL) &&
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002132 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) &&
2133
Christopher Fauletf2824e62018-10-01 12:12:37 +02002134 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2135 return;
2136
Christopher Faulet51dbc942018-09-13 09:05:15 +02002137 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2138 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2139 return;
2140
Christopher Faulet666a0c42019-01-08 11:12:04 +01002141 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002142}
2143
Christopher Faulet666a0c42019-01-08 11:12:04 +01002144static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002145{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002146 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002147
Christopher Faulet666a0c42019-01-08 11:12:04 +01002148 conn_xprt_shutw(conn);
2149 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2150 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2151 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002152}
2153
2154/* Called from the upper layer, to unsubscribe to events */
2155static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2156{
2157 struct wait_event *sw;
2158 struct h1s *h1s = cs->ctx;
2159
2160 if (!h1s)
2161 return 0;
2162
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002163 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002164 sw = param;
2165 if (h1s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002166 sw->events &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002167 h1s->recv_wait = NULL;
2168 }
2169 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002170 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002171 sw = param;
2172 if (h1s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002173 sw->events &= ~SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002174 h1s->send_wait = NULL;
2175 }
2176 }
2177 return 0;
2178}
2179
2180/* Called from the upper layer, to subscribe to events, such as being able to send */
2181static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2182{
2183 struct wait_event *sw;
2184 struct h1s *h1s = cs->ctx;
2185
2186 if (!h1s)
2187 return -1;
2188
2189 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002190 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002191 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002192 if (!(sw->events & SUB_RETRY_RECV)) {
2193 sw->events |= SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002194 sw->handle = h1s;
2195 h1s->recv_wait = sw;
2196 }
2197 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002198 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002199 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002200 if (!(sw->events & SUB_RETRY_SEND)) {
2201 sw->events |= SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002202 sw->handle = h1s;
2203 h1s->send_wait = sw;
2204 }
2205 return 0;
2206 default:
2207 break;
2208 }
2209 return -1;
2210}
2211
2212/* Called from the upper layer, to receive data */
2213static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2214{
2215 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002216 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002217 size_t ret = 0;
2218
Christopher Faulet539e0292018-11-19 10:40:09 +01002219 if (!(h1c->flags & H1C_F_IN_ALLOC))
2220 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002221
2222 if (flags & CO_RFL_BUF_FLUSH)
2223 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002224 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2225 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002226 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002227 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002228 }
2229 return ret;
2230}
2231
2232
2233/* Called from the upper layer, to send data */
2234static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2235{
2236 struct h1s *h1s = cs->ctx;
2237 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002238 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002239
2240 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002241 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002242
2243 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002244 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2245 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002246
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002247 while (total != count) {
2248 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002249
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002250 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2251 ret = h1_process_output(h1c, buf, count);
2252 if (!ret)
2253 break;
2254 total += ret;
2255 if (!h1_send(h1c))
2256 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002257 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002258
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002259 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002260}
2261
Christopher Faulet1be55f92018-10-02 15:59:23 +02002262#if defined(CONFIG_HAP_LINUX_SPLICE)
2263/* Send and get, using splicing */
2264static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2265{
2266 struct h1s *h1s = cs->ctx;
2267 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2268 int ret = 0;
2269
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002270 if (b_data(&h1s->h1c->ibuf)) {
2271 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002272 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002273 }
2274
2275 h1s->flags &= ~H1S_F_BUF_FLUSH;
2276 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002277 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2278 count = h1m->curr_len;
2279 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2280 if (h1m->state == H1_MSG_DATA && ret > 0)
2281 h1m->curr_len -= ret;
2282 end:
2283 return ret;
2284
2285}
2286
2287static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2288{
2289 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002290 int ret = 0;
2291
2292 if (b_data(&h1s->h1c->obuf))
2293 goto end;
2294
2295 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002296 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002297 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002298 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
2299 cs->conn->xprt->subscribe(cs->conn, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002300 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002301 return ret;
2302}
2303#endif
2304
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002305/* for debugging with CLI's "show fd" command */
2306static void h1_show_fd(struct buffer *msg, struct connection *conn)
2307{
2308 struct h1c *h1c = conn->ctx;
2309 struct h1s *h1s = h1c->h1s;
2310
Christopher Fauletf376a312019-01-04 15:16:06 +01002311 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2312 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002313 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2314 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2315 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2316 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2317
2318 if (h1s) {
2319 char *method;
2320
2321 if (h1s->meth < HTTP_METH_OTHER)
2322 method = http_known_methods[h1s->meth].ptr;
2323 else
2324 method = "UNKNOWN";
2325 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2326 " .meth=%s status=%d",
2327 h1s, h1s->flags,
2328 h1m_state_str(h1s->req.state),
2329 h1m_state_str(h1s->res.state), method, h1s->status);
2330 if (h1s->cs)
2331 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2332 h1s->cs->flags, h1s->cs->data);
2333 }
2334}
2335
Christopher Faulet51dbc942018-09-13 09:05:15 +02002336/****************************************/
2337/* MUX initialization and instanciation */
2338/****************************************/
2339
2340/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002341static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002342 .init = h1_init,
2343 .wake = h1_wake,
2344 .attach = h1_attach,
2345 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002346 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002347 .detach = h1_detach,
2348 .destroy = h1_destroy,
2349 .avail_streams = h1_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01002350 .max_streams = h1_max_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002351 .rcv_buf = h1_rcv_buf,
2352 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002353#if defined(CONFIG_HAP_LINUX_SPLICE)
2354 .rcv_pipe = h1_rcv_pipe,
2355 .snd_pipe = h1_snd_pipe,
2356#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002357 .subscribe = h1_subscribe,
2358 .unsubscribe = h1_unsubscribe,
2359 .shutr = h1_shutr,
2360 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002361 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002362 .reset = h1_reset,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002363 .flags = MX_FL_NONE,
2364 .name = "h1",
2365};
2366
2367
2368/* this mux registers default HTX proto */
2369static struct mux_proto_list mux_proto_htx =
2370{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2371
Willy Tarreau0108d902018-11-25 19:14:37 +01002372INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2373
Christopher Faulet51dbc942018-09-13 09:05:15 +02002374/*
2375 * Local variables:
2376 * c-indent-level: 8
2377 * c-basic-offset: 8
2378 * End:
2379 */