blob: 86c6076dcd522df622ec35a7f6d93258c51c8fb7 [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) {
1040 /* Incomplete or invalid message. If the buffer is full, it's an
1041 * error because headers are too large to be handled by the
1042 * parser. */
Christopher Faulete5438b72019-06-26 14:56:27 +02001043 if (ret < 0 || (!ret && !buf_room_for_htx_data(buf)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001044 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001045 goto end;
1046 }
1047
1048 /* messages headers fully parsed, do some checks to prepare the body
1049 * parsing.
1050 */
1051
Christopher Faulet9768c262018-10-22 09:34:31 +02001052 /* Save the request's method or the response's status, check if the body
1053 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001054 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001055 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001056
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001057 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001058 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001059
1060 if (h1s->meth == HTTP_METH_CONNECT) {
1061 /* Switch CONNECT requests to tunnel mode */
1062 h1_set_req_tunnel_mode(h1s);
1063 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001064
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001065 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1066 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001067 h1m->err_state = h1m->state;
1068 goto vsn_error;
1069 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001070 }
1071 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001072 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001073
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001074 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1075 h1s->status == 101) {
1076 /* Switch successfull replies to CONNECT requests and
1077 * protocol switching to tunnel mode. */
1078 h1_set_res_tunnel_mode(h1s);
1079 }
1080 else if ((h1s->meth == HTTP_METH_HEAD) ||
1081 (h1s->status >= 100 && h1s->status < 200) ||
1082 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001083 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001084 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1085 h1m->flags |= H1_MF_XFER_LEN;
1086 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001087 }
1088 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001089 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001090 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001091 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001092 else {
1093 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001094 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001095 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001096
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001097 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1098 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001099 h1m->err_state = h1m->state;
1100 goto vsn_error;
1101 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001102 }
1103
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001104 /* Set HTX start-line flags */
1105 if (h1m->flags & H1_MF_VER_11)
1106 flags |= HTX_SL_F_VER_11;
1107 if (h1m->flags & H1_MF_XFER_ENC)
1108 flags |= HTX_SL_F_XFER_ENC;
1109 if (h1m->flags & H1_MF_XFER_LEN) {
1110 flags |= HTX_SL_F_XFER_LEN;
1111 if (h1m->flags & H1_MF_CHNK)
1112 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001113 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001114 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001115 if (h1m->body_len == 0)
1116 flags |= HTX_SL_F_BODYLESS;
1117 }
1118 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001119 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001120 }
1121
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001122 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001123 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001124 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1125 if (htx_is_empty(htx))
1126 goto error;
1127 h1m_init_req(h1m);
1128 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1129 ret = 0;
1130 goto end;
1131 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001132
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001133 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1134 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001135 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001136 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001137
1138 /* Check if the uri contains an explicit scheme and if it is
1139 * "http" or "https". */
1140 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1141 sl->flags |= HTX_SL_F_HAS_SCHM;
1142 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1143 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1144 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001145 }
1146 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001147 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1148 if (htx_is_empty(htx))
1149 goto error;
1150 h1m_init_res(h1m);
1151 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1152 ret = 0;
1153 goto end;
1154 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001155
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001156 flags |= HTX_SL_F_IS_RESP;
1157 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1158 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001159 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001160 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001161 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001162
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001163 /* Set bytes used in the HTX mesage for the headers now */
1164 sl->hdrs_bytes = htx_used_space(htx) - used;
1165
Christopher Fauletb992af02019-03-28 15:42:24 +01001166 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001167
1168 /* If body length cannot be determined, set htx->extra to
1169 * ULLONG_MAX. This value is impossible in other cases.
1170 */
1171 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001172 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001173 end:
1174 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001175
1176 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001177 h1m->err_state = h1m->state;
1178 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001179 vsn_error:
1180 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001181 h1s->cs->flags |= CS_FL_EOI;
1182 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001183 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001184 ret = 0;
1185 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001186
1187 h2c_upgrade:
1188 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001189 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001190 htx->flags |= HTX_FL_UPGRADE;
1191 ret = 0;
1192 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001193}
1194
1195/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001196 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1197 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001198 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001199 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001200 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001201static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001202 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001203 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001204{
1205 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001206 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001207
Christopher Faulet129817b2018-09-20 16:14:40 +02001208 if (h1m->flags & H1_MF_XFER_LEN) {
1209 if (h1m->flags & H1_MF_CLEN) {
1210 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001211 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001212 if ((uint64_t)ret > h1m->curr_len)
1213 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001214 if (ret > b_contig_data(buf, *ofs))
1215 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001216
Christopher Faulet9768c262018-10-22 09:34:31 +02001217 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001218 /* very often with large files we'll face the following
1219 * situation :
1220 * - htx is empty and points to <htxbuf>
1221 * - ret == buf->data
1222 * - buf->head == sizeof(struct htx)
1223 * => we can swap the buffers and place an htx header into
1224 * the target buffer instead
1225 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001226 int32_t try = ret;
1227
Willy Tarreau78f548f2018-12-05 10:02:39 +01001228 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1229 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1230 void *raw_area = buf->area;
1231 void *htx_area = htxbuf->area;
1232 struct htx_blk *blk;
1233
1234 buf->area = htx_area;
1235 htxbuf->area = raw_area;
1236 htx = (struct htx *)htxbuf->area;
1237 htx->size = htxbuf->size - sizeof(*htx);
1238 htx_reset(htx);
1239 b_set_data(htxbuf, b_size(htxbuf));
1240
1241 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1242 blk->info += ret;
1243 /* nothing else to do, the old buffer now contains an
1244 * empty pre-initialized HTX header
1245 */
1246 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001247 else {
1248 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1249 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001250 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001251 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001252 *ofs += ret;
1253 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001254 if (ret < try)
1255 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001256 }
1257
1258 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001259 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001261 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001262 }
1263 else if (h1m->flags & H1_MF_CHNK) {
1264 new_chunk:
1265 /* te:chunked : parse chunks */
1266 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001267 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001268 if (ret <= 0)
1269 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001270 h1m->state = H1_MSG_CHUNK_SIZE;
1271
Christopher Fauletf2824e62018-10-01 12:12:37 +02001272 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001273 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001274 }
1275
1276 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1277 unsigned int chksz;
1278
Christopher Faulet30db3d72019-05-17 15:35:33 +02001279 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001280 if (ret <= 0)
1281 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001282 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001283
Christopher Faulet129817b2018-09-20 16:14:40 +02001284 h1m->curr_len = chksz;
1285 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001286 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001287 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001288
1289 if (!h1m->curr_len)
1290 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001291 }
1292
1293 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001294 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 if ((uint64_t)ret > h1m->curr_len)
1296 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001297 if (ret > b_contig_data(buf, *ofs))
1298 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001299
Christopher Faulet9768c262018-10-22 09:34:31 +02001300 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001301 int32_t try = ret;
1302
1303 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001304 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001305 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001306 *ofs += ret;
1307 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001308 if (ret < try)
1309 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001310 }
1311 if (!h1m->curr_len) {
1312 h1m->state = H1_MSG_CHUNK_CRLF;
1313 goto new_chunk;
1314 }
1315 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001316 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001317 }
1318 else {
1319 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1320 * is no body. Switch the message in DONE state
1321 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001322 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001323 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001324 }
1325 }
1326 else {
1327 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001328 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001329 if (ret > b_contig_data(buf, *ofs))
1330 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001331
Christopher Faulet9768c262018-10-22 09:34:31 +02001332 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001333 int32_t try = ret;
1334
1335 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001336
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001337 *ofs += ret;
1338 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001339 if (ret < try)
1340 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001341 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001342 }
1343
1344 end:
1345 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001346 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001347 h1s->cs->flags |= CS_FL_EOI;
1348 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001349 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001350 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001351 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001352 return 0;
1353 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001354 /* update htx->extra, only when the body length is known */
1355 if (h1m->flags & H1_MF_XFER_LEN)
1356 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001357 return total;
1358}
1359
1360/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001361 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1362 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1363 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1364 * responsible to update the parser state <h1m>.
1365 */
1366static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1367 struct buffer *buf, size_t *ofs, size_t max)
1368{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001369 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001370 struct h1m tlr_h1m;
1371 int ret = 0;
1372
1373 if (!max || !b_data(buf))
1374 goto end;
1375
1376 /* Realing input buffer if necessary */
1377 if (b_peek(buf, *ofs) > b_tail(buf))
1378 b_slow_realign(buf, trash.area, 0);
1379
1380 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1381 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1382 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1383 if (ret <= 0) {
1384 /* Incomplete or invalid trailers. If the buffer is full, it's
1385 * an error because traliers are too large to be handled by the
1386 * parser. */
1387 if (ret < 0 || (!ret && !buf_room_for_htx_data(buf)))
1388 goto error;
1389 goto end;
1390 }
1391
1392 /* messages trailers fully parsed. */
1393 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1394 if (htx_is_empty(htx))
1395 goto error;
1396 ret = 0;
1397 goto end;
1398 }
1399
1400 if (!htx_add_all_trailers(htx, hdrs))
1401 goto error;
1402
1403 *ofs += ret;
1404 h1s->flags |= H1S_F_HAVE_I_TLR;
1405 end:
1406 return ret;
1407
1408 error:
1409 h1m->err_state = h1m->state;
1410 h1m->err_pos = h1m->next;
1411 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1412 h1s->cs->flags |= CS_FL_EOI;
1413 htx->flags |= HTX_FL_PARSING_ERROR;
1414 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1415 ret = 0;
1416 goto end;
1417}
1418
1419/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001420 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001421 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1422 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001423 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001424static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001425{
Christopher Faulet539e0292018-11-19 10:40:09 +01001426 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001427 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001428 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001429 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001430 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001431 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001432
Christopher Faulet539e0292018-11-19 10:40:09 +01001433 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001434
Christopher Fauletf2824e62018-10-01 12:12:37 +02001435 if (!conn_is_back(h1c->conn)) {
1436 h1m = &h1s->req;
1437 errflag = H1S_F_REQ_ERROR;
1438 }
1439 else {
1440 h1m = &h1s->res;
1441 errflag = H1S_F_RES_ERROR;
1442 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001443
Christopher Faulet74027762019-02-26 14:45:05 +01001444 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001445 if (h1s->flags & errflag)
1446 goto end;
1447
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001448 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001449 size_t used = htx_used_space(htx);
1450
Christopher Faulet129817b2018-09-20 16:14:40 +02001451 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001452 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001453 if (!ret)
1454 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001455 if ((h1m->flags & H1_MF_RESP) &&
1456 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1457 h1m_init_res(&h1s->res);
1458 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1459 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001460 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001461 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001462 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001463 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001464 if (!ret)
1465 break;
1466 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001467 else if (h1m->state == H1_MSG_TRAILERS) {
1468 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1469 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1470 if (!ret)
1471 break;
1472 }
1473 if (!h1_process_eom(h1s, h1m, htx, count))
1474 break;
1475 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001476 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001477 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1478 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001479 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001480 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001481 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001482 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001483 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001484 if (!ret)
1485 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001486 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001487 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001488 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001489 break;
1490 }
1491
Christopher Faulet30db3d72019-05-17 15:35:33 +02001492 count -= htx_used_space(htx) - used;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001493 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001494
Christopher Faulet47365272018-10-31 17:40:50 +01001495 if (h1s->flags & errflag)
1496 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001497
Christopher Faulet539e0292018-11-19 10:40:09 +01001498 b_del(&h1c->ibuf, total);
1499
1500 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001501 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001502 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001503 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001504 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001505 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001506 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001507
Christopher Fauletcf56b992018-12-11 16:12:31 +01001508 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1509
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001510 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001511 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001512 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001513 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001514
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001515 if ((h1s->flags & H1S_F_REOS) && (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001516 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001517 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001518 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001519 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001520
Christopher Faulet30db3d72019-05-17 15:35:33 +02001521 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001522
1523 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001524 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001525 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001526 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001527}
1528
Christopher Faulet129817b2018-09-20 16:14:40 +02001529/*
1530 * Process outgoing data. It parses data and transfer them from the channel buffer into
1531 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1532 * 0 if it couldn't proceed.
1533 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001534static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1535{
Christopher Faulet129817b2018-09-20 16:14:40 +02001536 struct h1s *h1s = h1c->h1s;
1537 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001538 struct htx *chn_htx;
1539 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001540 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001541 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001542 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001543
Christopher Faulet47365272018-10-31 17:40:50 +01001544 if (!count)
1545 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001546
Christopher Faulet94b2c762019-05-24 15:28:57 +02001547 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001548 if (htx_is_empty(chn_htx))
1549 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001550
Christopher Faulet51dbc942018-09-13 09:05:15 +02001551 if (!h1_get_buf(h1c, &h1c->obuf)) {
1552 h1c->flags |= H1C_F_OUT_ALLOC;
1553 goto end;
1554 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001555
Christopher Fauletf2824e62018-10-01 12:12:37 +02001556 if (!conn_is_back(h1c->conn)) {
1557 h1m = &h1s->res;
1558 errflag = H1S_F_RES_ERROR;
1559 }
1560 else {
1561 h1m = &h1s->req;
1562 errflag = H1S_F_REQ_ERROR;
1563 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001564
Christopher Faulet0e54d542019-07-04 21:22:34 +02001565 if (h1s->flags & errflag)
1566 goto end;
1567
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001568 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001569 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001570
Willy Tarreau3815b222018-12-11 19:50:43 +01001571 /* Perform some optimizations to reduce the number of buffer copies.
1572 * First, if the mux's buffer is empty and the htx area contains
1573 * exactly one data block of the same size as the requested count,
1574 * then it's possible to simply swap the caller's buffer with the
1575 * mux's output buffer and adjust offsets and length to match the
1576 * entire DATA HTX block in the middle. In this case we perform a
1577 * true zero-copy operation from end-to-end. This is the situation
1578 * that happens all the time with large files. Second, if this is not
1579 * possible, but the mux's output buffer is empty, we still have an
1580 * opportunity to avoid the copy to the intermediary buffer, by making
1581 * the intermediary buffer's area point to the output buffer's area.
1582 * In this case we want to skip the HTX header to make sure that copies
1583 * remain aligned and that this operation remains possible all the
1584 * time. This goes for headers, data blocks and any data extracted from
1585 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001586 */
1587 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001588 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001589 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001590 htx_get_blk_value(chn_htx, blk).len == count) {
1591 void *old_area = h1c->obuf.area;
1592
1593 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001594 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001595 h1c->obuf.data = count;
1596
1597 buf->area = old_area;
1598 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001599
1600 /* The message is chunked. We need to emit the chunk
1601 * size. We have at least the size of the struct htx to
1602 * write the chunk envelope. It should be enough.
1603 */
1604 if (h1m->flags & H1_MF_CHNK) {
1605 h1_emit_chunk_size(&h1c->obuf, count);
1606 h1_emit_chunk_crlf(&h1c->obuf);
1607 }
1608
Willy Tarreau3815b222018-12-11 19:50:43 +01001609 total += count;
1610 goto out;
1611 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001612 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001613 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001614 else
1615 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001616
Christopher Fauletc2518a52019-06-25 21:41:02 +02001617 tmp.data = 0;
1618 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001619 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001620 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001621 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001622 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001623 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001624 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001625
Christopher Fauletb2e84162018-12-06 11:39:49 +01001626 vlen = sz;
1627 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001628 if (type != HTX_BLK_DATA)
Christopher Fauletb2e84162018-12-06 11:39:49 +01001629 goto copy;
1630 vlen = count;
1631 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001632
Christopher Faulet94b2c762019-05-24 15:28:57 +02001633 if (type == HTX_BLK_UNUSED)
1634 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001635
Christopher Faulet94b2c762019-05-24 15:28:57 +02001636 switch (h1m->state) {
1637 case H1_MSG_RQBEFORE:
1638 if (type != HTX_BLK_REQ_SL)
1639 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001640 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001641 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001642 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001643 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 goto copy;
1645 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001646 if (sl->flags & HTX_SL_F_BODYLESS)
1647 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001648 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001649 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001650
Christopher Faulet94b2c762019-05-24 15:28:57 +02001651 case H1_MSG_RPBEFORE:
1652 if (type != HTX_BLK_RES_SL)
1653 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001654 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001655 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001656 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001657 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001658 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001659 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001660 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001661 if (sl->info.res.status < 200 &&
1662 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001663 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001664 h1m->state = H1_MSG_HDR_FIRST;
1665 break;
1666
Christopher Faulet94b2c762019-05-24 15:28:57 +02001667 case H1_MSG_HDR_FIRST:
1668 case H1_MSG_HDR_NAME:
1669 case H1_MSG_HDR_L2_LWS:
1670 if (type == HTX_BLK_EOH)
1671 goto last_lf;
1672 if (type != HTX_BLK_HDR)
1673 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001674 h1m->state = H1_MSG_HDR_NAME;
1675 n = htx_get_blk_name(chn_htx, blk);
1676 v = htx_get_blk_value(chn_htx, blk);
1677
1678 if (isteqi(n, ist("transfer-encoding")))
1679 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001680 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001681 /* Only skip C-L header with invalid value. */
1682 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001683 goto skip_hdr;
1684 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001685 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001686 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 if (!v.len)
1688 goto skip_hdr;
1689 }
1690
Christopher Faulet98fbe952019-07-22 16:18:24 +02001691 /* Try to adjust the case of the header name */
1692 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1693 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001694 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001695 goto copy;
1696 skip_hdr:
1697 h1m->state = H1_MSG_HDR_L2_LWS;
1698 break;
1699
Christopher Faulet94b2c762019-05-24 15:28:57 +02001700 case H1_MSG_LAST_LF:
1701 if (type != HTX_BLK_EOH)
1702 goto error;
1703 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001704 h1m->state = H1_MSG_LAST_LF;
1705 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001706 /* There is no "Connection:" header and
1707 * it the conn_mode must be
1708 * processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001709 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001710 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001711 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001712 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001713 /* Try to adjust the case of the header name */
1714 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1715 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001716 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001717 goto copy;
1718 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001719 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001720 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001721
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001722 if ((h1s->meth != HTTP_METH_CONNECT &&
1723 (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 +01001724 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1725 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1726 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1727 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1728 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001729 /* chunking needed but header not seen */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001730 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Willy Tarreau4710d202019-01-03 17:39:54 +01001731 goto copy;
1732 h1m->flags |= H1_MF_CHNK;
1733 }
1734
Christopher Fauletc2518a52019-06-25 21:41:02 +02001735 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet9768c262018-10-22 09:34:31 +02001736 goto copy;
1737
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001738 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1739 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1740 h1_set_req_tunnel_mode(h1s);
1741 }
1742 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1743 /* a successfull reply to a CONNECT or a protocol switching is sent
1744 * to the client . Switch the response to tunnel mode. */
1745 h1_set_res_tunnel_mode(h1s);
1746 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001747 else if ((h1m->flags & H1_MF_RESP) &&
1748 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1749 h1m_init_res(&h1s->res);
1750 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001751 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001752 }
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001753 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1754 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001755 else
1756 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001757 break;
1758
Christopher Faulet94b2c762019-05-24 15:28:57 +02001759 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001760 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001761 if (type == HTX_BLK_EOM) {
1762 /* Chunked message without explicit trailers */
1763 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001764 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001765 goto copy;
1766 }
1767 goto done;
1768 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001769 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001770 /* If the message is not chunked, never
1771 * add the last chunk. */
1772 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet94b2c762019-05-24 15:28:57 +02001773 goto copy;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001774 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001775 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001776 else if (type != HTX_BLK_DATA)
1777 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001778 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001779 v.len = vlen;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001780 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001781 goto copy;
1782 break;
1783
Christopher Faulet94b2c762019-05-24 15:28:57 +02001784 case H1_MSG_TRAILERS:
1785 if (type == HTX_BLK_EOM)
1786 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001787 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001788 goto error;
1789 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001790 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001791 /* If the message is not chunked, ignore
1792 * trailers. It may happen with H2 messages. */
1793 if (!(h1m->flags & H1_MF_CHNK))
1794 break;
1795
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001796 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001797 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet32188212018-11-20 18:21:43 +01001798 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001799 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001800 else { // HTX_BLK_TLR
1801 n = htx_get_blk_name(chn_htx, blk);
1802 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001803
1804 /* Try to adjust the case of the header name */
1805 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1806 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001807 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001808 goto copy;
1809 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001810 break;
1811
Christopher Faulet94b2c762019-05-24 15:28:57 +02001812 case H1_MSG_DONE:
1813 if (type != HTX_BLK_EOM)
1814 goto error;
1815 done:
1816 h1m->state = H1_MSG_DONE;
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001817 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1818 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1819 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1820 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001821 break;
1822
Christopher Faulet9768c262018-10-22 09:34:31 +02001823 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001824 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001825 /* Unexpected error during output processing */
1826 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001827 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001828 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001829 break;
1830 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001831
1832 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001833 total += vlen;
1834 count -= vlen;
1835 if (sz == vlen)
1836 blk = htx_remove_blk(chn_htx, blk);
1837 else {
1838 htx_cut_data_blk(chn_htx, blk, vlen);
1839 break;
1840 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001841 }
1842
Christopher Faulet9768c262018-10-22 09:34:31 +02001843 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001844 /* when the output buffer is empty, tmp shares the same area so that we
1845 * only have to update pointers and lengths.
1846 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001847 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1848 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001849 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001850 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001851
Willy Tarreau3815b222018-12-11 19:50:43 +01001852 htx_to_buf(chn_htx, buf);
1853 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001854 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001855 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001856 end:
1857 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001858}
1859
Christopher Faulet51dbc942018-09-13 09:05:15 +02001860/*********************************************************/
1861/* functions below are I/O callbacks from the connection */
1862/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001863static void h1_wake_stream_for_recv(struct h1s *h1s)
1864{
1865 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001866 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001867 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001868 h1s->recv_wait = NULL;
1869 }
1870}
1871static void h1_wake_stream_for_send(struct h1s *h1s)
1872{
1873 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001874 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001875 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001876 h1s->send_wait = NULL;
1877 }
1878}
1879
Christopher Faulet51dbc942018-09-13 09:05:15 +02001880/*
1881 * Attempt to read data, and subscribe if none available
1882 */
1883static int h1_recv(struct h1c *h1c)
1884{
1885 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001886 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001887 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001888 int rcvd = 0;
1889
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001890 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001891 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001892
Olivier Houchard75159a92018-12-03 18:46:09 +01001893 if (!h1_recv_allowed(h1c)) {
1894 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001895 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001896 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001897
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001898 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1899 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001900 goto end;
1901 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001902
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001903 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001904 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001905 h1_wake_stream_for_recv(h1s);
1906 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001907 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001908 }
1909
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001910 /*
1911 * If we only have a small amount of data, realign it,
1912 * it's probably cheaper than doing 2 recv() calls.
1913 */
1914 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1915 b_slow_realign(&h1c->ibuf, trash.area, 0);
1916
Willy Tarreau45f2b892018-12-05 07:59:27 +01001917 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001918 if (max) {
1919 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001920
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001921 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001922 if (!b_data(&h1c->ibuf)) {
1923 /* try to pre-align the buffer like the rxbufs will be
1924 * to optimize memory copies.
1925 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001926 h1c->ibuf.head = sizeof(struct htx);
1927 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001928 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001929 }
Christopher Faulet47365272018-10-31 17:40:50 +01001930 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001931 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001932 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001933 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001934 if (h1s->csinfo.t_idle == -1)
1935 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1936 }
Christopher Faulet47365272018-10-31 17:40:50 +01001937 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001938
Olivier Houchardcc3fec82019-07-26 15:11:11 +02001939 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001940 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001941 goto end;
1942 }
1943
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001944 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001945
Christopher Faulet9768c262018-10-22 09:34:31 +02001946 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001947 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1948 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001949
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001950 if (conn_xprt_read0_pending(conn) && h1s) {
1951 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001952 rcvd = 1;
1953 }
1954
Christopher Faulet51dbc942018-09-13 09:05:15 +02001955 if (!b_data(&h1c->ibuf))
1956 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001957 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001958 h1c->flags |= H1C_F_IN_FULL;
1959 return rcvd;
1960}
1961
1962
1963/*
1964 * Try to send data if possible
1965 */
1966static int h1_send(struct h1c *h1c)
1967{
1968 struct connection *conn = h1c->conn;
1969 unsigned int flags = 0;
1970 size_t ret;
1971 int sent = 0;
1972
1973 if (conn->flags & CO_FL_ERROR)
1974 return 0;
1975
1976 if (!b_data(&h1c->obuf))
1977 goto end;
1978
1979 if (h1c->flags & H1C_F_OUT_FULL)
1980 flags |= CO_SFL_MSG_MORE;
1981
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001982 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001983 if (ret > 0) {
1984 h1c->flags &= ~H1C_F_OUT_FULL;
1985 b_del(&h1c->obuf, ret);
1986 sent = 1;
1987 }
1988
Christopher Faulet145aa472018-12-06 10:56:20 +01001989 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1990 /* error or output closed, nothing to send, clear the buffer to release it */
1991 b_reset(&h1c->obuf);
1992 }
1993
Christopher Faulet51dbc942018-09-13 09:05:15 +02001994 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001995 if (!(h1c->flags & H1C_F_OUT_FULL))
1996 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001997
Christopher Faulet51dbc942018-09-13 09:05:15 +02001998 /* We're done, no more to send */
1999 if (!b_data(&h1c->obuf)) {
2000 h1_release_buf(h1c, &h1c->obuf);
2001 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01002002 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002003 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002004 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002005 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002006
2007 return sent;
2008}
2009
Christopher Faulet51dbc942018-09-13 09:05:15 +02002010
2011/* callback called on any event by the connection handler.
2012 * It applies changes and returns zero, or < 0 if it wants immediate
2013 * destruction of the connection.
2014 */
2015static int h1_process(struct h1c * h1c)
2016{
2017 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002018 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002019
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002020 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002021 return -1;
2022
Christopher Fauletfeb11742018-11-29 15:12:34 +01002023 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002024 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002025 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01002026 conn_xprt_read0_pending(conn))
2027 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002028 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002029 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002030 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002031 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002032 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002033 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002034 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002035 }
2036
Christopher Fauletfeb11742018-11-29 15:12:34 +01002037 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2038 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2039
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002040 if (conn_xprt_read0_pending(conn))
2041 h1s->flags |= H1S_F_REOS;
2042
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002043 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002044 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002045 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002046 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002047 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002048 h1s->cs->data_cb->wake(h1s->cs);
2049 }
Christopher Faulet47365272018-10-31 17:40:50 +01002050 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002051 if (h1c->task) {
2052 h1c->task->expire = TICK_ETERNITY;
2053 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002054 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 +01002055 ? h1c->shut_timeout
2056 : h1c->timeout));
2057 task_queue(h1c->task);
2058 }
2059 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002060 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002061
2062 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002063 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002064 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002065}
2066
2067static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2068{
2069 struct h1c *h1c = ctx;
2070 int ret = 0;
2071
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002072 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002073 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002074 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002075 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002076 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002077 h1_process(h1c);
2078 return NULL;
2079}
2080
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002081static void h1_reset(struct connection *conn)
2082{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002083
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002084}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002085
2086static int h1_wake(struct connection *conn)
2087{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002088 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002089 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002090
Christopher Faulet539e0292018-11-19 10:40:09 +01002091 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002092 ret = h1_process(h1c);
2093 if (ret == 0) {
2094 struct h1s *h1s = h1c->h1s;
2095
2096 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2097 ret = h1s->cs->data_cb->wake(h1s->cs);
2098 }
2099 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002100}
2101
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002102/* Connection timeout management. The principle is that if there's no receipt
2103 * nor sending for a certain amount of time, the connection is closed.
2104 */
2105static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2106{
2107 struct h1c *h1c = context;
2108 int expired = tick_is_expired(t->expire, now_ms);
2109
2110 if (!expired && h1c)
2111 return t;
2112
Olivier Houchard3f795f72019-04-17 22:51:06 +02002113 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002114
2115 if (!h1c) {
2116 /* resources were already deleted */
2117 return NULL;
2118 }
2119
2120 h1c->task = NULL;
2121 /* If a stream is still attached to the mux, just set an error and wait
2122 * for the stream's timeout. Otherwise, release the mux. This is only ok
2123 * because same timeouts are used.
2124 */
2125 if (h1c->h1s && h1c->h1s->cs)
2126 h1c->flags |= H1C_F_CS_ERROR;
2127 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002128 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002129 return NULL;
2130}
2131
Christopher Faulet51dbc942018-09-13 09:05:15 +02002132/*******************************************/
2133/* functions below are used by the streams */
2134/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002135
Christopher Faulet51dbc942018-09-13 09:05:15 +02002136/*
2137 * Attach a new stream to a connection
2138 * (Used for outgoing connections)
2139 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002140static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002141{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002142 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002143 struct conn_stream *cs = NULL;
2144 struct h1s *h1s;
2145
2146 if (h1c->flags & H1C_F_CS_ERROR)
2147 goto end;
2148
2149 cs = cs_new(h1c->conn);
2150 if (!cs)
2151 goto end;
2152
Olivier Houchardf502aca2018-12-14 19:42:40 +01002153 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002154 if (h1s == NULL)
2155 goto end;
2156
2157 return cs;
2158 end:
2159 cs_free(cs);
2160 return NULL;
2161}
2162
2163/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2164 * this mux, it's easy as we can only store a single conn_stream.
2165 */
2166static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2167{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002168 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002169 struct h1s *h1s = h1c->h1s;
2170
2171 if (h1s)
2172 return h1s->cs;
2173
2174 return NULL;
2175}
2176
Christopher Faulet73c12072019-04-08 11:23:22 +02002177static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002178{
Christopher Faulet73c12072019-04-08 11:23:22 +02002179 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002180
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002181 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002182 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002183}
2184
2185/*
2186 * Detach the stream from the connection and possibly release the connection.
2187 */
2188static void h1_detach(struct conn_stream *cs)
2189{
2190 struct h1s *h1s = cs->ctx;
2191 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002192 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002193 int has_keepalive;
2194 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002195
2196 cs->ctx = NULL;
2197 if (!h1s)
2198 return;
2199
Olivier Houchardf502aca2018-12-14 19:42:40 +01002200 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002201 h1c = h1s->h1c;
2202 h1s->cs = NULL;
2203
Olivier Houchard8a786902018-12-15 16:05:40 +01002204 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2205 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2206 h1s_destroy(h1s);
2207
2208 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002209 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002210 /* If there are any excess server data in the input buffer,
2211 * release it and close the connection ASAP (some data may
2212 * remain in the output buffer). This happens if a server sends
2213 * invalid responses. So in such case, we don't want to reuse
2214 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002215 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002216 if (b_data(&h1c->ibuf)) {
2217 h1_release_buf(h1c, &h1c->ibuf);
2218 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2219 goto release;
2220 }
Christopher Faulet03627242019-07-19 11:34:08 +02002221
Christopher Faulet9400a392018-11-23 23:10:39 +01002222 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002223 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002224 h1c->conn->flags |= CO_FL_PRIVATE;
2225
Olivier Houchard44d59142018-12-13 18:46:22 +01002226 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002227 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002228 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2229 h1c->conn->owner = NULL;
2230 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2231 /* The server doesn't want it, let's kill the connection right away */
2232 h1c->conn->mux->destroy(h1c->conn);
2233 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002234 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002235 return;
2236
2237 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002238 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002239 if (h1c->conn->owner == sess) {
2240 int ret = session_check_idle_conn(sess, h1c->conn);
2241 if (ret == -1)
2242 /* The connection got destroyed, let's leave */
2243 return;
2244 else if (ret == 1) {
2245 /* The connection was added to the server list,
2246 * wake the task so we can subscribe to events
2247 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002248 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002249 return;
2250 }
2251 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002252 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002253 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002254 struct server *srv = objt_server(h1c->conn->target);
2255
2256 if (srv) {
2257 if (h1c->conn->flags & CO_FL_PRIVATE)
2258 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002259 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002260 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2261 else
2262 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2263 }
2264 }
2265 }
2266
Christopher Fauletf1204b82019-07-19 14:51:06 +02002267 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002268 /* 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 +02002269 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2270 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2271 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2272 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002273 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002274 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002275 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002276 if (h1c->task) {
2277 h1c->task->expire = TICK_ETERNITY;
2278 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002279 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 +01002280 ? h1c->shut_timeout
2281 : h1c->timeout));
2282 task_queue(h1c->task);
2283 }
2284 }
2285 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002286}
2287
2288
2289static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2290{
2291 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002292 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002293
2294 if (!h1s)
2295 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002296 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002297
Christopher Faulet7f366362019-04-08 10:51:20 +02002298 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2299 goto do_shutr;
2300
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002301 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002302 return;
2303
Christopher Faulet7f366362019-04-08 10:51:20 +02002304 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002305 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2306 if (cs->flags & CS_FL_SHR)
2307 return;
2308 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002309 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2310 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002311 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 +02002312 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002313}
2314
2315static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2316{
2317 struct h1s *h1s = cs->ctx;
2318 struct h1c *h1c;
2319
2320 if (!h1s)
2321 return;
2322 h1c = h1s->h1c;
2323
Christopher Faulet7f366362019-04-08 10:51:20 +02002324 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2325 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002326
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002327 if ((h1c->flags & H1C_F_UPG_H2C) ||
2328 ((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 +02002329 return;
2330
Christopher Faulet7f366362019-04-08 10:51:20 +02002331 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002332 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2333 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2334 return;
2335
Christopher Faulet666a0c42019-01-08 11:12:04 +01002336 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002337}
2338
Christopher Faulet666a0c42019-01-08 11:12:04 +01002339static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002340{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002341 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002342
Christopher Faulet666a0c42019-01-08 11:12:04 +01002343 conn_xprt_shutw(conn);
2344 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2345 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2346 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002347}
2348
2349/* Called from the upper layer, to unsubscribe to events */
2350static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2351{
2352 struct wait_event *sw;
2353 struct h1s *h1s = cs->ctx;
2354
2355 if (!h1s)
2356 return 0;
2357
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002358 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002359 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002360 BUG_ON(h1s->recv_wait != sw);
2361 sw->events &= ~SUB_RETRY_RECV;
2362 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002363 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002364 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002365 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002366 BUG_ON(h1s->send_wait != sw);
2367 sw->events &= ~SUB_RETRY_SEND;
2368 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002369 }
2370 return 0;
2371}
2372
2373/* Called from the upper layer, to subscribe to events, such as being able to send */
2374static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2375{
2376 struct wait_event *sw;
2377 struct h1s *h1s = cs->ctx;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002378 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002379
2380 if (!h1s)
2381 return -1;
2382
2383 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002384 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002385 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002386 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2387 sw->events |= SUB_RETRY_RECV;
2388 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002389 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002390 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002391 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002392 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2393 sw->events |= SUB_RETRY_SEND;
2394 h1s->send_wait = sw;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002395 /*
2396 * If the conn_stream attempt to subscribe, and the
2397 * mux isn't subscribed to the connection, then it
2398 * probably means the connection wasn't established
2399 * yet, so we have to subscribe.
2400 */
2401 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2402 h1c->conn->xprt->subscribe(h1c->conn,
2403 h1c->conn->xprt_ctx,
2404 SUB_RETRY_SEND,
2405 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002406 return 0;
2407 default:
2408 break;
2409 }
2410 return -1;
2411}
2412
2413/* Called from the upper layer, to receive data */
2414static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2415{
2416 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002417 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002418 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002419 size_t ret = 0;
2420
Christopher Faulet539e0292018-11-19 10:40:09 +01002421 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002422 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002423
Christopher Faulete18777b2019-04-16 16:46:36 +02002424 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002425
2426 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2427 h1s->flags |= H1S_F_BUF_FLUSH;
2428 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002429 else {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002430 h1s->flags &= ~H1S_F_SPLICED_DATA;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002431 if (h1m->state != H1_MSG_DONE &&
2432 !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002433 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002434 }
2435 return ret;
2436}
2437
2438
2439/* Called from the upper layer, to send data */
2440static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2441{
2442 struct h1s *h1s = cs->ctx;
2443 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002444 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002445
2446 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002447 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002448
2449 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002450
2451 /* If we're not connected yet, or we're waiting for a handshake, stop
2452 * now, as we don't want to remove everything from the channel buffer
2453 * before we're sure we can send it.
2454 */
2455 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
2456 (h1c->conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002457 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002458
Christopher Fauletc31872f2019-06-04 22:09:36 +02002459 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002460 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002461
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002462 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2463 ret = h1_process_output(h1c, buf, count);
2464 if (!ret)
2465 break;
2466 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002467 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002468 if (!h1_send(h1c))
2469 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002470 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002471
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002472 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002473}
2474
Willy Tarreaue5733232019-05-22 19:24:06 +02002475#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002476/* Send and get, using splicing */
2477static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2478{
2479 struct h1s *h1s = cs->ctx;
2480 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2481 int ret = 0;
2482
Christopher Faulet1146f972019-05-29 14:35:24 +02002483 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002484 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002485 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2486 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002487 goto end;
2488 }
2489
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002490 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002491 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002492 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002493 }
2494
2495 h1s->flags &= ~H1S_F_BUF_FLUSH;
2496 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002497 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2498 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002499 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002500 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002501 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002502 if (!h1m->curr_len)
2503 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2504 }
2505
Christopher Faulet1be55f92018-10-02 15:59:23 +02002506 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002507 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002508 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002509 if (!pipe->data)
2510 cs->flags |= CS_FL_EOS;
2511 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002512 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002513}
2514
2515static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2516{
2517 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002518 int ret = 0;
2519
2520 if (b_data(&h1s->h1c->obuf))
2521 goto end;
2522
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002523 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002524 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002525 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002526 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002527 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002528 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002529 return ret;
2530}
2531#endif
2532
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002533/* for debugging with CLI's "show fd" command */
2534static void h1_show_fd(struct buffer *msg, struct connection *conn)
2535{
2536 struct h1c *h1c = conn->ctx;
2537 struct h1s *h1s = h1c->h1s;
2538
Christopher Fauletf376a312019-01-04 15:16:06 +01002539 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2540 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002541 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2542 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2543 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2544 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2545
2546 if (h1s) {
2547 char *method;
2548
2549 if (h1s->meth < HTTP_METH_OTHER)
2550 method = http_known_methods[h1s->meth].ptr;
2551 else
2552 method = "UNKNOWN";
2553 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2554 " .meth=%s status=%d",
2555 h1s, h1s->flags,
2556 h1m_state_str(h1s->req.state),
2557 h1m_state_str(h1s->res.state), method, h1s->status);
2558 if (h1s->cs)
2559 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2560 h1s->cs->flags, h1s->cs->data);
2561 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002562}
2563
2564
2565/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2566static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2567{
2568 struct h1_hdr_entry *entry;
2569
2570 /* Be sure there is a non-empty <to> */
2571 if (!strlen(to)) {
2572 memprintf(err, "expect <to>");
2573 return -1;
2574 }
2575
2576 /* Be sure only the case differs between <from> and <to> */
2577 if (strcasecmp(from, to)) {
2578 memprintf(err, "<from> and <to> must not differ execpt the case");
2579 return -1;
2580 }
2581
2582 /* Be sure <from> does not already existsin the tree */
2583 if (ebis_lookup(&hdrs_map.map, from)) {
2584 memprintf(err, "duplicate entry '%s'", from);
2585 return -1;
2586 }
2587
2588 /* Create the entry and insert it in the tree */
2589 entry = malloc(sizeof(*entry));
2590 if (!entry) {
2591 memprintf(err, "out of memory");
2592 return -1;
2593 }
2594
2595 entry->node.key = strdup(from);
2596 entry->name.ptr = strdup(to);
2597 entry->name.len = strlen(to);
2598 if (!entry->node.key || !entry->name.ptr) {
2599 free(entry->node.key);
2600 free(entry->name.ptr);
2601 free(entry);
2602 memprintf(err, "out of memory");
2603 return -1;
2604 }
2605 ebis_insert(&hdrs_map.map, &entry->node);
2606 return 0;
2607}
2608
2609static void h1_hdeaders_case_adjust_deinit()
2610{
2611 struct ebpt_node *node, *next;
2612 struct h1_hdr_entry *entry;
2613
2614 node = ebpt_first(&hdrs_map.map);
2615 while (node) {
2616 next = ebpt_next(node);
2617 ebpt_delete(node);
2618 entry = container_of(node, struct h1_hdr_entry, node);
2619 free(entry->node.key);
2620 free(entry->name.ptr);
2621 free(entry);
2622 node = next;
2623 }
2624 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002625}
2626
Christopher Faulet98fbe952019-07-22 16:18:24 +02002627static int cfg_h1_headers_case_adjust_postparser()
2628{
2629 FILE *file = NULL;
2630 char *c, *key_beg, *key_end, *value_beg, *value_end;
2631 char *err;
2632 int rc, line = 0, err_code = 0;
2633
2634 if (!hdrs_map.name)
2635 goto end;
2636
2637 file = fopen(hdrs_map.name, "r");
2638 if (!file) {
2639 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2640 hdrs_map.name);
2641 err_code |= ERR_ALERT | ERR_FATAL;
2642 goto end;
2643 }
2644
2645 /* now parse all lines. The file may contain only two header name per
2646 * line, separated by spaces. All heading and trailing spaces will be
2647 * ignored. Lines starting with a # are ignored.
2648 */
2649 while (fgets(trash.area, trash.size, file) != NULL) {
2650 line++;
2651 c = trash.area;
2652
2653 /* strip leading spaces and tabs */
2654 while (*c == ' ' || *c == '\t')
2655 c++;
2656
2657 /* ignore emptu lines, or lines beginning with a dash */
2658 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2659 continue;
2660
2661 /* look for the end of the key */
2662 key_beg = c;
2663 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2664 c++;
2665 key_end = c;
2666
2667 /* strip middle spaces and tabs */
2668 while (*c == ' ' || *c == '\t')
2669 c++;
2670
2671 /* look for the end of the value, it is the end of the line */
2672 value_beg = c;
2673 while (*c && *c != '\n' && *c != '\r')
2674 c++;
2675 value_end = c;
2676
2677 /* trim possibly trailing spaces and tabs */
2678 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2679 value_end--;
2680
2681 /* set final \0 and check entries */
2682 *key_end = '\0';
2683 *value_end = '\0';
2684
2685 err = NULL;
2686 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2687 if (rc < 0) {
2688 free(err);
2689 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2690 hdrs_map.name, err, line);
2691 err_code |= ERR_ALERT | ERR_FATAL;
2692 goto end;
2693 }
2694 if (rc > 0) {
2695 free(err);
2696 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2697 hdrs_map.name, err, line);
2698 err_code |= ERR_WARN;
2699 }
2700 }
2701
2702 end:
2703 if (file)
2704 fclose(file);
2705 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2706 return err_code;
2707}
2708
2709
2710/* config parser for global "h1-outgoing-header-case-adjust" */
2711static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2712 struct proxy *defpx, const char *file, int line,
2713 char **err)
2714{
2715 if (too_many_args(2, args, err, NULL))
2716 return -1;
2717 if (!*(args[1]) || !*(args[2])) {
2718 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2719 return -1;
2720 }
2721 return add_hdr_case_adjust(args[1], args[2], err);
2722}
2723
2724/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2725static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2726 struct proxy *defpx, const char *file, int line,
2727 char **err)
2728{
2729 if (too_many_args(1, args, err, NULL))
2730 return -1;
2731 if (!*(args[1])) {
2732 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2733 return -1;
2734 }
2735 free(hdrs_map.name);
2736 hdrs_map.name = strdup(args[1]);
2737 return 0;
2738}
2739
2740
2741/* config keyword parsers */
2742static struct cfg_kw_list cfg_kws = {{ }, {
2743 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2744 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2745 { 0, NULL, NULL },
2746 }
2747};
2748
2749INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2750REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2751
2752
Christopher Faulet51dbc942018-09-13 09:05:15 +02002753/****************************************/
2754/* MUX initialization and instanciation */
2755/****************************************/
2756
2757/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002758static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002759 .init = h1_init,
2760 .wake = h1_wake,
2761 .attach = h1_attach,
2762 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002763 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002764 .detach = h1_detach,
2765 .destroy = h1_destroy,
2766 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002767 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002768 .rcv_buf = h1_rcv_buf,
2769 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002770#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002771 .rcv_pipe = h1_rcv_pipe,
2772 .snd_pipe = h1_snd_pipe,
2773#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002774 .subscribe = h1_subscribe,
2775 .unsubscribe = h1_unsubscribe,
2776 .shutr = h1_shutr,
2777 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002778 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002779 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002780 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002781 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002782};
2783
2784
2785/* this mux registers default HTX proto */
2786static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02002787{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02002788
Willy Tarreau0108d902018-11-25 19:14:37 +01002789INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2790
Christopher Faulet51dbc942018-09-13 09:05:15 +02002791/*
2792 * Local variables:
2793 * c-indent-level: 8
2794 * c-basic-offset: 8
2795 * End:
2796 */