blob: 7cc7ba6eba76e97bce6c72a5304c17613c718984 [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 Faulet4f0f88a2019-08-10 11:17:44 +020026#include <proto/h1_htx.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020027#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020028#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010029#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030#include <proto/stream.h>
31#include <proto/stream_interface.h>
32
33/*
34 * H1 Connection flags (32 bits)
35 */
36#define H1C_F_NONE 0x00000000
37
38/* Flags indicating why writing output data are blocked */
39#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
40#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
41/* 0x00000004 - 0x00000008 unused */
42
43/* Flags indicating why reading input data are blocked. */
44#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
45#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010046#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010047/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020048
49#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
50#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010051#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Faulet51dbc942018-09-13 09:05:15 +020052
Christopher Fauletf2824e62018-10-01 12:12:37 +020053#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 +020054#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020055
Christopher Faulet51dbc942018-09-13 09:05:15 +020056/*
57 * H1 Stream flags (32 bits)
58 */
Christopher Faulet129817b2018-09-20 16:14:40 +020059#define H1S_F_NONE 0x00000000
60#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020061#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
62#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020063#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020064#define H1S_F_WANT_KAL 0x00000010
65#define H1S_F_WANT_TUN 0x00000020
66#define H1S_F_WANT_CLO 0x00000040
67#define H1S_F_WANT_MSK 0x00000070
68#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010069#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010070#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010071#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +020072#define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */
73/* 0x00002000 .. 0x00002000 unused */
Christopher Fauletada34b62019-05-24 16:36:43 +020074#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020075
76/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020077struct h1c {
78 struct connection *conn;
79 struct proxy *px;
80 uint32_t flags; /* Connection flags: H1C_F_* */
81
82 struct buffer ibuf; /* Input buffer to store data before parsing */
83 struct buffer obuf; /* Output buffer to store data after reformatting */
84
85 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
86 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
87
88 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010089 struct task *task; /* timeout management task */
90 int timeout; /* idle timeout duration in ticks */
91 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020092};
93
94/* H1 stream descriptor */
95struct h1s {
96 struct h1c *h1c;
97 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010098 struct cs_info csinfo; /* CS info, only used for client connections */
99 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200100
Christopher Faulet51dbc942018-09-13 09:05:15 +0200101 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
102 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200103
Olivier Houchardf502aca2018-12-14 19:42:40 +0100104 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200105 struct h1m req;
106 struct h1m res;
107
108 enum http_meth_t meth; /* HTTP resquest method */
109 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200110};
111
Christopher Faulet98fbe952019-07-22 16:18:24 +0200112/* Map of headers used to convert outgoing headers */
113struct h1_hdrs_map {
114 char *name;
115 struct eb_root map;
116};
117
118/* An entry in a headers map */
119struct h1_hdr_entry {
120 struct ist name;
121 struct ebpt_node node;
122};
123
124/* Declare the headers map */
125static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
126
127
Christopher Faulet51dbc942018-09-13 09:05:15 +0200128/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100129DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
130DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200131
Christopher Faulet51dbc942018-09-13 09:05:15 +0200132static int h1_recv(struct h1c *h1c);
133static int h1_send(struct h1c *h1c);
134static int h1_process(struct h1c *h1c);
135static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100136static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100137static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200138
139/*****************************************************/
140/* functions below are for dynamic buffer management */
141/*****************************************************/
142/*
143 * Indicates whether or not the we may call the h1_recv() function to
144 * attempt to receive data into the buffer and/or parse pending data. The
145 * condition is a bit complex due to some API limits for now. The rules are the
146 * following :
147 * - if an error or a shutdown was detected on the connection and the buffer
148 * is empty, we must not attempt to receive
149 * - if the input buffer failed to be allocated, we must not try to receive
150 * and we know there is nothing pending
151 * - if no flag indicates a blocking condition, we may attempt to receive,
152 * regardless of whether the input buffer is full or not, so that only de
153 * receiving part decides whether or not to block. This is needed because
154 * the connection API indeed prevents us from re-enabling receipt that is
155 * already enabled in a polled state, so we must always immediately stop as
156 * soon as the mux can't proceed so as never to hit an end of read with data
157 * pending in the buffers.
158 * - otherwise must may not attempt to receive
159 */
160static inline int h1_recv_allowed(const struct h1c *h1c)
161{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100162 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200163 return 0;
164
Christopher Fauletcf56b992018-12-11 16:12:31 +0100165 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
166 return 0;
167
Christopher Fauletcb55f482018-12-10 11:56:47 +0100168 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200169 return 1;
170
171 return 0;
172}
173
174/*
175 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
176 * flags are used to figure what buffer was requested. It returns 1 if the
177 * allocation succeeds, in which case the connection is woken up, or 0 if it's
178 * impossible to wake up and we prefer to be woken up later.
179 */
180static int h1_buf_available(void *target)
181{
182 struct h1c *h1c = target;
183
184 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
185 h1c->flags &= ~H1C_F_IN_ALLOC;
186 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200187 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200188 return 1;
189 }
190
191 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
192 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200193 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200194 return 1;
195 }
196
Christopher Faulet51dbc942018-09-13 09:05:15 +0200197 return 0;
198}
199
200/*
201 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
202 */
203static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
204{
205 struct buffer *buf = NULL;
206
207 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
208 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
209 h1c->buf_wait.target = h1c;
210 h1c->buf_wait.wakeup_cb = h1_buf_available;
211 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
212 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
213 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
214 __conn_xprt_stop_recv(h1c->conn);
215 }
216 return buf;
217}
218
219/*
220 * Release a buffer, if any, and try to wake up entities waiting in the buffer
221 * wait queue.
222 */
223static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
224{
225 if (bptr->size) {
226 b_free(bptr);
227 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
228 }
229}
230
Willy Tarreau00f18a32019-01-26 12:19:01 +0100231/* returns the number of streams in use on a connection to figure if it's
232 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
233 * as the caller will want to know if it was the last one after a detach().
234 */
235static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200236{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100237 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200238
Willy Tarreau00f18a32019-01-26 12:19:01 +0100239 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200240}
241
Willy Tarreau00f18a32019-01-26 12:19:01 +0100242/* returns the number of streams still available on a connection */
243static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100244{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100245 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100246}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200247
Willy Tarreau00f18a32019-01-26 12:19:01 +0100248
Christopher Faulet51dbc942018-09-13 09:05:15 +0200249/*****************************************************************/
250/* functions below are dedicated to the mux setup and management */
251/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200252
253/* returns non-zero if there are input data pending for stream h1s. */
254static inline size_t h1s_data_pending(const struct h1s *h1s)
255{
256 const struct h1m *h1m;
257
258 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
259 if (h1m->state == H1_MSG_DONE)
260 return 0; // data not for this stream (e.g. pipelining)
261
262 return b_data(&h1s->h1c->ibuf);
263}
264
Christopher Faulet47365272018-10-31 17:40:50 +0100265static struct conn_stream *h1s_new_cs(struct h1s *h1s)
266{
267 struct conn_stream *cs;
268
269 cs = cs_new(h1s->h1c->conn);
270 if (!cs)
271 goto err;
272 h1s->cs = cs;
273 cs->ctx = h1s;
274
275 if (h1s->flags & H1S_F_NOT_FIRST)
276 cs->flags |= CS_FL_NOT_FIRST;
277
278 if (stream_create_from_cs(cs) < 0)
279 goto err;
280 return cs;
281
282 err:
283 cs_free(cs);
284 h1s->cs = NULL;
285 return NULL;
286}
287
Olivier Houchardf502aca2018-12-14 19:42:40 +0100288static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200289{
290 struct h1s *h1s;
291
292 h1s = pool_alloc(pool_head_h1s);
293 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100294 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200295
296 h1s->h1c = h1c;
297 h1c->h1s = h1s;
298
Olivier Houchardf502aca2018-12-14 19:42:40 +0100299 h1s->sess = sess;
300
Christopher Faulet51dbc942018-09-13 09:05:15 +0200301 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100302 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200303
304 h1s->recv_wait = NULL;
305 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200306
307 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100308 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200309
Christopher Faulet129817b2018-09-20 16:14:40 +0200310 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100311 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200312
313 h1s->status = 0;
314 h1s->meth = HTTP_METH_OTHER;
315
Christopher Faulet47365272018-10-31 17:40:50 +0100316 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
317 h1s->flags |= H1S_F_NOT_FIRST;
318 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
319
Christopher Faulet129817b2018-09-20 16:14:40 +0200320 if (!conn_is_back(h1c->conn)) {
321 if (h1c->px->options2 & PR_O2_REQBUG_OK)
322 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200323
324 /* For frontend connections we should always have a session */
325 if (!sess)
326 sess = h1c->conn->owner;
327
Dave Pirotte234740f2019-07-10 13:57:38 +0000328 /* Timers for subsequent sessions on the same HTTP 1.x connection
329 * measure from `now`, not from the connection accept time */
330 if (h1s->flags & H1S_F_NOT_FIRST) {
331 h1s->csinfo.create_date = date;
332 h1s->csinfo.tv_create = now;
333 h1s->csinfo.t_handshake = 0;
334 h1s->csinfo.t_idle = -1;
335 }
336 else {
337 h1s->csinfo.create_date = sess->accept_date;
338 h1s->csinfo.tv_create = sess->tv_accept;
339 h1s->csinfo.t_handshake = sess->t_handshake;
340 h1s->csinfo.t_idle = -1;
341 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200342 }
343 else {
344 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
345 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200346
Christopher Fauletfeb11742018-11-29 15:12:34 +0100347 h1s->csinfo.create_date = date;
348 h1s->csinfo.tv_create = now;
349 h1s->csinfo.t_handshake = 0;
350 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200351 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100352
Christopher Faulete9b70722019-04-08 10:46:02 +0200353 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
354 * create a new one.
355 */
356 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200357 cs->ctx = h1s;
358 h1s->cs = cs;
359 }
Christopher Faulet47365272018-10-31 17:40:50 +0100360 else {
361 cs = h1s_new_cs(h1s);
362 if (!cs)
363 goto fail;
364 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200365 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100366
367 fail:
368 pool_free(pool_head_h1s, h1s);
369 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200370}
371
372static void h1s_destroy(struct h1s *h1s)
373{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200374 if (h1s) {
375 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200376
Christopher Fauletf2824e62018-10-01 12:12:37 +0200377 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200378
Christopher Fauletf2824e62018-10-01 12:12:37 +0200379 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100380 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200381 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100382 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200383
Christopher Fauletcb55f482018-12-10 11:56:47 +0100384 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100385 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
386 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
387 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200388 pool_free(pool_head_h1s, h1s);
389 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200390}
391
Christopher Fauletfeb11742018-11-29 15:12:34 +0100392static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
393{
394 struct h1s *h1s = cs->ctx;
395
396 if (h1s && !conn_is_back(cs->conn))
397 return &h1s->csinfo;
398 return NULL;
399}
400
Christopher Faulet51dbc942018-09-13 09:05:15 +0200401/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200402 * Initialize the mux once it's attached. It is expected that conn->ctx points
403 * to the existing conn_stream (for outgoing connections or for incoming onces
404 * during a mux upgrade) or NULL (for incoming ones during the connexion
405 * establishment). <input> is always used as Input buffer and may contain
406 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
407 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200408 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200409static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
410 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200411{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200412 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100413 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200414
415 h1c = pool_alloc(pool_head_h1c);
416 if (!h1c)
417 goto fail_h1c;
418 h1c->conn = conn;
419 h1c->px = proxy;
420
421 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200422 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200423 h1c->obuf = BUF_NULL;
424 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200425 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200426
Christopher Faulet51dbc942018-09-13 09:05:15 +0200427 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200428 h1c->wait_event.tasklet = tasklet_new();
429 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200430 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200431 h1c->wait_event.tasklet->process = h1_io_cb;
432 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100433 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200434
Christopher Faulete9b70722019-04-08 10:46:02 +0200435 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100436 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
437 if (tick_isset(proxy->timeout.serverfin))
438 h1c->shut_timeout = proxy->timeout.serverfin;
439 } else {
440 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
441 if (tick_isset(proxy->timeout.clientfin))
442 h1c->shut_timeout = proxy->timeout.clientfin;
443 }
444 if (tick_isset(h1c->timeout)) {
445 t = task_new(tid_bit);
446 if (!t)
447 goto fail;
448
449 h1c->task = t;
450 t->process = h1_timeout_task;
451 t->context = h1c;
452 t->expire = tick_add(now_ms, h1c->timeout);
453 }
454
Christopher Fauletf2824e62018-10-01 12:12:37 +0200455 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100456 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200457 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200458
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100459 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200460
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100461
462 if (t)
463 task_queue(t);
464
Christopher Faulet51dbc942018-09-13 09:05:15 +0200465 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200466 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200467
468 /* mux->wake will be called soon to complete the operation */
469 return 0;
470
471 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200472 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200473 if (h1c->wait_event.tasklet)
474 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200475 pool_free(pool_head_h1c, h1c);
476 fail_h1c:
477 return -1;
478}
479
Christopher Faulet73c12072019-04-08 11:23:22 +0200480/* release function. This one should be called to free all resources allocated
481 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200482 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200483static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200484{
Christopher Faulet61840e72019-04-15 09:33:32 +0200485 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200486
Christopher Faulet51dbc942018-09-13 09:05:15 +0200487 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200488 /* The connection must be aattached to this mux to be released */
489 if (h1c->conn && h1c->conn->ctx == h1c)
490 conn = h1c->conn;
491
492 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200493 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200494 /* Make sure we're no longer subscribed to anything */
495 if (h1c->wait_event.events)
496 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
497 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200498 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200499 /* connection successfully upgraded to H2, this
500 * mux was already released */
501 return;
502 }
503 sess_log(conn->owner); /* Log if the upgrade failed */
504 }
505
Olivier Houchard45c44372019-06-11 14:06:23 +0200506
Christopher Faulet51dbc942018-09-13 09:05:15 +0200507 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
508 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
509 LIST_DEL(&h1c->buf_wait.list);
510 LIST_INIT(&h1c->buf_wait.list);
511 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
512 }
513
514 h1_release_buf(h1c, &h1c->ibuf);
515 h1_release_buf(h1c, &h1c->obuf);
516
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100517 if (h1c->task) {
518 h1c->task->context = NULL;
519 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
520 h1c->task = NULL;
521 }
522
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200523 if (h1c->wait_event.tasklet)
524 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200525
Christopher Fauletf2824e62018-10-01 12:12:37 +0200526 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200527 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100528 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200529 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200530 pool_free(pool_head_h1c, h1c);
531 }
532
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200533 if (conn) {
534 conn->mux = NULL;
535 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200536
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200537 conn_stop_tracking(conn);
538 conn_full_close(conn);
539 if (conn->destroy_cb)
540 conn->destroy_cb(conn);
541 conn_free(conn);
542 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200543}
544
545/******************************************************/
546/* functions below are for the H1 protocol processing */
547/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200548/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
549 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200550 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100551static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200552{
Christopher Faulet570d1612018-11-26 11:13:57 +0100553 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200554
Christopher Faulet570d1612018-11-26 11:13:57 +0100555 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200556 (*(p + 5) > '1' ||
557 (*(p + 5) == '1' && *(p + 7) >= '1')))
558 h1m->flags |= H1_MF_VER_11;
559}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200560
Christopher Faulet9768c262018-10-22 09:34:31 +0200561/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
562 * greater or equal to 1.1
563 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100564static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200565{
Christopher Faulet570d1612018-11-26 11:13:57 +0100566 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200567
Christopher Faulet570d1612018-11-26 11:13:57 +0100568 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200569 (*(p + 5) > '1' ||
570 (*(p + 5) == '1' && *(p + 7) >= '1')))
571 h1m->flags |= H1_MF_VER_11;
572}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200573
Christopher Fauletf2824e62018-10-01 12:12:37 +0200574/* Deduce the connection mode of the client connection, depending on the
575 * configuration and the H1 message flags. This function is called twice, the
576 * first time when the request is parsed and the second time when the response
577 * is parsed.
578 */
579static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
580{
581 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200582
583 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100584 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200585 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100586 h1s->status == 101) {
587 /* Either we've established an explicit tunnel, or we're
588 * switching the protocol. In both cases, we're very unlikely to
589 * understand the next protocols. We have to switch to tunnel
590 * mode, so that we transfer the request and responses then let
591 * this protocol pass unmodified. When we later implement
592 * specific parsers for such protocols, we'll want to check the
593 * Upgrade header which contains information about that protocol
594 * for responses with status 101 (eg: see RFC2817 about TLS).
595 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200596 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100597 }
598 else if (h1s->flags & H1S_F_WANT_KAL) {
599 /* By default the client is in KAL mode. CLOSE mode mean
600 * it is imposed by the client itself. So only change
601 * KAL mode here. */
602 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
603 /* no length known or explicit close => close */
604 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
605 }
606 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
607 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
608 /* no explict keep-alive and option httpclose => close */
609 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
610 }
611 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200612 }
613 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100614 /* Input direction: first pass */
615 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
616 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200617 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100618 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200619 }
620
621 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
622 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
623 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
624}
625
626/* Deduce the connection mode of the client connection, depending on the
627 * configuration and the H1 message flags. This function is called twice, the
628 * first time when the request is parsed and the second time when the response
629 * is parsed.
630 */
631static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
632{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100633 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100634 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100635 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200636
Christopher Fauletf2824e62018-10-01 12:12:37 +0200637 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100638 /* Input direction: second pass */
639
Christopher Fauletf2824e62018-10-01 12:12:37 +0200640 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100641 h1s->status == 101) {
642 /* Either we've established an explicit tunnel, or we're
643 * switching the protocol. In both cases, we're very unlikely to
644 * understand the next protocols. We have to switch to tunnel
645 * mode, so that we transfer the request and responses then let
646 * this protocol pass unmodified. When we later implement
647 * specific parsers for such protocols, we'll want to check the
648 * Upgrade header which contains information about that protocol
649 * for responses with status 101 (eg: see RFC2817 about TLS).
650 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200651 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100652 }
653 else if (h1s->flags & H1S_F_WANT_KAL) {
654 /* By default the server is in KAL mode. CLOSE mode mean
655 * it is imposed by haproxy itself. So only change KAL
656 * mode here. */
657 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
658 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
659 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
660 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
661 }
662 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200663 }
Christopher Faulet70033782018-12-05 13:50:11 +0100664 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100665 /* Output direction: first pass */
666 if (h1m->flags & H1_MF_CONN_CLO) {
667 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100668 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100669 }
670 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
671 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
672 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
673 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
674 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
675 /* no explicit keep-alive option httpclose/server-close => close */
676 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
677 }
Christopher Faulet70033782018-12-05 13:50:11 +0100678 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200679
680 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
681 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
682 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200683}
684
Christopher Fauletb992af02019-03-28 15:42:24 +0100685static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200686{
687 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200688
689 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
690 * token is found
691 */
692 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200693 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200694
695 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100696 if (!(h1m->flags & H1_MF_VER_11))
697 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200698 }
699 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100700 if (h1m->flags & H1_MF_VER_11)
701 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200702 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200703}
704
Christopher Fauletb992af02019-03-28 15:42:24 +0100705static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200706{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200707 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
708 * token is found
709 */
710 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200711 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200712
713 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100714 if (!(h1m->flags & H1_MF_VER_11) ||
715 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
716 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200717 }
718 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100719 if (h1m->flags & H1_MF_VER_11)
720 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200721 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200722}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200723
Christopher Fauletb992af02019-03-28 15:42:24 +0100724static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200725{
Christopher Fauletb992af02019-03-28 15:42:24 +0100726 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200727 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100728 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200729 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200730}
731
Christopher Fauletb992af02019-03-28 15:42:24 +0100732static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
733{
734 if (!conn_is_back(h1s->h1c->conn))
735 h1_set_cli_conn_mode(h1s, h1m);
736 else
737 h1_set_srv_conn_mode(h1s, h1m);
738
739 if (!(h1m->flags & H1_MF_RESP))
740 h1_update_req_conn_value(h1s, h1m, conn_val);
741 else
742 h1_update_res_conn_value(h1s, h1m, conn_val);
743}
Christopher Faulete44769b2018-11-29 23:01:45 +0100744
Christopher Faulet98fbe952019-07-22 16:18:24 +0200745/* Try to adjust the case of the message header name using the global map
746 * <hdrs_map>.
747 */
748static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
749{
750 struct ebpt_node *node;
751 struct h1_hdr_entry *entry;
752
753 /* No entry in the map, do nothing */
754 if (eb_is_empty(&hdrs_map.map))
755 return;
756
757 /* No conversion fo the request headers */
758 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
759 return;
760
761 /* No conversion fo the response headers */
762 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
763 return;
764
765 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
766 if (!node)
767 return;
768 entry = container_of(node, struct h1_hdr_entry, node);
769 name->ptr = entry->name.ptr;
770 name->len = entry->name.len;
771}
772
Christopher Faulete44769b2018-11-29 23:01:45 +0100773/* Append the description of what is present in error snapshot <es> into <out>.
774 * The description must be small enough to always fit in a buffer. The output
775 * buffer may be the trash so the trash must not be used inside this function.
776 */
777static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
778{
779 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100780 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
781 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
782 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
783 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
784 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
785 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100786}
787/*
788 * Capture a bad request or response and archive it in the proxy's structure.
789 * By default it tries to report the error position as h1m->err_pos. However if
790 * this one is not set, it will then report h1m->next, which is the last known
791 * parsing point. The function is able to deal with wrapping buffers. It always
792 * displays buffers as a contiguous area starting at buf->p. The direction is
793 * determined thanks to the h1m's flags.
794 */
795static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
796 struct h1m *h1m, struct buffer *buf)
797{
798 struct session *sess = h1c->conn->owner;
799 struct proxy *proxy = h1c->px;
800 struct proxy *other_end = sess->fe;
801 union error_snapshot_ctx ctx;
802
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100803 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100804 other_end = si_strm(h1s->cs->data)->be;
805
806 /* http-specific part now */
807 ctx.h1.state = h1m->state;
808 ctx.h1.c_flags = h1c->flags;
809 ctx.h1.s_flags = h1s->flags;
810 ctx.h1.m_flags = h1m->flags;
811 ctx.h1.m_clen = h1m->curr_len;
812 ctx.h1.m_blen = h1m->body_len;
813
814 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
815 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100816 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
817 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100818}
819
Christopher Fauletadb22202018-12-12 10:32:09 +0100820/* Emit the chunksize followed by a CRLF in front of data of the buffer
821 * <buf>. It goes backwards and starts with the byte before the buffer's
822 * head. The caller is responsible for ensuring there is enough room left before
823 * the buffer's head for the string.
824 */
825static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
826{
827 char *beg, *end;
828
829 beg = end = b_head(buf);
830 *--beg = '\n';
831 *--beg = '\r';
832 do {
833 *--beg = hextab[chksz & 0xF];
834 } while (chksz >>= 4);
835 buf->head -= (end - beg);
836 b_add(buf, end - beg);
837}
838
839/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
840 * ensuring there is enough room left in the buffer for the string. */
841static void h1_emit_chunk_crlf(struct buffer *buf)
842{
843 *(b_peek(buf, b_data(buf))) = '\r';
844 *(b_peek(buf, b_data(buf) + 1)) = '\n';
845 b_add(buf, 2);
846}
847
Christopher Faulet129817b2018-09-20 16:14:40 +0200848/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100849 * Switch the request to tunnel mode. This function must only be called for
850 * CONNECT requests. On the client side, the mux is mark as busy on input,
851 * waiting the response.
852 */
853static void h1_set_req_tunnel_mode(struct h1s *h1s)
854{
855 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
856 h1s->req.state = H1_MSG_TUNNEL;
857 if (!conn_is_back(h1s->h1c->conn))
858 h1s->h1c->flags |= H1C_F_IN_BUSY;
859}
860
861/*
862 * Switch the response to tunnel mode. This function must only be called on
863 * successfull replies to CONNECT requests or on protocol switching. On the
864 * server side, if the request is not finished, the mux is mark as busy on
865 * input. Otherwise the request is also switch to tunnel mode.
866 */
867static void h1_set_res_tunnel_mode(struct h1s *h1s)
868{
869 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
870 h1s->res.state = H1_MSG_TUNNEL;
871 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
872 h1s->h1c->flags |= H1C_F_IN_BUSY;
873 else {
874 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
875 h1s->req.state = H1_MSG_TUNNEL;
876 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
877 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200878 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100879 }
880 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200881}
882
883/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200884 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200885 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200886 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +0200887 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200888static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200889 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200890{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100891 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +0200892 int ret = 0;
893
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200894 if (!(h1m->flags & H1_MF_RESP)) {
895 /* Try to match H2 preface before parsing the request headers. */
896 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
897 if (ret > 0)
898 goto h2c_upgrade;
899 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200900 else {
901 if (h1s->meth == HTTP_METH_CONNECT)
902 h1m->flags |= H1_MF_METH_CONNECT;
903 if (h1s->meth == HTTP_METH_HEAD)
904 h1m->flags |= H1_MF_METH_HEAD;
905 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200906
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200907 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
908 if (!ret) {
909 if (htx->flags & HTX_FL_PARSING_ERROR) {
910 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
911 h1s->cs->flags |= CS_FL_EOI;
912 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
913 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200914 goto end;
915 }
916
Christopher Faulet129817b2018-09-20 16:14:40 +0200917 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100918 h1s->meth = h1sl.rq.meth;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200919 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100920 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +0200921 }
922 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100923 h1s->status = h1sl.st.status;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200924 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100925 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100926 }
Christopher Fauletb992af02019-03-28 15:42:24 +0100927 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +0200928 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200929
Christopher Faulet129817b2018-09-20 16:14:40 +0200930 end:
931 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200932
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200933 h2c_upgrade:
934 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +0200935 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200936 htx->flags |= HTX_FL_UPGRADE;
937 ret = 0;
938 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200939}
940
941/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200942 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200943 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
944 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +0200945 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200946static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100947 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +0200948 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +0200949{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200950 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200951
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200952
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200953 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
954 if (ret <= 0) {
955 if (ret < 0) {
956 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
957 h1s->cs->flags |= CS_FL_EOI;
958 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200959 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200960 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +0200961 }
962
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200963 if (h1m->state == H1_MSG_DONE)
Christopher Fauletb75b5ea2019-05-17 08:37:28 +0200964 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200965 *ofs += ret;
966 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200967}
968
969/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200970 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
971 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
972 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
973 * responsible to update the parser state <h1m>.
974 */
975static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
976 struct buffer *buf, size_t *ofs, size_t max)
977{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200978 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200979
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200980 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200981 if (ret <= 0) {
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200982 if (ret < 0) {
983 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
984 h1s->cs->flags |= CS_FL_EOI;
985 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
986 }
987 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200988 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200989 *ofs += ret;
990 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200991 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200992}
993
994/*
995 * Add the EOM in the HTX message and switch the message to the DONE state. It
996 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
997 * functions is responsible to update the parser state <h1m>. It also add the
998 * flag CS_FL_EOI on the CS.
999 */
1000static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
1001{
1002 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
1003 h1s->flags |= H1S_F_APPEND_EOM;
1004 return 0;
1005 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001006
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001007 h1s->flags &= ~H1S_F_APPEND_EOM;
1008 h1m->state = H1_MSG_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001009 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001010 return (sizeof(struct htx_blk) + 1);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001011}
1012
1013/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001014 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001015 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1016 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001017 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001018static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001019{
Christopher Faulet539e0292018-11-19 10:40:09 +01001020 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001021 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001022 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001023 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001024 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001025 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001026
Christopher Faulet539e0292018-11-19 10:40:09 +01001027 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001028
Christopher Fauletf2824e62018-10-01 12:12:37 +02001029 if (!conn_is_back(h1c->conn)) {
1030 h1m = &h1s->req;
1031 errflag = H1S_F_REQ_ERROR;
1032 }
1033 else {
1034 h1m = &h1s->res;
1035 errflag = H1S_F_RES_ERROR;
1036 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001037
Christopher Faulet74027762019-02-26 14:45:05 +01001038 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001039 if (h1s->flags & errflag)
1040 goto end;
1041
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001042 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001043 size_t used = htx_used_space(htx);
1044
Christopher Faulet129817b2018-09-20 16:14:40 +02001045 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001046 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001047 if (!ret)
1048 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001049 if ((h1m->flags & H1_MF_RESP) &&
1050 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1051 h1m_init_res(&h1s->res);
1052 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1053 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001054 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001055 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001056 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001057 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001058 if (!ret)
1059 break;
1060 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001061 else if (h1m->state == H1_MSG_TRAILERS) {
1062 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1063 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1064 if (!ret)
1065 break;
1066 }
Christopher Fauletf1ef7f62019-09-03 21:55:14 +02001067 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001068 break;
1069 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001070 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001071 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1072 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001073 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001074 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001075 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001076 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001077 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001078 if (!ret)
1079 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001080 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001081 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001082 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001083 break;
1084 }
1085
Christopher Faulet30db3d72019-05-17 15:35:33 +02001086 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001087 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001088
Christopher Faulet47365272018-10-31 17:40:50 +01001089 if (h1s->flags & errflag)
1090 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001091
Christopher Faulet539e0292018-11-19 10:40:09 +01001092 b_del(&h1c->ibuf, total);
1093
1094 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001095 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001096 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001097 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001098 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001099 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001100 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001101
Christopher Fauletcf56b992018-12-11 16:12:31 +01001102 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1103
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001104 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001105 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001106 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001107 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001108
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +02001109 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1110 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001111 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001112 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001113 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001114 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001115
Christopher Faulet30db3d72019-05-17 15:35:33 +02001116 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001117
1118 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001119 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001120 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001121 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001122}
1123
Christopher Faulet129817b2018-09-20 16:14:40 +02001124/*
1125 * Process outgoing data. It parses data and transfer them from the channel buffer into
1126 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1127 * 0 if it couldn't proceed.
1128 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001129static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1130{
Christopher Faulet129817b2018-09-20 16:14:40 +02001131 struct h1s *h1s = h1c->h1s;
1132 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001133 struct htx *chn_htx;
1134 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001135 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001136 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001137 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001138
Christopher Faulet47365272018-10-31 17:40:50 +01001139 if (!count)
1140 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001141
Christopher Faulet94b2c762019-05-24 15:28:57 +02001142 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001143 if (htx_is_empty(chn_htx))
1144 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001145
Christopher Faulet51dbc942018-09-13 09:05:15 +02001146 if (!h1_get_buf(h1c, &h1c->obuf)) {
1147 h1c->flags |= H1C_F_OUT_ALLOC;
1148 goto end;
1149 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001150
Christopher Fauletf2824e62018-10-01 12:12:37 +02001151 if (!conn_is_back(h1c->conn)) {
1152 h1m = &h1s->res;
1153 errflag = H1S_F_RES_ERROR;
1154 }
1155 else {
1156 h1m = &h1s->req;
1157 errflag = H1S_F_REQ_ERROR;
1158 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001159
Christopher Faulet0e54d542019-07-04 21:22:34 +02001160 if (h1s->flags & errflag)
1161 goto end;
1162
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001163 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001164 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001165
Willy Tarreau3815b222018-12-11 19:50:43 +01001166 /* Perform some optimizations to reduce the number of buffer copies.
1167 * First, if the mux's buffer is empty and the htx area contains
1168 * exactly one data block of the same size as the requested count,
1169 * then it's possible to simply swap the caller's buffer with the
1170 * mux's output buffer and adjust offsets and length to match the
1171 * entire DATA HTX block in the middle. In this case we perform a
1172 * true zero-copy operation from end-to-end. This is the situation
1173 * that happens all the time with large files. Second, if this is not
1174 * possible, but the mux's output buffer is empty, we still have an
1175 * opportunity to avoid the copy to the intermediary buffer, by making
1176 * the intermediary buffer's area point to the output buffer's area.
1177 * In this case we want to skip the HTX header to make sure that copies
1178 * remain aligned and that this operation remains possible all the
1179 * time. This goes for headers, data blocks and any data extracted from
1180 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001181 */
1182 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001183 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001184 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001185 htx_get_blk_value(chn_htx, blk).len == count) {
1186 void *old_area = h1c->obuf.area;
1187
1188 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001189 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001190 h1c->obuf.data = count;
1191
1192 buf->area = old_area;
1193 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001194
1195 /* The message is chunked. We need to emit the chunk
1196 * size. We have at least the size of the struct htx to
1197 * write the chunk envelope. It should be enough.
1198 */
1199 if (h1m->flags & H1_MF_CHNK) {
1200 h1_emit_chunk_size(&h1c->obuf, count);
1201 h1_emit_chunk_crlf(&h1c->obuf);
1202 }
1203
Willy Tarreau3815b222018-12-11 19:50:43 +01001204 total += count;
1205 goto out;
1206 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001207 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001208 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001209 else
1210 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001211
Christopher Fauletc2518a52019-06-25 21:41:02 +02001212 tmp.data = 0;
1213 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001214 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001215 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001216 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001217 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001218 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001219 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001220
Christopher Fauletb2e84162018-12-06 11:39:49 +01001221 vlen = sz;
1222 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001223 if (type != HTX_BLK_DATA)
Christopher Fauletb2e84162018-12-06 11:39:49 +01001224 goto copy;
1225 vlen = count;
1226 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001227
Christopher Faulet94b2c762019-05-24 15:28:57 +02001228 if (type == HTX_BLK_UNUSED)
1229 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001230
Christopher Faulet94b2c762019-05-24 15:28:57 +02001231 switch (h1m->state) {
1232 case H1_MSG_RQBEFORE:
1233 if (type != HTX_BLK_REQ_SL)
1234 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001235 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001236 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001237 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001238 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001239 goto copy;
1240 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001241 if (sl->flags & HTX_SL_F_BODYLESS)
1242 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001243 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001244 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001245
Christopher Faulet94b2c762019-05-24 15:28:57 +02001246 case H1_MSG_RPBEFORE:
1247 if (type != HTX_BLK_RES_SL)
1248 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001249 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001250 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001251 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001252 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001253 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001254 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001255 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001256 if (sl->info.res.status < 200 &&
1257 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001258 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001259 h1m->state = H1_MSG_HDR_FIRST;
1260 break;
1261
Christopher Faulet94b2c762019-05-24 15:28:57 +02001262 case H1_MSG_HDR_FIRST:
1263 case H1_MSG_HDR_NAME:
1264 case H1_MSG_HDR_L2_LWS:
1265 if (type == HTX_BLK_EOH)
1266 goto last_lf;
1267 if (type != HTX_BLK_HDR)
1268 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001269 h1m->state = H1_MSG_HDR_NAME;
1270 n = htx_get_blk_name(chn_htx, blk);
1271 v = htx_get_blk_value(chn_htx, blk);
1272
1273 if (isteqi(n, ist("transfer-encoding")))
1274 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001275 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001276 /* Only skip C-L header with invalid value. */
1277 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001278 goto skip_hdr;
1279 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001280 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001281 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001282 if (!v.len)
1283 goto skip_hdr;
1284 }
1285
Christopher Faulet98fbe952019-07-22 16:18:24 +02001286 /* Try to adjust the case of the header name */
1287 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1288 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001289 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001290 goto copy;
1291 skip_hdr:
1292 h1m->state = H1_MSG_HDR_L2_LWS;
1293 break;
1294
Christopher Faulet94b2c762019-05-24 15:28:57 +02001295 case H1_MSG_LAST_LF:
1296 if (type != HTX_BLK_EOH)
1297 goto error;
1298 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001299 h1m->state = H1_MSG_LAST_LF;
1300 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001301 /* There is no "Connection:" header and
1302 * it the conn_mode must be
1303 * processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001304 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001305 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001306 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001307 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001308 /* Try to adjust the case of the header name */
1309 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1310 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001311 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001312 goto copy;
1313 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001314 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001315 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001316
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001317 if ((h1s->meth != HTTP_METH_CONNECT &&
1318 (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 +01001319 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1320 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1321 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1322 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1323 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001324 /* chunking needed but header not seen */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001325 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Willy Tarreau4710d202019-01-03 17:39:54 +01001326 goto copy;
1327 h1m->flags |= H1_MF_CHNK;
1328 }
1329
Christopher Fauletc2518a52019-06-25 21:41:02 +02001330 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet9768c262018-10-22 09:34:31 +02001331 goto copy;
1332
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001333 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1334 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1335 h1_set_req_tunnel_mode(h1s);
1336 }
1337 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1338 /* a successfull reply to a CONNECT or a protocol switching is sent
1339 * to the client . Switch the response to tunnel mode. */
1340 h1_set_res_tunnel_mode(h1s);
1341 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001342 else if ((h1m->flags & H1_MF_RESP) &&
1343 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1344 h1m_init_res(&h1s->res);
1345 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001346 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001347 }
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001348 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1349 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001350 else
1351 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001352 break;
1353
Christopher Faulet94b2c762019-05-24 15:28:57 +02001354 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001355 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001356 if (type == HTX_BLK_EOM) {
1357 /* Chunked message without explicit trailers */
1358 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001359 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001360 goto copy;
1361 }
1362 goto done;
1363 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001364 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001365 /* If the message is not chunked, never
1366 * add the last chunk. */
1367 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet94b2c762019-05-24 15:28:57 +02001368 goto copy;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001369 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001370 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001371 else if (type != HTX_BLK_DATA)
1372 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001373 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001374 v.len = vlen;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001375 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001376 goto copy;
1377 break;
1378
Christopher Faulet94b2c762019-05-24 15:28:57 +02001379 case H1_MSG_TRAILERS:
1380 if (type == HTX_BLK_EOM)
1381 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001382 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001383 goto error;
1384 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001385 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001386 /* If the message is not chunked, ignore
1387 * trailers. It may happen with H2 messages. */
1388 if (!(h1m->flags & H1_MF_CHNK))
1389 break;
1390
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001391 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001392 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet32188212018-11-20 18:21:43 +01001393 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001394 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001395 else { // HTX_BLK_TLR
1396 n = htx_get_blk_name(chn_htx, blk);
1397 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001398
1399 /* Try to adjust the case of the header name */
1400 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1401 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001402 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001403 goto copy;
1404 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001405 break;
1406
Christopher Faulet94b2c762019-05-24 15:28:57 +02001407 case H1_MSG_DONE:
1408 if (type != HTX_BLK_EOM)
1409 goto error;
1410 done:
1411 h1m->state = H1_MSG_DONE;
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001412 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1413 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1414 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1415 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001416 break;
1417
Christopher Faulet9768c262018-10-22 09:34:31 +02001418 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001419 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001420 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001421 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001422 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001423 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001424 break;
1425 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001426
1427 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001428 total += vlen;
1429 count -= vlen;
1430 if (sz == vlen)
1431 blk = htx_remove_blk(chn_htx, blk);
1432 else {
1433 htx_cut_data_blk(chn_htx, blk, vlen);
1434 break;
1435 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001436 }
1437
Christopher Faulet9768c262018-10-22 09:34:31 +02001438 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001439 /* when the output buffer is empty, tmp shares the same area so that we
1440 * only have to update pointers and lengths.
1441 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001442 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1443 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001444 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001445 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001446
Willy Tarreau3815b222018-12-11 19:50:43 +01001447 htx_to_buf(chn_htx, buf);
1448 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001449 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001450 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001451 end:
1452 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001453}
1454
Christopher Faulet51dbc942018-09-13 09:05:15 +02001455/*********************************************************/
1456/* functions below are I/O callbacks from the connection */
1457/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001458static void h1_wake_stream_for_recv(struct h1s *h1s)
1459{
1460 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001461 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001462 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001463 h1s->recv_wait = NULL;
1464 }
1465}
1466static void h1_wake_stream_for_send(struct h1s *h1s)
1467{
1468 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001469 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001470 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001471 h1s->send_wait = NULL;
1472 }
1473}
1474
Christopher Faulet51dbc942018-09-13 09:05:15 +02001475/*
1476 * Attempt to read data, and subscribe if none available
1477 */
1478static int h1_recv(struct h1c *h1c)
1479{
1480 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001481 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001482 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001483 int rcvd = 0;
1484
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001485 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001486 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001487
Olivier Houchard75159a92018-12-03 18:46:09 +01001488 if (!h1_recv_allowed(h1c)) {
1489 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001490 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001491 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001492
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001493 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1494 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001495 goto end;
1496 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001497
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001498 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001499 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001500 h1_wake_stream_for_recv(h1s);
1501 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001502 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001503 }
1504
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001505 /*
1506 * If we only have a small amount of data, realign it,
1507 * it's probably cheaper than doing 2 recv() calls.
1508 */
1509 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1510 b_slow_realign(&h1c->ibuf, trash.area, 0);
1511
Willy Tarreau45f2b892018-12-05 07:59:27 +01001512 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001513 if (max) {
1514 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001515
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001516 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001517 if (!b_data(&h1c->ibuf)) {
1518 /* try to pre-align the buffer like the rxbufs will be
1519 * to optimize memory copies.
1520 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001521 h1c->ibuf.head = sizeof(struct htx);
1522 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001523 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001524 }
Christopher Faulet47365272018-10-31 17:40:50 +01001525 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001526 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001527 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001528 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001529 if (h1s->csinfo.t_idle == -1)
1530 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1531 }
Christopher Faulet47365272018-10-31 17:40:50 +01001532 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001533
Olivier Houchardcc3fec82019-07-26 15:11:11 +02001534 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001535 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001536 goto end;
1537 }
1538
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001539 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001540
Christopher Faulet9768c262018-10-22 09:34:31 +02001541 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001542 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1543 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001544
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001545 if (conn_xprt_read0_pending(conn) && h1s) {
1546 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001547 rcvd = 1;
1548 }
1549
Christopher Faulet51dbc942018-09-13 09:05:15 +02001550 if (!b_data(&h1c->ibuf))
1551 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001552 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001553 h1c->flags |= H1C_F_IN_FULL;
1554 return rcvd;
1555}
1556
1557
1558/*
1559 * Try to send data if possible
1560 */
1561static int h1_send(struct h1c *h1c)
1562{
1563 struct connection *conn = h1c->conn;
1564 unsigned int flags = 0;
1565 size_t ret;
1566 int sent = 0;
1567
1568 if (conn->flags & CO_FL_ERROR)
1569 return 0;
1570
1571 if (!b_data(&h1c->obuf))
1572 goto end;
1573
1574 if (h1c->flags & H1C_F_OUT_FULL)
1575 flags |= CO_SFL_MSG_MORE;
1576
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001577 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001578 if (ret > 0) {
1579 h1c->flags &= ~H1C_F_OUT_FULL;
1580 b_del(&h1c->obuf, ret);
1581 sent = 1;
1582 }
1583
Christopher Faulet145aa472018-12-06 10:56:20 +01001584 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1585 /* error or output closed, nothing to send, clear the buffer to release it */
1586 b_reset(&h1c->obuf);
1587 }
1588
Christopher Faulet51dbc942018-09-13 09:05:15 +02001589 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001590 if (!(h1c->flags & H1C_F_OUT_FULL))
1591 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001592
Christopher Faulet51dbc942018-09-13 09:05:15 +02001593 /* We're done, no more to send */
1594 if (!b_data(&h1c->obuf)) {
1595 h1_release_buf(h1c, &h1c->obuf);
1596 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001597 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001598 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001599 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001600 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001601
1602 return sent;
1603}
1604
Christopher Faulet51dbc942018-09-13 09:05:15 +02001605
1606/* callback called on any event by the connection handler.
1607 * It applies changes and returns zero, or < 0 if it wants immediate
1608 * destruction of the connection.
1609 */
1610static int h1_process(struct h1c * h1c)
1611{
1612 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001613 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001614
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001615 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001616 return -1;
1617
Christopher Fauletfeb11742018-11-29 15:12:34 +01001618 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02001619 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001620 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001621 conn_xprt_read0_pending(conn))
1622 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001623 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001624 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001625 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001626 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001627 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001628 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001629 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001630 }
1631
Christopher Fauletfeb11742018-11-29 15:12:34 +01001632 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1633 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1634
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001635 if (conn_xprt_read0_pending(conn))
1636 h1s->flags |= H1S_F_REOS;
1637
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001638 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001639 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001640 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001641 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001642 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01001643 h1s->cs->data_cb->wake(h1s->cs);
1644 }
Christopher Faulet47365272018-10-31 17:40:50 +01001645 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001646 if (h1c->task) {
1647 h1c->task->expire = TICK_ETERNITY;
1648 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001649 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 +01001650 ? h1c->shut_timeout
1651 : h1c->timeout));
1652 task_queue(h1c->task);
1653 }
1654 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001655 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001656
1657 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02001658 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01001659 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001660}
1661
1662static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1663{
1664 struct h1c *h1c = ctx;
1665 int ret = 0;
1666
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001667 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001668 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001669 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001670 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001671 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001672 h1_process(h1c);
1673 return NULL;
1674}
1675
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001676static void h1_reset(struct connection *conn)
1677{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001678
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001679}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001680
1681static int h1_wake(struct connection *conn)
1682{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001683 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001684 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001685
Christopher Faulet539e0292018-11-19 10:40:09 +01001686 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001687 ret = h1_process(h1c);
1688 if (ret == 0) {
1689 struct h1s *h1s = h1c->h1s;
1690
1691 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1692 ret = h1s->cs->data_cb->wake(h1s->cs);
1693 }
1694 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001695}
1696
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001697/* Connection timeout management. The principle is that if there's no receipt
1698 * nor sending for a certain amount of time, the connection is closed.
1699 */
1700static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
1701{
1702 struct h1c *h1c = context;
1703 int expired = tick_is_expired(t->expire, now_ms);
1704
1705 if (!expired && h1c)
1706 return t;
1707
Olivier Houchard3f795f72019-04-17 22:51:06 +02001708 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001709
1710 if (!h1c) {
1711 /* resources were already deleted */
1712 return NULL;
1713 }
1714
1715 h1c->task = NULL;
1716 /* If a stream is still attached to the mux, just set an error and wait
1717 * for the stream's timeout. Otherwise, release the mux. This is only ok
1718 * because same timeouts are used.
1719 */
1720 if (h1c->h1s && h1c->h1s->cs)
1721 h1c->flags |= H1C_F_CS_ERROR;
1722 else
Christopher Faulet73c12072019-04-08 11:23:22 +02001723 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001724 return NULL;
1725}
1726
Christopher Faulet51dbc942018-09-13 09:05:15 +02001727/*******************************************/
1728/* functions below are used by the streams */
1729/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02001730
Christopher Faulet51dbc942018-09-13 09:05:15 +02001731/*
1732 * Attach a new stream to a connection
1733 * (Used for outgoing connections)
1734 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001735static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001736{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001737 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001738 struct conn_stream *cs = NULL;
1739 struct h1s *h1s;
1740
1741 if (h1c->flags & H1C_F_CS_ERROR)
1742 goto end;
1743
1744 cs = cs_new(h1c->conn);
1745 if (!cs)
1746 goto end;
1747
Olivier Houchardf502aca2018-12-14 19:42:40 +01001748 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001749 if (h1s == NULL)
1750 goto end;
1751
1752 return cs;
1753 end:
1754 cs_free(cs);
1755 return NULL;
1756}
1757
1758/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1759 * this mux, it's easy as we can only store a single conn_stream.
1760 */
1761static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1762{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001763 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001764 struct h1s *h1s = h1c->h1s;
1765
1766 if (h1s)
1767 return h1s->cs;
1768
1769 return NULL;
1770}
1771
Christopher Faulet73c12072019-04-08 11:23:22 +02001772static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001773{
Christopher Faulet73c12072019-04-08 11:23:22 +02001774 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001775
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001776 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02001777 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001778}
1779
1780/*
1781 * Detach the stream from the connection and possibly release the connection.
1782 */
1783static void h1_detach(struct conn_stream *cs)
1784{
1785 struct h1s *h1s = cs->ctx;
1786 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001787 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01001788 int has_keepalive;
1789 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001790
1791 cs->ctx = NULL;
1792 if (!h1s)
1793 return;
1794
Olivier Houchardf502aca2018-12-14 19:42:40 +01001795 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001796 h1c = h1s->h1c;
1797 h1s->cs = NULL;
1798
Olivier Houchard8a786902018-12-15 16:05:40 +01001799 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
1800 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
1801 h1s_destroy(h1s);
1802
1803 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01001804 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02001805 /* If there are any excess server data in the input buffer,
1806 * release it and close the connection ASAP (some data may
1807 * remain in the output buffer). This happens if a server sends
1808 * invalid responses. So in such case, we don't want to reuse
1809 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02001810 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02001811 if (b_data(&h1c->ibuf)) {
1812 h1_release_buf(h1c, &h1c->ibuf);
1813 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1814 goto release;
1815 }
Christopher Faulet03627242019-07-19 11:34:08 +02001816
Christopher Faulet9400a392018-11-23 23:10:39 +01001817 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01001818 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01001819 h1c->conn->flags |= CO_FL_PRIVATE;
1820
Olivier Houchard44d59142018-12-13 18:46:22 +01001821 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001822 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01001823 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
1824 h1c->conn->owner = NULL;
1825 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
1826 /* The server doesn't want it, let's kill the connection right away */
1827 h1c->conn->mux->destroy(h1c->conn);
1828 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001829 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01001830 return;
1831
1832 }
Olivier Houchard44d59142018-12-13 18:46:22 +01001833 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01001834 if (h1c->conn->owner == sess) {
1835 int ret = session_check_idle_conn(sess, h1c->conn);
1836 if (ret == -1)
1837 /* The connection got destroyed, let's leave */
1838 return;
1839 else if (ret == 1) {
1840 /* The connection was added to the server list,
1841 * wake the task so we can subscribe to events
1842 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001843 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01001844 return;
1845 }
1846 }
Christopher Faulet9400a392018-11-23 23:10:39 +01001847 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01001848 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001849 struct server *srv = objt_server(h1c->conn->target);
1850
1851 if (srv) {
1852 if (h1c->conn->flags & CO_FL_PRIVATE)
1853 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01001854 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01001855 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1856 else
1857 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1858 }
1859 }
1860 }
1861
Christopher Fauletf1204b82019-07-19 14:51:06 +02001862 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02001863 /* 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 +02001864 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
1865 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
1866 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
1867 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02001868 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001869 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001870 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001871 if (h1c->task) {
1872 h1c->task->expire = TICK_ETERNITY;
1873 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001874 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 +01001875 ? h1c->shut_timeout
1876 : h1c->timeout));
1877 task_queue(h1c->task);
1878 }
1879 }
1880 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001881}
1882
1883
1884static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1885{
1886 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02001887 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001888
1889 if (!h1s)
1890 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02001891 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001892
Christopher Faulet7f366362019-04-08 10:51:20 +02001893 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
1894 goto do_shutr;
1895
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001896 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001897 return;
1898
Christopher Faulet7f366362019-04-08 10:51:20 +02001899 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001900 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1901 if (cs->flags & CS_FL_SHR)
1902 return;
1903 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001904 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
1905 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01001906 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 +02001907 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001908}
1909
1910static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1911{
1912 struct h1s *h1s = cs->ctx;
1913 struct h1c *h1c;
1914
1915 if (!h1s)
1916 return;
1917 h1c = h1s->h1c;
1918
Christopher Faulet7f366362019-04-08 10:51:20 +02001919 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
1920 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01001921
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001922 if ((h1c->flags & H1C_F_UPG_H2C) ||
1923 ((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 +02001924 return;
1925
Christopher Faulet7f366362019-04-08 10:51:20 +02001926 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001927 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1928 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1929 return;
1930
Christopher Faulet666a0c42019-01-08 11:12:04 +01001931 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001932}
1933
Christopher Faulet666a0c42019-01-08 11:12:04 +01001934static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001935{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001936 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001937
Christopher Faulet666a0c42019-01-08 11:12:04 +01001938 conn_xprt_shutw(conn);
1939 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
1940 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
1941 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001942}
1943
1944/* Called from the upper layer, to unsubscribe to events */
1945static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1946{
1947 struct wait_event *sw;
1948 struct h1s *h1s = cs->ctx;
1949
1950 if (!h1s)
1951 return 0;
1952
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001953 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001954 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02001955 BUG_ON(h1s->recv_wait != sw);
1956 sw->events &= ~SUB_RETRY_RECV;
1957 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001958 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001959 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001960 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02001961 BUG_ON(h1s->send_wait != sw);
1962 sw->events &= ~SUB_RETRY_SEND;
1963 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001964 }
1965 return 0;
1966}
1967
1968/* Called from the upper layer, to subscribe to events, such as being able to send */
1969static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1970{
1971 struct wait_event *sw;
1972 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02001973 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001974
1975 if (!h1s)
1976 return -1;
1977
1978 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001979 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001980 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02001981 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
1982 sw->events |= SUB_RETRY_RECV;
1983 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001984 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001985 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02001987 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
1988 sw->events |= SUB_RETRY_SEND;
1989 h1s->send_wait = sw;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02001990 /*
1991 * If the conn_stream attempt to subscribe, and the
1992 * mux isn't subscribed to the connection, then it
1993 * probably means the connection wasn't established
1994 * yet, so we have to subscribe.
1995 */
Christopher Faulet51bb1852019-09-04 10:22:34 +02001996 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02001997 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1998 h1c->conn->xprt->subscribe(h1c->conn,
1999 h1c->conn->xprt_ctx,
2000 SUB_RETRY_SEND,
2001 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002002 return 0;
2003 default:
2004 break;
2005 }
2006 return -1;
2007}
2008
2009/* Called from the upper layer, to receive data */
2010static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2011{
2012 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002013 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002014 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002015 size_t ret = 0;
2016
Christopher Faulet539e0292018-11-19 10:40:09 +01002017 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002018 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002019
Christopher Faulete18777b2019-04-16 16:46:36 +02002020 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002021
2022 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2023 h1s->flags |= H1S_F_BUF_FLUSH;
2024 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002025 else {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002026 h1s->flags &= ~H1S_F_SPLICED_DATA;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002027 if (h1m->state != H1_MSG_DONE &&
2028 !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002029 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002030 }
2031 return ret;
2032}
2033
2034
2035/* Called from the upper layer, to send data */
2036static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2037{
2038 struct h1s *h1s = cs->ctx;
2039 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002040 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002041
2042 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002043 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002044
2045 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002046
2047 /* If we're not connected yet, or we're waiting for a handshake, stop
2048 * now, as we don't want to remove everything from the channel buffer
2049 * before we're sure we can send it.
2050 */
2051 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
2052 (h1c->conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002053 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002054
Christopher Fauletc31872f2019-06-04 22:09:36 +02002055 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002056 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002057
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002058 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2059 ret = h1_process_output(h1c, buf, count);
2060 if (!ret)
2061 break;
2062 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002063 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002064 if (!h1_send(h1c))
2065 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002066 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002067
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002068 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002069}
2070
Willy Tarreaue5733232019-05-22 19:24:06 +02002071#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002072/* Send and get, using splicing */
2073static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2074{
2075 struct h1s *h1s = cs->ctx;
2076 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2077 int ret = 0;
2078
Christopher Faulet1146f972019-05-29 14:35:24 +02002079 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002080 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002081 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2082 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002083 goto end;
2084 }
2085
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002086 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002087 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002088 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002089 }
2090
2091 h1s->flags &= ~H1S_F_BUF_FLUSH;
2092 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002093 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2094 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002095 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002096 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002097 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002098 if (!h1m->curr_len)
2099 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2100 }
2101
Christopher Faulet1be55f92018-10-02 15:59:23 +02002102 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002103 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002104 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002105 if (!pipe->data)
2106 cs->flags |= CS_FL_EOS;
2107 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002108 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002109}
2110
2111static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2112{
2113 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002114 int ret = 0;
2115
2116 if (b_data(&h1s->h1c->obuf))
2117 goto end;
2118
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002119 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002120 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002121 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002122 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002123 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002124 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002125 return ret;
2126}
2127#endif
2128
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002129/* for debugging with CLI's "show fd" command */
2130static void h1_show_fd(struct buffer *msg, struct connection *conn)
2131{
2132 struct h1c *h1c = conn->ctx;
2133 struct h1s *h1s = h1c->h1s;
2134
Christopher Fauletf376a312019-01-04 15:16:06 +01002135 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2136 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002137 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2138 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2139 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2140 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2141
2142 if (h1s) {
2143 char *method;
2144
2145 if (h1s->meth < HTTP_METH_OTHER)
2146 method = http_known_methods[h1s->meth].ptr;
2147 else
2148 method = "UNKNOWN";
2149 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2150 " .meth=%s status=%d",
2151 h1s, h1s->flags,
2152 h1m_state_str(h1s->req.state),
2153 h1m_state_str(h1s->res.state), method, h1s->status);
2154 if (h1s->cs)
2155 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2156 h1s->cs->flags, h1s->cs->data);
2157 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002158}
2159
2160
2161/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2162static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2163{
2164 struct h1_hdr_entry *entry;
2165
2166 /* Be sure there is a non-empty <to> */
2167 if (!strlen(to)) {
2168 memprintf(err, "expect <to>");
2169 return -1;
2170 }
2171
2172 /* Be sure only the case differs between <from> and <to> */
2173 if (strcasecmp(from, to)) {
2174 memprintf(err, "<from> and <to> must not differ execpt the case");
2175 return -1;
2176 }
2177
2178 /* Be sure <from> does not already existsin the tree */
2179 if (ebis_lookup(&hdrs_map.map, from)) {
2180 memprintf(err, "duplicate entry '%s'", from);
2181 return -1;
2182 }
2183
2184 /* Create the entry and insert it in the tree */
2185 entry = malloc(sizeof(*entry));
2186 if (!entry) {
2187 memprintf(err, "out of memory");
2188 return -1;
2189 }
2190
2191 entry->node.key = strdup(from);
2192 entry->name.ptr = strdup(to);
2193 entry->name.len = strlen(to);
2194 if (!entry->node.key || !entry->name.ptr) {
2195 free(entry->node.key);
2196 free(entry->name.ptr);
2197 free(entry);
2198 memprintf(err, "out of memory");
2199 return -1;
2200 }
2201 ebis_insert(&hdrs_map.map, &entry->node);
2202 return 0;
2203}
2204
2205static void h1_hdeaders_case_adjust_deinit()
2206{
2207 struct ebpt_node *node, *next;
2208 struct h1_hdr_entry *entry;
2209
2210 node = ebpt_first(&hdrs_map.map);
2211 while (node) {
2212 next = ebpt_next(node);
2213 ebpt_delete(node);
2214 entry = container_of(node, struct h1_hdr_entry, node);
2215 free(entry->node.key);
2216 free(entry->name.ptr);
2217 free(entry);
2218 node = next;
2219 }
2220 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002221}
2222
Christopher Faulet98fbe952019-07-22 16:18:24 +02002223static int cfg_h1_headers_case_adjust_postparser()
2224{
2225 FILE *file = NULL;
2226 char *c, *key_beg, *key_end, *value_beg, *value_end;
2227 char *err;
2228 int rc, line = 0, err_code = 0;
2229
2230 if (!hdrs_map.name)
2231 goto end;
2232
2233 file = fopen(hdrs_map.name, "r");
2234 if (!file) {
2235 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2236 hdrs_map.name);
2237 err_code |= ERR_ALERT | ERR_FATAL;
2238 goto end;
2239 }
2240
2241 /* now parse all lines. The file may contain only two header name per
2242 * line, separated by spaces. All heading and trailing spaces will be
2243 * ignored. Lines starting with a # are ignored.
2244 */
2245 while (fgets(trash.area, trash.size, file) != NULL) {
2246 line++;
2247 c = trash.area;
2248
2249 /* strip leading spaces and tabs */
2250 while (*c == ' ' || *c == '\t')
2251 c++;
2252
2253 /* ignore emptu lines, or lines beginning with a dash */
2254 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2255 continue;
2256
2257 /* look for the end of the key */
2258 key_beg = c;
2259 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2260 c++;
2261 key_end = c;
2262
2263 /* strip middle spaces and tabs */
2264 while (*c == ' ' || *c == '\t')
2265 c++;
2266
2267 /* look for the end of the value, it is the end of the line */
2268 value_beg = c;
2269 while (*c && *c != '\n' && *c != '\r')
2270 c++;
2271 value_end = c;
2272
2273 /* trim possibly trailing spaces and tabs */
2274 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2275 value_end--;
2276
2277 /* set final \0 and check entries */
2278 *key_end = '\0';
2279 *value_end = '\0';
2280
2281 err = NULL;
2282 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2283 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002284 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2285 hdrs_map.name, err, line);
2286 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002287 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002288 goto end;
2289 }
2290 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002291 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2292 hdrs_map.name, err, line);
2293 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002294 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002295 }
2296 }
2297
2298 end:
2299 if (file)
2300 fclose(file);
2301 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2302 return err_code;
2303}
2304
2305
2306/* config parser for global "h1-outgoing-header-case-adjust" */
2307static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2308 struct proxy *defpx, const char *file, int line,
2309 char **err)
2310{
2311 if (too_many_args(2, args, err, NULL))
2312 return -1;
2313 if (!*(args[1]) || !*(args[2])) {
2314 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2315 return -1;
2316 }
2317 return add_hdr_case_adjust(args[1], args[2], err);
2318}
2319
2320/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2321static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2322 struct proxy *defpx, const char *file, int line,
2323 char **err)
2324{
2325 if (too_many_args(1, args, err, NULL))
2326 return -1;
2327 if (!*(args[1])) {
2328 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2329 return -1;
2330 }
2331 free(hdrs_map.name);
2332 hdrs_map.name = strdup(args[1]);
2333 return 0;
2334}
2335
2336
2337/* config keyword parsers */
2338static struct cfg_kw_list cfg_kws = {{ }, {
2339 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2340 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2341 { 0, NULL, NULL },
2342 }
2343};
2344
2345INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2346REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2347
2348
Christopher Faulet51dbc942018-09-13 09:05:15 +02002349/****************************************/
2350/* MUX initialization and instanciation */
2351/****************************************/
2352
2353/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002354static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002355 .init = h1_init,
2356 .wake = h1_wake,
2357 .attach = h1_attach,
2358 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002359 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002360 .detach = h1_detach,
2361 .destroy = h1_destroy,
2362 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002363 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002364 .rcv_buf = h1_rcv_buf,
2365 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002366#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002367 .rcv_pipe = h1_rcv_pipe,
2368 .snd_pipe = h1_snd_pipe,
2369#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002370 .subscribe = h1_subscribe,
2371 .unsubscribe = h1_unsubscribe,
2372 .shutr = h1_shutr,
2373 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002374 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002375 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002376 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002377 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002378};
2379
2380
2381/* this mux registers default HTX proto */
2382static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02002383{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02002384
Willy Tarreau0108d902018-11-25 19:14:37 +01002385INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2386
Christopher Faulet51dbc942018-09-13 09:05:15 +02002387/*
2388 * Local variables:
2389 * c-indent-level: 8
2390 * c-basic-offset: 8
2391 * End:
2392 */