blob: 86fadc4bf14d3ec09fddb15d31194a7878bbca02 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <common/cfgparse.h>
13#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010014#include <common/h1.h>
Christopher Faulet0ef372a2019-04-08 10:57:20 +020015#include <common/h2.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010017#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020018
Christopher Faulet98fbe952019-07-22 16:18:24 +020019#include <ebistree.h>
20
Christopher Faulet1be55f92018-10-02 15:59:23 +020021#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020022#include <types/proxy.h>
23#include <types/session.h>
24
Christopher Faulet51dbc942018-09-13 09:05:15 +020025#include <proto/connection.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020026#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020027#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010028#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020029#include <proto/stream.h>
30#include <proto/stream_interface.h>
31
32/*
33 * H1 Connection flags (32 bits)
34 */
35#define H1C_F_NONE 0x00000000
36
37/* Flags indicating why writing output data are blocked */
38#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
39#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
40/* 0x00000004 - 0x00000008 unused */
41
42/* Flags indicating why reading input data are blocked. */
43#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
44#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010045#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010046/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020047
48#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
49#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010050#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Faulet51dbc942018-09-13 09:05:15 +020051
Christopher Fauletf2824e62018-10-01 12:12:37 +020052#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020053#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020054
Christopher Faulet51dbc942018-09-13 09:05:15 +020055/*
56 * H1 Stream flags (32 bits)
57 */
Christopher Faulet129817b2018-09-20 16:14:40 +020058#define H1S_F_NONE 0x00000000
59#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020060#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
61#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020062#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020063#define H1S_F_WANT_KAL 0x00000010
64#define H1S_F_WANT_TUN 0x00000020
65#define H1S_F_WANT_CLO 0x00000040
66#define H1S_F_WANT_MSK 0x00000070
67#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010068#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010069#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010070#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
Christopher Faulet2d7c5392019-06-03 10:41:26 +020071/* 0x00001000 .. 0x00002000 unused */
Christopher Fauletada34b62019-05-24 16:36:43 +020072#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020073
74/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020075struct h1c {
76 struct connection *conn;
77 struct proxy *px;
78 uint32_t flags; /* Connection flags: H1C_F_* */
79
80 struct buffer ibuf; /* Input buffer to store data before parsing */
81 struct buffer obuf; /* Output buffer to store data after reformatting */
82
83 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
84 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
85
86 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010087 struct task *task; /* timeout management task */
88 int timeout; /* idle timeout duration in ticks */
89 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020090};
91
92/* H1 stream descriptor */
93struct h1s {
94 struct h1c *h1c;
95 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010096 struct cs_info csinfo; /* CS info, only used for client connections */
97 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020098
Christopher Faulet51dbc942018-09-13 09:05:15 +020099 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
100 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200101
Olivier Houchardf502aca2018-12-14 19:42:40 +0100102 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200103 struct h1m req;
104 struct h1m res;
105
106 enum http_meth_t meth; /* HTTP resquest method */
107 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200108};
109
Christopher Faulet98fbe952019-07-22 16:18:24 +0200110/* Map of headers used to convert outgoing headers */
111struct h1_hdrs_map {
112 char *name;
113 struct eb_root map;
114};
115
116/* An entry in a headers map */
117struct h1_hdr_entry {
118 struct ist name;
119 struct ebpt_node node;
120};
121
122/* Declare the headers map */
123static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
124
125
Christopher Faulet51dbc942018-09-13 09:05:15 +0200126/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100127DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
128DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200129
Christopher Faulet51dbc942018-09-13 09:05:15 +0200130static int h1_recv(struct h1c *h1c);
131static int h1_send(struct h1c *h1c);
132static int h1_process(struct h1c *h1c);
133static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100134static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100135static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200136
137/*****************************************************/
138/* functions below are for dynamic buffer management */
139/*****************************************************/
140/*
141 * Indicates whether or not the we may call the h1_recv() function to
142 * attempt to receive data into the buffer and/or parse pending data. The
143 * condition is a bit complex due to some API limits for now. The rules are the
144 * following :
145 * - if an error or a shutdown was detected on the connection and the buffer
146 * is empty, we must not attempt to receive
147 * - if the input buffer failed to be allocated, we must not try to receive
148 * and we know there is nothing pending
149 * - if no flag indicates a blocking condition, we may attempt to receive,
150 * regardless of whether the input buffer is full or not, so that only de
151 * receiving part decides whether or not to block. This is needed because
152 * the connection API indeed prevents us from re-enabling receipt that is
153 * already enabled in a polled state, so we must always immediately stop as
154 * soon as the mux can't proceed so as never to hit an end of read with data
155 * pending in the buffers.
156 * - otherwise must may not attempt to receive
157 */
158static inline int h1_recv_allowed(const struct h1c *h1c)
159{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100160 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200161 return 0;
162
Christopher Fauletcf56b992018-12-11 16:12:31 +0100163 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
164 return 0;
165
Christopher Fauletcb55f482018-12-10 11:56:47 +0100166 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200167 return 1;
168
169 return 0;
170}
171
172/*
173 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
174 * flags are used to figure what buffer was requested. It returns 1 if the
175 * allocation succeeds, in which case the connection is woken up, or 0 if it's
176 * impossible to wake up and we prefer to be woken up later.
177 */
178static int h1_buf_available(void *target)
179{
180 struct h1c *h1c = target;
181
182 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
183 h1c->flags &= ~H1C_F_IN_ALLOC;
184 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200185 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200186 return 1;
187 }
188
189 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
190 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200191 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200192 return 1;
193 }
194
Christopher Faulet51dbc942018-09-13 09:05:15 +0200195 return 0;
196}
197
198/*
199 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
200 */
201static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
202{
203 struct buffer *buf = NULL;
204
205 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
206 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
207 h1c->buf_wait.target = h1c;
208 h1c->buf_wait.wakeup_cb = h1_buf_available;
209 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
210 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
211 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
212 __conn_xprt_stop_recv(h1c->conn);
213 }
214 return buf;
215}
216
217/*
218 * Release a buffer, if any, and try to wake up entities waiting in the buffer
219 * wait queue.
220 */
221static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
222{
223 if (bptr->size) {
224 b_free(bptr);
225 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
226 }
227}
228
Willy Tarreau00f18a32019-01-26 12:19:01 +0100229/* returns the number of streams in use on a connection to figure if it's
230 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
231 * as the caller will want to know if it was the last one after a detach().
232 */
233static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200234{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100235 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200236
Willy Tarreau00f18a32019-01-26 12:19:01 +0100237 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200238}
239
Willy Tarreau00f18a32019-01-26 12:19:01 +0100240/* returns the number of streams still available on a connection */
241static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100242{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100243 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100244}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200245
Willy Tarreau00f18a32019-01-26 12:19:01 +0100246
Christopher Faulet51dbc942018-09-13 09:05:15 +0200247/*****************************************************************/
248/* functions below are dedicated to the mux setup and management */
249/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200250
251/* returns non-zero if there are input data pending for stream h1s. */
252static inline size_t h1s_data_pending(const struct h1s *h1s)
253{
254 const struct h1m *h1m;
255
256 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
257 if (h1m->state == H1_MSG_DONE)
258 return 0; // data not for this stream (e.g. pipelining)
259
260 return b_data(&h1s->h1c->ibuf);
261}
262
Christopher Faulet47365272018-10-31 17:40:50 +0100263static struct conn_stream *h1s_new_cs(struct h1s *h1s)
264{
265 struct conn_stream *cs;
266
267 cs = cs_new(h1s->h1c->conn);
268 if (!cs)
269 goto err;
270 h1s->cs = cs;
271 cs->ctx = h1s;
272
273 if (h1s->flags & H1S_F_NOT_FIRST)
274 cs->flags |= CS_FL_NOT_FIRST;
275
276 if (stream_create_from_cs(cs) < 0)
277 goto err;
278 return cs;
279
280 err:
281 cs_free(cs);
282 h1s->cs = NULL;
283 return NULL;
284}
285
Olivier Houchardf502aca2018-12-14 19:42:40 +0100286static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200287{
288 struct h1s *h1s;
289
290 h1s = pool_alloc(pool_head_h1s);
291 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100292 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200293
294 h1s->h1c = h1c;
295 h1c->h1s = h1s;
296
Olivier Houchardf502aca2018-12-14 19:42:40 +0100297 h1s->sess = sess;
298
Christopher Faulet51dbc942018-09-13 09:05:15 +0200299 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100300 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200301
302 h1s->recv_wait = NULL;
303 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200304
305 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100306 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200307
Christopher Faulet129817b2018-09-20 16:14:40 +0200308 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100309 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200310
311 h1s->status = 0;
312 h1s->meth = HTTP_METH_OTHER;
313
Christopher Faulet47365272018-10-31 17:40:50 +0100314 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
315 h1s->flags |= H1S_F_NOT_FIRST;
316 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
317
Christopher Faulet129817b2018-09-20 16:14:40 +0200318 if (!conn_is_back(h1c->conn)) {
319 if (h1c->px->options2 & PR_O2_REQBUG_OK)
320 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200321
322 /* For frontend connections we should always have a session */
323 if (!sess)
324 sess = h1c->conn->owner;
325
Dave Pirotte234740f2019-07-10 13:57:38 +0000326 /* Timers for subsequent sessions on the same HTTP 1.x connection
327 * measure from `now`, not from the connection accept time */
328 if (h1s->flags & H1S_F_NOT_FIRST) {
329 h1s->csinfo.create_date = date;
330 h1s->csinfo.tv_create = now;
331 h1s->csinfo.t_handshake = 0;
332 h1s->csinfo.t_idle = -1;
333 }
334 else {
335 h1s->csinfo.create_date = sess->accept_date;
336 h1s->csinfo.tv_create = sess->tv_accept;
337 h1s->csinfo.t_handshake = sess->t_handshake;
338 h1s->csinfo.t_idle = -1;
339 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200340 }
341 else {
342 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
343 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200344
Christopher Fauletfeb11742018-11-29 15:12:34 +0100345 h1s->csinfo.create_date = date;
346 h1s->csinfo.tv_create = now;
347 h1s->csinfo.t_handshake = 0;
348 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200349 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100350
Christopher Faulete9b70722019-04-08 10:46:02 +0200351 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
352 * create a new one.
353 */
354 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200355 cs->ctx = h1s;
356 h1s->cs = cs;
357 }
Christopher Faulet47365272018-10-31 17:40:50 +0100358 else {
359 cs = h1s_new_cs(h1s);
360 if (!cs)
361 goto fail;
362 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200363 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100364
365 fail:
366 pool_free(pool_head_h1s, h1s);
367 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200368}
369
370static void h1s_destroy(struct h1s *h1s)
371{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200372 if (h1s) {
373 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200374
Christopher Fauletf2824e62018-10-01 12:12:37 +0200375 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200376
Christopher Fauletf2824e62018-10-01 12:12:37 +0200377 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100378 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200379 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100380 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200381
Christopher Fauletcb55f482018-12-10 11:56:47 +0100382 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100383 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
384 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
385 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200386 pool_free(pool_head_h1s, h1s);
387 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200388}
389
Christopher Fauletfeb11742018-11-29 15:12:34 +0100390static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
391{
392 struct h1s *h1s = cs->ctx;
393
394 if (h1s && !conn_is_back(cs->conn))
395 return &h1s->csinfo;
396 return NULL;
397}
398
Christopher Faulet51dbc942018-09-13 09:05:15 +0200399/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200400 * Initialize the mux once it's attached. It is expected that conn->ctx points
401 * to the existing conn_stream (for outgoing connections or for incoming onces
402 * during a mux upgrade) or NULL (for incoming ones during the connexion
403 * establishment). <input> is always used as Input buffer and may contain
404 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
405 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200406 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200407static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
408 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200409{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200410 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100411 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200412
413 h1c = pool_alloc(pool_head_h1c);
414 if (!h1c)
415 goto fail_h1c;
416 h1c->conn = conn;
417 h1c->px = proxy;
418
419 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200420 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200421 h1c->obuf = BUF_NULL;
422 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200423 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200424
Christopher Faulet51dbc942018-09-13 09:05:15 +0200425 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200426 h1c->wait_event.tasklet = tasklet_new();
427 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200428 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200429 h1c->wait_event.tasklet->process = h1_io_cb;
430 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100431 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200432
Christopher Faulete9b70722019-04-08 10:46:02 +0200433 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100434 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
435 if (tick_isset(proxy->timeout.serverfin))
436 h1c->shut_timeout = proxy->timeout.serverfin;
437 } else {
438 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
439 if (tick_isset(proxy->timeout.clientfin))
440 h1c->shut_timeout = proxy->timeout.clientfin;
441 }
442 if (tick_isset(h1c->timeout)) {
443 t = task_new(tid_bit);
444 if (!t)
445 goto fail;
446
447 h1c->task = t;
448 t->process = h1_timeout_task;
449 t->context = h1c;
450 t->expire = tick_add(now_ms, h1c->timeout);
451 }
452
Christopher Fauletf2824e62018-10-01 12:12:37 +0200453 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100454 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200455 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200456
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100457 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200458
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100459
460 if (t)
461 task_queue(t);
462
Christopher Faulet51dbc942018-09-13 09:05:15 +0200463 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200464 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200465
466 /* mux->wake will be called soon to complete the operation */
467 return 0;
468
469 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200470 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200471 if (h1c->wait_event.tasklet)
472 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200473 pool_free(pool_head_h1c, h1c);
474 fail_h1c:
475 return -1;
476}
477
Christopher Faulet73c12072019-04-08 11:23:22 +0200478/* release function. This one should be called to free all resources allocated
479 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200480 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200481static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200482{
Christopher Faulet61840e72019-04-15 09:33:32 +0200483 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200484
Christopher Faulet51dbc942018-09-13 09:05:15 +0200485 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200486 /* The connection must be aattached to this mux to be released */
487 if (h1c->conn && h1c->conn->ctx == h1c)
488 conn = h1c->conn;
489
490 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200491 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200492 /* Make sure we're no longer subscribed to anything */
493 if (h1c->wait_event.events)
494 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
495 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200496 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200497 /* connection successfully upgraded to H2, this
498 * mux was already released */
499 return;
500 }
501 sess_log(conn->owner); /* Log if the upgrade failed */
502 }
503
Olivier Houchard45c44372019-06-11 14:06:23 +0200504
Christopher Faulet51dbc942018-09-13 09:05:15 +0200505 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
506 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
507 LIST_DEL(&h1c->buf_wait.list);
508 LIST_INIT(&h1c->buf_wait.list);
509 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
510 }
511
512 h1_release_buf(h1c, &h1c->ibuf);
513 h1_release_buf(h1c, &h1c->obuf);
514
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100515 if (h1c->task) {
516 h1c->task->context = NULL;
517 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
518 h1c->task = NULL;
519 }
520
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200521 if (h1c->wait_event.tasklet)
522 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200523
Christopher Fauletf2824e62018-10-01 12:12:37 +0200524 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200525 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100526 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200527 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200528 pool_free(pool_head_h1c, h1c);
529 }
530
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200531 if (conn) {
532 conn->mux = NULL;
533 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200534
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200535 conn_stop_tracking(conn);
536 conn_full_close(conn);
537 if (conn->destroy_cb)
538 conn->destroy_cb(conn);
539 conn_free(conn);
540 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200541}
542
543/******************************************************/
544/* functions below are for the H1 protocol processing */
545/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200546/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
547 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200548 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100549static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200550{
Christopher Faulet570d1612018-11-26 11:13:57 +0100551 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200552
Christopher Faulet570d1612018-11-26 11:13:57 +0100553 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200554 (*(p + 5) > '1' ||
555 (*(p + 5) == '1' && *(p + 7) >= '1')))
556 h1m->flags |= H1_MF_VER_11;
557}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200558
Christopher Faulet9768c262018-10-22 09:34:31 +0200559/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
560 * greater or equal to 1.1
561 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100562static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200563{
Christopher Faulet570d1612018-11-26 11:13:57 +0100564 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200565
Christopher Faulet570d1612018-11-26 11:13:57 +0100566 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200567 (*(p + 5) > '1' ||
568 (*(p + 5) == '1' && *(p + 7) >= '1')))
569 h1m->flags |= H1_MF_VER_11;
570}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200571
Christopher Faulet9768c262018-10-22 09:34:31 +0200572/*
573 * Check the validity of the request version. If the version is valid, it
574 * returns 1. Otherwise, it returns 0.
575 */
576static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
577{
578 struct h1c *h1c = h1s->h1c;
579
580 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
581 * exactly one digit "." one digit. This check may be disabled using
582 * option accept-invalid-http-request.
583 */
584 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
585 if (sl.rq.v.len != 8)
586 return 0;
587
588 if (*(sl.rq.v.ptr + 4) != '/' ||
589 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
590 *(sl.rq.v.ptr + 6) != '.' ||
591 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
592 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200593 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200594 else if (!sl.rq.v.len) {
595 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200596
Christopher Faulet9768c262018-10-22 09:34:31 +0200597 /* RFC 1945 allows only GET for HTTP/0.9 requests */
598 if (sl.rq.meth != HTTP_METH_GET)
599 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200600
Christopher Faulet9768c262018-10-22 09:34:31 +0200601 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
602 if (!sl.rq.u.len)
603 return 0;
604
605 /* Add HTTP version */
606 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100607 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200608 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100609
610 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100611 ((*(sl.rq.v.ptr + 5) > '1') ||
612 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100613 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200614 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200615}
616
Christopher Faulet9768c262018-10-22 09:34:31 +0200617/*
618 * Check the validity of the response version. If the version is valid, it
619 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200620 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200621static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200622{
Christopher Faulet9768c262018-10-22 09:34:31 +0200623 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200624
Christopher Faulet9768c262018-10-22 09:34:31 +0200625 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
626 * exactly one digit "." one digit. This check may be disabled using
627 * option accept-invalid-http-request.
628 */
629 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
630 if (sl.st.v.len != 8)
631 return 0;
632
633 if (*(sl.st.v.ptr + 4) != '/' ||
634 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
635 *(sl.st.v.ptr + 6) != '.' ||
636 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
637 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200638 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100639
640 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100641 ((*(sl.st.v.ptr + 5) > '1') ||
642 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100643 h1m->flags |= H1_MF_VER_11;
644
Christopher Faulet9768c262018-10-22 09:34:31 +0200645 return 1;
646}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200647
648/* Deduce the connection mode of the client connection, depending on the
649 * configuration and the H1 message flags. This function is called twice, the
650 * first time when the request is parsed and the second time when the response
651 * is parsed.
652 */
653static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
654{
655 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200656
657 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100658 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200659 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100660 h1s->status == 101) {
661 /* Either we've established an explicit tunnel, or we're
662 * switching the protocol. In both cases, we're very unlikely to
663 * understand the next protocols. We have to switch to tunnel
664 * mode, so that we transfer the request and responses then let
665 * this protocol pass unmodified. When we later implement
666 * specific parsers for such protocols, we'll want to check the
667 * Upgrade header which contains information about that protocol
668 * for responses with status 101 (eg: see RFC2817 about TLS).
669 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200670 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100671 }
672 else if (h1s->flags & H1S_F_WANT_KAL) {
673 /* By default the client is in KAL mode. CLOSE mode mean
674 * it is imposed by the client itself. So only change
675 * KAL mode here. */
676 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
677 /* no length known or explicit close => close */
678 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
679 }
680 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
681 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
682 /* no explict keep-alive and option httpclose => close */
683 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
684 }
685 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200686 }
687 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100688 /* Input direction: first pass */
689 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
690 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200691 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100692 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200693 }
694
695 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
696 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
697 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
698}
699
700/* Deduce the connection mode of the client connection, depending on the
701 * configuration and the H1 message flags. This function is called twice, the
702 * first time when the request is parsed and the second time when the response
703 * is parsed.
704 */
705static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
706{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100707 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100708 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100709 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200710
Christopher Fauletf2824e62018-10-01 12:12:37 +0200711 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100712 /* Input direction: second pass */
713
Christopher Fauletf2824e62018-10-01 12:12:37 +0200714 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100715 h1s->status == 101) {
716 /* Either we've established an explicit tunnel, or we're
717 * switching the protocol. In both cases, we're very unlikely to
718 * understand the next protocols. We have to switch to tunnel
719 * mode, so that we transfer the request and responses then let
720 * this protocol pass unmodified. When we later implement
721 * specific parsers for such protocols, we'll want to check the
722 * Upgrade header which contains information about that protocol
723 * for responses with status 101 (eg: see RFC2817 about TLS).
724 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200725 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100726 }
727 else if (h1s->flags & H1S_F_WANT_KAL) {
728 /* By default the server is in KAL mode. CLOSE mode mean
729 * it is imposed by haproxy itself. So only change KAL
730 * mode here. */
731 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
732 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
733 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
734 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
735 }
736 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200737 }
Christopher Faulet70033782018-12-05 13:50:11 +0100738 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100739 /* Output direction: first pass */
740 if (h1m->flags & H1_MF_CONN_CLO) {
741 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100742 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100743 }
744 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
745 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
746 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
747 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
748 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
749 /* no explicit keep-alive option httpclose/server-close => close */
750 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
751 }
Christopher Faulet70033782018-12-05 13:50:11 +0100752 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200753
754 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
755 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
756 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200757}
758
Christopher Fauletb992af02019-03-28 15:42:24 +0100759static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200760{
761 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200762
763 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
764 * token is found
765 */
766 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200767 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200768
769 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100770 if (!(h1m->flags & H1_MF_VER_11))
771 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200772 }
773 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100774 if (h1m->flags & H1_MF_VER_11)
775 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200776 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200777}
778
Christopher Fauletb992af02019-03-28 15:42:24 +0100779static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200780{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200781 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
782 * token is found
783 */
784 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200785 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200786
787 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100788 if (!(h1m->flags & H1_MF_VER_11) ||
789 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
790 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200791 }
792 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100793 if (h1m->flags & H1_MF_VER_11)
794 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200795 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200796}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200797
Christopher Fauletb992af02019-03-28 15:42:24 +0100798static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200799{
Christopher Fauletb992af02019-03-28 15:42:24 +0100800 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200801 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100802 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200803 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200804}
805
Christopher Fauletb992af02019-03-28 15:42:24 +0100806static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
807{
808 if (!conn_is_back(h1s->h1c->conn))
809 h1_set_cli_conn_mode(h1s, h1m);
810 else
811 h1_set_srv_conn_mode(h1s, h1m);
812
813 if (!(h1m->flags & H1_MF_RESP))
814 h1_update_req_conn_value(h1s, h1m, conn_val);
815 else
816 h1_update_res_conn_value(h1s, h1m, conn_val);
817}
Christopher Faulete44769b2018-11-29 23:01:45 +0100818
Christopher Faulet98fbe952019-07-22 16:18:24 +0200819/* Try to adjust the case of the message header name using the global map
820 * <hdrs_map>.
821 */
822static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
823{
824 struct ebpt_node *node;
825 struct h1_hdr_entry *entry;
826
827 /* No entry in the map, do nothing */
828 if (eb_is_empty(&hdrs_map.map))
829 return;
830
831 /* No conversion fo the request headers */
832 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
833 return;
834
835 /* No conversion fo the response headers */
836 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
837 return;
838
839 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
840 if (!node)
841 return;
842 entry = container_of(node, struct h1_hdr_entry, node);
843 name->ptr = entry->name.ptr;
844 name->len = entry->name.len;
845}
846
Christopher Faulete44769b2018-11-29 23:01:45 +0100847/* Append the description of what is present in error snapshot <es> into <out>.
848 * The description must be small enough to always fit in a buffer. The output
849 * buffer may be the trash so the trash must not be used inside this function.
850 */
851static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
852{
853 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100854 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
855 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
856 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
857 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
858 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
859 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100860}
861/*
862 * Capture a bad request or response and archive it in the proxy's structure.
863 * By default it tries to report the error position as h1m->err_pos. However if
864 * this one is not set, it will then report h1m->next, which is the last known
865 * parsing point. The function is able to deal with wrapping buffers. It always
866 * displays buffers as a contiguous area starting at buf->p. The direction is
867 * determined thanks to the h1m's flags.
868 */
869static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
870 struct h1m *h1m, struct buffer *buf)
871{
872 struct session *sess = h1c->conn->owner;
873 struct proxy *proxy = h1c->px;
874 struct proxy *other_end = sess->fe;
875 union error_snapshot_ctx ctx;
876
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100877 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100878 other_end = si_strm(h1s->cs->data)->be;
879
880 /* http-specific part now */
881 ctx.h1.state = h1m->state;
882 ctx.h1.c_flags = h1c->flags;
883 ctx.h1.s_flags = h1s->flags;
884 ctx.h1.m_flags = h1m->flags;
885 ctx.h1.m_clen = h1m->curr_len;
886 ctx.h1.m_blen = h1m->body_len;
887
888 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
889 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100890 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
891 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100892}
893
Christopher Fauletadb22202018-12-12 10:32:09 +0100894/* Emit the chunksize followed by a CRLF in front of data of the buffer
895 * <buf>. It goes backwards and starts with the byte before the buffer's
896 * head. The caller is responsible for ensuring there is enough room left before
897 * the buffer's head for the string.
898 */
899static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
900{
901 char *beg, *end;
902
903 beg = end = b_head(buf);
904 *--beg = '\n';
905 *--beg = '\r';
906 do {
907 *--beg = hextab[chksz & 0xF];
908 } while (chksz >>= 4);
909 buf->head -= (end - beg);
910 b_add(buf, end - beg);
911}
912
913/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
914 * ensuring there is enough room left in the buffer for the string. */
915static void h1_emit_chunk_crlf(struct buffer *buf)
916{
917 *(b_peek(buf, b_data(buf))) = '\r';
918 *(b_peek(buf, b_data(buf) + 1)) = '\n';
919 b_add(buf, 2);
920}
921
Christopher Faulet129817b2018-09-20 16:14:40 +0200922/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100923 * Switch the request to tunnel mode. This function must only be called for
924 * CONNECT requests. On the client side, the mux is mark as busy on input,
925 * waiting the response.
926 */
927static void h1_set_req_tunnel_mode(struct h1s *h1s)
928{
929 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
930 h1s->req.state = H1_MSG_TUNNEL;
931 if (!conn_is_back(h1s->h1c->conn))
932 h1s->h1c->flags |= H1C_F_IN_BUSY;
933}
934
935/*
936 * Switch the response to tunnel mode. This function must only be called on
937 * successfull replies to CONNECT requests or on protocol switching. On the
938 * server side, if the request is not finished, the mux is mark as busy on
939 * input. Otherwise the request is also switch to tunnel mode.
940 */
941static void h1_set_res_tunnel_mode(struct h1s *h1s)
942{
943 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
944 h1s->res.state = H1_MSG_TUNNEL;
945 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
946 h1s->h1c->flags |= H1C_F_IN_BUSY;
947 else {
948 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
949 h1s->req.state = H1_MSG_TUNNEL;
950 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
951 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200952 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100953 }
954 }
955}
956
Christopher Faulet82f01602019-05-24 16:50:16 +0200957/* Estimate the size of the HTX headers after the parsing, including the EOH. */
958static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
959{
960 size_t sz = 0;
961 int i;
962
963 for (i = 0; hdrs[i].n.len; i++)
964 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
965 sz += sizeof(struct htx_blk) + 1;
966 return sz;
967}
968
Christopher Faulet30db3d72019-05-17 15:35:33 +0200969/* Estimate the size of the HTX request after the parsing. */
970static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
971{
972 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200973
974 /* size of the HTX start-line */
975 sz = sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200976 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200977 return sz;
978}
979
980/* Estimate the size of the HTX response after the parsing. */
981static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
982{
983 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200984
985 /* size of the HTX start-line */
986 sz = sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200987 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200988 return sz;
989}
990
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100991/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200992 * Add the EOM in the HTX message and switch the message to the DONE state. It
993 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
994 * functions is responsible to update the parser state <h1m>. It also add the
995 * flag CS_FL_EOI on the CS.
996 */
997static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
998{
999 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
1000 return 0;
1001
1002 h1m->state = H1_MSG_DONE;
1003 h1s->cs->flags |= CS_FL_EOI;
1004 return (sizeof(struct htx_blk) + 1);
1005}
1006
1007/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001008 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001009 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1010 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001011 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001012 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001013static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001014 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001015{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001016 struct htx_sl *sl;
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001017 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001018 union h1_sl h1sl;
1019 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001020 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +02001021 int ret = 0;
1022
Christopher Faulet30db3d72019-05-17 15:35:33 +02001023 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001024 goto end;
1025
Christopher Faulet129817b2018-09-20 16:14:40 +02001026 /* Realing input buffer if necessary */
1027 if (b_head(buf) + b_data(buf) > b_wrap(buf))
1028 b_slow_realign(buf, trash.area, 0);
1029
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001030 if (!(h1m->flags & H1_MF_RESP)) {
1031 /* Try to match H2 preface before parsing the request headers. */
1032 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
1033 if (ret > 0)
1034 goto h2c_upgrade;
1035 }
1036
Christopher Faulet30db3d72019-05-17 15:35:33 +02001037 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001038 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +02001039 if (ret <= 0) {
Willy Tarreau347f4642019-08-23 08:11:36 +02001040 /* Incomplete or invalid message. If the input buffer only
1041 * contains headers and is full, which is detected by it being
1042 * full and the offset to be zero, it's an error because
1043 * headers are too large to be handled by the parser. */
1044 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001045 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001046 goto end;
1047 }
1048
1049 /* messages headers fully parsed, do some checks to prepare the body
1050 * parsing.
1051 */
1052
Christopher Faulet9768c262018-10-22 09:34:31 +02001053 /* Save the request's method or the response's status, check if the body
1054 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001055 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001056 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001057
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001058 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001059 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001060
1061 if (h1s->meth == HTTP_METH_CONNECT) {
1062 /* Switch CONNECT requests to tunnel mode */
1063 h1_set_req_tunnel_mode(h1s);
1064 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001065
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001066 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1067 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001068 h1m->err_state = h1m->state;
1069 goto vsn_error;
1070 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001071 }
1072 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001073 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001074
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001075 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1076 h1s->status == 101) {
1077 /* Switch successfull replies to CONNECT requests and
1078 * protocol switching to tunnel mode. */
1079 h1_set_res_tunnel_mode(h1s);
1080 }
1081 else if ((h1s->meth == HTTP_METH_HEAD) ||
1082 (h1s->status >= 100 && h1s->status < 200) ||
1083 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001084 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001085 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1086 h1m->flags |= H1_MF_XFER_LEN;
1087 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001088 }
1089 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001090 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001091 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001092 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001093 else {
1094 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001095 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001096 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001097
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001098 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1099 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001100 h1m->err_state = h1m->state;
1101 goto vsn_error;
1102 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001103 }
1104
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001105 /* Set HTX start-line flags */
1106 if (h1m->flags & H1_MF_VER_11)
1107 flags |= HTX_SL_F_VER_11;
1108 if (h1m->flags & H1_MF_XFER_ENC)
1109 flags |= HTX_SL_F_XFER_ENC;
1110 if (h1m->flags & H1_MF_XFER_LEN) {
1111 flags |= HTX_SL_F_XFER_LEN;
1112 if (h1m->flags & H1_MF_CHNK)
1113 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001114 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001115 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001116 if (h1m->body_len == 0)
1117 flags |= HTX_SL_F_BODYLESS;
1118 }
1119 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001120 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001121 }
1122
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001123 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001124 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001125 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1126 if (htx_is_empty(htx))
1127 goto error;
1128 h1m_init_req(h1m);
1129 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1130 ret = 0;
1131 goto end;
1132 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001133
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001134 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1135 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001136 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001137 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001138
1139 /* Check if the uri contains an explicit scheme and if it is
1140 * "http" or "https". */
1141 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1142 sl->flags |= HTX_SL_F_HAS_SCHM;
1143 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1144 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1145 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001146 }
1147 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001148 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1149 if (htx_is_empty(htx))
1150 goto error;
1151 h1m_init_res(h1m);
1152 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1153 ret = 0;
1154 goto end;
1155 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001156
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001157 flags |= HTX_SL_F_IS_RESP;
1158 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1159 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001160 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001161 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001162 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001163
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001164 /* Set bytes used in the HTX mesage for the headers now */
1165 sl->hdrs_bytes = htx_used_space(htx) - used;
1166
Christopher Fauletb992af02019-03-28 15:42:24 +01001167 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001168
1169 /* If body length cannot be determined, set htx->extra to
1170 * ULLONG_MAX. This value is impossible in other cases.
1171 */
1172 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001173 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001174 end:
1175 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001176
1177 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001178 h1m->err_state = h1m->state;
1179 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001180 vsn_error:
1181 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001182 h1s->cs->flags |= CS_FL_EOI;
1183 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001184 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001185 ret = 0;
1186 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001187
1188 h2c_upgrade:
1189 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001190 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001191 htx->flags |= HTX_FL_UPGRADE;
1192 ret = 0;
1193 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001194}
1195
1196/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001197 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1198 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001199 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001200 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001201 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001202static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001203 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001204 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001205{
1206 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001207 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001208
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 if (h1m->flags & H1_MF_XFER_LEN) {
1210 if (h1m->flags & H1_MF_CLEN) {
1211 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001212 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001213 if ((uint64_t)ret > h1m->curr_len)
1214 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001215 if (ret > b_contig_data(buf, *ofs))
1216 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001217
Christopher Faulet9768c262018-10-22 09:34:31 +02001218 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001219 /* very often with large files we'll face the following
1220 * situation :
1221 * - htx is empty and points to <htxbuf>
1222 * - ret == buf->data
1223 * - buf->head == sizeof(struct htx)
1224 * => we can swap the buffers and place an htx header into
1225 * the target buffer instead
1226 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001227 int32_t try = ret;
1228
Willy Tarreau78f548f2018-12-05 10:02:39 +01001229 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1230 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1231 void *raw_area = buf->area;
1232 void *htx_area = htxbuf->area;
1233 struct htx_blk *blk;
1234
1235 buf->area = htx_area;
1236 htxbuf->area = raw_area;
1237 htx = (struct htx *)htxbuf->area;
1238 htx->size = htxbuf->size - sizeof(*htx);
1239 htx_reset(htx);
1240 b_set_data(htxbuf, b_size(htxbuf));
1241
1242 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1243 blk->info += ret;
1244 /* nothing else to do, the old buffer now contains an
1245 * empty pre-initialized HTX header
1246 */
1247 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001248 else {
1249 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1250 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001251 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001252 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001253 *ofs += ret;
1254 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001255 if (ret < try)
1256 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001257 }
1258
1259 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001260 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001261 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001262 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001263 }
1264 else if (h1m->flags & H1_MF_CHNK) {
1265 new_chunk:
1266 /* te:chunked : parse chunks */
1267 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001268 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001269 if (ret <= 0)
1270 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001271 h1m->state = H1_MSG_CHUNK_SIZE;
1272
Christopher Fauletf2824e62018-10-01 12:12:37 +02001273 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001274 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001275 }
1276
1277 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1278 unsigned int chksz;
1279
Christopher Faulet30db3d72019-05-17 15:35:33 +02001280 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001281 if (ret <= 0)
1282 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001283 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001284
Christopher Faulet129817b2018-09-20 16:14:40 +02001285 h1m->curr_len = chksz;
1286 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001287 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001288 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001289
1290 if (!h1m->curr_len)
1291 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001292 }
1293
1294 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001295 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001296 if ((uint64_t)ret > h1m->curr_len)
1297 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001298 if (ret > b_contig_data(buf, *ofs))
1299 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001300
Christopher Faulet9768c262018-10-22 09:34:31 +02001301 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001302 int32_t try = ret;
1303
1304 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001305 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001306 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001307 *ofs += ret;
1308 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001309 if (ret < try)
1310 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001311 }
1312 if (!h1m->curr_len) {
1313 h1m->state = H1_MSG_CHUNK_CRLF;
1314 goto new_chunk;
1315 }
1316 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001317 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001318 }
1319 else {
1320 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1321 * is no body. Switch the message in DONE state
1322 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001323 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001324 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001325 }
1326 }
1327 else {
1328 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001329 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001330 if (ret > b_contig_data(buf, *ofs))
1331 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001332
Christopher Faulet9768c262018-10-22 09:34:31 +02001333 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001334 int32_t try = ret;
1335
1336 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001337
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001338 *ofs += ret;
1339 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001340 if (ret < try)
1341 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001342 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001343 }
1344
1345 end:
1346 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001347 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001348 h1s->cs->flags |= CS_FL_EOI;
1349 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001350 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001351 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001352 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001353 return 0;
1354 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001355 /* update htx->extra, only when the body length is known */
1356 if (h1m->flags & H1_MF_XFER_LEN)
1357 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001358 return total;
1359}
1360
1361/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001362 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1363 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1364 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1365 * responsible to update the parser state <h1m>.
1366 */
1367static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1368 struct buffer *buf, size_t *ofs, size_t max)
1369{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001370 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001371 struct h1m tlr_h1m;
1372 int ret = 0;
1373
1374 if (!max || !b_data(buf))
1375 goto end;
1376
1377 /* Realing input buffer if necessary */
1378 if (b_peek(buf, *ofs) > b_tail(buf))
1379 b_slow_realign(buf, trash.area, 0);
1380
1381 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1382 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1383 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1384 if (ret <= 0) {
Willy Tarreau347f4642019-08-23 08:11:36 +02001385 /* Incomplete or invalid trailers. If the input buffer only
1386 * contains trailers and is full, which is detected by it being
1387 * full and the offset to be zero, it's an error because
1388 * trailers are too large to be handled by the parser. */
1389 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001390 goto error;
1391 goto end;
1392 }
1393
1394 /* messages trailers fully parsed. */
1395 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1396 if (htx_is_empty(htx))
1397 goto error;
1398 ret = 0;
1399 goto end;
1400 }
1401
1402 if (!htx_add_all_trailers(htx, hdrs))
1403 goto error;
1404
1405 *ofs += ret;
1406 h1s->flags |= H1S_F_HAVE_I_TLR;
1407 end:
1408 return ret;
1409
1410 error:
1411 h1m->err_state = h1m->state;
1412 h1m->err_pos = h1m->next;
1413 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1414 h1s->cs->flags |= CS_FL_EOI;
1415 htx->flags |= HTX_FL_PARSING_ERROR;
1416 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1417 ret = 0;
1418 goto end;
1419}
1420
1421/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001422 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001423 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1424 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001425 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001426static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001427{
Christopher Faulet539e0292018-11-19 10:40:09 +01001428 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001429 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001430 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001431 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001432 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001433 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001434
Christopher Faulet539e0292018-11-19 10:40:09 +01001435 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001436
Christopher Fauletf2824e62018-10-01 12:12:37 +02001437 if (!conn_is_back(h1c->conn)) {
1438 h1m = &h1s->req;
1439 errflag = H1S_F_REQ_ERROR;
1440 }
1441 else {
1442 h1m = &h1s->res;
1443 errflag = H1S_F_RES_ERROR;
1444 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001445
Christopher Faulet74027762019-02-26 14:45:05 +01001446 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001447 if (h1s->flags & errflag)
1448 goto end;
1449
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001450 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001451 size_t used = htx_used_space(htx);
1452
Christopher Faulet129817b2018-09-20 16:14:40 +02001453 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001454 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001455 if (!ret)
1456 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001457 if ((h1m->flags & H1_MF_RESP) &&
1458 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1459 h1m_init_res(&h1s->res);
1460 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1461 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001462 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001463 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001464 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001465 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001466 if (!ret)
1467 break;
1468 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001469 else if (h1m->state == H1_MSG_TRAILERS) {
1470 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1471 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1472 if (!ret)
1473 break;
1474 }
1475 if (!h1_process_eom(h1s, h1m, htx, count))
1476 break;
1477 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001478 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001479 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1480 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001481 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001482 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001483 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001484 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001485 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001486 if (!ret)
1487 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001488 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001489 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001490 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001491 break;
1492 }
1493
Christopher Faulet30db3d72019-05-17 15:35:33 +02001494 count -= htx_used_space(htx) - used;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001495 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001496
Christopher Faulet47365272018-10-31 17:40:50 +01001497 if (h1s->flags & errflag)
1498 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001499
Christopher Faulet539e0292018-11-19 10:40:09 +01001500 b_del(&h1c->ibuf, total);
1501
1502 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001503 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001504 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001505 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001506 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001507 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001508 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001509
Christopher Fauletcf56b992018-12-11 16:12:31 +01001510 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1511
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001512 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001513 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001514 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001515 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001516
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001517 if ((h1s->flags & H1S_F_REOS) && (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001518 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001519 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001520 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001521 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001522
Christopher Faulet30db3d72019-05-17 15:35:33 +02001523 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001524
1525 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001526 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001527 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001528 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001529}
1530
Christopher Faulet129817b2018-09-20 16:14:40 +02001531/*
1532 * Process outgoing data. It parses data and transfer them from the channel buffer into
1533 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1534 * 0 if it couldn't proceed.
1535 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001536static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1537{
Christopher Faulet129817b2018-09-20 16:14:40 +02001538 struct h1s *h1s = h1c->h1s;
1539 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001540 struct htx *chn_htx;
1541 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001542 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001543 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001544 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001545
Christopher Faulet47365272018-10-31 17:40:50 +01001546 if (!count)
1547 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001548
Christopher Faulet94b2c762019-05-24 15:28:57 +02001549 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001550 if (htx_is_empty(chn_htx))
1551 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001552
Christopher Faulet51dbc942018-09-13 09:05:15 +02001553 if (!h1_get_buf(h1c, &h1c->obuf)) {
1554 h1c->flags |= H1C_F_OUT_ALLOC;
1555 goto end;
1556 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001557
Christopher Fauletf2824e62018-10-01 12:12:37 +02001558 if (!conn_is_back(h1c->conn)) {
1559 h1m = &h1s->res;
1560 errflag = H1S_F_RES_ERROR;
1561 }
1562 else {
1563 h1m = &h1s->req;
1564 errflag = H1S_F_REQ_ERROR;
1565 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001566
Christopher Faulet0e54d542019-07-04 21:22:34 +02001567 if (h1s->flags & errflag)
1568 goto end;
1569
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001570 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001571 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001572
Willy Tarreau3815b222018-12-11 19:50:43 +01001573 /* Perform some optimizations to reduce the number of buffer copies.
1574 * First, if the mux's buffer is empty and the htx area contains
1575 * exactly one data block of the same size as the requested count,
1576 * then it's possible to simply swap the caller's buffer with the
1577 * mux's output buffer and adjust offsets and length to match the
1578 * entire DATA HTX block in the middle. In this case we perform a
1579 * true zero-copy operation from end-to-end. This is the situation
1580 * that happens all the time with large files. Second, if this is not
1581 * possible, but the mux's output buffer is empty, we still have an
1582 * opportunity to avoid the copy to the intermediary buffer, by making
1583 * the intermediary buffer's area point to the output buffer's area.
1584 * In this case we want to skip the HTX header to make sure that copies
1585 * remain aligned and that this operation remains possible all the
1586 * time. This goes for headers, data blocks and any data extracted from
1587 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001588 */
1589 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001590 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001591 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001592 htx_get_blk_value(chn_htx, blk).len == count) {
1593 void *old_area = h1c->obuf.area;
1594
1595 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001596 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001597 h1c->obuf.data = count;
1598
1599 buf->area = old_area;
1600 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001601
1602 /* The message is chunked. We need to emit the chunk
1603 * size. We have at least the size of the struct htx to
1604 * write the chunk envelope. It should be enough.
1605 */
1606 if (h1m->flags & H1_MF_CHNK) {
1607 h1_emit_chunk_size(&h1c->obuf, count);
1608 h1_emit_chunk_crlf(&h1c->obuf);
1609 }
1610
Willy Tarreau3815b222018-12-11 19:50:43 +01001611 total += count;
1612 goto out;
1613 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001614 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001615 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001616 else
1617 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001618
Christopher Fauletc2518a52019-06-25 21:41:02 +02001619 tmp.data = 0;
1620 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001621 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001622 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001623 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001624 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001625 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001626 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001627
Christopher Fauletb2e84162018-12-06 11:39:49 +01001628 vlen = sz;
1629 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001630 if (type != HTX_BLK_DATA)
Christopher Fauletb2e84162018-12-06 11:39:49 +01001631 goto copy;
1632 vlen = count;
1633 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001634
Christopher Faulet94b2c762019-05-24 15:28:57 +02001635 if (type == HTX_BLK_UNUSED)
1636 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001637
Christopher Faulet94b2c762019-05-24 15:28:57 +02001638 switch (h1m->state) {
1639 case H1_MSG_RQBEFORE:
1640 if (type != HTX_BLK_REQ_SL)
1641 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001642 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001643 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001645 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001646 goto copy;
1647 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001648 if (sl->flags & HTX_SL_F_BODYLESS)
1649 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001650 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001651 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001652
Christopher Faulet94b2c762019-05-24 15:28:57 +02001653 case H1_MSG_RPBEFORE:
1654 if (type != HTX_BLK_RES_SL)
1655 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001656 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001657 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001658 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001659 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001660 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001661 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001662 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001663 if (sl->info.res.status < 200 &&
1664 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001665 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001666 h1m->state = H1_MSG_HDR_FIRST;
1667 break;
1668
Christopher Faulet94b2c762019-05-24 15:28:57 +02001669 case H1_MSG_HDR_FIRST:
1670 case H1_MSG_HDR_NAME:
1671 case H1_MSG_HDR_L2_LWS:
1672 if (type == HTX_BLK_EOH)
1673 goto last_lf;
1674 if (type != HTX_BLK_HDR)
1675 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001676 h1m->state = H1_MSG_HDR_NAME;
1677 n = htx_get_blk_name(chn_htx, blk);
1678 v = htx_get_blk_value(chn_htx, blk);
1679
1680 if (isteqi(n, ist("transfer-encoding")))
1681 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001682 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001683 /* Only skip C-L header with invalid value. */
1684 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001685 goto skip_hdr;
1686 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001688 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001689 if (!v.len)
1690 goto skip_hdr;
1691 }
1692
Christopher Faulet98fbe952019-07-22 16:18:24 +02001693 /* Try to adjust the case of the header name */
1694 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1695 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001696 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001697 goto copy;
1698 skip_hdr:
1699 h1m->state = H1_MSG_HDR_L2_LWS;
1700 break;
1701
Christopher Faulet94b2c762019-05-24 15:28:57 +02001702 case H1_MSG_LAST_LF:
1703 if (type != HTX_BLK_EOH)
1704 goto error;
1705 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001706 h1m->state = H1_MSG_LAST_LF;
1707 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001708 /* There is no "Connection:" header and
1709 * it the conn_mode must be
1710 * processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001711 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001712 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001713 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001714 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001715 /* Try to adjust the case of the header name */
1716 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1717 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001718 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001719 goto copy;
1720 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001721 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001722 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001723
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001724 if ((h1s->meth != HTTP_METH_CONNECT &&
1725 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001726 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1727 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1728 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1729 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1730 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001731 /* chunking needed but header not seen */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001732 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Willy Tarreau4710d202019-01-03 17:39:54 +01001733 goto copy;
1734 h1m->flags |= H1_MF_CHNK;
1735 }
1736
Christopher Fauletc2518a52019-06-25 21:41:02 +02001737 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet9768c262018-10-22 09:34:31 +02001738 goto copy;
1739
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001740 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1741 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1742 h1_set_req_tunnel_mode(h1s);
1743 }
1744 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1745 /* a successfull reply to a CONNECT or a protocol switching is sent
1746 * to the client . Switch the response to tunnel mode. */
1747 h1_set_res_tunnel_mode(h1s);
1748 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001749 else if ((h1m->flags & H1_MF_RESP) &&
1750 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1751 h1m_init_res(&h1s->res);
1752 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001753 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001754 }
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001755 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1756 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001757 else
1758 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001759 break;
1760
Christopher Faulet94b2c762019-05-24 15:28:57 +02001761 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001762 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001763 if (type == HTX_BLK_EOM) {
1764 /* Chunked message without explicit trailers */
1765 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001766 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001767 goto copy;
1768 }
1769 goto done;
1770 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001771 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001772 /* If the message is not chunked, never
1773 * add the last chunk. */
1774 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet94b2c762019-05-24 15:28:57 +02001775 goto copy;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001776 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001777 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001778 else if (type != HTX_BLK_DATA)
1779 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001780 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001781 v.len = vlen;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001782 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001783 goto copy;
1784 break;
1785
Christopher Faulet94b2c762019-05-24 15:28:57 +02001786 case H1_MSG_TRAILERS:
1787 if (type == HTX_BLK_EOM)
1788 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001789 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001790 goto error;
1791 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001792 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001793 /* If the message is not chunked, ignore
1794 * trailers. It may happen with H2 messages. */
1795 if (!(h1m->flags & H1_MF_CHNK))
1796 break;
1797
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001798 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001799 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet32188212018-11-20 18:21:43 +01001800 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001801 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001802 else { // HTX_BLK_TLR
1803 n = htx_get_blk_name(chn_htx, blk);
1804 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001805
1806 /* Try to adjust the case of the header name */
1807 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1808 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001809 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001810 goto copy;
1811 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001812 break;
1813
Christopher Faulet94b2c762019-05-24 15:28:57 +02001814 case H1_MSG_DONE:
1815 if (type != HTX_BLK_EOM)
1816 goto error;
1817 done:
1818 h1m->state = H1_MSG_DONE;
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001819 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1820 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1821 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1822 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001823 break;
1824
Christopher Faulet9768c262018-10-22 09:34:31 +02001825 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001826 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001827 /* Unexpected error during output processing */
1828 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001829 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001830 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001831 break;
1832 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001833
1834 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001835 total += vlen;
1836 count -= vlen;
1837 if (sz == vlen)
1838 blk = htx_remove_blk(chn_htx, blk);
1839 else {
1840 htx_cut_data_blk(chn_htx, blk, vlen);
1841 break;
1842 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001843 }
1844
Christopher Faulet9768c262018-10-22 09:34:31 +02001845 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001846 /* when the output buffer is empty, tmp shares the same area so that we
1847 * only have to update pointers and lengths.
1848 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001849 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1850 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001851 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001852 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001853
Willy Tarreau3815b222018-12-11 19:50:43 +01001854 htx_to_buf(chn_htx, buf);
1855 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001856 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001857 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001858 end:
1859 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001860}
1861
Christopher Faulet51dbc942018-09-13 09:05:15 +02001862/*********************************************************/
1863/* functions below are I/O callbacks from the connection */
1864/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001865static void h1_wake_stream_for_recv(struct h1s *h1s)
1866{
1867 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001868 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001869 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001870 h1s->recv_wait = NULL;
1871 }
1872}
1873static void h1_wake_stream_for_send(struct h1s *h1s)
1874{
1875 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001876 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001877 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001878 h1s->send_wait = NULL;
1879 }
1880}
1881
Christopher Faulet51dbc942018-09-13 09:05:15 +02001882/*
1883 * Attempt to read data, and subscribe if none available
1884 */
1885static int h1_recv(struct h1c *h1c)
1886{
1887 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001888 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001889 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001890 int rcvd = 0;
1891
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001892 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001893 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001894
Olivier Houchard75159a92018-12-03 18:46:09 +01001895 if (!h1_recv_allowed(h1c)) {
1896 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001897 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001898 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001899
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001900 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1901 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001902 goto end;
1903 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001904
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001905 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001906 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001907 h1_wake_stream_for_recv(h1s);
1908 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001909 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001910 }
1911
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001912 /*
1913 * If we only have a small amount of data, realign it,
1914 * it's probably cheaper than doing 2 recv() calls.
1915 */
1916 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1917 b_slow_realign(&h1c->ibuf, trash.area, 0);
1918
Willy Tarreau45f2b892018-12-05 07:59:27 +01001919 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001920 if (max) {
1921 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001922
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001923 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001924 if (!b_data(&h1c->ibuf)) {
1925 /* try to pre-align the buffer like the rxbufs will be
1926 * to optimize memory copies.
1927 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001928 h1c->ibuf.head = sizeof(struct htx);
1929 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001930 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001931 }
Christopher Faulet47365272018-10-31 17:40:50 +01001932 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001933 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001934 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001935 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001936 if (h1s->csinfo.t_idle == -1)
1937 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1938 }
Christopher Faulet47365272018-10-31 17:40:50 +01001939 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001940
Olivier Houchardcc3fec82019-07-26 15:11:11 +02001941 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001942 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001943 goto end;
1944 }
1945
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001946 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001947
Christopher Faulet9768c262018-10-22 09:34:31 +02001948 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001949 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1950 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001951
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001952 if (conn_xprt_read0_pending(conn) && h1s) {
1953 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001954 rcvd = 1;
1955 }
1956
Christopher Faulet51dbc942018-09-13 09:05:15 +02001957 if (!b_data(&h1c->ibuf))
1958 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001959 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001960 h1c->flags |= H1C_F_IN_FULL;
1961 return rcvd;
1962}
1963
1964
1965/*
1966 * Try to send data if possible
1967 */
1968static int h1_send(struct h1c *h1c)
1969{
1970 struct connection *conn = h1c->conn;
1971 unsigned int flags = 0;
1972 size_t ret;
1973 int sent = 0;
1974
1975 if (conn->flags & CO_FL_ERROR)
1976 return 0;
1977
1978 if (!b_data(&h1c->obuf))
1979 goto end;
1980
1981 if (h1c->flags & H1C_F_OUT_FULL)
1982 flags |= CO_SFL_MSG_MORE;
1983
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001984 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001985 if (ret > 0) {
1986 h1c->flags &= ~H1C_F_OUT_FULL;
1987 b_del(&h1c->obuf, ret);
1988 sent = 1;
1989 }
1990
Christopher Faulet145aa472018-12-06 10:56:20 +01001991 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1992 /* error or output closed, nothing to send, clear the buffer to release it */
1993 b_reset(&h1c->obuf);
1994 }
1995
Christopher Faulet51dbc942018-09-13 09:05:15 +02001996 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001997 if (!(h1c->flags & H1C_F_OUT_FULL))
1998 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001999
Christopher Faulet51dbc942018-09-13 09:05:15 +02002000 /* We're done, no more to send */
2001 if (!b_data(&h1c->obuf)) {
2002 h1_release_buf(h1c, &h1c->obuf);
2003 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01002004 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002005 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002006 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002007 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002008
2009 return sent;
2010}
2011
Christopher Faulet51dbc942018-09-13 09:05:15 +02002012
2013/* callback called on any event by the connection handler.
2014 * It applies changes and returns zero, or < 0 if it wants immediate
2015 * destruction of the connection.
2016 */
2017static int h1_process(struct h1c * h1c)
2018{
2019 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002020 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002021
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002022 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002023 return -1;
2024
Christopher Fauletfeb11742018-11-29 15:12:34 +01002025 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002026 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002027 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01002028 conn_xprt_read0_pending(conn))
2029 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002030 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002031 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002032 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002033 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002034 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002035 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002036 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002037 }
2038
Christopher Fauletfeb11742018-11-29 15:12:34 +01002039 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2040 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2041
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002042 if (conn_xprt_read0_pending(conn))
2043 h1s->flags |= H1S_F_REOS;
2044
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002045 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002046 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002047 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002048 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002049 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002050 h1s->cs->data_cb->wake(h1s->cs);
2051 }
Christopher Faulet47365272018-10-31 17:40:50 +01002052 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002053 if (h1c->task) {
2054 h1c->task->expire = TICK_ETERNITY;
2055 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002056 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002057 ? h1c->shut_timeout
2058 : h1c->timeout));
2059 task_queue(h1c->task);
2060 }
2061 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002062 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002063
2064 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002065 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002066 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002067}
2068
2069static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2070{
2071 struct h1c *h1c = ctx;
2072 int ret = 0;
2073
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002074 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002075 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002076 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002077 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002078 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002079 h1_process(h1c);
2080 return NULL;
2081}
2082
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002083static void h1_reset(struct connection *conn)
2084{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002085
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002086}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002087
2088static int h1_wake(struct connection *conn)
2089{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002090 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002091 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002092
Christopher Faulet539e0292018-11-19 10:40:09 +01002093 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002094 ret = h1_process(h1c);
2095 if (ret == 0) {
2096 struct h1s *h1s = h1c->h1s;
2097
2098 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2099 ret = h1s->cs->data_cb->wake(h1s->cs);
2100 }
2101 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002102}
2103
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002104/* Connection timeout management. The principle is that if there's no receipt
2105 * nor sending for a certain amount of time, the connection is closed.
2106 */
2107static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2108{
2109 struct h1c *h1c = context;
2110 int expired = tick_is_expired(t->expire, now_ms);
2111
2112 if (!expired && h1c)
2113 return t;
2114
Olivier Houchard3f795f72019-04-17 22:51:06 +02002115 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002116
2117 if (!h1c) {
2118 /* resources were already deleted */
2119 return NULL;
2120 }
2121
2122 h1c->task = NULL;
2123 /* If a stream is still attached to the mux, just set an error and wait
2124 * for the stream's timeout. Otherwise, release the mux. This is only ok
2125 * because same timeouts are used.
2126 */
2127 if (h1c->h1s && h1c->h1s->cs)
2128 h1c->flags |= H1C_F_CS_ERROR;
2129 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002130 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002131 return NULL;
2132}
2133
Christopher Faulet51dbc942018-09-13 09:05:15 +02002134/*******************************************/
2135/* functions below are used by the streams */
2136/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002137
Christopher Faulet51dbc942018-09-13 09:05:15 +02002138/*
2139 * Attach a new stream to a connection
2140 * (Used for outgoing connections)
2141 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002142static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002143{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002144 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002145 struct conn_stream *cs = NULL;
2146 struct h1s *h1s;
2147
2148 if (h1c->flags & H1C_F_CS_ERROR)
2149 goto end;
2150
2151 cs = cs_new(h1c->conn);
2152 if (!cs)
2153 goto end;
2154
Olivier Houchardf502aca2018-12-14 19:42:40 +01002155 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002156 if (h1s == NULL)
2157 goto end;
2158
2159 return cs;
2160 end:
2161 cs_free(cs);
2162 return NULL;
2163}
2164
2165/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2166 * this mux, it's easy as we can only store a single conn_stream.
2167 */
2168static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2169{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002170 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002171 struct h1s *h1s = h1c->h1s;
2172
2173 if (h1s)
2174 return h1s->cs;
2175
2176 return NULL;
2177}
2178
Christopher Faulet73c12072019-04-08 11:23:22 +02002179static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002180{
Christopher Faulet73c12072019-04-08 11:23:22 +02002181 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002182
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002183 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002184 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002185}
2186
2187/*
2188 * Detach the stream from the connection and possibly release the connection.
2189 */
2190static void h1_detach(struct conn_stream *cs)
2191{
2192 struct h1s *h1s = cs->ctx;
2193 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002194 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002195 int has_keepalive;
2196 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002197
2198 cs->ctx = NULL;
2199 if (!h1s)
2200 return;
2201
Olivier Houchardf502aca2018-12-14 19:42:40 +01002202 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002203 h1c = h1s->h1c;
2204 h1s->cs = NULL;
2205
Olivier Houchard8a786902018-12-15 16:05:40 +01002206 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2207 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2208 h1s_destroy(h1s);
2209
2210 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002211 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002212 /* If there are any excess server data in the input buffer,
2213 * release it and close the connection ASAP (some data may
2214 * remain in the output buffer). This happens if a server sends
2215 * invalid responses. So in such case, we don't want to reuse
2216 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002217 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002218 if (b_data(&h1c->ibuf)) {
2219 h1_release_buf(h1c, &h1c->ibuf);
2220 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2221 goto release;
2222 }
Christopher Faulet03627242019-07-19 11:34:08 +02002223
Christopher Faulet9400a392018-11-23 23:10:39 +01002224 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002225 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002226 h1c->conn->flags |= CO_FL_PRIVATE;
2227
Olivier Houchard44d59142018-12-13 18:46:22 +01002228 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002229 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002230 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2231 h1c->conn->owner = NULL;
2232 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2233 /* The server doesn't want it, let's kill the connection right away */
2234 h1c->conn->mux->destroy(h1c->conn);
2235 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002236 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002237 return;
2238
2239 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002240 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002241 if (h1c->conn->owner == sess) {
2242 int ret = session_check_idle_conn(sess, h1c->conn);
2243 if (ret == -1)
2244 /* The connection got destroyed, let's leave */
2245 return;
2246 else if (ret == 1) {
2247 /* The connection was added to the server list,
2248 * wake the task so we can subscribe to events
2249 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002250 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002251 return;
2252 }
2253 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002254 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002255 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002256 struct server *srv = objt_server(h1c->conn->target);
2257
2258 if (srv) {
2259 if (h1c->conn->flags & CO_FL_PRIVATE)
2260 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002261 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002262 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2263 else
2264 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2265 }
2266 }
2267 }
2268
Christopher Fauletf1204b82019-07-19 14:51:06 +02002269 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002270 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet37243bc2019-07-11 15:40:25 +02002271 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2272 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2273 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2274 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002275 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002276 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002277 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002278 if (h1c->task) {
2279 h1c->task->expire = TICK_ETERNITY;
2280 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002281 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002282 ? h1c->shut_timeout
2283 : h1c->timeout));
2284 task_queue(h1c->task);
2285 }
2286 }
2287 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002288}
2289
2290
2291static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2292{
2293 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002294 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002295
2296 if (!h1s)
2297 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002298 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002299
Christopher Faulet7f366362019-04-08 10:51:20 +02002300 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2301 goto do_shutr;
2302
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002303 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002304 return;
2305
Christopher Faulet7f366362019-04-08 10:51:20 +02002306 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002307 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2308 if (cs->flags & CS_FL_SHR)
2309 return;
2310 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002311 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2312 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002313 if ((cs->conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet7f366362019-04-08 10:51:20 +02002314 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002315}
2316
2317static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2318{
2319 struct h1s *h1s = cs->ctx;
2320 struct h1c *h1c;
2321
2322 if (!h1s)
2323 return;
2324 h1c = h1s->h1c;
2325
Christopher Faulet7f366362019-04-08 10:51:20 +02002326 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2327 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002328
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002329 if ((h1c->flags & H1C_F_UPG_H2C) ||
2330 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002331 return;
2332
Christopher Faulet7f366362019-04-08 10:51:20 +02002333 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002334 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2335 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2336 return;
2337
Christopher Faulet666a0c42019-01-08 11:12:04 +01002338 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002339}
2340
Christopher Faulet666a0c42019-01-08 11:12:04 +01002341static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002342{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002343 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002344
Christopher Faulet666a0c42019-01-08 11:12:04 +01002345 conn_xprt_shutw(conn);
2346 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2347 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2348 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002349}
2350
2351/* Called from the upper layer, to unsubscribe to events */
2352static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2353{
2354 struct wait_event *sw;
2355 struct h1s *h1s = cs->ctx;
2356
2357 if (!h1s)
2358 return 0;
2359
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002360 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002361 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002362 BUG_ON(h1s->recv_wait != sw);
2363 sw->events &= ~SUB_RETRY_RECV;
2364 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002365 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002366 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002367 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002368 BUG_ON(h1s->send_wait != sw);
2369 sw->events &= ~SUB_RETRY_SEND;
2370 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002371 }
2372 return 0;
2373}
2374
2375/* Called from the upper layer, to subscribe to events, such as being able to send */
2376static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2377{
2378 struct wait_event *sw;
2379 struct h1s *h1s = cs->ctx;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002380 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002381
2382 if (!h1s)
2383 return -1;
2384
2385 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002386 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002387 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002388 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2389 sw->events |= SUB_RETRY_RECV;
2390 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002391 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002392 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002393 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002394 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2395 sw->events |= SUB_RETRY_SEND;
2396 h1s->send_wait = sw;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002397 /*
2398 * If the conn_stream attempt to subscribe, and the
2399 * mux isn't subscribed to the connection, then it
2400 * probably means the connection wasn't established
2401 * yet, so we have to subscribe.
2402 */
2403 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2404 h1c->conn->xprt->subscribe(h1c->conn,
2405 h1c->conn->xprt_ctx,
2406 SUB_RETRY_SEND,
2407 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002408 return 0;
2409 default:
2410 break;
2411 }
2412 return -1;
2413}
2414
2415/* Called from the upper layer, to receive data */
2416static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2417{
2418 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002419 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002420 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002421 size_t ret = 0;
2422
Christopher Faulet539e0292018-11-19 10:40:09 +01002423 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002424 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002425
Christopher Faulete18777b2019-04-16 16:46:36 +02002426 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002427
2428 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2429 h1s->flags |= H1S_F_BUF_FLUSH;
2430 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002431 else {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002432 h1s->flags &= ~H1S_F_SPLICED_DATA;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002433 if (h1m->state != H1_MSG_DONE &&
2434 !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002435 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002436 }
2437 return ret;
2438}
2439
2440
2441/* Called from the upper layer, to send data */
2442static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2443{
2444 struct h1s *h1s = cs->ctx;
2445 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002446 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002447
2448 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002449 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002450
2451 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002452
2453 /* If we're not connected yet, or we're waiting for a handshake, stop
2454 * now, as we don't want to remove everything from the channel buffer
2455 * before we're sure we can send it.
2456 */
2457 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
2458 (h1c->conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002459 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002460
Christopher Fauletc31872f2019-06-04 22:09:36 +02002461 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002462 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002463
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002464 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2465 ret = h1_process_output(h1c, buf, count);
2466 if (!ret)
2467 break;
2468 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002469 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002470 if (!h1_send(h1c))
2471 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002472 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002473
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002474 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002475}
2476
Willy Tarreaue5733232019-05-22 19:24:06 +02002477#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002478/* Send and get, using splicing */
2479static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2480{
2481 struct h1s *h1s = cs->ctx;
2482 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2483 int ret = 0;
2484
Christopher Faulet1146f972019-05-29 14:35:24 +02002485 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002486 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002487 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2488 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002489 goto end;
2490 }
2491
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002492 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002493 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002494 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002495 }
2496
2497 h1s->flags &= ~H1S_F_BUF_FLUSH;
2498 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002499 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2500 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002501 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002502 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002503 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002504 if (!h1m->curr_len)
2505 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2506 }
2507
Christopher Faulet1be55f92018-10-02 15:59:23 +02002508 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002509 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002510 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002511 if (!pipe->data)
2512 cs->flags |= CS_FL_EOS;
2513 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002514 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002515}
2516
2517static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2518{
2519 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002520 int ret = 0;
2521
2522 if (b_data(&h1s->h1c->obuf))
2523 goto end;
2524
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002525 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002526 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002527 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002528 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002529 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002530 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002531 return ret;
2532}
2533#endif
2534
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002535/* for debugging with CLI's "show fd" command */
2536static void h1_show_fd(struct buffer *msg, struct connection *conn)
2537{
2538 struct h1c *h1c = conn->ctx;
2539 struct h1s *h1s = h1c->h1s;
2540
Christopher Fauletf376a312019-01-04 15:16:06 +01002541 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2542 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002543 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2544 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2545 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2546 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2547
2548 if (h1s) {
2549 char *method;
2550
2551 if (h1s->meth < HTTP_METH_OTHER)
2552 method = http_known_methods[h1s->meth].ptr;
2553 else
2554 method = "UNKNOWN";
2555 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2556 " .meth=%s status=%d",
2557 h1s, h1s->flags,
2558 h1m_state_str(h1s->req.state),
2559 h1m_state_str(h1s->res.state), method, h1s->status);
2560 if (h1s->cs)
2561 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2562 h1s->cs->flags, h1s->cs->data);
2563 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002564}
2565
2566
2567/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2568static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2569{
2570 struct h1_hdr_entry *entry;
2571
2572 /* Be sure there is a non-empty <to> */
2573 if (!strlen(to)) {
2574 memprintf(err, "expect <to>");
2575 return -1;
2576 }
2577
2578 /* Be sure only the case differs between <from> and <to> */
2579 if (strcasecmp(from, to)) {
2580 memprintf(err, "<from> and <to> must not differ execpt the case");
2581 return -1;
2582 }
2583
2584 /* Be sure <from> does not already existsin the tree */
2585 if (ebis_lookup(&hdrs_map.map, from)) {
2586 memprintf(err, "duplicate entry '%s'", from);
2587 return -1;
2588 }
2589
2590 /* Create the entry and insert it in the tree */
2591 entry = malloc(sizeof(*entry));
2592 if (!entry) {
2593 memprintf(err, "out of memory");
2594 return -1;
2595 }
2596
2597 entry->node.key = strdup(from);
2598 entry->name.ptr = strdup(to);
2599 entry->name.len = strlen(to);
2600 if (!entry->node.key || !entry->name.ptr) {
2601 free(entry->node.key);
2602 free(entry->name.ptr);
2603 free(entry);
2604 memprintf(err, "out of memory");
2605 return -1;
2606 }
2607 ebis_insert(&hdrs_map.map, &entry->node);
2608 return 0;
2609}
2610
2611static void h1_hdeaders_case_adjust_deinit()
2612{
2613 struct ebpt_node *node, *next;
2614 struct h1_hdr_entry *entry;
2615
2616 node = ebpt_first(&hdrs_map.map);
2617 while (node) {
2618 next = ebpt_next(node);
2619 ebpt_delete(node);
2620 entry = container_of(node, struct h1_hdr_entry, node);
2621 free(entry->node.key);
2622 free(entry->name.ptr);
2623 free(entry);
2624 node = next;
2625 }
2626 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002627}
2628
Christopher Faulet98fbe952019-07-22 16:18:24 +02002629static int cfg_h1_headers_case_adjust_postparser()
2630{
2631 FILE *file = NULL;
2632 char *c, *key_beg, *key_end, *value_beg, *value_end;
2633 char *err;
2634 int rc, line = 0, err_code = 0;
2635
2636 if (!hdrs_map.name)
2637 goto end;
2638
2639 file = fopen(hdrs_map.name, "r");
2640 if (!file) {
2641 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2642 hdrs_map.name);
2643 err_code |= ERR_ALERT | ERR_FATAL;
2644 goto end;
2645 }
2646
2647 /* now parse all lines. The file may contain only two header name per
2648 * line, separated by spaces. All heading and trailing spaces will be
2649 * ignored. Lines starting with a # are ignored.
2650 */
2651 while (fgets(trash.area, trash.size, file) != NULL) {
2652 line++;
2653 c = trash.area;
2654
2655 /* strip leading spaces and tabs */
2656 while (*c == ' ' || *c == '\t')
2657 c++;
2658
2659 /* ignore emptu lines, or lines beginning with a dash */
2660 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2661 continue;
2662
2663 /* look for the end of the key */
2664 key_beg = c;
2665 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2666 c++;
2667 key_end = c;
2668
2669 /* strip middle spaces and tabs */
2670 while (*c == ' ' || *c == '\t')
2671 c++;
2672
2673 /* look for the end of the value, it is the end of the line */
2674 value_beg = c;
2675 while (*c && *c != '\n' && *c != '\r')
2676 c++;
2677 value_end = c;
2678
2679 /* trim possibly trailing spaces and tabs */
2680 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2681 value_end--;
2682
2683 /* set final \0 and check entries */
2684 *key_end = '\0';
2685 *value_end = '\0';
2686
2687 err = NULL;
2688 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2689 if (rc < 0) {
2690 free(err);
2691 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2692 hdrs_map.name, err, line);
2693 err_code |= ERR_ALERT | ERR_FATAL;
2694 goto end;
2695 }
2696 if (rc > 0) {
2697 free(err);
2698 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2699 hdrs_map.name, err, line);
2700 err_code |= ERR_WARN;
2701 }
2702 }
2703
2704 end:
2705 if (file)
2706 fclose(file);
2707 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2708 return err_code;
2709}
2710
2711
2712/* config parser for global "h1-outgoing-header-case-adjust" */
2713static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2714 struct proxy *defpx, const char *file, int line,
2715 char **err)
2716{
2717 if (too_many_args(2, args, err, NULL))
2718 return -1;
2719 if (!*(args[1]) || !*(args[2])) {
2720 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2721 return -1;
2722 }
2723 return add_hdr_case_adjust(args[1], args[2], err);
2724}
2725
2726/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2727static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2728 struct proxy *defpx, const char *file, int line,
2729 char **err)
2730{
2731 if (too_many_args(1, args, err, NULL))
2732 return -1;
2733 if (!*(args[1])) {
2734 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2735 return -1;
2736 }
2737 free(hdrs_map.name);
2738 hdrs_map.name = strdup(args[1]);
2739 return 0;
2740}
2741
2742
2743/* config keyword parsers */
2744static struct cfg_kw_list cfg_kws = {{ }, {
2745 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2746 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2747 { 0, NULL, NULL },
2748 }
2749};
2750
2751INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2752REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2753
2754
Christopher Faulet51dbc942018-09-13 09:05:15 +02002755/****************************************/
2756/* MUX initialization and instanciation */
2757/****************************************/
2758
2759/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002760static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002761 .init = h1_init,
2762 .wake = h1_wake,
2763 .attach = h1_attach,
2764 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002765 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002766 .detach = h1_detach,
2767 .destroy = h1_destroy,
2768 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002769 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002770 .rcv_buf = h1_rcv_buf,
2771 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002772#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002773 .rcv_pipe = h1_rcv_pipe,
2774 .snd_pipe = h1_snd_pipe,
2775#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002776 .subscribe = h1_subscribe,
2777 .unsubscribe = h1_unsubscribe,
2778 .shutr = h1_shutr,
2779 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002780 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002781 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002782 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002783 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002784};
2785
2786
2787/* this mux registers default HTX proto */
2788static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02002789{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02002790
Willy Tarreau0108d902018-11-25 19:14:37 +01002791INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2792
Christopher Faulet51dbc942018-09-13 09:05:15 +02002793/*
2794 * Local variables:
2795 * c-indent-level: 8
2796 * c-basic-offset: 8
2797 * End:
2798 */