blob: 4658e48a8bae60043a56ef6be6ef6476c344ab10 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <common/cfgparse.h>
13#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010014#include <common/h1.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010015#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010016#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020017
Christopher Faulet1be55f92018-10-02 15:59:23 +020018#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020019#include <types/proxy.h>
20#include <types/session.h>
21
Christopher Faulet51dbc942018-09-13 09:05:15 +020022#include <proto/connection.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020023#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020024#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020026#include <proto/stream.h>
27#include <proto/stream_interface.h>
28
29/*
30 * H1 Connection flags (32 bits)
31 */
32#define H1C_F_NONE 0x00000000
33
34/* Flags indicating why writing output data are blocked */
35#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
36#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
37/* 0x00000004 - 0x00000008 unused */
38
39/* Flags indicating why reading input data are blocked. */
40#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
41#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010042#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010043/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020044
45#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
46#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
47#define H1C_F_CS_SHUTW 0x00004000 /* connection is already shut down */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020048#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020049
Christopher Fauletf2824e62018-10-01 12:12:37 +020050#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet129817b2018-09-20 16:14:40 +020051
Christopher Faulet51dbc942018-09-13 09:05:15 +020052/*
53 * H1 Stream flags (32 bits)
54 */
Christopher Faulet129817b2018-09-20 16:14:40 +020055#define H1S_F_NONE 0x00000000
56#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020057#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
58#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Christopher Faulet539e0292018-11-19 10:40:09 +010059/* 0x00000008 unused */
Christopher Fauletf2824e62018-10-01 12:12:37 +020060#define H1S_F_WANT_KAL 0x00000010
61#define H1S_F_WANT_TUN 0x00000020
62#define H1S_F_WANT_CLO 0x00000040
63#define H1S_F_WANT_MSK 0x00000070
64#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010065#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010066#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010067#define H1S_F_HAVE_I_EOD 0x00000400 /* Set during input process to know the last empty chunk was processed */
68#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
69#define H1S_F_HAVE_O_EOD 0x00001000 /* Set during output process to know the last empty chunk was processed */
70#define H1S_F_HAVE_O_TLR 0x00002000 /* Set during output process to know the trailers were processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020071
72/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020073struct h1c {
74 struct connection *conn;
75 struct proxy *px;
76 uint32_t flags; /* Connection flags: H1C_F_* */
77
78 struct buffer ibuf; /* Input buffer to store data before parsing */
79 struct buffer obuf; /* Output buffer to store data after reformatting */
80
81 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
82 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
83
84 struct h1s *h1s; /* H1 stream descriptor */
Christopher 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);
116static void h1_shutw_conn(struct connection *conn);
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 Fauletcf56b992018-12-11 16:12:31 +0100142 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)))
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 */
417 if (h1_recv(h1c))
418 h1_process(h1c);
419
420 /* mux->wake will be called soon to complete the operation */
421 return 0;
422
423 fail:
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100424 if (t)
425 task_free(t);
Christopher Faulet47365272018-10-31 17:40:50 +0100426 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200427 tasklet_free(h1c->wait_event.task);
428 pool_free(pool_head_h1c, h1c);
429 fail_h1c:
430 return -1;
431}
432
433
434/* release function for a connection. This one should be called to free all
435 * resources allocated to the mux.
436 */
437static void h1_release(struct connection *conn)
438{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100439 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200440
441 LIST_DEL(&conn->list);
442
443 if (h1c) {
444 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
445 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
446 LIST_DEL(&h1c->buf_wait.list);
447 LIST_INIT(&h1c->buf_wait.list);
448 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
449 }
450
451 h1_release_buf(h1c, &h1c->ibuf);
452 h1_release_buf(h1c, &h1c->obuf);
453
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100454 if (h1c->task) {
455 h1c->task->context = NULL;
456 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
457 h1c->task = NULL;
458 }
459
Christopher Faulet51dbc942018-09-13 09:05:15 +0200460 if (h1c->wait_event.task)
461 tasklet_free(h1c->wait_event.task);
462
Christopher Fauletf2824e62018-10-01 12:12:37 +0200463 h1s_destroy(h1c->h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100464 if (h1c->wait_event.events != 0)
465 conn->xprt->unsubscribe(conn, h1c->wait_event.events,
Christopher Faulet51dbc942018-09-13 09:05:15 +0200466 &h1c->wait_event);
467 pool_free(pool_head_h1c, h1c);
468 }
469
470 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100471 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200472
473 conn_stop_tracking(conn);
474 conn_full_close(conn);
475 if (conn->destroy_cb)
476 conn->destroy_cb(conn);
477 conn_free(conn);
478}
479
480/******************************************************/
481/* functions below are for the H1 protocol processing */
482/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200483/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
484 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200485 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100486static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200487{
Christopher Faulet570d1612018-11-26 11:13:57 +0100488 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200489
Christopher Faulet570d1612018-11-26 11:13:57 +0100490 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200491 (*(p + 5) > '1' ||
492 (*(p + 5) == '1' && *(p + 7) >= '1')))
493 h1m->flags |= H1_MF_VER_11;
494}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200495
Christopher Faulet9768c262018-10-22 09:34:31 +0200496/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
497 * greater or equal to 1.1
498 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100499static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200500{
Christopher Faulet570d1612018-11-26 11:13:57 +0100501 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200502
Christopher Faulet570d1612018-11-26 11:13:57 +0100503 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200504 (*(p + 5) > '1' ||
505 (*(p + 5) == '1' && *(p + 7) >= '1')))
506 h1m->flags |= H1_MF_VER_11;
507}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200508
Christopher Faulet9768c262018-10-22 09:34:31 +0200509/*
510 * Check the validity of the request version. If the version is valid, it
511 * returns 1. Otherwise, it returns 0.
512 */
513static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
514{
515 struct h1c *h1c = h1s->h1c;
516
517 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
518 * exactly one digit "." one digit. This check may be disabled using
519 * option accept-invalid-http-request.
520 */
521 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
522 if (sl.rq.v.len != 8)
523 return 0;
524
525 if (*(sl.rq.v.ptr + 4) != '/' ||
526 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
527 *(sl.rq.v.ptr + 6) != '.' ||
528 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
529 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200530 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200531 else if (!sl.rq.v.len) {
532 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200533
Christopher Faulet9768c262018-10-22 09:34:31 +0200534 /* RFC 1945 allows only GET for HTTP/0.9 requests */
535 if (sl.rq.meth != HTTP_METH_GET)
536 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200537
Christopher Faulet9768c262018-10-22 09:34:31 +0200538 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
539 if (!sl.rq.u.len)
540 return 0;
541
542 /* Add HTTP version */
543 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100544 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200545 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100546
547 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100548 ((*(sl.rq.v.ptr + 5) > '1') ||
549 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100550 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200551 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200552}
553
Christopher Faulet9768c262018-10-22 09:34:31 +0200554/*
555 * Check the validity of the response version. If the version is valid, it
556 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200557 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200558static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200559{
Christopher Faulet9768c262018-10-22 09:34:31 +0200560 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200561
Christopher Faulet9768c262018-10-22 09:34:31 +0200562 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
563 * exactly one digit "." one digit. This check may be disabled using
564 * option accept-invalid-http-request.
565 */
566 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
567 if (sl.st.v.len != 8)
568 return 0;
569
570 if (*(sl.st.v.ptr + 4) != '/' ||
571 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
572 *(sl.st.v.ptr + 6) != '.' ||
573 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
574 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200575 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100576
577 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100578 ((*(sl.st.v.ptr + 5) > '1') ||
579 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100580 h1m->flags |= H1_MF_VER_11;
581
Christopher Faulet9768c262018-10-22 09:34:31 +0200582 return 1;
583}
584/* Remove all "Connection:" headers from the HTX message <htx> */
585static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
586{
587 struct ist hdr = {.ptr = "Connection", .len = 10};
588 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200589
Christopher Faulet9768c262018-10-22 09:34:31 +0200590 while (http_find_header(htx, hdr, &ctx, 1))
591 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200592
Christopher Faulet9768c262018-10-22 09:34:31 +0200593 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
594}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200595
Christopher Faulet9768c262018-10-22 09:34:31 +0200596/* Add a "Connection:" header with the value <value> into the HTX message
597 * <htx>.
598 */
599static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
600{
601 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200602
Christopher Faulet9768c262018-10-22 09:34:31 +0200603 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200604}
605
606/* Deduce the connection mode of the client connection, depending on the
607 * configuration and the H1 message flags. This function is called twice, the
608 * first time when the request is parsed and the second time when the response
609 * is parsed.
610 */
611static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
612{
613 struct proxy *fe = h1s->h1c->px;
614 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
615
616 /* Tunnel mode can only by set on the frontend */
617 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
618 flag = H1S_F_WANT_TUN;
619 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
620 flag = H1S_F_WANT_CLO;
621
622 /* flags order: CLO > SCL > TUN > KAL */
623 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
624 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
625
626 if (h1m->flags & H1_MF_RESP) {
627 /* Either we've established an explicit tunnel, or we're
628 * switching the protocol. In both cases, we're very unlikely to
629 * understand the next protocols. We have to switch to tunnel
630 * mode, so that we transfer the request and responses then let
631 * this protocol pass unmodified. When we later implement
632 * specific parsers for such protocols, we'll want to check the
633 * Upgrade header which contains information about that protocol
634 * for responses with status 101 (eg: see RFC2817 about TLS).
635 */
636 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
637 h1s->status == 101)
638 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletbd44ca62018-11-29 09:55:22 +0100639 else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */
640 (h1m->flags & H1_MF_CONN_CLO && h1s->req.state != H1_MSG_DONE)) /*explicit close and unfinished request */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200641 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
642 }
643 else {
644 if (h1s->flags & H1S_F_WANT_KAL &&
645 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
646 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
647 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
648 }
649
650 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
651 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
652 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
653}
654
655/* Deduce the connection mode of the client connection, depending on the
656 * configuration and the H1 message flags. This function is called twice, the
657 * first time when the request is parsed and the second time when the response
658 * is parsed.
659 */
660static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
661{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100662 struct h1c *h1c = h1s->h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100663 struct session *sess = h1s->sess;
Christopher Fauleta1692f52018-11-22 10:19:50 +0100664 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200665 int flag = H1S_F_WANT_KAL;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100666 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200667
668 /* Tunnel mode can only by set on the frontend */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100669 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200670 flag = H1S_F_WANT_TUN;
671
672 /* For the server connection: server-close == httpclose */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100673 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200674 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Olivier Houchardf502aca2018-12-14 19:42:40 +0100675 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200676 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
677 flag = H1S_F_WANT_CLO;
678
679 /* flags order: CLO > SCL > TUN > KAL */
680 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
681 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
682
683 if (h1m->flags & H1_MF_RESP) {
684 /* Either we've established an explicit tunnel, or we're
685 * switching the protocol. In both cases, we're very unlikely to
686 * understand the next protocols. We have to switch to tunnel
687 * mode, so that we transfer the request and responses then let
688 * this protocol pass unmodified. When we later implement
689 * specific parsers for such protocols, we'll want to check the
690 * Upgrade header which contains information about that protocol
691 * for responses with status 101 (eg: see RFC2817 about TLS).
692 */
693 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
694 h1s->status == 101)
695 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
696 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
697 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
698 else if (h1s->flags & H1S_F_WANT_KAL &&
699 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
700 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
701 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
702 }
Christopher Faulet70033782018-12-05 13:50:11 +0100703 else {
704 if (h1s->flags & H1S_F_WANT_KAL &&
705 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
706 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
707 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
708 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200709
710 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
711 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
712 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200713}
714
Christopher Faulet9768c262018-10-22 09:34:31 +0200715static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
716 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200717{
718 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200719
720 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
721 * token is found
722 */
723 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200724 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200725
726 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200727 if (h1m->flags & H1_MF_CONN_CLO) {
728 if (conn_val)
729 *conn_val = ist("");
730 if (htx)
731 h1_remove_conn_hdrs(h1m, htx);
732 }
733 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
734 if (conn_val)
735 *conn_val = ist("keep-alive");
736 if (htx)
737 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
738 }
Christopher Fauletce851492018-12-07 18:06:59 +0100739 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
740 if (conn_val)
741 *conn_val = ist("");
742 if (htx)
743 h1_remove_conn_hdrs(h1m, htx);
744 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200745 }
746 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200747 if (h1m->flags & H1_MF_CONN_KAL) {
748 if (conn_val)
749 *conn_val = ist("");
750 if (htx)
751 h1_remove_conn_hdrs(h1m, htx);
752 }
753 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
754 if (conn_val)
755 *conn_val = ist("close");
756 if (htx)
757 h1_add_conn_hdr(h1m, htx, ist("close"));
758 }
Christopher Fauletce851492018-12-07 18:06:59 +0100759 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
760 if (conn_val)
761 *conn_val = ist("");
762 if (htx)
763 h1_remove_conn_hdrs(h1m, htx);
764 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200765 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200766}
767
Christopher Faulet9768c262018-10-22 09:34:31 +0200768static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
769 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200770{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200771 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
772 * token is found
773 */
774 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200775 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200776
777 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200778 if (h1m->flags & H1_MF_CONN_CLO) {
779 if (conn_val)
780 *conn_val = ist("");
781 if (htx)
782 h1_remove_conn_hdrs(h1m, htx);
783 }
784 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
785 if (conn_val)
786 *conn_val = ist("keep-alive");
787 if (htx)
788 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
789 }
Christopher Fauletce851492018-12-07 18:06:59 +0100790 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
791 if (conn_val)
792 *conn_val = ist("");
793 if (htx)
794 h1_remove_conn_hdrs(h1m, htx);
795 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200796 }
797 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200798 if (h1m->flags & H1_MF_CONN_KAL) {
799 if (conn_val)
800 *conn_val = ist("");
801 if (htx)
802 h1_remove_conn_hdrs(h1m, htx);
803 }
804 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
805 if (conn_val)
806 *conn_val = ist("close");
807 if (htx)
808 h1_add_conn_hdr(h1m, htx, ist("close"));
809 }
Christopher Fauletce851492018-12-07 18:06:59 +0100810 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
811 if (conn_val)
812 *conn_val = ist("");
813 if (htx)
814 h1_remove_conn_hdrs(h1m, htx);
815 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200816 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200817}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200818
Christopher Faulet9768c262018-10-22 09:34:31 +0200819/* Set the right connection mode and update "Connection:" header if
820 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
821 * message is updated accordingly. When <conn_val> is not NULL, it is set with
822 * the new header value.
823 */
824static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
825 struct htx *htx, struct ist *conn_val)
826{
827 if (!conn_is_back(h1s->h1c->conn)) {
828 h1_set_cli_conn_mode(h1s, h1m);
829 if (h1m->flags & H1_MF_RESP)
830 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
831 }
832 else {
833 h1_set_srv_conn_mode(h1s, h1m);
834 if (!(h1m->flags & H1_MF_RESP))
835 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
836 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200837}
838
Christopher Faulete44769b2018-11-29 23:01:45 +0100839
840/* Append the description of what is present in error snapshot <es> into <out>.
841 * The description must be small enough to always fit in a buffer. The output
842 * buffer may be the trash so the trash must not be used inside this function.
843 */
844static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
845{
846 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100847 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
848 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
849 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
850 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
851 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
852 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100853}
854/*
855 * Capture a bad request or response and archive it in the proxy's structure.
856 * By default it tries to report the error position as h1m->err_pos. However if
857 * this one is not set, it will then report h1m->next, which is the last known
858 * parsing point. The function is able to deal with wrapping buffers. It always
859 * displays buffers as a contiguous area starting at buf->p. The direction is
860 * determined thanks to the h1m's flags.
861 */
862static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
863 struct h1m *h1m, struct buffer *buf)
864{
865 struct session *sess = h1c->conn->owner;
866 struct proxy *proxy = h1c->px;
867 struct proxy *other_end = sess->fe;
868 union error_snapshot_ctx ctx;
869
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100870 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100871 other_end = si_strm(h1s->cs->data)->be;
872
873 /* http-specific part now */
874 ctx.h1.state = h1m->state;
875 ctx.h1.c_flags = h1c->flags;
876 ctx.h1.s_flags = h1s->flags;
877 ctx.h1.m_flags = h1m->flags;
878 ctx.h1.m_clen = h1m->curr_len;
879 ctx.h1.m_blen = h1m->body_len;
880
881 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
882 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100883 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
884 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100885}
886
Christopher Fauletadb22202018-12-12 10:32:09 +0100887/* Emit the chunksize followed by a CRLF in front of data of the buffer
888 * <buf>. It goes backwards and starts with the byte before the buffer's
889 * head. The caller is responsible for ensuring there is enough room left before
890 * the buffer's head for the string.
891 */
892static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
893{
894 char *beg, *end;
895
896 beg = end = b_head(buf);
897 *--beg = '\n';
898 *--beg = '\r';
899 do {
900 *--beg = hextab[chksz & 0xF];
901 } while (chksz >>= 4);
902 buf->head -= (end - beg);
903 b_add(buf, end - beg);
904}
905
906/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
907 * ensuring there is enough room left in the buffer for the string. */
908static void h1_emit_chunk_crlf(struct buffer *buf)
909{
910 *(b_peek(buf, b_data(buf))) = '\r';
911 *(b_peek(buf, b_data(buf) + 1)) = '\n';
912 b_add(buf, 2);
913}
914
Christopher Faulet129817b2018-09-20 16:14:40 +0200915/*
916 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200917 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
918 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800919 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200920 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200921static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200922 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200923{
924 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100925 union h1_sl h1sl;
926 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200927 int ret = 0;
928
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100929 if (!max)
930 goto end;
931
Christopher Faulet129817b2018-09-20 16:14:40 +0200932 /* Realing input buffer if necessary */
933 if (b_head(buf) + b_data(buf) > b_wrap(buf))
934 b_slow_realign(buf, trash.area, 0);
935
Christopher Fauletf2824e62018-10-01 12:12:37 +0200936 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100937 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200938 if (ret <= 0) {
939 /* Incomplete or invalid message. If the buffer is full, it's an
940 * error because headers are too large to be handled by the
941 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200942 if (ret < 0 || (!ret && b_full(buf)))
943 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200944 goto end;
945 }
946
947 /* messages headers fully parsed, do some checks to prepare the body
948 * parsing.
949 */
950
951 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200952 if (ret > (b_size(buf) - global.tune.maxrewrite))
953 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200954
Christopher Faulet9768c262018-10-22 09:34:31 +0200955 /* Save the request's method or the response's status, check if the body
956 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200957 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100958 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200959
Christopher Faulet129817b2018-09-20 16:14:40 +0200960 /* Request have always a known length */
961 h1m->flags |= H1_MF_XFER_LEN;
962 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
963 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200964
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100965 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
966 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200967 h1m->err_state = h1m->state;
968 goto vsn_error;
969 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200970 }
971 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100972 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200973
974 if ((h1s->meth == HTTP_METH_HEAD) ||
975 (h1s->status >= 100 && h1s->status < 200) ||
976 (h1s->status == 204) || (h1s->status == 304) ||
977 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
978 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
979 h1m->flags |= H1_MF_XFER_LEN;
980 h1m->curr_len = h1m->body_len = 0;
981 h1m->state = H1_MSG_DONE;
982 }
983 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
984 h1m->flags |= H1_MF_XFER_LEN;
985 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
986 h1m->state = H1_MSG_DONE;
987 }
988 else
989 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200990
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100991 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
992 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200993 h1m->err_state = h1m->state;
994 goto vsn_error;
995 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200996 }
997
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100998 /* Set HTX start-line flags */
999 if (h1m->flags & H1_MF_VER_11)
1000 flags |= HTX_SL_F_VER_11;
1001 if (h1m->flags & H1_MF_XFER_ENC)
1002 flags |= HTX_SL_F_XFER_ENC;
1003 if (h1m->flags & H1_MF_XFER_LEN) {
1004 flags |= HTX_SL_F_XFER_LEN;
1005 if (h1m->flags & H1_MF_CHNK)
1006 flags |= HTX_SL_F_CHNK;
1007 else if (h1m->flags & H1_MF_CLEN)
1008 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001009 if (h1m->state == H1_MSG_DONE)
1010 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001011 }
1012
Christopher Faulet9768c262018-10-22 09:34:31 +02001013 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001014 struct htx_sl *sl;
1015
1016 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1017 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001018 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001019 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001020 }
1021 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001022 struct htx_sl *sl;
1023
1024 flags |= HTX_SL_F_IS_RESP;
1025 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1026 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001027 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001028 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001029 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001030
Christopher Faulet9768c262018-10-22 09:34:31 +02001031 if (h1m->state == H1_MSG_DONE)
1032 if (!htx_add_endof(htx, HTX_BLK_EOM))
1033 goto error;
1034
1035 h1_process_conn_mode(h1s, h1m, htx, NULL);
1036
1037 /* If body length cannot be determined, set htx->extra to
1038 * ULLONG_MAX. This value is impossible in other cases.
1039 */
1040 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
1041
1042 /* Recheck there is enough space to do headers rewritting */
1043 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
1044 goto error;
1045
1046 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001047 end:
1048 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001049
1050 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001051 h1m->err_state = h1m->state;
1052 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001053 vsn_error:
1054 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +01001055 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001056 ret = 0;
1057 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001058}
1059
1060/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001061 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1062 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001063 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001064 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001065 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001066static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001067 struct buffer *buf, size_t *ofs, size_t max,
1068 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001069{
Christopher Faulet9768c262018-10-22 09:34:31 +02001070 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +02001071 size_t total = 0;
1072 int ret = 0;
1073
1074 if (h1m->flags & H1_MF_XFER_LEN) {
1075 if (h1m->flags & H1_MF_CLEN) {
1076 /* content-length: read only h2m->body_len */
1077 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001078 if (ret > data_space)
1079 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001080 if ((uint64_t)ret > h1m->curr_len)
1081 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001082 if (ret > b_contig_data(buf, *ofs))
1083 ret = b_contig_data(buf, *ofs);
1084 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001085 /* very often with large files we'll face the following
1086 * situation :
1087 * - htx is empty and points to <htxbuf>
1088 * - ret == buf->data
1089 * - buf->head == sizeof(struct htx)
1090 * => we can swap the buffers and place an htx header into
1091 * the target buffer instead
1092 */
1093 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1094 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1095 void *raw_area = buf->area;
1096 void *htx_area = htxbuf->area;
1097 struct htx_blk *blk;
1098
1099 buf->area = htx_area;
1100 htxbuf->area = raw_area;
1101 htx = (struct htx *)htxbuf->area;
1102 htx->size = htxbuf->size - sizeof(*htx);
1103 htx_reset(htx);
1104 b_set_data(htxbuf, b_size(htxbuf));
1105
1106 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1107 blk->info += ret;
1108 /* nothing else to do, the old buffer now contains an
1109 * empty pre-initialized HTX header
1110 */
1111 }
1112 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001113 goto end;
1114 h1m->curr_len -= ret;
1115 *ofs += ret;
1116 total += ret;
1117 }
1118
1119 if (!h1m->curr_len) {
1120 if (!htx_add_endof(htx, HTX_BLK_EOM))
1121 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001122 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +02001123 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001124 }
1125 else if (h1m->flags & H1_MF_CHNK) {
1126 new_chunk:
1127 /* te:chunked : parse chunks */
1128 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001129 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001130 if (ret <= 0)
1131 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001132 h1m->state = H1_MSG_CHUNK_SIZE;
1133
Christopher Faulet129817b2018-09-20 16:14:40 +02001134 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001135 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001136 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001137 }
1138
1139 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1140 unsigned int chksz;
1141
Christopher Fauletf2824e62018-10-01 12:12:37 +02001142 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001143 if (ret <= 0)
1144 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001145 if (!chksz) {
1146 if (!htx_add_endof(htx, HTX_BLK_EOD))
1147 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001148 h1s->flags |= H1S_F_HAVE_I_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001149 h1m->state = H1_MSG_TRAILERS;
1150 }
1151 else
1152 h1m->state = H1_MSG_DATA;
1153
Christopher Faulet129817b2018-09-20 16:14:40 +02001154 h1m->curr_len = chksz;
1155 h1m->body_len += chksz;
1156 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001157 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001158 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001159 }
1160
1161 if (h1m->state == H1_MSG_DATA) {
1162 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001163 if (ret > data_space)
1164 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001165 if ((uint64_t)ret > h1m->curr_len)
1166 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001167 if (ret > b_contig_data(buf, *ofs))
1168 ret = b_contig_data(buf, *ofs);
1169 if (ret) {
1170 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1171 goto end;
1172 h1m->curr_len -= ret;
1173 max -= ret;
1174 *ofs += ret;
1175 total += ret;
1176 }
1177 if (!h1m->curr_len) {
1178 h1m->state = H1_MSG_CHUNK_CRLF;
1179 goto new_chunk;
1180 }
1181 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001182 }
1183
1184 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001185 /* Trailers were alread parsed, only the EOM
1186 * need to be added */
Willy Tarreau34d23482019-01-03 17:46:56 +01001187 if (h1s->flags & H1S_F_HAVE_I_TLR)
Christopher Faulet17276482018-11-22 11:44:35 +01001188 goto skip_tlr_parsing;
1189
Christopher Fauletf2824e62018-10-01 12:12:37 +02001190 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001191 if (ret > data_space)
1192 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001193 if (ret <= 0)
1194 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001195
1196 /* Realing input buffer if tailers wrap. For now
1197 * this is a workaroung. Because trailers are
1198 * not split on CRLF, like headers, there is no
1199 * way to know where to split it when trailers
1200 * wrap. This is a limitation of
1201 * h1_measure_trailers.
1202 */
1203 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1204 b_slow_realign(buf, trash.area, 0);
1205
1206 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1207 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001208 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001210 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001211 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001212
Christopher Faulet17276482018-11-22 11:44:35 +01001213 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001214 if (!htx_add_endof(htx, HTX_BLK_EOM))
1215 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001216 h1m->state = H1_MSG_DONE;
1217 }
1218 }
1219 else {
1220 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1221 * is no body. Switch the message in DONE state
1222 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001223 if (!htx_add_endof(htx, HTX_BLK_EOM))
1224 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001225 h1m->state = H1_MSG_DONE;
1226 }
1227 }
1228 else {
1229 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001230 ret = max;
1231 if (ret > data_space)
1232 ret = data_space;
1233 if (ret > b_contig_data(buf, *ofs))
1234 ret = b_contig_data(buf, *ofs);
1235 if (ret) {
1236 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1237 goto end;
1238
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001239 *ofs += ret;
1240 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001241 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001242 }
1243
1244 end:
1245 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001246 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001247 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001248 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001249 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001250 return 0;
1251 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001252 /* update htx->extra, only when the body length is known */
1253 if (h1m->flags & H1_MF_XFER_LEN)
1254 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001255 return total;
1256}
1257
1258/*
1259 * Synchronize the request and the response before reseting them. Except for 1xx
1260 * responses, we wait that the request and the response are in DONE state and
1261 * that all data are forwarded for both. For 1xx responses, only the response is
1262 * reset, waiting the final one. Many 1xx messages can be sent.
1263 */
1264static void h1_sync_messages(struct h1c *h1c)
1265{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001266 struct h1s *h1s = h1c->h1s;
1267
1268 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001269 return;
1270
Christopher Fauletf2824e62018-10-01 12:12:37 +02001271 if (h1s->res.state == H1_MSG_DONE &&
1272 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001273 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001274 /* For 100-Continue response or any other informational 1xx
1275 * response which is non-final, don't reset the request, the
1276 * transaction is not finished. We take care the response was
1277 * transferred before.
1278 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001279 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001280 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001281 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet129817b2018-09-20 16:14:40 +02001282 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001283 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001284 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1285 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001286 h1m_init_req(&h1s->req);
1287 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001288 h1s->req.state = H1_MSG_TUNNEL;
1289 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001290 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001291 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001292 }
1293}
1294
1295/*
1296 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001297 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1298 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001299 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001300static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001301{
Christopher Faulet539e0292018-11-19 10:40:09 +01001302 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001303 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001304 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001305 size_t total = 0;
1306 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001307 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001308 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001309
Christopher Faulet539e0292018-11-19 10:40:09 +01001310 htx = htx_from_buf(buf);
1311 count = b_data(&h1c->ibuf);
1312 max = htx_free_space(htx);
1313 if (flags & CO_RFL_KEEP_RSV) {
1314 if (max < global.tune.maxrewrite)
1315 goto end;
1316 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001317 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001318 if (count > max)
1319 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001320
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001321 if (!count)
1322 goto end;
1323
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) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001340 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1341 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) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001350 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1351 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 */
1661 if (tmp->area == h1c->obuf.area)
1662 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)
1818 h1_shutw_conn(conn);
1819 }
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 ||
1848 conn->flags & CO_FL_ERROR ||
1849 conn_xprt_read0_pending(conn))
1850 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001851 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
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 ||
1865 conn->flags & CO_FL_ERROR)) {
1866 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)) {
1879 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))
1880 ? 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 */
2084 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
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)) {
2092 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))
2093 ? 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));
2118 if (cs->flags & CS_FL_SHW) {
2119 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
2120 conn_full_close(cs->conn);
2121 }
2122}
2123
2124static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2125{
2126 struct h1s *h1s = cs->ctx;
2127 struct h1c *h1c;
2128
2129 if (!h1s)
2130 return;
2131 h1c = h1s->h1c;
2132
Christopher Fauletf2824e62018-10-01 12:12:37 +02002133 if ((h1s->flags & H1S_F_WANT_KAL) &&
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002134 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) &&
2135
Christopher Fauletf2824e62018-10-01 12:12:37 +02002136 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2137 return;
2138
Christopher Faulet51dbc942018-09-13 09:05:15 +02002139 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2140 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2141 return;
2142
2143 h1_shutw_conn(cs->conn);
2144}
2145
2146static void h1_shutw_conn(struct connection *conn)
2147{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002148 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002149
2150 if (conn_xprt_ready(conn) && conn->xprt->shutw)
2151 conn->xprt->shutw(conn, 1);
2152 if (!(conn->flags & CO_FL_SOCK_RD_SH))
2153 conn_sock_shutw(conn, 1);
2154 else {
2155 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
2156 conn_full_close(conn);
2157 }
2158}
2159
2160/* Called from the upper layer, to unsubscribe to events */
2161static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2162{
2163 struct wait_event *sw;
2164 struct h1s *h1s = cs->ctx;
2165
2166 if (!h1s)
2167 return 0;
2168
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002169 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002170 sw = param;
2171 if (h1s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002172 sw->events &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002173 h1s->recv_wait = NULL;
2174 }
2175 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002176 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002177 sw = param;
2178 if (h1s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002179 sw->events &= ~SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002180 h1s->send_wait = NULL;
2181 }
2182 }
2183 return 0;
2184}
2185
2186/* Called from the upper layer, to subscribe to events, such as being able to send */
2187static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2188{
2189 struct wait_event *sw;
2190 struct h1s *h1s = cs->ctx;
2191
2192 if (!h1s)
2193 return -1;
2194
2195 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002196 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002197 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002198 if (!(sw->events & SUB_RETRY_RECV)) {
2199 sw->events |= SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002200 sw->handle = h1s;
2201 h1s->recv_wait = sw;
2202 }
2203 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002204 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002205 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002206 if (!(sw->events & SUB_RETRY_SEND)) {
2207 sw->events |= SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002208 sw->handle = h1s;
2209 h1s->send_wait = sw;
2210 }
2211 return 0;
2212 default:
2213 break;
2214 }
2215 return -1;
2216}
2217
2218/* Called from the upper layer, to receive data */
2219static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2220{
2221 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002222 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002223 size_t ret = 0;
2224
Christopher Faulet539e0292018-11-19 10:40:09 +01002225 if (!(h1c->flags & H1C_F_IN_ALLOC))
2226 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002227
2228 if (flags & CO_RFL_BUF_FLUSH)
2229 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002230 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2231 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002232 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002233 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002234 }
2235 return ret;
2236}
2237
2238
2239/* Called from the upper layer, to send data */
2240static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2241{
2242 struct h1s *h1s = cs->ctx;
2243 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002244 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002245
2246 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002247 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002248
2249 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002250 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2251 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002252
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002253 while (total != count) {
2254 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002255
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002256 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2257 ret = h1_process_output(h1c, buf, count);
2258 if (!ret)
2259 break;
2260 total += ret;
2261 if (!h1_send(h1c))
2262 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002263 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002264
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002265 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002266}
2267
Christopher Faulet1be55f92018-10-02 15:59:23 +02002268#if defined(CONFIG_HAP_LINUX_SPLICE)
2269/* Send and get, using splicing */
2270static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2271{
2272 struct h1s *h1s = cs->ctx;
2273 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2274 int ret = 0;
2275
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002276 if (b_data(&h1s->h1c->ibuf)) {
2277 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002278 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002279 }
2280
2281 h1s->flags &= ~H1S_F_BUF_FLUSH;
2282 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002283 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2284 count = h1m->curr_len;
2285 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2286 if (h1m->state == H1_MSG_DATA && ret > 0)
2287 h1m->curr_len -= ret;
2288 end:
2289 return ret;
2290
2291}
2292
2293static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2294{
2295 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002296 int ret = 0;
2297
2298 if (b_data(&h1s->h1c->obuf))
2299 goto end;
2300
2301 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002302 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002303 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002304 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
2305 cs->conn->xprt->subscribe(cs->conn, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002306 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002307 return ret;
2308}
2309#endif
2310
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002311/* for debugging with CLI's "show fd" command */
2312static void h1_show_fd(struct buffer *msg, struct connection *conn)
2313{
2314 struct h1c *h1c = conn->ctx;
2315 struct h1s *h1s = h1c->h1s;
2316
Christopher Fauletf376a312019-01-04 15:16:06 +01002317 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2318 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002319 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2320 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2321 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2322 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2323
2324 if (h1s) {
2325 char *method;
2326
2327 if (h1s->meth < HTTP_METH_OTHER)
2328 method = http_known_methods[h1s->meth].ptr;
2329 else
2330 method = "UNKNOWN";
2331 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2332 " .meth=%s status=%d",
2333 h1s, h1s->flags,
2334 h1m_state_str(h1s->req.state),
2335 h1m_state_str(h1s->res.state), method, h1s->status);
2336 if (h1s->cs)
2337 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2338 h1s->cs->flags, h1s->cs->data);
2339 }
2340}
2341
Christopher Faulet51dbc942018-09-13 09:05:15 +02002342/****************************************/
2343/* MUX initialization and instanciation */
2344/****************************************/
2345
2346/* The mux operations */
2347const struct mux_ops mux_h1_ops = {
2348 .init = h1_init,
2349 .wake = h1_wake,
2350 .attach = h1_attach,
2351 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002352 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002353 .detach = h1_detach,
2354 .destroy = h1_destroy,
2355 .avail_streams = h1_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01002356 .max_streams = h1_max_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002357 .rcv_buf = h1_rcv_buf,
2358 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002359#if defined(CONFIG_HAP_LINUX_SPLICE)
2360 .rcv_pipe = h1_rcv_pipe,
2361 .snd_pipe = h1_snd_pipe,
2362#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002363 .subscribe = h1_subscribe,
2364 .unsubscribe = h1_unsubscribe,
2365 .shutr = h1_shutr,
2366 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002367 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002368 .reset = h1_reset,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002369 .flags = MX_FL_NONE,
2370 .name = "h1",
2371};
2372
2373
2374/* this mux registers default HTX proto */
2375static struct mux_proto_list mux_proto_htx =
2376{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2377
Willy Tarreau0108d902018-11-25 19:14:37 +01002378INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2379
Christopher Faulet51dbc942018-09-13 09:05:15 +02002380/*
2381 * Local variables:
2382 * c-indent-level: 8
2383 * c-basic-offset: 8
2384 * End:
2385 */