blob: 5375ad536373e59a3a3dac9ba2f9e93e4562da6b [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 */
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020073/* 0x00002000 .. 0x00001000 unused */
74#define H1S_F_HAVE_SRV_NAME 0x00002000 /* Set during output process if the server name header was added to the request */
Christopher Fauletada34b62019-05-24 16:36:43 +020075#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020076
77/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020078struct h1c {
79 struct connection *conn;
80 struct proxy *px;
81 uint32_t flags; /* Connection flags: H1C_F_* */
82
83 struct buffer ibuf; /* Input buffer to store data before parsing */
84 struct buffer obuf; /* Output buffer to store data after reformatting */
85
86 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
87 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
88
89 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010090 struct task *task; /* timeout management task */
91 int timeout; /* idle timeout duration in ticks */
92 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020093};
94
95/* H1 stream descriptor */
96struct h1s {
97 struct h1c *h1c;
98 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010099 struct cs_info csinfo; /* CS info, only used for client connections */
100 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200101
Christopher Faulet51dbc942018-09-13 09:05:15 +0200102 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
103 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200104
Olivier Houchardf502aca2018-12-14 19:42:40 +0100105 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200106 struct h1m req;
107 struct h1m res;
108
109 enum http_meth_t meth; /* HTTP resquest method */
110 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200111};
112
Christopher Faulet98fbe952019-07-22 16:18:24 +0200113/* Map of headers used to convert outgoing headers */
114struct h1_hdrs_map {
115 char *name;
116 struct eb_root map;
117};
118
119/* An entry in a headers map */
120struct h1_hdr_entry {
121 struct ist name;
122 struct ebpt_node node;
123};
124
125/* Declare the headers map */
126static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
127
128
Christopher Faulet51dbc942018-09-13 09:05:15 +0200129/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100130DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
131DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200132
Christopher Faulet51dbc942018-09-13 09:05:15 +0200133static int h1_recv(struct h1c *h1c);
134static int h1_send(struct h1c *h1c);
135static int h1_process(struct h1c *h1c);
136static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100137static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100138static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200139
140/*****************************************************/
141/* functions below are for dynamic buffer management */
142/*****************************************************/
143/*
144 * Indicates whether or not the we may call the h1_recv() function to
145 * attempt to receive data into the buffer and/or parse pending data. The
146 * condition is a bit complex due to some API limits for now. The rules are the
147 * following :
148 * - if an error or a shutdown was detected on the connection and the buffer
149 * is empty, we must not attempt to receive
150 * - if the input buffer failed to be allocated, we must not try to receive
151 * and we know there is nothing pending
152 * - if no flag indicates a blocking condition, we may attempt to receive,
153 * regardless of whether the input buffer is full or not, so that only de
154 * receiving part decides whether or not to block. This is needed because
155 * the connection API indeed prevents us from re-enabling receipt that is
156 * already enabled in a polled state, so we must always immediately stop as
157 * soon as the mux can't proceed so as never to hit an end of read with data
158 * pending in the buffers.
159 * - otherwise must may not attempt to receive
160 */
161static inline int h1_recv_allowed(const struct h1c *h1c)
162{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100163 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200164 return 0;
165
Christopher Fauletcf56b992018-12-11 16:12:31 +0100166 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
167 return 0;
168
Christopher Fauletcb55f482018-12-10 11:56:47 +0100169 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200170 return 1;
171
172 return 0;
173}
174
175/*
176 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
177 * flags are used to figure what buffer was requested. It returns 1 if the
178 * allocation succeeds, in which case the connection is woken up, or 0 if it's
179 * impossible to wake up and we prefer to be woken up later.
180 */
181static int h1_buf_available(void *target)
182{
183 struct h1c *h1c = target;
184
185 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
186 h1c->flags &= ~H1C_F_IN_ALLOC;
187 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200188 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200189 return 1;
190 }
191
192 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
193 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200194 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200195 return 1;
196 }
197
Christopher Faulet51dbc942018-09-13 09:05:15 +0200198 return 0;
199}
200
201/*
202 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
203 */
204static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
205{
206 struct buffer *buf = NULL;
207
208 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
209 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
210 h1c->buf_wait.target = h1c;
211 h1c->buf_wait.wakeup_cb = h1_buf_available;
212 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
213 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
214 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
215 __conn_xprt_stop_recv(h1c->conn);
216 }
217 return buf;
218}
219
220/*
221 * Release a buffer, if any, and try to wake up entities waiting in the buffer
222 * wait queue.
223 */
224static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
225{
226 if (bptr->size) {
227 b_free(bptr);
228 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
229 }
230}
231
Willy Tarreau00f18a32019-01-26 12:19:01 +0100232/* returns the number of streams in use on a connection to figure if it's
233 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
234 * as the caller will want to know if it was the last one after a detach().
235 */
236static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200237{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100238 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200239
Willy Tarreau00f18a32019-01-26 12:19:01 +0100240 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200241}
242
Willy Tarreau00f18a32019-01-26 12:19:01 +0100243/* returns the number of streams still available on a connection */
244static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100245{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100246 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100247}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200248
Willy Tarreau00f18a32019-01-26 12:19:01 +0100249
Christopher Faulet51dbc942018-09-13 09:05:15 +0200250/*****************************************************************/
251/* functions below are dedicated to the mux setup and management */
252/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200253
254/* returns non-zero if there are input data pending for stream h1s. */
255static inline size_t h1s_data_pending(const struct h1s *h1s)
256{
257 const struct h1m *h1m;
258
259 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
260 if (h1m->state == H1_MSG_DONE)
261 return 0; // data not for this stream (e.g. pipelining)
262
263 return b_data(&h1s->h1c->ibuf);
264}
265
Christopher Faulet47365272018-10-31 17:40:50 +0100266static struct conn_stream *h1s_new_cs(struct h1s *h1s)
267{
268 struct conn_stream *cs;
269
270 cs = cs_new(h1s->h1c->conn);
271 if (!cs)
272 goto err;
273 h1s->cs = cs;
274 cs->ctx = h1s;
275
276 if (h1s->flags & H1S_F_NOT_FIRST)
277 cs->flags |= CS_FL_NOT_FIRST;
278
279 if (stream_create_from_cs(cs) < 0)
280 goto err;
281 return cs;
282
283 err:
284 cs_free(cs);
285 h1s->cs = NULL;
286 return NULL;
287}
288
Olivier Houchardf502aca2018-12-14 19:42:40 +0100289static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200290{
291 struct h1s *h1s;
292
293 h1s = pool_alloc(pool_head_h1s);
294 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100295 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200296
297 h1s->h1c = h1c;
298 h1c->h1s = h1s;
299
Olivier Houchardf502aca2018-12-14 19:42:40 +0100300 h1s->sess = sess;
301
Christopher Faulet51dbc942018-09-13 09:05:15 +0200302 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100303 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200304
305 h1s->recv_wait = NULL;
306 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200307
308 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100309 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200310
Christopher Faulet129817b2018-09-20 16:14:40 +0200311 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100312 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200313
314 h1s->status = 0;
315 h1s->meth = HTTP_METH_OTHER;
316
Christopher Faulet47365272018-10-31 17:40:50 +0100317 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
318 h1s->flags |= H1S_F_NOT_FIRST;
319 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
320
Christopher Faulet129817b2018-09-20 16:14:40 +0200321 if (!conn_is_back(h1c->conn)) {
322 if (h1c->px->options2 & PR_O2_REQBUG_OK)
323 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200324
325 /* For frontend connections we should always have a session */
326 if (!sess)
327 sess = h1c->conn->owner;
328
Dave Pirotte234740f2019-07-10 13:57:38 +0000329 /* Timers for subsequent sessions on the same HTTP 1.x connection
330 * measure from `now`, not from the connection accept time */
331 if (h1s->flags & H1S_F_NOT_FIRST) {
332 h1s->csinfo.create_date = date;
333 h1s->csinfo.tv_create = now;
334 h1s->csinfo.t_handshake = 0;
335 h1s->csinfo.t_idle = -1;
336 }
337 else {
338 h1s->csinfo.create_date = sess->accept_date;
339 h1s->csinfo.tv_create = sess->tv_accept;
340 h1s->csinfo.t_handshake = sess->t_handshake;
341 h1s->csinfo.t_idle = -1;
342 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200343 }
344 else {
345 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
346 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200347
Christopher Fauletfeb11742018-11-29 15:12:34 +0100348 h1s->csinfo.create_date = date;
349 h1s->csinfo.tv_create = now;
350 h1s->csinfo.t_handshake = 0;
351 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200352 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100353
Christopher Faulete9b70722019-04-08 10:46:02 +0200354 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
355 * create a new one.
356 */
357 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200358 cs->ctx = h1s;
359 h1s->cs = cs;
360 }
Christopher Faulet47365272018-10-31 17:40:50 +0100361 else {
362 cs = h1s_new_cs(h1s);
363 if (!cs)
364 goto fail;
365 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200366 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100367
368 fail:
369 pool_free(pool_head_h1s, h1s);
370 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200371}
372
373static void h1s_destroy(struct h1s *h1s)
374{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200375 if (h1s) {
376 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200377
Christopher Fauletf2824e62018-10-01 12:12:37 +0200378 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200379
Christopher Fauletf2824e62018-10-01 12:12:37 +0200380 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100381 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200382 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100383 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200384
Christopher Fauletcb55f482018-12-10 11:56:47 +0100385 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100386 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
387 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
388 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200389 pool_free(pool_head_h1s, h1s);
390 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200391}
392
Christopher Fauletfeb11742018-11-29 15:12:34 +0100393static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
394{
395 struct h1s *h1s = cs->ctx;
396
397 if (h1s && !conn_is_back(cs->conn))
398 return &h1s->csinfo;
399 return NULL;
400}
401
Christopher Faulet51dbc942018-09-13 09:05:15 +0200402/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200403 * Initialize the mux once it's attached. It is expected that conn->ctx points
404 * to the existing conn_stream (for outgoing connections or for incoming onces
405 * during a mux upgrade) or NULL (for incoming ones during the connexion
406 * establishment). <input> is always used as Input buffer and may contain
407 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
408 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200409 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200410static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
411 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200412{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200413 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100414 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200415
416 h1c = pool_alloc(pool_head_h1c);
417 if (!h1c)
418 goto fail_h1c;
419 h1c->conn = conn;
420 h1c->px = proxy;
421
422 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200423 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200424 h1c->obuf = BUF_NULL;
425 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200426 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200427
Christopher Faulet51dbc942018-09-13 09:05:15 +0200428 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200429 h1c->wait_event.tasklet = tasklet_new();
430 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200431 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200432 h1c->wait_event.tasklet->process = h1_io_cb;
433 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100434 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200435
Christopher Faulete9b70722019-04-08 10:46:02 +0200436 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100437 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
438 if (tick_isset(proxy->timeout.serverfin))
439 h1c->shut_timeout = proxy->timeout.serverfin;
440 } else {
441 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
442 if (tick_isset(proxy->timeout.clientfin))
443 h1c->shut_timeout = proxy->timeout.clientfin;
444 }
445 if (tick_isset(h1c->timeout)) {
446 t = task_new(tid_bit);
447 if (!t)
448 goto fail;
449
450 h1c->task = t;
451 t->process = h1_timeout_task;
452 t->context = h1c;
453 t->expire = tick_add(now_ms, h1c->timeout);
454 }
455
Christopher Fauletf2824e62018-10-01 12:12:37 +0200456 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100457 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200458 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200459
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100460 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200461
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100462
463 if (t)
464 task_queue(t);
465
Christopher Faulet51dbc942018-09-13 09:05:15 +0200466 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200467 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200468
469 /* mux->wake will be called soon to complete the operation */
470 return 0;
471
472 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200473 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200474 if (h1c->wait_event.tasklet)
475 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200476 pool_free(pool_head_h1c, h1c);
477 fail_h1c:
478 return -1;
479}
480
Christopher Faulet73c12072019-04-08 11:23:22 +0200481/* release function. This one should be called to free all resources allocated
482 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200483 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200484static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200485{
Christopher Faulet61840e72019-04-15 09:33:32 +0200486 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200487
Christopher Faulet51dbc942018-09-13 09:05:15 +0200488 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200489 /* The connection must be aattached to this mux to be released */
490 if (h1c->conn && h1c->conn->ctx == h1c)
491 conn = h1c->conn;
492
493 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200494 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200495 /* Make sure we're no longer subscribed to anything */
496 if (h1c->wait_event.events)
497 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
498 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200499 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200500 /* connection successfully upgraded to H2, this
501 * mux was already released */
502 return;
503 }
504 sess_log(conn->owner); /* Log if the upgrade failed */
505 }
506
Olivier Houchard45c44372019-06-11 14:06:23 +0200507
Christopher Faulet51dbc942018-09-13 09:05:15 +0200508 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
509 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
510 LIST_DEL(&h1c->buf_wait.list);
511 LIST_INIT(&h1c->buf_wait.list);
512 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
513 }
514
515 h1_release_buf(h1c, &h1c->ibuf);
516 h1_release_buf(h1c, &h1c->obuf);
517
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100518 if (h1c->task) {
519 h1c->task->context = NULL;
520 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
521 h1c->task = NULL;
522 }
523
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200524 if (h1c->wait_event.tasklet)
525 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200526
Christopher Fauletf2824e62018-10-01 12:12:37 +0200527 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200528 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100529 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200530 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200531 pool_free(pool_head_h1c, h1c);
532 }
533
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200534 if (conn) {
535 conn->mux = NULL;
536 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200537
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200538 conn_stop_tracking(conn);
539 conn_full_close(conn);
540 if (conn->destroy_cb)
541 conn->destroy_cb(conn);
542 conn_free(conn);
543 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200544}
545
546/******************************************************/
547/* functions below are for the H1 protocol processing */
548/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200549/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
550 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200551 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100552static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200553{
Christopher Faulet570d1612018-11-26 11:13:57 +0100554 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200555
Christopher Faulet570d1612018-11-26 11:13:57 +0100556 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200557 (*(p + 5) > '1' ||
558 (*(p + 5) == '1' && *(p + 7) >= '1')))
559 h1m->flags |= H1_MF_VER_11;
560}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200561
Christopher Faulet9768c262018-10-22 09:34:31 +0200562/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
563 * greater or equal to 1.1
564 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100565static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200566{
Christopher Faulet570d1612018-11-26 11:13:57 +0100567 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200568
Christopher Faulet570d1612018-11-26 11:13:57 +0100569 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200570 (*(p + 5) > '1' ||
571 (*(p + 5) == '1' && *(p + 7) >= '1')))
572 h1m->flags |= H1_MF_VER_11;
573}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200574
Christopher Fauletf2824e62018-10-01 12:12:37 +0200575/* Deduce the connection mode of the client connection, depending on the
576 * configuration and the H1 message flags. This function is called twice, the
577 * first time when the request is parsed and the second time when the response
578 * is parsed.
579 */
580static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
581{
582 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200583
584 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100585 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200586 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100587 h1s->status == 101) {
588 /* Either we've established an explicit tunnel, or we're
589 * switching the protocol. In both cases, we're very unlikely to
590 * understand the next protocols. We have to switch to tunnel
591 * mode, so that we transfer the request and responses then let
592 * this protocol pass unmodified. When we later implement
593 * specific parsers for such protocols, we'll want to check the
594 * Upgrade header which contains information about that protocol
595 * for responses with status 101 (eg: see RFC2817 about TLS).
596 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200597 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100598 }
599 else if (h1s->flags & H1S_F_WANT_KAL) {
600 /* By default the client is in KAL mode. CLOSE mode mean
601 * it is imposed by the client itself. So only change
602 * KAL mode here. */
603 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
604 /* no length known or explicit close => close */
605 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
606 }
607 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
608 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
609 /* no explict keep-alive and option httpclose => close */
610 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
611 }
612 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200613 }
614 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100615 /* Input direction: first pass */
616 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
617 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200618 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100619 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200620 }
621
622 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
623 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
624 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
625}
626
627/* Deduce the connection mode of the client connection, depending on the
628 * configuration and the H1 message flags. This function is called twice, the
629 * first time when the request is parsed and the second time when the response
630 * is parsed.
631 */
632static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
633{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100634 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100635 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100636 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200637
Christopher Fauletf2824e62018-10-01 12:12:37 +0200638 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100639 /* Input direction: second pass */
640
Christopher Fauletf2824e62018-10-01 12:12:37 +0200641 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100642 h1s->status == 101) {
643 /* Either we've established an explicit tunnel, or we're
644 * switching the protocol. In both cases, we're very unlikely to
645 * understand the next protocols. We have to switch to tunnel
646 * mode, so that we transfer the request and responses then let
647 * this protocol pass unmodified. When we later implement
648 * specific parsers for such protocols, we'll want to check the
649 * Upgrade header which contains information about that protocol
650 * for responses with status 101 (eg: see RFC2817 about TLS).
651 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200652 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100653 }
654 else if (h1s->flags & H1S_F_WANT_KAL) {
655 /* By default the server is in KAL mode. CLOSE mode mean
656 * it is imposed by haproxy itself. So only change KAL
657 * mode here. */
658 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
659 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
660 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
661 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
662 }
663 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200664 }
Christopher Faulet70033782018-12-05 13:50:11 +0100665 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100666 /* Output direction: first pass */
667 if (h1m->flags & H1_MF_CONN_CLO) {
668 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100669 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100670 }
671 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
672 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
673 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
674 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
675 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
676 /* no explicit keep-alive option httpclose/server-close => close */
677 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
678 }
Christopher Faulet70033782018-12-05 13:50:11 +0100679 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200680
681 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
682 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
683 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200684}
685
Christopher Fauletb992af02019-03-28 15:42:24 +0100686static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200687{
688 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200689
690 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
691 * token is found
692 */
693 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200694 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200695
696 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100697 if (!(h1m->flags & H1_MF_VER_11))
698 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200699 }
700 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100701 if (h1m->flags & H1_MF_VER_11)
702 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200703 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200704}
705
Christopher Fauletb992af02019-03-28 15:42:24 +0100706static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200707{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200708 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
709 * token is found
710 */
711 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200712 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200713
714 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100715 if (!(h1m->flags & H1_MF_VER_11) ||
716 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
717 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200718 }
719 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100720 if (h1m->flags & H1_MF_VER_11)
721 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200722 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200723}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200724
Christopher Fauletb992af02019-03-28 15:42:24 +0100725static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200726{
Christopher Fauletb992af02019-03-28 15:42:24 +0100727 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200728 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100729 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200730 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200731}
732
Christopher Fauletb992af02019-03-28 15:42:24 +0100733static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
734{
735 if (!conn_is_back(h1s->h1c->conn))
736 h1_set_cli_conn_mode(h1s, h1m);
737 else
738 h1_set_srv_conn_mode(h1s, h1m);
739
740 if (!(h1m->flags & H1_MF_RESP))
741 h1_update_req_conn_value(h1s, h1m, conn_val);
742 else
743 h1_update_res_conn_value(h1s, h1m, conn_val);
744}
Christopher Faulete44769b2018-11-29 23:01:45 +0100745
Christopher Faulet98fbe952019-07-22 16:18:24 +0200746/* Try to adjust the case of the message header name using the global map
747 * <hdrs_map>.
748 */
749static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
750{
751 struct ebpt_node *node;
752 struct h1_hdr_entry *entry;
753
754 /* No entry in the map, do nothing */
755 if (eb_is_empty(&hdrs_map.map))
756 return;
757
758 /* No conversion fo the request headers */
759 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
760 return;
761
762 /* No conversion fo the response headers */
763 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
764 return;
765
766 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
767 if (!node)
768 return;
769 entry = container_of(node, struct h1_hdr_entry, node);
770 name->ptr = entry->name.ptr;
771 name->len = entry->name.len;
772}
773
Christopher Faulete44769b2018-11-29 23:01:45 +0100774/* Append the description of what is present in error snapshot <es> into <out>.
775 * The description must be small enough to always fit in a buffer. The output
776 * buffer may be the trash so the trash must not be used inside this function.
777 */
778static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
779{
780 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100781 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
782 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
783 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
784 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
785 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
786 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100787}
788/*
789 * Capture a bad request or response and archive it in the proxy's structure.
790 * By default it tries to report the error position as h1m->err_pos. However if
791 * this one is not set, it will then report h1m->next, which is the last known
792 * parsing point. The function is able to deal with wrapping buffers. It always
793 * displays buffers as a contiguous area starting at buf->p. The direction is
794 * determined thanks to the h1m's flags.
795 */
796static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
797 struct h1m *h1m, struct buffer *buf)
798{
799 struct session *sess = h1c->conn->owner;
800 struct proxy *proxy = h1c->px;
801 struct proxy *other_end = sess->fe;
802 union error_snapshot_ctx ctx;
803
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100804 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100805 other_end = si_strm(h1s->cs->data)->be;
806
807 /* http-specific part now */
808 ctx.h1.state = h1m->state;
809 ctx.h1.c_flags = h1c->flags;
810 ctx.h1.s_flags = h1s->flags;
811 ctx.h1.m_flags = h1m->flags;
812 ctx.h1.m_clen = h1m->curr_len;
813 ctx.h1.m_blen = h1m->body_len;
814
815 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
816 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100817 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
818 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100819}
820
Christopher Fauletadb22202018-12-12 10:32:09 +0100821/* Emit the chunksize followed by a CRLF in front of data of the buffer
822 * <buf>. It goes backwards and starts with the byte before the buffer's
823 * head. The caller is responsible for ensuring there is enough room left before
824 * the buffer's head for the string.
825 */
826static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
827{
828 char *beg, *end;
829
830 beg = end = b_head(buf);
831 *--beg = '\n';
832 *--beg = '\r';
833 do {
834 *--beg = hextab[chksz & 0xF];
835 } while (chksz >>= 4);
836 buf->head -= (end - beg);
837 b_add(buf, end - beg);
838}
839
840/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
841 * ensuring there is enough room left in the buffer for the string. */
842static void h1_emit_chunk_crlf(struct buffer *buf)
843{
844 *(b_peek(buf, b_data(buf))) = '\r';
845 *(b_peek(buf, b_data(buf) + 1)) = '\n';
846 b_add(buf, 2);
847}
848
Christopher Faulet129817b2018-09-20 16:14:40 +0200849/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100850 * Switch the request to tunnel mode. This function must only be called for
851 * CONNECT requests. On the client side, the mux is mark as busy on input,
852 * waiting the response.
853 */
854static void h1_set_req_tunnel_mode(struct h1s *h1s)
855{
856 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
857 h1s->req.state = H1_MSG_TUNNEL;
858 if (!conn_is_back(h1s->h1c->conn))
859 h1s->h1c->flags |= H1C_F_IN_BUSY;
860}
861
862/*
863 * Switch the response to tunnel mode. This function must only be called on
864 * successfull replies to CONNECT requests or on protocol switching. On the
865 * server side, if the request is not finished, the mux is mark as busy on
866 * input. Otherwise the request is also switch to tunnel mode.
867 */
868static void h1_set_res_tunnel_mode(struct h1s *h1s)
869{
870 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
871 h1s->res.state = H1_MSG_TUNNEL;
872 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
873 h1s->h1c->flags |= H1C_F_IN_BUSY;
874 else {
875 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
876 h1s->req.state = H1_MSG_TUNNEL;
877 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
878 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200879 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100880 }
881 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200882}
883
884/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200885 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200886 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200887 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +0200888 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200889static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200890 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200891{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100892 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +0200893 int ret = 0;
894
Christopher Fauleteec96b52019-09-25 09:10:46 +0200895 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200896 /* Try to match H2 preface before parsing the request headers. */
897 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
898 if (ret > 0)
899 goto h2c_upgrade;
900 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200901 else {
902 if (h1s->meth == HTTP_METH_CONNECT)
903 h1m->flags |= H1_MF_METH_CONNECT;
904 if (h1s->meth == HTTP_METH_HEAD)
905 h1m->flags |= H1_MF_METH_HEAD;
906 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200907
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200908 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
909 if (!ret) {
910 if (htx->flags & HTX_FL_PARSING_ERROR) {
911 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
912 h1s->cs->flags |= CS_FL_EOI;
913 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
914 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200915 goto end;
916 }
917
Christopher Faulet129817b2018-09-20 16:14:40 +0200918 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100919 h1s->meth = h1sl.rq.meth;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200920 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100921 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +0200922 }
923 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100924 h1s->status = h1sl.st.status;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200925 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100926 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100927 }
Christopher Fauletb992af02019-03-28 15:42:24 +0100928 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +0200929 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200930
Christopher Faulet129817b2018-09-20 16:14:40 +0200931 end:
932 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200933
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200934 h2c_upgrade:
935 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +0200936 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200937 htx->flags |= HTX_FL_UPGRADE;
938 ret = 0;
939 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200940}
941
942/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200943 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200944 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
945 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +0200946 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200947static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100948 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +0200949 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +0200950{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200951 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200952
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200953
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200954 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
955 if (ret <= 0) {
956 if (ret < 0) {
957 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
958 h1s->cs->flags |= CS_FL_EOI;
959 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200960 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200961 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +0200962 }
963
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200964 if (h1m->state == H1_MSG_DONE)
Christopher Fauletb75b5ea2019-05-17 08:37:28 +0200965 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200966 *ofs += ret;
967 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200968}
969
970/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200971 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
972 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
973 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
974 * responsible to update the parser state <h1m>.
975 */
976static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
977 struct buffer *buf, size_t *ofs, size_t max)
978{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200979 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200980
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200981 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200982 if (ret <= 0) {
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200983 if (ret < 0) {
984 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
985 h1s->cs->flags |= CS_FL_EOI;
986 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
987 }
988 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200989 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200990 *ofs += ret;
991 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200992 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200993}
994
995/*
996 * Add the EOM in the HTX message and switch the message to the DONE state. It
997 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
998 * functions is responsible to update the parser state <h1m>. It also add the
999 * flag CS_FL_EOI on the CS.
1000 */
1001static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
1002{
1003 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
1004 h1s->flags |= H1S_F_APPEND_EOM;
1005 return 0;
1006 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001007
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001008 h1s->flags &= ~H1S_F_APPEND_EOM;
1009 h1m->state = H1_MSG_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001010 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001011 return (sizeof(struct htx_blk) + 1);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001012}
1013
1014/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001015 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001016 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1017 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001018 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001019static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001020{
Christopher Faulet539e0292018-11-19 10:40:09 +01001021 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001022 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001023 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001024 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001026 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001027
Christopher Faulet539e0292018-11-19 10:40:09 +01001028 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001029
Christopher Fauletf2824e62018-10-01 12:12:37 +02001030 if (!conn_is_back(h1c->conn)) {
1031 h1m = &h1s->req;
1032 errflag = H1S_F_REQ_ERROR;
1033 }
1034 else {
1035 h1m = &h1s->res;
1036 errflag = H1S_F_RES_ERROR;
1037 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001038
Christopher Faulet74027762019-02-26 14:45:05 +01001039 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001040 if (h1s->flags & errflag)
1041 goto end;
1042
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001043 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001044 size_t used = htx_used_space(htx);
1045
Christopher Faulet129817b2018-09-20 16:14:40 +02001046 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001047 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001048 if (!ret)
1049 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001050 if ((h1m->flags & H1_MF_RESP) &&
1051 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1052 h1m_init_res(&h1s->res);
1053 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1054 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001055 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001056 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001057 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001058 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001059 if (!ret)
1060 break;
1061 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001062 else if (h1m->state == H1_MSG_TRAILERS) {
1063 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1064 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1065 if (!ret)
1066 break;
1067 }
Christopher Fauletf1ef7f62019-09-03 21:55:14 +02001068 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001069 break;
1070 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001071 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001072 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1073 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001074 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001075 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001076 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001077 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001078 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001079 if (!ret)
1080 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001081 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001082 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001083 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001084 break;
1085 }
1086
Christopher Faulet30db3d72019-05-17 15:35:33 +02001087 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001088 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001089
Christopher Faulet47365272018-10-31 17:40:50 +01001090 if (h1s->flags & errflag)
1091 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001092
Christopher Faulet539e0292018-11-19 10:40:09 +01001093 b_del(&h1c->ibuf, total);
1094
1095 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001096 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001097 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001098 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001099 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001100 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001101 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001102
Christopher Fauletcf56b992018-12-11 16:12:31 +01001103 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1104
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001105 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001106 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001107 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001108 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001109
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +02001110 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1111 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001112 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001113 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001114 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001115 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001116
Christopher Faulet30db3d72019-05-17 15:35:33 +02001117 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001118
1119 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001120 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001121 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001122 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001123}
1124
Christopher Faulet129817b2018-09-20 16:14:40 +02001125/*
1126 * Process outgoing data. It parses data and transfer them from the channel buffer into
1127 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1128 * 0 if it couldn't proceed.
1129 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001130static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1131{
Christopher Faulet129817b2018-09-20 16:14:40 +02001132 struct h1s *h1s = h1c->h1s;
1133 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001134 struct htx *chn_htx;
1135 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001136 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001137 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001138 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001139
Christopher Faulet47365272018-10-31 17:40:50 +01001140 if (!count)
1141 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001142
Christopher Faulet94b2c762019-05-24 15:28:57 +02001143 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001144 if (htx_is_empty(chn_htx))
1145 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001146
Christopher Faulet51dbc942018-09-13 09:05:15 +02001147 if (!h1_get_buf(h1c, &h1c->obuf)) {
1148 h1c->flags |= H1C_F_OUT_ALLOC;
1149 goto end;
1150 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001151
Christopher Fauletf2824e62018-10-01 12:12:37 +02001152 if (!conn_is_back(h1c->conn)) {
1153 h1m = &h1s->res;
1154 errflag = H1S_F_RES_ERROR;
1155 }
1156 else {
1157 h1m = &h1s->req;
1158 errflag = H1S_F_REQ_ERROR;
1159 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001160
Christopher Faulet0e54d542019-07-04 21:22:34 +02001161 if (h1s->flags & errflag)
1162 goto end;
1163
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001164 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001165 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001166
Willy Tarreau3815b222018-12-11 19:50:43 +01001167 /* Perform some optimizations to reduce the number of buffer copies.
1168 * First, if the mux's buffer is empty and the htx area contains
1169 * exactly one data block of the same size as the requested count,
1170 * then it's possible to simply swap the caller's buffer with the
1171 * mux's output buffer and adjust offsets and length to match the
1172 * entire DATA HTX block in the middle. In this case we perform a
1173 * true zero-copy operation from end-to-end. This is the situation
1174 * that happens all the time with large files. Second, if this is not
1175 * possible, but the mux's output buffer is empty, we still have an
1176 * opportunity to avoid the copy to the intermediary buffer, by making
1177 * the intermediary buffer's area point to the output buffer's area.
1178 * In this case we want to skip the HTX header to make sure that copies
1179 * remain aligned and that this operation remains possible all the
1180 * time. This goes for headers, data blocks and any data extracted from
1181 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001182 */
1183 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001184 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001185 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001186 htx_get_blk_value(chn_htx, blk).len == count) {
1187 void *old_area = h1c->obuf.area;
1188
1189 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001190 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001191 h1c->obuf.data = count;
1192
1193 buf->area = old_area;
1194 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001195
1196 /* The message is chunked. We need to emit the chunk
1197 * size. We have at least the size of the struct htx to
1198 * write the chunk envelope. It should be enough.
1199 */
1200 if (h1m->flags & H1_MF_CHNK) {
1201 h1_emit_chunk_size(&h1c->obuf, count);
1202 h1_emit_chunk_crlf(&h1c->obuf);
1203 }
1204
Willy Tarreau3815b222018-12-11 19:50:43 +01001205 total += count;
1206 goto out;
1207 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001208 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001209 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001210 else
1211 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001212
Christopher Fauletc2518a52019-06-25 21:41:02 +02001213 tmp.data = 0;
1214 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001215 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001216 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001217 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001218 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001219 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001220 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001221
Christopher Fauletb2e84162018-12-06 11:39:49 +01001222 vlen = sz;
1223 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001224 if (type != HTX_BLK_DATA)
Christopher Fauletb2e84162018-12-06 11:39:49 +01001225 goto copy;
1226 vlen = count;
1227 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001228
Christopher Faulet94b2c762019-05-24 15:28:57 +02001229 if (type == HTX_BLK_UNUSED)
1230 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001231
Christopher Faulet94b2c762019-05-24 15:28:57 +02001232 switch (h1m->state) {
1233 case H1_MSG_RQBEFORE:
1234 if (type != HTX_BLK_REQ_SL)
1235 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001237 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001238 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001239 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 goto copy;
1241 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001242 if (sl->flags & HTX_SL_F_BODYLESS)
1243 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001244 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001245 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001246
Christopher Faulet94b2c762019-05-24 15:28:57 +02001247 case H1_MSG_RPBEFORE:
1248 if (type != HTX_BLK_RES_SL)
1249 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001250 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001251 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001252 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001253 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001254 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001255 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001256 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001257 if (sl->info.res.status < 200 &&
1258 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001259 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 h1m->state = H1_MSG_HDR_FIRST;
1261 break;
1262
Christopher Faulet94b2c762019-05-24 15:28:57 +02001263 case H1_MSG_HDR_FIRST:
1264 case H1_MSG_HDR_NAME:
1265 case H1_MSG_HDR_L2_LWS:
1266 if (type == HTX_BLK_EOH)
1267 goto last_lf;
1268 if (type != HTX_BLK_HDR)
1269 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001270 h1m->state = H1_MSG_HDR_NAME;
1271 n = htx_get_blk_name(chn_htx, blk);
1272 v = htx_get_blk_value(chn_htx, blk);
1273
Christopher Faulet86d144c2019-08-14 16:32:25 +02001274 /* Skip all pseudo-headers */
1275 if (*(n.ptr) == ':')
1276 goto skip_hdr;
1277
Christopher Faulet9768c262018-10-22 09:34:31 +02001278 if (isteqi(n, ist("transfer-encoding")))
1279 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001280 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001281 /* Only skip C-L header with invalid value. */
1282 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001283 goto skip_hdr;
1284 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001285 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001286 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001287 if (!v.len)
1288 goto skip_hdr;
1289 }
1290
Christopher Faulet98fbe952019-07-22 16:18:24 +02001291 /* Try to adjust the case of the header name */
1292 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1293 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001294 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001295 goto copy;
1296 skip_hdr:
1297 h1m->state = H1_MSG_HDR_L2_LWS;
1298 break;
1299
Christopher Faulet94b2c762019-05-24 15:28:57 +02001300 case H1_MSG_LAST_LF:
1301 if (type != HTX_BLK_EOH)
1302 goto error;
1303 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001304 h1m->state = H1_MSG_LAST_LF;
1305 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001306 /* There is no "Connection:" header and
1307 * it the conn_mode must be
1308 * processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001309 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001310 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001311 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001312 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001313 /* Try to adjust the case of the header name */
1314 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1315 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001316 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001317 goto copy;
1318 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001319 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001320 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001321
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001322 if ((h1s->meth != HTTP_METH_CONNECT &&
1323 (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 +01001324 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1325 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1326 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1327 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1328 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001329 /* chunking needed but header not seen */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001330 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Willy Tarreau4710d202019-01-03 17:39:54 +01001331 goto copy;
1332 h1m->flags |= H1_MF_CHNK;
1333 }
1334
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001335 /* Now add the server name to a header (if requested) */
1336 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1337 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1338 struct server *srv = objt_server(h1c->conn->target);
1339
1340 if (srv) {
1341 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1342 v = ist(srv->id);
1343 if (!htx_hdr_to_h1(n, v, &tmp))
1344 goto copy;
1345 }
1346 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1347 }
1348
Christopher Fauletc2518a52019-06-25 21:41:02 +02001349 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet9768c262018-10-22 09:34:31 +02001350 goto copy;
1351
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001352 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1353 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1354 h1_set_req_tunnel_mode(h1s);
1355 }
1356 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1357 /* a successfull reply to a CONNECT or a protocol switching is sent
1358 * to the client . Switch the response to tunnel mode. */
1359 h1_set_res_tunnel_mode(h1s);
1360 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001361 else if ((h1m->flags & H1_MF_RESP) &&
1362 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1363 h1m_init_res(&h1s->res);
1364 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001365 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001366 }
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001367 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1368 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001369 else
1370 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001371 break;
1372
Christopher Faulet94b2c762019-05-24 15:28:57 +02001373 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001374 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001375 if (type == HTX_BLK_EOM) {
1376 /* Chunked message without explicit trailers */
1377 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001378 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001379 goto copy;
1380 }
1381 goto done;
1382 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001383 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001384 /* If the message is not chunked, never
1385 * add the last chunk. */
1386 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet94b2c762019-05-24 15:28:57 +02001387 goto copy;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001388 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001389 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001390 else if (type != HTX_BLK_DATA)
1391 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001392 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001393 v.len = vlen;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001394 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001395 goto copy;
1396 break;
1397
Christopher Faulet94b2c762019-05-24 15:28:57 +02001398 case H1_MSG_TRAILERS:
1399 if (type == HTX_BLK_EOM)
1400 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001401 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001402 goto error;
1403 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001404 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001405 /* If the message is not chunked, ignore
1406 * trailers. It may happen with H2 messages. */
1407 if (!(h1m->flags & H1_MF_CHNK))
1408 break;
1409
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001410 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001411 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet32188212018-11-20 18:21:43 +01001412 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001413 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001414 else { // HTX_BLK_TLR
1415 n = htx_get_blk_name(chn_htx, blk);
1416 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001417
1418 /* Try to adjust the case of the header name */
1419 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1420 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001421 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001422 goto copy;
1423 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001424 break;
1425
Christopher Faulet94b2c762019-05-24 15:28:57 +02001426 case H1_MSG_DONE:
1427 if (type != HTX_BLK_EOM)
1428 goto error;
1429 done:
1430 h1m->state = H1_MSG_DONE;
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001431 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1432 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1433 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1434 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001435 break;
1436
Christopher Faulet9768c262018-10-22 09:34:31 +02001437 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001438 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001439 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001440 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001441 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001442 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001443 break;
1444 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001445
1446 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001447 total += vlen;
1448 count -= vlen;
1449 if (sz == vlen)
1450 blk = htx_remove_blk(chn_htx, blk);
1451 else {
1452 htx_cut_data_blk(chn_htx, blk, vlen);
1453 break;
1454 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001455 }
1456
Christopher Faulet9768c262018-10-22 09:34:31 +02001457 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001458 /* when the output buffer is empty, tmp shares the same area so that we
1459 * only have to update pointers and lengths.
1460 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001461 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1462 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001463 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001464 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001465
Willy Tarreau3815b222018-12-11 19:50:43 +01001466 htx_to_buf(chn_htx, buf);
1467 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001468 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001469 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001470 end:
1471 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001472}
1473
Christopher Faulet51dbc942018-09-13 09:05:15 +02001474/*********************************************************/
1475/* functions below are I/O callbacks from the connection */
1476/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001477static void h1_wake_stream_for_recv(struct h1s *h1s)
1478{
1479 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001480 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001481 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001482 h1s->recv_wait = NULL;
1483 }
1484}
1485static void h1_wake_stream_for_send(struct h1s *h1s)
1486{
1487 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001488 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001489 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001490 h1s->send_wait = NULL;
1491 }
1492}
1493
Christopher Faulet51dbc942018-09-13 09:05:15 +02001494/*
1495 * Attempt to read data, and subscribe if none available
1496 */
1497static int h1_recv(struct h1c *h1c)
1498{
1499 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001500 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001501 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001502 int rcvd = 0;
1503
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001504 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001505 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001506
Olivier Houchard75159a92018-12-03 18:46:09 +01001507 if (!h1_recv_allowed(h1c)) {
1508 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001509 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001510 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001511
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001512 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1513 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001514 goto end;
1515 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001516
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001517 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001518 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001519 h1_wake_stream_for_recv(h1s);
1520 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001521 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001522 }
1523
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001524 /*
1525 * If we only have a small amount of data, realign it,
1526 * it's probably cheaper than doing 2 recv() calls.
1527 */
1528 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1529 b_slow_realign(&h1c->ibuf, trash.area, 0);
1530
Willy Tarreau45f2b892018-12-05 07:59:27 +01001531 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001532 if (max) {
1533 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001534
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001535 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001536 if (!b_data(&h1c->ibuf)) {
1537 /* try to pre-align the buffer like the rxbufs will be
1538 * to optimize memory copies.
1539 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001540 h1c->ibuf.head = sizeof(struct htx);
1541 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001542 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001543 }
Christopher Faulet47365272018-10-31 17:40:50 +01001544 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001545 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001546 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001547 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001548 if (h1s->csinfo.t_idle == -1)
1549 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1550 }
Christopher Faulet47365272018-10-31 17:40:50 +01001551 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001552
Olivier Houchardcc3fec82019-07-26 15:11:11 +02001553 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001554 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001555 goto end;
1556 }
1557
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001558 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001559
Christopher Faulet9768c262018-10-22 09:34:31 +02001560 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001561 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1562 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001563
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001564 if (conn_xprt_read0_pending(conn) && h1s) {
1565 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001566 rcvd = 1;
1567 }
1568
Christopher Faulet51dbc942018-09-13 09:05:15 +02001569 if (!b_data(&h1c->ibuf))
1570 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001571 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001572 h1c->flags |= H1C_F_IN_FULL;
1573 return rcvd;
1574}
1575
1576
1577/*
1578 * Try to send data if possible
1579 */
1580static int h1_send(struct h1c *h1c)
1581{
1582 struct connection *conn = h1c->conn;
1583 unsigned int flags = 0;
1584 size_t ret;
1585 int sent = 0;
1586
1587 if (conn->flags & CO_FL_ERROR)
1588 return 0;
1589
1590 if (!b_data(&h1c->obuf))
1591 goto end;
1592
1593 if (h1c->flags & H1C_F_OUT_FULL)
1594 flags |= CO_SFL_MSG_MORE;
1595
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001596 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001597 if (ret > 0) {
1598 h1c->flags &= ~H1C_F_OUT_FULL;
1599 b_del(&h1c->obuf, ret);
1600 sent = 1;
1601 }
1602
Christopher Faulet145aa472018-12-06 10:56:20 +01001603 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1604 /* error or output closed, nothing to send, clear the buffer to release it */
1605 b_reset(&h1c->obuf);
1606 }
1607
Christopher Faulet51dbc942018-09-13 09:05:15 +02001608 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001609 if (!(h1c->flags & H1C_F_OUT_FULL))
1610 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001611
Christopher Faulet51dbc942018-09-13 09:05:15 +02001612 /* We're done, no more to send */
1613 if (!b_data(&h1c->obuf)) {
1614 h1_release_buf(h1c, &h1c->obuf);
1615 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001616 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001617 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001618 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001619 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001620
1621 return sent;
1622}
1623
Christopher Faulet51dbc942018-09-13 09:05:15 +02001624
1625/* callback called on any event by the connection handler.
1626 * It applies changes and returns zero, or < 0 if it wants immediate
1627 * destruction of the connection.
1628 */
1629static int h1_process(struct h1c * h1c)
1630{
1631 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001632 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001633
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001634 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001635 return -1;
1636
Christopher Fauletfeb11742018-11-29 15:12:34 +01001637 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02001638 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001639 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001640 conn_xprt_read0_pending(conn))
1641 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001642 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001643 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001644 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001645 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001646 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001647 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001648 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001649 }
1650
Christopher Fauletfeb11742018-11-29 15:12:34 +01001651 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1652 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1653
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001654 if (conn_xprt_read0_pending(conn))
1655 h1s->flags |= H1S_F_REOS;
1656
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001657 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001658 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001659 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001660 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001661 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01001662 h1s->cs->data_cb->wake(h1s->cs);
1663 }
Christopher Faulet47365272018-10-31 17:40:50 +01001664 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001665 if (h1c->task) {
1666 h1c->task->expire = TICK_ETERNITY;
1667 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001668 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 +01001669 ? h1c->shut_timeout
1670 : h1c->timeout));
1671 task_queue(h1c->task);
1672 }
1673 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001674 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001675
1676 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02001677 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01001678 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001679}
1680
1681static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1682{
1683 struct h1c *h1c = ctx;
1684 int ret = 0;
1685
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001686 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001687 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001688 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001689 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001690 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001691 h1_process(h1c);
1692 return NULL;
1693}
1694
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001695static void h1_reset(struct connection *conn)
1696{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001697
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001698}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001699
1700static int h1_wake(struct connection *conn)
1701{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001702 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001703 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001704
Christopher Faulet539e0292018-11-19 10:40:09 +01001705 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001706 ret = h1_process(h1c);
1707 if (ret == 0) {
1708 struct h1s *h1s = h1c->h1s;
1709
1710 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1711 ret = h1s->cs->data_cb->wake(h1s->cs);
1712 }
1713 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001714}
1715
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001716/* Connection timeout management. The principle is that if there's no receipt
1717 * nor sending for a certain amount of time, the connection is closed.
1718 */
1719static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
1720{
1721 struct h1c *h1c = context;
1722 int expired = tick_is_expired(t->expire, now_ms);
1723
1724 if (!expired && h1c)
1725 return t;
1726
Olivier Houchard3f795f72019-04-17 22:51:06 +02001727 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001728
1729 if (!h1c) {
1730 /* resources were already deleted */
1731 return NULL;
1732 }
1733
1734 h1c->task = NULL;
1735 /* If a stream is still attached to the mux, just set an error and wait
1736 * for the stream's timeout. Otherwise, release the mux. This is only ok
1737 * because same timeouts are used.
1738 */
1739 if (h1c->h1s && h1c->h1s->cs)
1740 h1c->flags |= H1C_F_CS_ERROR;
1741 else
Christopher Faulet73c12072019-04-08 11:23:22 +02001742 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001743 return NULL;
1744}
1745
Christopher Faulet51dbc942018-09-13 09:05:15 +02001746/*******************************************/
1747/* functions below are used by the streams */
1748/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02001749
Christopher Faulet51dbc942018-09-13 09:05:15 +02001750/*
1751 * Attach a new stream to a connection
1752 * (Used for outgoing connections)
1753 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001754static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001755{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001756 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001757 struct conn_stream *cs = NULL;
1758 struct h1s *h1s;
1759
1760 if (h1c->flags & H1C_F_CS_ERROR)
1761 goto end;
1762
1763 cs = cs_new(h1c->conn);
1764 if (!cs)
1765 goto end;
1766
Olivier Houchardf502aca2018-12-14 19:42:40 +01001767 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001768 if (h1s == NULL)
1769 goto end;
1770
1771 return cs;
1772 end:
1773 cs_free(cs);
1774 return NULL;
1775}
1776
1777/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1778 * this mux, it's easy as we can only store a single conn_stream.
1779 */
1780static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1781{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001782 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001783 struct h1s *h1s = h1c->h1s;
1784
1785 if (h1s)
1786 return h1s->cs;
1787
1788 return NULL;
1789}
1790
Christopher Faulet73c12072019-04-08 11:23:22 +02001791static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001792{
Christopher Faulet73c12072019-04-08 11:23:22 +02001793 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001794
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001795 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02001796 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001797}
1798
1799/*
1800 * Detach the stream from the connection and possibly release the connection.
1801 */
1802static void h1_detach(struct conn_stream *cs)
1803{
1804 struct h1s *h1s = cs->ctx;
1805 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001806 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01001807 int has_keepalive;
1808 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001809
1810 cs->ctx = NULL;
1811 if (!h1s)
1812 return;
1813
Olivier Houchardf502aca2018-12-14 19:42:40 +01001814 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001815 h1c = h1s->h1c;
1816 h1s->cs = NULL;
1817
Olivier Houchard8a786902018-12-15 16:05:40 +01001818 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
1819 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
1820 h1s_destroy(h1s);
1821
1822 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01001823 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02001824 /* If there are any excess server data in the input buffer,
1825 * release it and close the connection ASAP (some data may
1826 * remain in the output buffer). This happens if a server sends
1827 * invalid responses. So in such case, we don't want to reuse
1828 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02001829 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02001830 if (b_data(&h1c->ibuf)) {
1831 h1_release_buf(h1c, &h1c->ibuf);
1832 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1833 goto release;
1834 }
Christopher Faulet03627242019-07-19 11:34:08 +02001835
Christopher Faulet9400a392018-11-23 23:10:39 +01001836 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01001837 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01001838 h1c->conn->flags |= CO_FL_PRIVATE;
1839
Olivier Houchard44d59142018-12-13 18:46:22 +01001840 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001841 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01001842 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
1843 h1c->conn->owner = NULL;
1844 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
1845 /* The server doesn't want it, let's kill the connection right away */
1846 h1c->conn->mux->destroy(h1c->conn);
1847 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001848 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01001849 return;
1850
1851 }
Olivier Houchard44d59142018-12-13 18:46:22 +01001852 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01001853 if (h1c->conn->owner == sess) {
1854 int ret = session_check_idle_conn(sess, h1c->conn);
1855 if (ret == -1)
1856 /* The connection got destroyed, let's leave */
1857 return;
1858 else if (ret == 1) {
1859 /* The connection was added to the server list,
1860 * wake the task so we can subscribe to events
1861 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001862 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01001863 return;
1864 }
1865 }
Christopher Faulet9400a392018-11-23 23:10:39 +01001866 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01001867 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001868 struct server *srv = objt_server(h1c->conn->target);
1869
1870 if (srv) {
1871 if (h1c->conn->flags & CO_FL_PRIVATE)
1872 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01001873 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01001874 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1875 else
1876 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1877 }
1878 }
1879 }
1880
Christopher Fauletf1204b82019-07-19 14:51:06 +02001881 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02001882 /* 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 +02001883 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
1884 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
1885 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
1886 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02001887 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001888 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001889 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001890 if (h1c->task) {
1891 h1c->task->expire = TICK_ETERNITY;
1892 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001893 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 +01001894 ? h1c->shut_timeout
1895 : h1c->timeout));
1896 task_queue(h1c->task);
1897 }
1898 }
1899 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001900}
1901
1902
1903static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1904{
1905 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02001906 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001907
1908 if (!h1s)
1909 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02001910 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001911
Christopher Faulet7f366362019-04-08 10:51:20 +02001912 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
1913 goto do_shutr;
1914
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001915 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001916 return;
1917
Christopher Faulet7f366362019-04-08 10:51:20 +02001918 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001919 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1920 if (cs->flags & CS_FL_SHR)
1921 return;
1922 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001923 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
1924 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01001925 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 +02001926 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001927}
1928
1929static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1930{
1931 struct h1s *h1s = cs->ctx;
1932 struct h1c *h1c;
1933
1934 if (!h1s)
1935 return;
1936 h1c = h1s->h1c;
1937
Christopher Faulet7f366362019-04-08 10:51:20 +02001938 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
1939 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01001940
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001941 if ((h1c->flags & H1C_F_UPG_H2C) ||
1942 ((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 +02001943 return;
1944
Christopher Faulet7f366362019-04-08 10:51:20 +02001945 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001946 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1947 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1948 return;
1949
Christopher Faulet666a0c42019-01-08 11:12:04 +01001950 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001951}
1952
Christopher Faulet666a0c42019-01-08 11:12:04 +01001953static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001954{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001955 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001956
Christopher Faulet666a0c42019-01-08 11:12:04 +01001957 conn_xprt_shutw(conn);
1958 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
1959 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
1960 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001961}
1962
1963/* Called from the upper layer, to unsubscribe to events */
1964static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1965{
1966 struct wait_event *sw;
1967 struct h1s *h1s = cs->ctx;
1968
1969 if (!h1s)
1970 return 0;
1971
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001972 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001973 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02001974 BUG_ON(h1s->recv_wait != sw);
1975 sw->events &= ~SUB_RETRY_RECV;
1976 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001977 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001978 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001979 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02001980 BUG_ON(h1s->send_wait != sw);
1981 sw->events &= ~SUB_RETRY_SEND;
1982 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001983 }
1984 return 0;
1985}
1986
1987/* Called from the upper layer, to subscribe to events, such as being able to send */
1988static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1989{
1990 struct wait_event *sw;
1991 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02001992 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001993
1994 if (!h1s)
1995 return -1;
1996
1997 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001998 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001999 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002000 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2001 sw->events |= SUB_RETRY_RECV;
2002 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002003 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002004 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002005 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002006 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2007 sw->events |= SUB_RETRY_SEND;
2008 h1s->send_wait = sw;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002009 /*
2010 * If the conn_stream attempt to subscribe, and the
2011 * mux isn't subscribed to the connection, then it
2012 * probably means the connection wasn't established
2013 * yet, so we have to subscribe.
2014 */
Christopher Faulet51bb1852019-09-04 10:22:34 +02002015 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002016 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2017 h1c->conn->xprt->subscribe(h1c->conn,
2018 h1c->conn->xprt_ctx,
2019 SUB_RETRY_SEND,
2020 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002021 return 0;
2022 default:
2023 break;
2024 }
2025 return -1;
2026}
2027
2028/* Called from the upper layer, to receive data */
2029static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2030{
2031 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002032 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002033 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002034 size_t ret = 0;
2035
Christopher Faulet539e0292018-11-19 10:40:09 +01002036 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002037 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002038
Christopher Faulete18777b2019-04-16 16:46:36 +02002039 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002040
2041 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2042 h1s->flags |= H1S_F_BUF_FLUSH;
2043 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002044 else {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002045 h1s->flags &= ~H1S_F_SPLICED_DATA;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002046 if (h1m->state != H1_MSG_DONE &&
2047 !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002048 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002049 }
2050 return ret;
2051}
2052
2053
2054/* Called from the upper layer, to send data */
2055static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2056{
2057 struct h1s *h1s = cs->ctx;
2058 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002059 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002060
2061 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002062 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002063
2064 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002065
2066 /* If we're not connected yet, or we're waiting for a handshake, stop
2067 * now, as we don't want to remove everything from the channel buffer
2068 * before we're sure we can send it.
2069 */
2070 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
2071 (h1c->conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002072 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002073
Christopher Fauletc31872f2019-06-04 22:09:36 +02002074 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002075 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002076
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002077 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2078 ret = h1_process_output(h1c, buf, count);
2079 if (!ret)
2080 break;
2081 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002082 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002083 if (!h1_send(h1c))
2084 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002085 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002086
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002087 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002088}
2089
Willy Tarreaue5733232019-05-22 19:24:06 +02002090#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002091/* Send and get, using splicing */
2092static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2093{
2094 struct h1s *h1s = cs->ctx;
2095 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2096 int ret = 0;
2097
Christopher Faulet1146f972019-05-29 14:35:24 +02002098 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002099 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002100 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2101 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002102 goto end;
2103 }
2104
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002105 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002106 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002107 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002108 }
2109
2110 h1s->flags &= ~H1S_F_BUF_FLUSH;
2111 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002112 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2113 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002114 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002115 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002116 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002117 if (!h1m->curr_len)
2118 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2119 }
2120
Christopher Faulet1be55f92018-10-02 15:59:23 +02002121 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002122 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002123 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002124 if (!pipe->data)
2125 cs->flags |= CS_FL_EOS;
2126 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002127 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002128}
2129
2130static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2131{
2132 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002133 int ret = 0;
2134
2135 if (b_data(&h1s->h1c->obuf))
2136 goto end;
2137
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002138 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002139 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002140 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002141 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002142 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002143 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002144 return ret;
2145}
2146#endif
2147
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002148/* for debugging with CLI's "show fd" command */
2149static void h1_show_fd(struct buffer *msg, struct connection *conn)
2150{
2151 struct h1c *h1c = conn->ctx;
2152 struct h1s *h1s = h1c->h1s;
2153
Christopher Fauletf376a312019-01-04 15:16:06 +01002154 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2155 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002156 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2157 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2158 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2159 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2160
2161 if (h1s) {
2162 char *method;
2163
2164 if (h1s->meth < HTTP_METH_OTHER)
2165 method = http_known_methods[h1s->meth].ptr;
2166 else
2167 method = "UNKNOWN";
2168 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2169 " .meth=%s status=%d",
2170 h1s, h1s->flags,
2171 h1m_state_str(h1s->req.state),
2172 h1m_state_str(h1s->res.state), method, h1s->status);
2173 if (h1s->cs)
2174 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2175 h1s->cs->flags, h1s->cs->data);
2176 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002177}
2178
2179
2180/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2181static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2182{
2183 struct h1_hdr_entry *entry;
2184
2185 /* Be sure there is a non-empty <to> */
2186 if (!strlen(to)) {
2187 memprintf(err, "expect <to>");
2188 return -1;
2189 }
2190
2191 /* Be sure only the case differs between <from> and <to> */
2192 if (strcasecmp(from, to)) {
2193 memprintf(err, "<from> and <to> must not differ execpt the case");
2194 return -1;
2195 }
2196
2197 /* Be sure <from> does not already existsin the tree */
2198 if (ebis_lookup(&hdrs_map.map, from)) {
2199 memprintf(err, "duplicate entry '%s'", from);
2200 return -1;
2201 }
2202
2203 /* Create the entry and insert it in the tree */
2204 entry = malloc(sizeof(*entry));
2205 if (!entry) {
2206 memprintf(err, "out of memory");
2207 return -1;
2208 }
2209
2210 entry->node.key = strdup(from);
2211 entry->name.ptr = strdup(to);
2212 entry->name.len = strlen(to);
2213 if (!entry->node.key || !entry->name.ptr) {
2214 free(entry->node.key);
2215 free(entry->name.ptr);
2216 free(entry);
2217 memprintf(err, "out of memory");
2218 return -1;
2219 }
2220 ebis_insert(&hdrs_map.map, &entry->node);
2221 return 0;
2222}
2223
2224static void h1_hdeaders_case_adjust_deinit()
2225{
2226 struct ebpt_node *node, *next;
2227 struct h1_hdr_entry *entry;
2228
2229 node = ebpt_first(&hdrs_map.map);
2230 while (node) {
2231 next = ebpt_next(node);
2232 ebpt_delete(node);
2233 entry = container_of(node, struct h1_hdr_entry, node);
2234 free(entry->node.key);
2235 free(entry->name.ptr);
2236 free(entry);
2237 node = next;
2238 }
2239 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002240}
2241
Christopher Faulet98fbe952019-07-22 16:18:24 +02002242static int cfg_h1_headers_case_adjust_postparser()
2243{
2244 FILE *file = NULL;
2245 char *c, *key_beg, *key_end, *value_beg, *value_end;
2246 char *err;
2247 int rc, line = 0, err_code = 0;
2248
2249 if (!hdrs_map.name)
2250 goto end;
2251
2252 file = fopen(hdrs_map.name, "r");
2253 if (!file) {
2254 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2255 hdrs_map.name);
2256 err_code |= ERR_ALERT | ERR_FATAL;
2257 goto end;
2258 }
2259
2260 /* now parse all lines. The file may contain only two header name per
2261 * line, separated by spaces. All heading and trailing spaces will be
2262 * ignored. Lines starting with a # are ignored.
2263 */
2264 while (fgets(trash.area, trash.size, file) != NULL) {
2265 line++;
2266 c = trash.area;
2267
2268 /* strip leading spaces and tabs */
2269 while (*c == ' ' || *c == '\t')
2270 c++;
2271
2272 /* ignore emptu lines, or lines beginning with a dash */
2273 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2274 continue;
2275
2276 /* look for the end of the key */
2277 key_beg = c;
2278 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2279 c++;
2280 key_end = c;
2281
2282 /* strip middle spaces and tabs */
2283 while (*c == ' ' || *c == '\t')
2284 c++;
2285
2286 /* look for the end of the value, it is the end of the line */
2287 value_beg = c;
2288 while (*c && *c != '\n' && *c != '\r')
2289 c++;
2290 value_end = c;
2291
2292 /* trim possibly trailing spaces and tabs */
2293 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2294 value_end--;
2295
2296 /* set final \0 and check entries */
2297 *key_end = '\0';
2298 *value_end = '\0';
2299
2300 err = NULL;
2301 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2302 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002303 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2304 hdrs_map.name, err, line);
2305 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002306 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002307 goto end;
2308 }
2309 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002310 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2311 hdrs_map.name, err, line);
2312 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02002313 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002314 }
2315 }
2316
2317 end:
2318 if (file)
2319 fclose(file);
2320 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2321 return err_code;
2322}
2323
2324
2325/* config parser for global "h1-outgoing-header-case-adjust" */
2326static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2327 struct proxy *defpx, const char *file, int line,
2328 char **err)
2329{
2330 if (too_many_args(2, args, err, NULL))
2331 return -1;
2332 if (!*(args[1]) || !*(args[2])) {
2333 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2334 return -1;
2335 }
2336 return add_hdr_case_adjust(args[1], args[2], err);
2337}
2338
2339/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2340static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2341 struct proxy *defpx, const char *file, int line,
2342 char **err)
2343{
2344 if (too_many_args(1, args, err, NULL))
2345 return -1;
2346 if (!*(args[1])) {
2347 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2348 return -1;
2349 }
2350 free(hdrs_map.name);
2351 hdrs_map.name = strdup(args[1]);
2352 return 0;
2353}
2354
2355
2356/* config keyword parsers */
2357static struct cfg_kw_list cfg_kws = {{ }, {
2358 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2359 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2360 { 0, NULL, NULL },
2361 }
2362};
2363
2364INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2365REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2366
2367
Christopher Faulet51dbc942018-09-13 09:05:15 +02002368/****************************************/
2369/* MUX initialization and instanciation */
2370/****************************************/
2371
2372/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002373static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002374 .init = h1_init,
2375 .wake = h1_wake,
2376 .attach = h1_attach,
2377 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002378 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002379 .detach = h1_detach,
2380 .destroy = h1_destroy,
2381 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002382 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002383 .rcv_buf = h1_rcv_buf,
2384 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002385#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002386 .rcv_pipe = h1_rcv_pipe,
2387 .snd_pipe = h1_snd_pipe,
2388#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002389 .subscribe = h1_subscribe,
2390 .unsubscribe = h1_unsubscribe,
2391 .shutr = h1_shutr,
2392 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002393 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002394 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002395 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002396 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002397};
2398
2399
2400/* this mux registers default HTX proto */
2401static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02002402{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02002403
Willy Tarreau0108d902018-11-25 19:14:37 +01002404INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2405
Christopher Faulet51dbc942018-09-13 09:05:15 +02002406/*
2407 * Local variables:
2408 * c-indent-level: 8
2409 * c-basic-offset: 8
2410 * End:
2411 */