blob: ee12686bbc25d1443eb4b453272d25a5b3a45e64 [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
Christopher Faulet86d144c2019-08-14 16:32:25 +02001273 /* Skip all pseudo-headers */
1274 if (*(n.ptr) == ':')
1275 goto skip_hdr;
1276
Christopher Faulet9768c262018-10-22 09:34:31 +02001277 if (isteqi(n, ist("transfer-encoding")))
1278 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001279 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001280 /* Only skip C-L header with invalid value. */
1281 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001282 goto skip_hdr;
1283 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001284 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001285 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001286 if (!v.len)
1287 goto skip_hdr;
1288 }
1289
Christopher Faulet98fbe952019-07-22 16:18:24 +02001290 /* Try to adjust the case of the header name */
1291 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1292 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001293 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001294 goto copy;
1295 skip_hdr:
1296 h1m->state = H1_MSG_HDR_L2_LWS;
1297 break;
1298
Christopher Faulet94b2c762019-05-24 15:28:57 +02001299 case H1_MSG_LAST_LF:
1300 if (type != HTX_BLK_EOH)
1301 goto error;
1302 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001303 h1m->state = H1_MSG_LAST_LF;
1304 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001305 /* There is no "Connection:" header and
1306 * it the conn_mode must be
1307 * processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001308 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001309 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001310 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001311 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001312 /* Try to adjust the case of the header name */
1313 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1314 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001315 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001316 goto copy;
1317 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001318 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001319 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001320
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001321 if ((h1s->meth != HTTP_METH_CONNECT &&
1322 (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 +01001323 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1324 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1325 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1326 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1327 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001328 /* chunking needed but header not seen */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001329 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Willy Tarreau4710d202019-01-03 17:39:54 +01001330 goto copy;
1331 h1m->flags |= H1_MF_CHNK;
1332 }
1333
Christopher Fauletc2518a52019-06-25 21:41:02 +02001334 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet9768c262018-10-22 09:34:31 +02001335 goto copy;
1336
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001337 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1338 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1339 h1_set_req_tunnel_mode(h1s);
1340 }
1341 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1342 /* a successfull reply to a CONNECT or a protocol switching is sent
1343 * to the client . Switch the response to tunnel mode. */
1344 h1_set_res_tunnel_mode(h1s);
1345 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001346 else if ((h1m->flags & H1_MF_RESP) &&
1347 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1348 h1m_init_res(&h1s->res);
1349 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001350 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001351 }
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001352 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1353 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001354 else
1355 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001356 break;
1357
Christopher Faulet94b2c762019-05-24 15:28:57 +02001358 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001359 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001360 if (type == HTX_BLK_EOM) {
1361 /* Chunked message without explicit trailers */
1362 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001363 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001364 goto copy;
1365 }
1366 goto done;
1367 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001368 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001369 /* If the message is not chunked, never
1370 * add the last chunk. */
1371 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet94b2c762019-05-24 15:28:57 +02001372 goto copy;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001373 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001374 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001375 else if (type != HTX_BLK_DATA)
1376 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001377 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001378 v.len = vlen;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001379 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001380 goto copy;
1381 break;
1382
Christopher Faulet94b2c762019-05-24 15:28:57 +02001383 case H1_MSG_TRAILERS:
1384 if (type == HTX_BLK_EOM)
1385 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001386 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001387 goto error;
1388 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001389 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001390 /* If the message is not chunked, ignore
1391 * trailers. It may happen with H2 messages. */
1392 if (!(h1m->flags & H1_MF_CHNK))
1393 break;
1394
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001395 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001396 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet32188212018-11-20 18:21:43 +01001397 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001398 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001399 else { // HTX_BLK_TLR
1400 n = htx_get_blk_name(chn_htx, blk);
1401 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001402
1403 /* Try to adjust the case of the header name */
1404 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1405 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001406 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001407 goto copy;
1408 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001409 break;
1410
Christopher Faulet94b2c762019-05-24 15:28:57 +02001411 case H1_MSG_DONE:
1412 if (type != HTX_BLK_EOM)
1413 goto error;
1414 done:
1415 h1m->state = H1_MSG_DONE;
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001416 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1417 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1418 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1419 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001420 break;
1421
Christopher Faulet9768c262018-10-22 09:34:31 +02001422 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001423 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001424 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001425 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001426 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001427 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001428 break;
1429 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001430
1431 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001432 total += vlen;
1433 count -= vlen;
1434 if (sz == vlen)
1435 blk = htx_remove_blk(chn_htx, blk);
1436 else {
1437 htx_cut_data_blk(chn_htx, blk, vlen);
1438 break;
1439 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001440 }
1441
Christopher Faulet9768c262018-10-22 09:34:31 +02001442 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001443 /* when the output buffer is empty, tmp shares the same area so that we
1444 * only have to update pointers and lengths.
1445 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001446 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1447 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001448 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001449 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001450
Willy Tarreau3815b222018-12-11 19:50:43 +01001451 htx_to_buf(chn_htx, buf);
1452 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001453 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001454 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001455 end:
1456 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001457}
1458
Christopher Faulet51dbc942018-09-13 09:05:15 +02001459/*********************************************************/
1460/* functions below are I/O callbacks from the connection */
1461/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001462static void h1_wake_stream_for_recv(struct h1s *h1s)
1463{
1464 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001465 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001466 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001467 h1s->recv_wait = NULL;
1468 }
1469}
1470static void h1_wake_stream_for_send(struct h1s *h1s)
1471{
1472 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001473 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001474 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001475 h1s->send_wait = NULL;
1476 }
1477}
1478
Christopher Faulet51dbc942018-09-13 09:05:15 +02001479/*
1480 * Attempt to read data, and subscribe if none available
1481 */
1482static int h1_recv(struct h1c *h1c)
1483{
1484 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001485 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001486 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001487 int rcvd = 0;
1488
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001489 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001490 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001491
Olivier Houchard75159a92018-12-03 18:46:09 +01001492 if (!h1_recv_allowed(h1c)) {
1493 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001494 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001495 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001496
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001497 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1498 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001499 goto end;
1500 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001501
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001502 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001503 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001504 h1_wake_stream_for_recv(h1s);
1505 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001506 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001507 }
1508
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001509 /*
1510 * If we only have a small amount of data, realign it,
1511 * it's probably cheaper than doing 2 recv() calls.
1512 */
1513 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1514 b_slow_realign(&h1c->ibuf, trash.area, 0);
1515
Willy Tarreau45f2b892018-12-05 07:59:27 +01001516 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001517 if (max) {
1518 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001519
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001520 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001521 if (!b_data(&h1c->ibuf)) {
1522 /* try to pre-align the buffer like the rxbufs will be
1523 * to optimize memory copies.
1524 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001525 h1c->ibuf.head = sizeof(struct htx);
1526 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001527 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001528 }
Christopher Faulet47365272018-10-31 17:40:50 +01001529 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001530 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001531 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001532 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001533 if (h1s->csinfo.t_idle == -1)
1534 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1535 }
Christopher Faulet47365272018-10-31 17:40:50 +01001536 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001537
Olivier Houchardcc3fec82019-07-26 15:11:11 +02001538 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001539 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001540 goto end;
1541 }
1542
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001543 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001544
Christopher Faulet9768c262018-10-22 09:34:31 +02001545 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001546 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1547 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001548
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001549 if (conn_xprt_read0_pending(conn) && h1s) {
1550 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001551 rcvd = 1;
1552 }
1553
Christopher Faulet51dbc942018-09-13 09:05:15 +02001554 if (!b_data(&h1c->ibuf))
1555 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001556 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001557 h1c->flags |= H1C_F_IN_FULL;
1558 return rcvd;
1559}
1560
1561
1562/*
1563 * Try to send data if possible
1564 */
1565static int h1_send(struct h1c *h1c)
1566{
1567 struct connection *conn = h1c->conn;
1568 unsigned int flags = 0;
1569 size_t ret;
1570 int sent = 0;
1571
1572 if (conn->flags & CO_FL_ERROR)
1573 return 0;
1574
1575 if (!b_data(&h1c->obuf))
1576 goto end;
1577
1578 if (h1c->flags & H1C_F_OUT_FULL)
1579 flags |= CO_SFL_MSG_MORE;
1580
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001581 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001582 if (ret > 0) {
1583 h1c->flags &= ~H1C_F_OUT_FULL;
1584 b_del(&h1c->obuf, ret);
1585 sent = 1;
1586 }
1587
Christopher Faulet145aa472018-12-06 10:56:20 +01001588 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1589 /* error or output closed, nothing to send, clear the buffer to release it */
1590 b_reset(&h1c->obuf);
1591 }
1592
Christopher Faulet51dbc942018-09-13 09:05:15 +02001593 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001594 if (!(h1c->flags & H1C_F_OUT_FULL))
1595 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001596
Christopher Faulet51dbc942018-09-13 09:05:15 +02001597 /* We're done, no more to send */
1598 if (!b_data(&h1c->obuf)) {
1599 h1_release_buf(h1c, &h1c->obuf);
1600 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001601 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001602 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001603 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001604 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001605
1606 return sent;
1607}
1608
Christopher Faulet51dbc942018-09-13 09:05:15 +02001609
1610/* callback called on any event by the connection handler.
1611 * It applies changes and returns zero, or < 0 if it wants immediate
1612 * destruction of the connection.
1613 */
1614static int h1_process(struct h1c * h1c)
1615{
1616 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001617 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001618
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001619 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001620 return -1;
1621
Christopher Fauletfeb11742018-11-29 15:12:34 +01001622 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02001623 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001624 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001625 conn_xprt_read0_pending(conn))
1626 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001627 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001628 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001629 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001630 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001631 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001632 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001633 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001634 }
1635
Christopher Fauletfeb11742018-11-29 15:12:34 +01001636 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1637 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1638
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001639 if (conn_xprt_read0_pending(conn))
1640 h1s->flags |= H1S_F_REOS;
1641
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001642 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001643 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001644 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001645 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001646 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01001647 h1s->cs->data_cb->wake(h1s->cs);
1648 }
Christopher Faulet47365272018-10-31 17:40:50 +01001649 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001650 if (h1c->task) {
1651 h1c->task->expire = TICK_ETERNITY;
1652 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001653 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 +01001654 ? h1c->shut_timeout
1655 : h1c->timeout));
1656 task_queue(h1c->task);
1657 }
1658 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001659 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001660
1661 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02001662 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01001663 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001664}
1665
1666static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1667{
1668 struct h1c *h1c = ctx;
1669 int ret = 0;
1670
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001671 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001672 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001673 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001674 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001675 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001676 h1_process(h1c);
1677 return NULL;
1678}
1679
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001680static void h1_reset(struct connection *conn)
1681{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001682
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001683}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001684
1685static int h1_wake(struct connection *conn)
1686{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001687 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001688 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001689
Christopher Faulet539e0292018-11-19 10:40:09 +01001690 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001691 ret = h1_process(h1c);
1692 if (ret == 0) {
1693 struct h1s *h1s = h1c->h1s;
1694
1695 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1696 ret = h1s->cs->data_cb->wake(h1s->cs);
1697 }
1698 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001699}
1700
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001701/* Connection timeout management. The principle is that if there's no receipt
1702 * nor sending for a certain amount of time, the connection is closed.
1703 */
1704static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
1705{
1706 struct h1c *h1c = context;
1707 int expired = tick_is_expired(t->expire, now_ms);
1708
1709 if (!expired && h1c)
1710 return t;
1711
Olivier Houchard3f795f72019-04-17 22:51:06 +02001712 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001713
1714 if (!h1c) {
1715 /* resources were already deleted */
1716 return NULL;
1717 }
1718
1719 h1c->task = NULL;
1720 /* If a stream is still attached to the mux, just set an error and wait
1721 * for the stream's timeout. Otherwise, release the mux. This is only ok
1722 * because same timeouts are used.
1723 */
1724 if (h1c->h1s && h1c->h1s->cs)
1725 h1c->flags |= H1C_F_CS_ERROR;
1726 else
Christopher Faulet73c12072019-04-08 11:23:22 +02001727 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001728 return NULL;
1729}
1730
Christopher Faulet51dbc942018-09-13 09:05:15 +02001731/*******************************************/
1732/* functions below are used by the streams */
1733/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02001734
Christopher Faulet51dbc942018-09-13 09:05:15 +02001735/*
1736 * Attach a new stream to a connection
1737 * (Used for outgoing connections)
1738 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001739static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001740{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001741 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001742 struct conn_stream *cs = NULL;
1743 struct h1s *h1s;
1744
1745 if (h1c->flags & H1C_F_CS_ERROR)
1746 goto end;
1747
1748 cs = cs_new(h1c->conn);
1749 if (!cs)
1750 goto end;
1751
Olivier Houchardf502aca2018-12-14 19:42:40 +01001752 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001753 if (h1s == NULL)
1754 goto end;
1755
1756 return cs;
1757 end:
1758 cs_free(cs);
1759 return NULL;
1760}
1761
1762/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1763 * this mux, it's easy as we can only store a single conn_stream.
1764 */
1765static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1766{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001767 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001768 struct h1s *h1s = h1c->h1s;
1769
1770 if (h1s)
1771 return h1s->cs;
1772
1773 return NULL;
1774}
1775
Christopher Faulet73c12072019-04-08 11:23:22 +02001776static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001777{
Christopher Faulet73c12072019-04-08 11:23:22 +02001778 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001779
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001780 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02001781 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001782}
1783
1784/*
1785 * Detach the stream from the connection and possibly release the connection.
1786 */
1787static void h1_detach(struct conn_stream *cs)
1788{
1789 struct h1s *h1s = cs->ctx;
1790 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001791 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01001792 int has_keepalive;
1793 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001794
1795 cs->ctx = NULL;
1796 if (!h1s)
1797 return;
1798
Olivier Houchardf502aca2018-12-14 19:42:40 +01001799 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001800 h1c = h1s->h1c;
1801 h1s->cs = NULL;
1802
Olivier Houchard8a786902018-12-15 16:05:40 +01001803 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
1804 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
1805 h1s_destroy(h1s);
1806
1807 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01001808 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02001809 /* If there are any excess server data in the input buffer,
1810 * release it and close the connection ASAP (some data may
1811 * remain in the output buffer). This happens if a server sends
1812 * invalid responses. So in such case, we don't want to reuse
1813 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02001814 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02001815 if (b_data(&h1c->ibuf)) {
1816 h1_release_buf(h1c, &h1c->ibuf);
1817 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1818 goto release;
1819 }
Christopher Faulet03627242019-07-19 11:34:08 +02001820
Christopher Faulet9400a392018-11-23 23:10:39 +01001821 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01001822 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01001823 h1c->conn->flags |= CO_FL_PRIVATE;
1824
Olivier Houchard44d59142018-12-13 18:46:22 +01001825 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001826 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01001827 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
1828 h1c->conn->owner = NULL;
1829 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
1830 /* The server doesn't want it, let's kill the connection right away */
1831 h1c->conn->mux->destroy(h1c->conn);
1832 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001833 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01001834 return;
1835
1836 }
Olivier Houchard44d59142018-12-13 18:46:22 +01001837 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01001838 if (h1c->conn->owner == sess) {
1839 int ret = session_check_idle_conn(sess, h1c->conn);
1840 if (ret == -1)
1841 /* The connection got destroyed, let's leave */
1842 return;
1843 else if (ret == 1) {
1844 /* The connection was added to the server list,
1845 * wake the task so we can subscribe to events
1846 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001847 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01001848 return;
1849 }
1850 }
Christopher Faulet9400a392018-11-23 23:10:39 +01001851 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01001852 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001853 struct server *srv = objt_server(h1c->conn->target);
1854
1855 if (srv) {
1856 if (h1c->conn->flags & CO_FL_PRIVATE)
1857 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01001858 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01001859 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1860 else
1861 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1862 }
1863 }
1864 }
1865
Christopher Fauletf1204b82019-07-19 14:51:06 +02001866 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02001867 /* 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 +02001868 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
1869 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
1870 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
1871 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02001872 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001873 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001874 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001875 if (h1c->task) {
1876 h1c->task->expire = TICK_ETERNITY;
1877 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001878 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 +01001879 ? h1c->shut_timeout
1880 : h1c->timeout));
1881 task_queue(h1c->task);
1882 }
1883 }
1884 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001885}
1886
1887
1888static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1889{
1890 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02001891 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001892
1893 if (!h1s)
1894 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02001895 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001896
Christopher Faulet7f366362019-04-08 10:51:20 +02001897 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
1898 goto do_shutr;
1899
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001900 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001901 return;
1902
Christopher Faulet7f366362019-04-08 10:51:20 +02001903 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001904 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1905 if (cs->flags & CS_FL_SHR)
1906 return;
1907 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001908 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
1909 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01001910 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 +02001911 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001912}
1913
1914static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1915{
1916 struct h1s *h1s = cs->ctx;
1917 struct h1c *h1c;
1918
1919 if (!h1s)
1920 return;
1921 h1c = h1s->h1c;
1922
Christopher Faulet7f366362019-04-08 10:51:20 +02001923 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
1924 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01001925
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001926 if ((h1c->flags & H1C_F_UPG_H2C) ||
1927 ((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 +02001928 return;
1929
Christopher Faulet7f366362019-04-08 10:51:20 +02001930 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001931 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1932 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1933 return;
1934
Christopher Faulet666a0c42019-01-08 11:12:04 +01001935 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001936}
1937
Christopher Faulet666a0c42019-01-08 11:12:04 +01001938static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001939{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001940 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001941
Christopher Faulet666a0c42019-01-08 11:12:04 +01001942 conn_xprt_shutw(conn);
1943 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
1944 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
1945 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001946}
1947
1948/* Called from the upper layer, to unsubscribe to events */
1949static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1950{
1951 struct wait_event *sw;
1952 struct h1s *h1s = cs->ctx;
1953
1954 if (!h1s)
1955 return 0;
1956
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001957 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001958 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02001959 BUG_ON(h1s->recv_wait != sw);
1960 sw->events &= ~SUB_RETRY_RECV;
1961 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001962 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001963 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001964 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02001965 BUG_ON(h1s->send_wait != sw);
1966 sw->events &= ~SUB_RETRY_SEND;
1967 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001968 }
1969 return 0;
1970}
1971
1972/* Called from the upper layer, to subscribe to events, such as being able to send */
1973static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1974{
1975 struct wait_event *sw;
1976 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02001977 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001978
1979 if (!h1s)
1980 return -1;
1981
1982 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001983 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001984 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02001985 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
1986 sw->events |= SUB_RETRY_RECV;
1987 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001988 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001989 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001990 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02001991 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
1992 sw->events |= SUB_RETRY_SEND;
1993 h1s->send_wait = sw;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02001994 /*
1995 * If the conn_stream attempt to subscribe, and the
1996 * mux isn't subscribed to the connection, then it
1997 * probably means the connection wasn't established
1998 * yet, so we have to subscribe.
1999 */
Christopher Faulet51bb1852019-09-04 10:22:34 +02002000 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002001 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2002 h1c->conn->xprt->subscribe(h1c->conn,
2003 h1c->conn->xprt_ctx,
2004 SUB_RETRY_SEND,
2005 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002006 return 0;
2007 default:
2008 break;
2009 }
2010 return -1;
2011}
2012
2013/* Called from the upper layer, to receive data */
2014static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2015{
2016 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002017 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002018 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002019 size_t ret = 0;
2020
Christopher Faulet539e0292018-11-19 10:40:09 +01002021 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002022 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002023
Christopher Faulete18777b2019-04-16 16:46:36 +02002024 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002025
2026 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2027 h1s->flags |= H1S_F_BUF_FLUSH;
2028 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002029 else {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002030 h1s->flags &= ~H1S_F_SPLICED_DATA;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002031 if (h1m->state != H1_MSG_DONE &&
2032 !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002033 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002034 }
2035 return ret;
2036}
2037
2038
2039/* Called from the upper layer, to send data */
2040static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2041{
2042 struct h1s *h1s = cs->ctx;
2043 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002044 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002045
2046 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002047 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002048
2049 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002050
2051 /* If we're not connected yet, or we're waiting for a handshake, stop
2052 * now, as we don't want to remove everything from the channel buffer
2053 * before we're sure we can send it.
2054 */
2055 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
2056 (h1c->conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002057 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002058
Christopher Fauletc31872f2019-06-04 22:09:36 +02002059 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002060 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002061
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002062 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2063 ret = h1_process_output(h1c, buf, count);
2064 if (!ret)
2065 break;
2066 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002067 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002068 if (!h1_send(h1c))
2069 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002070 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002071
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002072 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002073}
2074
Willy Tarreaue5733232019-05-22 19:24:06 +02002075#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002076/* Send and get, using splicing */
2077static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2078{
2079 struct h1s *h1s = cs->ctx;
2080 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2081 int ret = 0;
2082
Christopher Faulet1146f972019-05-29 14:35:24 +02002083 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002084 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002085 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2086 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002087 goto end;
2088 }
2089
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002090 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002091 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002092 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002093 }
2094
2095 h1s->flags &= ~H1S_F_BUF_FLUSH;
2096 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002097 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2098 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002099 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002100 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002101 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002102 if (!h1m->curr_len)
2103 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2104 }
2105
Christopher Faulet1be55f92018-10-02 15:59:23 +02002106 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002107 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002108 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002109 if (!pipe->data)
2110 cs->flags |= CS_FL_EOS;
2111 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002112 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002113}
2114
2115static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2116{
2117 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002118 int ret = 0;
2119
2120 if (b_data(&h1s->h1c->obuf))
2121 goto end;
2122
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002123 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002124 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002125 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002126 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002127 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002128 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002129 return ret;
2130}
2131#endif
2132
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002133/* for debugging with CLI's "show fd" command */
2134static void h1_show_fd(struct buffer *msg, struct connection *conn)
2135{
2136 struct h1c *h1c = conn->ctx;
2137 struct h1s *h1s = h1c->h1s;
2138
Christopher Fauletf376a312019-01-04 15:16:06 +01002139 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2140 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002141 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2142 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2143 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2144 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2145
2146 if (h1s) {
2147 char *method;
2148
2149 if (h1s->meth < HTTP_METH_OTHER)
2150 method = http_known_methods[h1s->meth].ptr;
2151 else
2152 method = "UNKNOWN";
2153 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2154 " .meth=%s status=%d",
2155 h1s, h1s->flags,
2156 h1m_state_str(h1s->req.state),
2157 h1m_state_str(h1s->res.state), method, h1s->status);
2158 if (h1s->cs)
2159 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2160 h1s->cs->flags, h1s->cs->data);
2161 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002162}
2163
2164
2165/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2166static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2167{
2168 struct h1_hdr_entry *entry;
2169
2170 /* Be sure there is a non-empty <to> */
2171 if (!strlen(to)) {
2172 memprintf(err, "expect <to>");
2173 return -1;
2174 }
2175
2176 /* Be sure only the case differs between <from> and <to> */
2177 if (strcasecmp(from, to)) {
2178 memprintf(err, "<from> and <to> must not differ execpt the case");
2179 return -1;
2180 }
2181
2182 /* Be sure <from> does not already existsin the tree */
2183 if (ebis_lookup(&hdrs_map.map, from)) {
2184 memprintf(err, "duplicate entry '%s'", from);
2185 return -1;
2186 }
2187
2188 /* Create the entry and insert it in the tree */
2189 entry = malloc(sizeof(*entry));
2190 if (!entry) {
2191 memprintf(err, "out of memory");
2192 return -1;
2193 }
2194
2195 entry->node.key = strdup(from);
2196 entry->name.ptr = strdup(to);
2197 entry->name.len = strlen(to);
2198 if (!entry->node.key || !entry->name.ptr) {
2199 free(entry->node.key);
2200 free(entry->name.ptr);
2201 free(entry);
2202 memprintf(err, "out of memory");
2203 return -1;
2204 }
2205 ebis_insert(&hdrs_map.map, &entry->node);
2206 return 0;
2207}
2208
2209static void h1_hdeaders_case_adjust_deinit()
2210{
2211 struct ebpt_node *node, *next;
2212 struct h1_hdr_entry *entry;
2213
2214 node = ebpt_first(&hdrs_map.map);
2215 while (node) {
2216 next = ebpt_next(node);
2217 ebpt_delete(node);
2218 entry = container_of(node, struct h1_hdr_entry, node);
2219 free(entry->node.key);
2220 free(entry->name.ptr);
2221 free(entry);
2222 node = next;
2223 }
2224 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002225}
2226
Christopher Faulet98fbe952019-07-22 16:18:24 +02002227static int cfg_h1_headers_case_adjust_postparser()
2228{
2229 FILE *file = NULL;
2230 char *c, *key_beg, *key_end, *value_beg, *value_end;
2231 char *err;
2232 int rc, line = 0, err_code = 0;
2233
2234 if (!hdrs_map.name)
2235 goto end;
2236
2237 file = fopen(hdrs_map.name, "r");
2238 if (!file) {
2239 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2240 hdrs_map.name);
2241 err_code |= ERR_ALERT | ERR_FATAL;
2242 goto end;
2243 }
2244
2245 /* now parse all lines. The file may contain only two header name per
2246 * line, separated by spaces. All heading and trailing spaces will be
2247 * ignored. Lines starting with a # are ignored.
2248 */
2249 while (fgets(trash.area, trash.size, file) != NULL) {
2250 line++;
2251 c = trash.area;
2252
2253 /* strip leading spaces and tabs */
2254 while (*c == ' ' || *c == '\t')
2255 c++;
2256
2257 /* ignore emptu lines, or lines beginning with a dash */
2258 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2259 continue;
2260
2261 /* look for the end of the key */
2262 key_beg = c;
2263 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2264 c++;
2265 key_end = c;
2266
2267 /* strip middle spaces and tabs */
2268 while (*c == ' ' || *c == '\t')
2269 c++;
2270
2271 /* look for the end of the value, it is the end of the line */
2272 value_beg = c;
2273 while (*c && *c != '\n' && *c != '\r')
2274 c++;
2275 value_end = c;
2276
2277 /* trim possibly trailing spaces and tabs */
2278 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2279 value_end--;
2280
2281 /* set final \0 and check entries */
2282 *key_end = '\0';
2283 *value_end = '\0';
2284
2285 err = NULL;
2286 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2287 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002288 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2289 hdrs_map.name, err, line);
2290 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002291 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002292 goto end;
2293 }
2294 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002295 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2296 hdrs_map.name, err, line);
2297 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002298 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002299 }
2300 }
2301
2302 end:
2303 if (file)
2304 fclose(file);
2305 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2306 return err_code;
2307}
2308
2309
2310/* config parser for global "h1-outgoing-header-case-adjust" */
2311static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2312 struct proxy *defpx, const char *file, int line,
2313 char **err)
2314{
2315 if (too_many_args(2, args, err, NULL))
2316 return -1;
2317 if (!*(args[1]) || !*(args[2])) {
2318 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2319 return -1;
2320 }
2321 return add_hdr_case_adjust(args[1], args[2], err);
2322}
2323
2324/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2325static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2326 struct proxy *defpx, const char *file, int line,
2327 char **err)
2328{
2329 if (too_many_args(1, args, err, NULL))
2330 return -1;
2331 if (!*(args[1])) {
2332 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2333 return -1;
2334 }
2335 free(hdrs_map.name);
2336 hdrs_map.name = strdup(args[1]);
2337 return 0;
2338}
2339
2340
2341/* config keyword parsers */
2342static struct cfg_kw_list cfg_kws = {{ }, {
2343 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2344 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2345 { 0, NULL, NULL },
2346 }
2347};
2348
2349INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2350REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2351
2352
Christopher Faulet51dbc942018-09-13 09:05:15 +02002353/****************************************/
2354/* MUX initialization and instanciation */
2355/****************************************/
2356
2357/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002358static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002359 .init = h1_init,
2360 .wake = h1_wake,
2361 .attach = h1_attach,
2362 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002363 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002364 .detach = h1_detach,
2365 .destroy = h1_destroy,
2366 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002367 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002368 .rcv_buf = h1_rcv_buf,
2369 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002370#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002371 .rcv_pipe = h1_rcv_pipe,
2372 .snd_pipe = h1_snd_pipe,
2373#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002374 .subscribe = h1_subscribe,
2375 .unsubscribe = h1_unsubscribe,
2376 .shutr = h1_shutr,
2377 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002378 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002379 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002380 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002381 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002382};
2383
2384
2385/* this mux registers default HTX proto */
2386static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02002387{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02002388
Willy Tarreau0108d902018-11-25 19:14:37 +01002389INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2390
Christopher Faulet51dbc942018-09-13 09:05:15 +02002391/*
2392 * Local variables:
2393 * c-indent-level: 8
2394 * c-basic-offset: 8
2395 * End:
2396 */