blob: e6383305b027ba967eac502dd54dc289ec4842a9 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <common/cfgparse.h>
13#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010014#include <common/h1.h>
Christopher Faulet0ef372a2019-04-08 10:57:20 +020015#include <common/h2.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010017#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020018
Christopher Faulet98fbe952019-07-22 16:18:24 +020019#include <ebistree.h>
20
Christopher Faulet1be55f92018-10-02 15:59:23 +020021#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020022#include <types/proxy.h>
23#include <types/session.h>
24
Christopher Faulet51dbc942018-09-13 09:05:15 +020025#include <proto/connection.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020026#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020027#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010028#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020029#include <proto/stream.h>
30#include <proto/stream_interface.h>
31
32/*
33 * H1 Connection flags (32 bits)
34 */
35#define H1C_F_NONE 0x00000000
36
37/* Flags indicating why writing output data are blocked */
38#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
39#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
40/* 0x00000004 - 0x00000008 unused */
41
42/* Flags indicating why reading input data are blocked. */
43#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
44#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010045#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010046/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020047
48#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
49#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010050#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Faulet51dbc942018-09-13 09:05:15 +020051
Christopher Fauletf2824e62018-10-01 12:12:37 +020052#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020053#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020054
Christopher Faulet51dbc942018-09-13 09:05:15 +020055/*
56 * H1 Stream flags (32 bits)
57 */
Christopher Faulet129817b2018-09-20 16:14:40 +020058#define H1S_F_NONE 0x00000000
59#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020060#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
61#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020062#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020063#define H1S_F_WANT_KAL 0x00000010
64#define H1S_F_WANT_TUN 0x00000020
65#define H1S_F_WANT_CLO 0x00000040
66#define H1S_F_WANT_MSK 0x00000070
67#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010068#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010069#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010070#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +020071#define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */
72/* 0x00002000 .. 0x00002000 unused */
Christopher Fauletada34b62019-05-24 16:36:43 +020073#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020074
75/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020076struct h1c {
77 struct connection *conn;
78 struct proxy *px;
79 uint32_t flags; /* Connection flags: H1C_F_* */
80
81 struct buffer ibuf; /* Input buffer to store data before parsing */
82 struct buffer obuf; /* Output buffer to store data after reformatting */
83
84 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
85 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
86
87 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010088 struct task *task; /* timeout management task */
89 int timeout; /* idle timeout duration in ticks */
90 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020091};
92
93/* H1 stream descriptor */
94struct h1s {
95 struct h1c *h1c;
96 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010097 struct cs_info csinfo; /* CS info, only used for client connections */
98 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020099
Christopher Faulet51dbc942018-09-13 09:05:15 +0200100 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
101 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200102
Olivier Houchardf502aca2018-12-14 19:42:40 +0100103 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200104 struct h1m req;
105 struct h1m res;
106
107 enum http_meth_t meth; /* HTTP resquest method */
108 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200109};
110
Christopher Faulet98fbe952019-07-22 16:18:24 +0200111/* Map of headers used to convert outgoing headers */
112struct h1_hdrs_map {
113 char *name;
114 struct eb_root map;
115};
116
117/* An entry in a headers map */
118struct h1_hdr_entry {
119 struct ist name;
120 struct ebpt_node node;
121};
122
123/* Declare the headers map */
124static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
125
126
Christopher Faulet51dbc942018-09-13 09:05:15 +0200127/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100128DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
129DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200130
Christopher Faulet51dbc942018-09-13 09:05:15 +0200131static int h1_recv(struct h1c *h1c);
132static int h1_send(struct h1c *h1c);
133static int h1_process(struct h1c *h1c);
134static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100135static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100136static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200137
138/*****************************************************/
139/* functions below are for dynamic buffer management */
140/*****************************************************/
141/*
142 * Indicates whether or not the we may call the h1_recv() function to
143 * attempt to receive data into the buffer and/or parse pending data. The
144 * condition is a bit complex due to some API limits for now. The rules are the
145 * following :
146 * - if an error or a shutdown was detected on the connection and the buffer
147 * is empty, we must not attempt to receive
148 * - if the input buffer failed to be allocated, we must not try to receive
149 * and we know there is nothing pending
150 * - if no flag indicates a blocking condition, we may attempt to receive,
151 * regardless of whether the input buffer is full or not, so that only de
152 * receiving part decides whether or not to block. This is needed because
153 * the connection API indeed prevents us from re-enabling receipt that is
154 * already enabled in a polled state, so we must always immediately stop as
155 * soon as the mux can't proceed so as never to hit an end of read with data
156 * pending in the buffers.
157 * - otherwise must may not attempt to receive
158 */
159static inline int h1_recv_allowed(const struct h1c *h1c)
160{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100161 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200162 return 0;
163
Christopher Fauletcf56b992018-12-11 16:12:31 +0100164 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
165 return 0;
166
Christopher Fauletcb55f482018-12-10 11:56:47 +0100167 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200168 return 1;
169
170 return 0;
171}
172
173/*
174 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
175 * flags are used to figure what buffer was requested. It returns 1 if the
176 * allocation succeeds, in which case the connection is woken up, or 0 if it's
177 * impossible to wake up and we prefer to be woken up later.
178 */
179static int h1_buf_available(void *target)
180{
181 struct h1c *h1c = target;
182
183 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
184 h1c->flags &= ~H1C_F_IN_ALLOC;
185 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200186 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200187 return 1;
188 }
189
190 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
191 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200192 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200193 return 1;
194 }
195
Christopher Faulet51dbc942018-09-13 09:05:15 +0200196 return 0;
197}
198
199/*
200 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
201 */
202static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
203{
204 struct buffer *buf = NULL;
205
206 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
207 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
208 h1c->buf_wait.target = h1c;
209 h1c->buf_wait.wakeup_cb = h1_buf_available;
210 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
211 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
212 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
213 __conn_xprt_stop_recv(h1c->conn);
214 }
215 return buf;
216}
217
218/*
219 * Release a buffer, if any, and try to wake up entities waiting in the buffer
220 * wait queue.
221 */
222static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
223{
224 if (bptr->size) {
225 b_free(bptr);
226 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
227 }
228}
229
Willy Tarreau00f18a32019-01-26 12:19:01 +0100230/* returns the number of streams in use on a connection to figure if it's
231 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
232 * as the caller will want to know if it was the last one after a detach().
233 */
234static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200235{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100236 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200237
Willy Tarreau00f18a32019-01-26 12:19:01 +0100238 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200239}
240
Willy Tarreau00f18a32019-01-26 12:19:01 +0100241/* returns the number of streams still available on a connection */
242static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100243{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100244 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100245}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200246
Willy Tarreau00f18a32019-01-26 12:19:01 +0100247
Christopher Faulet51dbc942018-09-13 09:05:15 +0200248/*****************************************************************/
249/* functions below are dedicated to the mux setup and management */
250/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200251
252/* returns non-zero if there are input data pending for stream h1s. */
253static inline size_t h1s_data_pending(const struct h1s *h1s)
254{
255 const struct h1m *h1m;
256
257 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
258 if (h1m->state == H1_MSG_DONE)
259 return 0; // data not for this stream (e.g. pipelining)
260
261 return b_data(&h1s->h1c->ibuf);
262}
263
Christopher Faulet47365272018-10-31 17:40:50 +0100264static struct conn_stream *h1s_new_cs(struct h1s *h1s)
265{
266 struct conn_stream *cs;
267
268 cs = cs_new(h1s->h1c->conn);
269 if (!cs)
270 goto err;
271 h1s->cs = cs;
272 cs->ctx = h1s;
273
274 if (h1s->flags & H1S_F_NOT_FIRST)
275 cs->flags |= CS_FL_NOT_FIRST;
276
277 if (stream_create_from_cs(cs) < 0)
278 goto err;
279 return cs;
280
281 err:
282 cs_free(cs);
283 h1s->cs = NULL;
284 return NULL;
285}
286
Olivier Houchardf502aca2018-12-14 19:42:40 +0100287static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200288{
289 struct h1s *h1s;
290
291 h1s = pool_alloc(pool_head_h1s);
292 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100293 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200294
295 h1s->h1c = h1c;
296 h1c->h1s = h1s;
297
Olivier Houchardf502aca2018-12-14 19:42:40 +0100298 h1s->sess = sess;
299
Christopher Faulet51dbc942018-09-13 09:05:15 +0200300 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100301 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200302
303 h1s->recv_wait = NULL;
304 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200305
306 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100307 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200308
Christopher Faulet129817b2018-09-20 16:14:40 +0200309 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100310 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200311
312 h1s->status = 0;
313 h1s->meth = HTTP_METH_OTHER;
314
Christopher Faulet47365272018-10-31 17:40:50 +0100315 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
316 h1s->flags |= H1S_F_NOT_FIRST;
317 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
318
Christopher Faulet129817b2018-09-20 16:14:40 +0200319 if (!conn_is_back(h1c->conn)) {
320 if (h1c->px->options2 & PR_O2_REQBUG_OK)
321 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200322
323 /* For frontend connections we should always have a session */
324 if (!sess)
325 sess = h1c->conn->owner;
326
Dave Pirotte234740f2019-07-10 13:57:38 +0000327 /* Timers for subsequent sessions on the same HTTP 1.x connection
328 * measure from `now`, not from the connection accept time */
329 if (h1s->flags & H1S_F_NOT_FIRST) {
330 h1s->csinfo.create_date = date;
331 h1s->csinfo.tv_create = now;
332 h1s->csinfo.t_handshake = 0;
333 h1s->csinfo.t_idle = -1;
334 }
335 else {
336 h1s->csinfo.create_date = sess->accept_date;
337 h1s->csinfo.tv_create = sess->tv_accept;
338 h1s->csinfo.t_handshake = sess->t_handshake;
339 h1s->csinfo.t_idle = -1;
340 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200341 }
342 else {
343 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
344 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200345
Christopher Fauletfeb11742018-11-29 15:12:34 +0100346 h1s->csinfo.create_date = date;
347 h1s->csinfo.tv_create = now;
348 h1s->csinfo.t_handshake = 0;
349 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200350 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100351
Christopher Faulete9b70722019-04-08 10:46:02 +0200352 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
353 * create a new one.
354 */
355 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200356 cs->ctx = h1s;
357 h1s->cs = cs;
358 }
Christopher Faulet47365272018-10-31 17:40:50 +0100359 else {
360 cs = h1s_new_cs(h1s);
361 if (!cs)
362 goto fail;
363 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200364 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100365
366 fail:
367 pool_free(pool_head_h1s, h1s);
368 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200369}
370
371static void h1s_destroy(struct h1s *h1s)
372{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200373 if (h1s) {
374 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200375
Christopher Fauletf2824e62018-10-01 12:12:37 +0200376 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200377
Christopher Fauletf2824e62018-10-01 12:12:37 +0200378 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100379 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200380 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100381 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200382
Christopher Fauletcb55f482018-12-10 11:56:47 +0100383 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100384 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
385 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
386 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200387 pool_free(pool_head_h1s, h1s);
388 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200389}
390
Christopher Fauletfeb11742018-11-29 15:12:34 +0100391static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
392{
393 struct h1s *h1s = cs->ctx;
394
395 if (h1s && !conn_is_back(cs->conn))
396 return &h1s->csinfo;
397 return NULL;
398}
399
Christopher Faulet51dbc942018-09-13 09:05:15 +0200400/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200401 * Initialize the mux once it's attached. It is expected that conn->ctx points
402 * to the existing conn_stream (for outgoing connections or for incoming onces
403 * during a mux upgrade) or NULL (for incoming ones during the connexion
404 * establishment). <input> is always used as Input buffer and may contain
405 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
406 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200407 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200408static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
409 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200410{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200411 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100412 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200413
414 h1c = pool_alloc(pool_head_h1c);
415 if (!h1c)
416 goto fail_h1c;
417 h1c->conn = conn;
418 h1c->px = proxy;
419
420 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200421 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200422 h1c->obuf = BUF_NULL;
423 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200424 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200425
Christopher Faulet51dbc942018-09-13 09:05:15 +0200426 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200427 h1c->wait_event.tasklet = tasklet_new();
428 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200429 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200430 h1c->wait_event.tasklet->process = h1_io_cb;
431 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100432 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200433
Christopher Faulete9b70722019-04-08 10:46:02 +0200434 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100435 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
436 if (tick_isset(proxy->timeout.serverfin))
437 h1c->shut_timeout = proxy->timeout.serverfin;
438 } else {
439 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
440 if (tick_isset(proxy->timeout.clientfin))
441 h1c->shut_timeout = proxy->timeout.clientfin;
442 }
443 if (tick_isset(h1c->timeout)) {
444 t = task_new(tid_bit);
445 if (!t)
446 goto fail;
447
448 h1c->task = t;
449 t->process = h1_timeout_task;
450 t->context = h1c;
451 t->expire = tick_add(now_ms, h1c->timeout);
452 }
453
Christopher Fauletf2824e62018-10-01 12:12:37 +0200454 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100455 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200456 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200457
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100458 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200459
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100460
461 if (t)
462 task_queue(t);
463
Christopher Faulet51dbc942018-09-13 09:05:15 +0200464 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200465 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200466
467 /* mux->wake will be called soon to complete the operation */
468 return 0;
469
470 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200471 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200472 if (h1c->wait_event.tasklet)
473 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200474 pool_free(pool_head_h1c, h1c);
475 fail_h1c:
476 return -1;
477}
478
Christopher Faulet73c12072019-04-08 11:23:22 +0200479/* release function. This one should be called to free all resources allocated
480 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200481 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200482static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200483{
Christopher Faulet61840e72019-04-15 09:33:32 +0200484 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200485
Christopher Faulet51dbc942018-09-13 09:05:15 +0200486 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200487 /* The connection must be aattached to this mux to be released */
488 if (h1c->conn && h1c->conn->ctx == h1c)
489 conn = h1c->conn;
490
491 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200492 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200493 /* Make sure we're no longer subscribed to anything */
494 if (h1c->wait_event.events)
495 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
496 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200497 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200498 /* connection successfully upgraded to H2, this
499 * mux was already released */
500 return;
501 }
502 sess_log(conn->owner); /* Log if the upgrade failed */
503 }
504
Olivier Houchard45c44372019-06-11 14:06:23 +0200505
Christopher Faulet51dbc942018-09-13 09:05:15 +0200506 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
507 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
508 LIST_DEL(&h1c->buf_wait.list);
509 LIST_INIT(&h1c->buf_wait.list);
510 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
511 }
512
513 h1_release_buf(h1c, &h1c->ibuf);
514 h1_release_buf(h1c, &h1c->obuf);
515
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100516 if (h1c->task) {
517 h1c->task->context = NULL;
518 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
519 h1c->task = NULL;
520 }
521
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200522 if (h1c->wait_event.tasklet)
523 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200524
Christopher Fauletf2824e62018-10-01 12:12:37 +0200525 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200526 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100527 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200528 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200529 pool_free(pool_head_h1c, h1c);
530 }
531
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200532 if (conn) {
533 conn->mux = NULL;
534 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200535
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200536 conn_stop_tracking(conn);
537 conn_full_close(conn);
538 if (conn->destroy_cb)
539 conn->destroy_cb(conn);
540 conn_free(conn);
541 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200542}
543
544/******************************************************/
545/* functions below are for the H1 protocol processing */
546/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200547/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
548 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200549 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100550static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200551{
Christopher Faulet570d1612018-11-26 11:13:57 +0100552 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200553
Christopher Faulet570d1612018-11-26 11:13:57 +0100554 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200555 (*(p + 5) > '1' ||
556 (*(p + 5) == '1' && *(p + 7) >= '1')))
557 h1m->flags |= H1_MF_VER_11;
558}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200559
Christopher Faulet9768c262018-10-22 09:34:31 +0200560/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
561 * greater or equal to 1.1
562 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100563static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200564{
Christopher Faulet570d1612018-11-26 11:13:57 +0100565 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200566
Christopher Faulet570d1612018-11-26 11:13:57 +0100567 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200568 (*(p + 5) > '1' ||
569 (*(p + 5) == '1' && *(p + 7) >= '1')))
570 h1m->flags |= H1_MF_VER_11;
571}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200572
Christopher Faulet9768c262018-10-22 09:34:31 +0200573/*
574 * Check the validity of the request version. If the version is valid, it
575 * returns 1. Otherwise, it returns 0.
576 */
577static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
578{
579 struct h1c *h1c = h1s->h1c;
580
581 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
582 * exactly one digit "." one digit. This check may be disabled using
583 * option accept-invalid-http-request.
584 */
585 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
586 if (sl.rq.v.len != 8)
587 return 0;
588
589 if (*(sl.rq.v.ptr + 4) != '/' ||
590 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
591 *(sl.rq.v.ptr + 6) != '.' ||
592 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
593 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200594 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200595 else if (!sl.rq.v.len) {
596 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200597
Christopher Faulet9768c262018-10-22 09:34:31 +0200598 /* RFC 1945 allows only GET for HTTP/0.9 requests */
599 if (sl.rq.meth != HTTP_METH_GET)
600 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200601
Christopher Faulet9768c262018-10-22 09:34:31 +0200602 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
603 if (!sl.rq.u.len)
604 return 0;
605
606 /* Add HTTP version */
607 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100608 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200609 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100610
611 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100612 ((*(sl.rq.v.ptr + 5) > '1') ||
613 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100614 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200615 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200616}
617
Christopher Faulet9768c262018-10-22 09:34:31 +0200618/*
619 * Check the validity of the response version. If the version is valid, it
620 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200621 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200622static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200623{
Christopher Faulet9768c262018-10-22 09:34:31 +0200624 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200625
Christopher Faulet9768c262018-10-22 09:34:31 +0200626 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
627 * exactly one digit "." one digit. This check may be disabled using
628 * option accept-invalid-http-request.
629 */
630 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
631 if (sl.st.v.len != 8)
632 return 0;
633
634 if (*(sl.st.v.ptr + 4) != '/' ||
635 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
636 *(sl.st.v.ptr + 6) != '.' ||
637 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
638 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200639 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100640
641 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100642 ((*(sl.st.v.ptr + 5) > '1') ||
643 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100644 h1m->flags |= H1_MF_VER_11;
645
Christopher Faulet9768c262018-10-22 09:34:31 +0200646 return 1;
647}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200648
649/* Deduce the connection mode of the client connection, depending on the
650 * configuration and the H1 message flags. This function is called twice, the
651 * first time when the request is parsed and the second time when the response
652 * is parsed.
653 */
654static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
655{
656 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200657
658 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100659 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200660 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100661 h1s->status == 101) {
662 /* Either we've established an explicit tunnel, or we're
663 * switching the protocol. In both cases, we're very unlikely to
664 * understand the next protocols. We have to switch to tunnel
665 * mode, so that we transfer the request and responses then let
666 * this protocol pass unmodified. When we later implement
667 * specific parsers for such protocols, we'll want to check the
668 * Upgrade header which contains information about that protocol
669 * for responses with status 101 (eg: see RFC2817 about TLS).
670 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200671 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100672 }
673 else if (h1s->flags & H1S_F_WANT_KAL) {
674 /* By default the client is in KAL mode. CLOSE mode mean
675 * it is imposed by the client itself. So only change
676 * KAL mode here. */
677 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
678 /* no length known or explicit close => close */
679 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
680 }
681 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
682 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
683 /* no explict keep-alive and option httpclose => close */
684 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
685 }
686 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200687 }
688 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100689 /* Input direction: first pass */
690 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
691 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200692 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100693 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200694 }
695
696 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
697 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
698 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
699}
700
701/* Deduce the connection mode of the client connection, depending on the
702 * configuration and the H1 message flags. This function is called twice, the
703 * first time when the request is parsed and the second time when the response
704 * is parsed.
705 */
706static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
707{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100708 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100709 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100710 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200711
Christopher Fauletf2824e62018-10-01 12:12:37 +0200712 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100713 /* Input direction: second pass */
714
Christopher Fauletf2824e62018-10-01 12:12:37 +0200715 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100716 h1s->status == 101) {
717 /* Either we've established an explicit tunnel, or we're
718 * switching the protocol. In both cases, we're very unlikely to
719 * understand the next protocols. We have to switch to tunnel
720 * mode, so that we transfer the request and responses then let
721 * this protocol pass unmodified. When we later implement
722 * specific parsers for such protocols, we'll want to check the
723 * Upgrade header which contains information about that protocol
724 * for responses with status 101 (eg: see RFC2817 about TLS).
725 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200726 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100727 }
728 else if (h1s->flags & H1S_F_WANT_KAL) {
729 /* By default the server is in KAL mode. CLOSE mode mean
730 * it is imposed by haproxy itself. So only change KAL
731 * mode here. */
732 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
733 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
734 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
735 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
736 }
737 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200738 }
Christopher Faulet70033782018-12-05 13:50:11 +0100739 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100740 /* Output direction: first pass */
741 if (h1m->flags & H1_MF_CONN_CLO) {
742 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100743 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100744 }
745 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
746 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
747 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
748 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
749 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
750 /* no explicit keep-alive option httpclose/server-close => close */
751 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
752 }
Christopher Faulet70033782018-12-05 13:50:11 +0100753 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200754
755 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
756 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
757 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200758}
759
Christopher Fauletb992af02019-03-28 15:42:24 +0100760static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200761{
762 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200763
764 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
765 * token is found
766 */
767 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200768 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200769
770 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100771 if (!(h1m->flags & H1_MF_VER_11))
772 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200773 }
774 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100775 if (h1m->flags & H1_MF_VER_11)
776 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200777 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200778}
779
Christopher Fauletb992af02019-03-28 15:42:24 +0100780static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200781{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200782 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
783 * token is found
784 */
785 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200786 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200787
788 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100789 if (!(h1m->flags & H1_MF_VER_11) ||
790 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
791 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200792 }
793 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100794 if (h1m->flags & H1_MF_VER_11)
795 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200796 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200797}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200798
Christopher Fauletb992af02019-03-28 15:42:24 +0100799static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200800{
Christopher Fauletb992af02019-03-28 15:42:24 +0100801 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200802 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100803 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200804 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200805}
806
Christopher Fauletb992af02019-03-28 15:42:24 +0100807static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
808{
809 if (!conn_is_back(h1s->h1c->conn))
810 h1_set_cli_conn_mode(h1s, h1m);
811 else
812 h1_set_srv_conn_mode(h1s, h1m);
813
814 if (!(h1m->flags & H1_MF_RESP))
815 h1_update_req_conn_value(h1s, h1m, conn_val);
816 else
817 h1_update_res_conn_value(h1s, h1m, conn_val);
818}
Christopher Faulete44769b2018-11-29 23:01:45 +0100819
Christopher Faulet98fbe952019-07-22 16:18:24 +0200820/* Try to adjust the case of the message header name using the global map
821 * <hdrs_map>.
822 */
823static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
824{
825 struct ebpt_node *node;
826 struct h1_hdr_entry *entry;
827
828 /* No entry in the map, do nothing */
829 if (eb_is_empty(&hdrs_map.map))
830 return;
831
832 /* No conversion fo the request headers */
833 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
834 return;
835
836 /* No conversion fo the response headers */
837 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
838 return;
839
840 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
841 if (!node)
842 return;
843 entry = container_of(node, struct h1_hdr_entry, node);
844 name->ptr = entry->name.ptr;
845 name->len = entry->name.len;
846}
847
Christopher Faulete44769b2018-11-29 23:01:45 +0100848/* Append the description of what is present in error snapshot <es> into <out>.
849 * The description must be small enough to always fit in a buffer. The output
850 * buffer may be the trash so the trash must not be used inside this function.
851 */
852static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
853{
854 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100855 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
856 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
857 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
858 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
859 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
860 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100861}
862/*
863 * Capture a bad request or response and archive it in the proxy's structure.
864 * By default it tries to report the error position as h1m->err_pos. However if
865 * this one is not set, it will then report h1m->next, which is the last known
866 * parsing point. The function is able to deal with wrapping buffers. It always
867 * displays buffers as a contiguous area starting at buf->p. The direction is
868 * determined thanks to the h1m's flags.
869 */
870static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
871 struct h1m *h1m, struct buffer *buf)
872{
873 struct session *sess = h1c->conn->owner;
874 struct proxy *proxy = h1c->px;
875 struct proxy *other_end = sess->fe;
876 union error_snapshot_ctx ctx;
877
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100878 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100879 other_end = si_strm(h1s->cs->data)->be;
880
881 /* http-specific part now */
882 ctx.h1.state = h1m->state;
883 ctx.h1.c_flags = h1c->flags;
884 ctx.h1.s_flags = h1s->flags;
885 ctx.h1.m_flags = h1m->flags;
886 ctx.h1.m_clen = h1m->curr_len;
887 ctx.h1.m_blen = h1m->body_len;
888
889 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
890 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100891 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
892 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100893}
894
Christopher Fauletadb22202018-12-12 10:32:09 +0100895/* Emit the chunksize followed by a CRLF in front of data of the buffer
896 * <buf>. It goes backwards and starts with the byte before the buffer's
897 * head. The caller is responsible for ensuring there is enough room left before
898 * the buffer's head for the string.
899 */
900static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
901{
902 char *beg, *end;
903
904 beg = end = b_head(buf);
905 *--beg = '\n';
906 *--beg = '\r';
907 do {
908 *--beg = hextab[chksz & 0xF];
909 } while (chksz >>= 4);
910 buf->head -= (end - beg);
911 b_add(buf, end - beg);
912}
913
914/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
915 * ensuring there is enough room left in the buffer for the string. */
916static void h1_emit_chunk_crlf(struct buffer *buf)
917{
918 *(b_peek(buf, b_data(buf))) = '\r';
919 *(b_peek(buf, b_data(buf) + 1)) = '\n';
920 b_add(buf, 2);
921}
922
Christopher Faulet129817b2018-09-20 16:14:40 +0200923/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100924 * Switch the request to tunnel mode. This function must only be called for
925 * CONNECT requests. On the client side, the mux is mark as busy on input,
926 * waiting the response.
927 */
928static void h1_set_req_tunnel_mode(struct h1s *h1s)
929{
930 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
931 h1s->req.state = H1_MSG_TUNNEL;
932 if (!conn_is_back(h1s->h1c->conn))
933 h1s->h1c->flags |= H1C_F_IN_BUSY;
934}
935
936/*
937 * Switch the response to tunnel mode. This function must only be called on
938 * successfull replies to CONNECT requests or on protocol switching. On the
939 * server side, if the request is not finished, the mux is mark as busy on
940 * input. Otherwise the request is also switch to tunnel mode.
941 */
942static void h1_set_res_tunnel_mode(struct h1s *h1s)
943{
944 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
945 h1s->res.state = H1_MSG_TUNNEL;
946 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
947 h1s->h1c->flags |= H1C_F_IN_BUSY;
948 else {
949 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
950 h1s->req.state = H1_MSG_TUNNEL;
951 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
952 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200953 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100954 }
955 }
956}
957
Christopher Faulet82f01602019-05-24 16:50:16 +0200958/* Estimate the size of the HTX headers after the parsing, including the EOH. */
959static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
960{
961 size_t sz = 0;
962 int i;
963
964 for (i = 0; hdrs[i].n.len; i++)
965 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
966 sz += sizeof(struct htx_blk) + 1;
967 return sz;
968}
969
Christopher Faulet30db3d72019-05-17 15:35:33 +0200970/* Estimate the size of the HTX request after the parsing. */
971static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
972{
973 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200974
975 /* size of the HTX start-line */
Christopher Faulet8427d0d2019-09-03 16:16:50 +0200976 sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200977 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200978 return sz;
979}
980
981/* Estimate the size of the HTX response after the parsing. */
982static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
983{
984 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200985
986 /* size of the HTX start-line */
Christopher Faulet8427d0d2019-09-03 16:16:50 +0200987 sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200988 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200989 return sz;
990}
991
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100992/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200993 * Add the EOM in the HTX message and switch the message to the DONE state. It
994 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
995 * functions is responsible to update the parser state <h1m>. It also add the
996 * flag CS_FL_EOI on the CS.
997 */
998static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
999{
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +02001000 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
1001 h1s->flags |= H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001002 return 0;
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +02001003 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001004
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +02001005 h1s->flags &= ~H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001006 h1m->state = H1_MSG_DONE;
1007 h1s->cs->flags |= CS_FL_EOI;
1008 return (sizeof(struct htx_blk) + 1);
1009}
1010
1011/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001012 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001013 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1014 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001015 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001016 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001017static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001018 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001019{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001020 struct htx_sl *sl;
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001021 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001022 union h1_sl h1sl;
1023 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001024 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 int ret = 0;
1026
Christopher Faulet30db3d72019-05-17 15:35:33 +02001027 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001028 goto end;
1029
Christopher Faulet129817b2018-09-20 16:14:40 +02001030 /* Realing input buffer if necessary */
1031 if (b_head(buf) + b_data(buf) > b_wrap(buf))
1032 b_slow_realign(buf, trash.area, 0);
1033
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001034 if (!(h1m->flags & H1_MF_RESP)) {
1035 /* Try to match H2 preface before parsing the request headers. */
1036 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
1037 if (ret > 0)
1038 goto h2c_upgrade;
1039 }
1040
Christopher Faulet30db3d72019-05-17 15:35:33 +02001041 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001042 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +02001043 if (ret <= 0) {
Willy Tarreau347f4642019-08-23 08:11:36 +02001044 /* Incomplete or invalid message. If the input buffer only
1045 * contains headers and is full, which is detected by it being
1046 * full and the offset to be zero, it's an error because
1047 * headers are too large to be handled by the parser. */
1048 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001049 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001050 goto end;
1051 }
1052
1053 /* messages headers fully parsed, do some checks to prepare the body
1054 * parsing.
1055 */
1056
Christopher Faulet9768c262018-10-22 09:34:31 +02001057 /* Save the request's method or the response's status, check if the body
1058 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001059 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001060 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001061
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001062 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001063 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001064
1065 if (h1s->meth == HTTP_METH_CONNECT) {
1066 /* Switch CONNECT requests to tunnel mode */
1067 h1_set_req_tunnel_mode(h1s);
1068 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001069
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001070 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1071 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001072 h1m->err_state = h1m->state;
1073 goto vsn_error;
1074 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001075 }
1076 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001077 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001078
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001079 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1080 h1s->status == 101) {
1081 /* Switch successfull replies to CONNECT requests and
1082 * protocol switching to tunnel mode. */
1083 h1_set_res_tunnel_mode(h1s);
1084 }
1085 else if ((h1s->meth == HTTP_METH_HEAD) ||
1086 (h1s->status >= 100 && h1s->status < 200) ||
1087 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001088 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001089 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1090 h1m->flags |= H1_MF_XFER_LEN;
1091 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001092 }
1093 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001094 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001095 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001096 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001097 else {
1098 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001099 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001100 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001101
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001102 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1103 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001104 h1m->err_state = h1m->state;
1105 goto vsn_error;
1106 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001107 }
1108
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001109 /* Set HTX start-line flags */
1110 if (h1m->flags & H1_MF_VER_11)
1111 flags |= HTX_SL_F_VER_11;
1112 if (h1m->flags & H1_MF_XFER_ENC)
1113 flags |= HTX_SL_F_XFER_ENC;
1114 if (h1m->flags & H1_MF_XFER_LEN) {
1115 flags |= HTX_SL_F_XFER_LEN;
1116 if (h1m->flags & H1_MF_CHNK)
1117 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001118 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001119 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001120 if (h1m->body_len == 0)
1121 flags |= HTX_SL_F_BODYLESS;
1122 }
1123 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001124 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001125 }
1126
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001127 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001128 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001129 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1130 if (htx_is_empty(htx))
1131 goto error;
1132 h1m_init_req(h1m);
1133 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1134 ret = 0;
1135 goto end;
1136 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001137
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001138 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1139 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001140 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001141 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001142
1143 /* Check if the uri contains an explicit scheme and if it is
1144 * "http" or "https". */
1145 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1146 sl->flags |= HTX_SL_F_HAS_SCHM;
1147 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1148 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1149 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001150 }
1151 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001152 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1153 if (htx_is_empty(htx))
1154 goto error;
1155 h1m_init_res(h1m);
1156 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1157 ret = 0;
1158 goto end;
1159 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001160
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001161 flags |= HTX_SL_F_IS_RESP;
1162 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1163 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001164 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001165 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001166 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001167
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001168 /* Set bytes used in the HTX mesage for the headers now */
1169 sl->hdrs_bytes = htx_used_space(htx) - used;
1170
Christopher Fauletb992af02019-03-28 15:42:24 +01001171 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001172
1173 /* If body length cannot be determined, set htx->extra to
1174 * ULLONG_MAX. This value is impossible in other cases.
1175 */
1176 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001177 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001178 end:
1179 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001180
1181 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001182 h1m->err_state = h1m->state;
1183 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001184 vsn_error:
1185 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001186 h1s->cs->flags |= CS_FL_EOI;
1187 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001188 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001189 ret = 0;
1190 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001191
1192 h2c_upgrade:
1193 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001194 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001195 htx->flags |= HTX_FL_UPGRADE;
1196 ret = 0;
1197 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001198}
1199
1200/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001201 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1202 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001203 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001204 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001205 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001206static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001207 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001208 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001209{
1210 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001211 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001212
Christopher Faulet129817b2018-09-20 16:14:40 +02001213 if (h1m->flags & H1_MF_XFER_LEN) {
1214 if (h1m->flags & H1_MF_CLEN) {
1215 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001216 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001217 if ((uint64_t)ret > h1m->curr_len)
1218 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001219 if (ret > b_contig_data(buf, *ofs))
1220 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001221
Christopher Faulet9768c262018-10-22 09:34:31 +02001222 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001223 /* very often with large files we'll face the following
1224 * situation :
1225 * - htx is empty and points to <htxbuf>
1226 * - ret == buf->data
1227 * - buf->head == sizeof(struct htx)
1228 * => we can swap the buffers and place an htx header into
1229 * the target buffer instead
1230 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001231 int32_t try = ret;
1232
Willy Tarreau78f548f2018-12-05 10:02:39 +01001233 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1234 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1235 void *raw_area = buf->area;
1236 void *htx_area = htxbuf->area;
1237 struct htx_blk *blk;
1238
1239 buf->area = htx_area;
1240 htxbuf->area = raw_area;
1241 htx = (struct htx *)htxbuf->area;
1242 htx->size = htxbuf->size - sizeof(*htx);
1243 htx_reset(htx);
1244 b_set_data(htxbuf, b_size(htxbuf));
1245
1246 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1247 blk->info += ret;
1248 /* nothing else to do, the old buffer now contains an
1249 * empty pre-initialized HTX header
1250 */
1251 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001252 else {
1253 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1254 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001255 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001256 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001257 *ofs += ret;
1258 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001259 if (ret < try)
1260 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001261 }
1262
1263 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001264 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001265 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001266 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001267 }
1268 else if (h1m->flags & H1_MF_CHNK) {
1269 new_chunk:
1270 /* te:chunked : parse chunks */
1271 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001272 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001273 if (ret <= 0)
1274 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001275 h1m->state = H1_MSG_CHUNK_SIZE;
1276
Christopher Fauletf2824e62018-10-01 12:12:37 +02001277 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001278 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001279 }
1280
1281 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1282 unsigned int chksz;
1283
Christopher Faulet30db3d72019-05-17 15:35:33 +02001284 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001285 if (ret <= 0)
1286 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001287 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001288
Christopher Faulet129817b2018-09-20 16:14:40 +02001289 h1m->curr_len = chksz;
1290 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001291 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001292 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001293
1294 if (!h1m->curr_len)
1295 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001296 }
1297
1298 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001299 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001300 if ((uint64_t)ret > h1m->curr_len)
1301 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001302 if (ret > b_contig_data(buf, *ofs))
1303 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001304
Christopher Faulet9768c262018-10-22 09:34:31 +02001305 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001306 int32_t try = ret;
1307
1308 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001309 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001310 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001311 *ofs += ret;
1312 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001313 if (ret < try)
1314 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001315 }
1316 if (!h1m->curr_len) {
1317 h1m->state = H1_MSG_CHUNK_CRLF;
1318 goto new_chunk;
1319 }
1320 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001321 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001322 }
1323 else {
1324 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1325 * is no body. Switch the message in DONE state
1326 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001327 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001328 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001329 }
1330 }
1331 else {
1332 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001333 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 if (ret > b_contig_data(buf, *ofs))
1335 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001336
Christopher Faulet9768c262018-10-22 09:34:31 +02001337 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001338 int32_t try = ret;
1339
1340 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001341
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001342 *ofs += ret;
1343 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001344 if (ret < try)
1345 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001346 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001347 }
1348
1349 end:
1350 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001351 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001352 h1s->cs->flags |= CS_FL_EOI;
1353 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001354 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001355 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001356 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001357 return 0;
1358 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001359 /* update htx->extra, only when the body length is known */
1360 if (h1m->flags & H1_MF_XFER_LEN)
1361 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001362 return total;
1363}
1364
1365/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001366 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1367 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1368 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1369 * responsible to update the parser state <h1m>.
1370 */
1371static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1372 struct buffer *buf, size_t *ofs, size_t max)
1373{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001374 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001375 struct h1m tlr_h1m;
1376 int ret = 0;
1377
1378 if (!max || !b_data(buf))
1379 goto end;
1380
1381 /* Realing input buffer if necessary */
1382 if (b_peek(buf, *ofs) > b_tail(buf))
1383 b_slow_realign(buf, trash.area, 0);
1384
1385 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1386 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1387 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1388 if (ret <= 0) {
Willy Tarreau347f4642019-08-23 08:11:36 +02001389 /* Incomplete or invalid trailers. If the input buffer only
1390 * contains trailers and is full, which is detected by it being
1391 * full and the offset to be zero, it's an error because
1392 * trailers are too large to be handled by the parser. */
1393 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001394 goto error;
1395 goto end;
1396 }
1397
1398 /* messages trailers fully parsed. */
1399 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1400 if (htx_is_empty(htx))
1401 goto error;
1402 ret = 0;
1403 goto end;
1404 }
1405
1406 if (!htx_add_all_trailers(htx, hdrs))
1407 goto error;
1408
1409 *ofs += ret;
1410 h1s->flags |= H1S_F_HAVE_I_TLR;
1411 end:
1412 return ret;
1413
1414 error:
1415 h1m->err_state = h1m->state;
1416 h1m->err_pos = h1m->next;
1417 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1418 h1s->cs->flags |= CS_FL_EOI;
1419 htx->flags |= HTX_FL_PARSING_ERROR;
1420 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1421 ret = 0;
1422 goto end;
1423}
1424
1425/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001426 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001427 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1428 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001429 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001430static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001431{
Christopher Faulet539e0292018-11-19 10:40:09 +01001432 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001433 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001434 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001435 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001436 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001437 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001438
Christopher Faulet539e0292018-11-19 10:40:09 +01001439 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001440
Christopher Fauletf2824e62018-10-01 12:12:37 +02001441 if (!conn_is_back(h1c->conn)) {
1442 h1m = &h1s->req;
1443 errflag = H1S_F_REQ_ERROR;
1444 }
1445 else {
1446 h1m = &h1s->res;
1447 errflag = H1S_F_RES_ERROR;
1448 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001449
Christopher Faulet74027762019-02-26 14:45:05 +01001450 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001451 if (h1s->flags & errflag)
1452 goto end;
1453
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001454 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001455 size_t used = htx_used_space(htx);
1456
Christopher Faulet129817b2018-09-20 16:14:40 +02001457 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001458 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001459 if (!ret)
1460 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001461 if ((h1m->flags & H1_MF_RESP) &&
1462 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1463 h1m_init_res(&h1s->res);
1464 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1465 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001466 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001467 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001468 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001469 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001470 if (!ret)
1471 break;
1472 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001473 else if (h1m->state == H1_MSG_TRAILERS) {
1474 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1475 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1476 if (!ret)
1477 break;
1478 }
Christopher Fauletf1ef7f62019-09-03 21:55:14 +02001479 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001480 break;
1481 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001482 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001483 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1484 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001485 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001486 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001487 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001488 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001489 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001490 if (!ret)
1491 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001492 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001493 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001494 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001495 break;
1496 }
1497
Christopher Faulet30db3d72019-05-17 15:35:33 +02001498 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001499 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001500
Christopher Faulet47365272018-10-31 17:40:50 +01001501 if (h1s->flags & errflag)
1502 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001503
Christopher Faulet539e0292018-11-19 10:40:09 +01001504 b_del(&h1c->ibuf, total);
1505
1506 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001507 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001508 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001509 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001510 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001511 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001512 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001513
Christopher Fauletcf56b992018-12-11 16:12:31 +01001514 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1515
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001516 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001517 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001518 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001519 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001520
Willy Tarreau0bb5a5c2019-08-23 09:29:29 +02001521 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1522 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001523 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001524 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001525 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001526 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001527
Christopher Faulet30db3d72019-05-17 15:35:33 +02001528 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001529
1530 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001531 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001532 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001533 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001534}
1535
Christopher Faulet129817b2018-09-20 16:14:40 +02001536/*
1537 * Process outgoing data. It parses data and transfer them from the channel buffer into
1538 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1539 * 0 if it couldn't proceed.
1540 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001541static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1542{
Christopher Faulet129817b2018-09-20 16:14:40 +02001543 struct h1s *h1s = h1c->h1s;
1544 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001545 struct htx *chn_htx;
1546 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001547 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001548 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001549 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001550
Christopher Faulet47365272018-10-31 17:40:50 +01001551 if (!count)
1552 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001553
Christopher Faulet94b2c762019-05-24 15:28:57 +02001554 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001555 if (htx_is_empty(chn_htx))
1556 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001557
Christopher Faulet51dbc942018-09-13 09:05:15 +02001558 if (!h1_get_buf(h1c, &h1c->obuf)) {
1559 h1c->flags |= H1C_F_OUT_ALLOC;
1560 goto end;
1561 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001562
Christopher Fauletf2824e62018-10-01 12:12:37 +02001563 if (!conn_is_back(h1c->conn)) {
1564 h1m = &h1s->res;
1565 errflag = H1S_F_RES_ERROR;
1566 }
1567 else {
1568 h1m = &h1s->req;
1569 errflag = H1S_F_REQ_ERROR;
1570 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001571
Christopher Faulet0e54d542019-07-04 21:22:34 +02001572 if (h1s->flags & errflag)
1573 goto end;
1574
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001575 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001576 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001577
Willy Tarreau3815b222018-12-11 19:50:43 +01001578 /* Perform some optimizations to reduce the number of buffer copies.
1579 * First, if the mux's buffer is empty and the htx area contains
1580 * exactly one data block of the same size as the requested count,
1581 * then it's possible to simply swap the caller's buffer with the
1582 * mux's output buffer and adjust offsets and length to match the
1583 * entire DATA HTX block in the middle. In this case we perform a
1584 * true zero-copy operation from end-to-end. This is the situation
1585 * that happens all the time with large files. Second, if this is not
1586 * possible, but the mux's output buffer is empty, we still have an
1587 * opportunity to avoid the copy to the intermediary buffer, by making
1588 * the intermediary buffer's area point to the output buffer's area.
1589 * In this case we want to skip the HTX header to make sure that copies
1590 * remain aligned and that this operation remains possible all the
1591 * time. This goes for headers, data blocks and any data extracted from
1592 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001593 */
1594 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001595 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001596 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001597 htx_get_blk_value(chn_htx, blk).len == count) {
1598 void *old_area = h1c->obuf.area;
1599
1600 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001601 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001602 h1c->obuf.data = count;
1603
1604 buf->area = old_area;
1605 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001606
1607 /* The message is chunked. We need to emit the chunk
1608 * size. We have at least the size of the struct htx to
1609 * write the chunk envelope. It should be enough.
1610 */
1611 if (h1m->flags & H1_MF_CHNK) {
1612 h1_emit_chunk_size(&h1c->obuf, count);
1613 h1_emit_chunk_crlf(&h1c->obuf);
1614 }
1615
Willy Tarreau3815b222018-12-11 19:50:43 +01001616 total += count;
1617 goto out;
1618 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001619 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001620 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001621 else
1622 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001623
Christopher Fauletc2518a52019-06-25 21:41:02 +02001624 tmp.data = 0;
1625 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001626 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001627 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001628 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001629 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001630 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001631 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001632
Christopher Fauletb2e84162018-12-06 11:39:49 +01001633 vlen = sz;
1634 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001635 if (type != HTX_BLK_DATA)
Christopher Fauletb2e84162018-12-06 11:39:49 +01001636 goto copy;
1637 vlen = count;
1638 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001639
Christopher Faulet94b2c762019-05-24 15:28:57 +02001640 if (type == HTX_BLK_UNUSED)
1641 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001642
Christopher Faulet94b2c762019-05-24 15:28:57 +02001643 switch (h1m->state) {
1644 case H1_MSG_RQBEFORE:
1645 if (type != HTX_BLK_REQ_SL)
1646 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001647 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001648 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001650 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001651 goto copy;
1652 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001653 if (sl->flags & HTX_SL_F_BODYLESS)
1654 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001655 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001656 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001657
Christopher Faulet94b2c762019-05-24 15:28:57 +02001658 case H1_MSG_RPBEFORE:
1659 if (type != HTX_BLK_RES_SL)
1660 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001661 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001662 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001663 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001664 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001665 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001666 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001667 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001668 if (sl->info.res.status < 200 &&
1669 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001670 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001671 h1m->state = H1_MSG_HDR_FIRST;
1672 break;
1673
Christopher Faulet94b2c762019-05-24 15:28:57 +02001674 case H1_MSG_HDR_FIRST:
1675 case H1_MSG_HDR_NAME:
1676 case H1_MSG_HDR_L2_LWS:
1677 if (type == HTX_BLK_EOH)
1678 goto last_lf;
1679 if (type != HTX_BLK_HDR)
1680 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001681 h1m->state = H1_MSG_HDR_NAME;
1682 n = htx_get_blk_name(chn_htx, blk);
1683 v = htx_get_blk_value(chn_htx, blk);
1684
1685 if (isteqi(n, ist("transfer-encoding")))
1686 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001687 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001688 /* Only skip C-L header with invalid value. */
1689 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001690 goto skip_hdr;
1691 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001692 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001693 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001694 if (!v.len)
1695 goto skip_hdr;
1696 }
1697
Christopher Faulet98fbe952019-07-22 16:18:24 +02001698 /* Try to adjust the case of the header name */
1699 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1700 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001701 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001702 goto copy;
1703 skip_hdr:
1704 h1m->state = H1_MSG_HDR_L2_LWS;
1705 break;
1706
Christopher Faulet94b2c762019-05-24 15:28:57 +02001707 case H1_MSG_LAST_LF:
1708 if (type != HTX_BLK_EOH)
1709 goto error;
1710 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001711 h1m->state = H1_MSG_LAST_LF;
1712 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001713 /* There is no "Connection:" header and
1714 * it the conn_mode must be
1715 * processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001716 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001717 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001718 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001719 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001720 /* Try to adjust the case of the header name */
1721 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1722 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001723 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001724 goto copy;
1725 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001726 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001727 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001728
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001729 if ((h1s->meth != HTTP_METH_CONNECT &&
1730 (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 +01001731 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1732 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1733 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1734 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1735 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001736 /* chunking needed but header not seen */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001737 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Willy Tarreau4710d202019-01-03 17:39:54 +01001738 goto copy;
1739 h1m->flags |= H1_MF_CHNK;
1740 }
1741
Christopher Fauletc2518a52019-06-25 21:41:02 +02001742 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet9768c262018-10-22 09:34:31 +02001743 goto copy;
1744
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001745 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1746 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1747 h1_set_req_tunnel_mode(h1s);
1748 }
1749 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1750 /* a successfull reply to a CONNECT or a protocol switching is sent
1751 * to the client . Switch the response to tunnel mode. */
1752 h1_set_res_tunnel_mode(h1s);
1753 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001754 else if ((h1m->flags & H1_MF_RESP) &&
1755 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1756 h1m_init_res(&h1s->res);
1757 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001758 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001759 }
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001760 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1761 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001762 else
1763 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001764 break;
1765
Christopher Faulet94b2c762019-05-24 15:28:57 +02001766 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001767 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001768 if (type == HTX_BLK_EOM) {
1769 /* Chunked message without explicit trailers */
1770 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001771 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001772 goto copy;
1773 }
1774 goto done;
1775 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001776 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001777 /* If the message is not chunked, never
1778 * add the last chunk. */
1779 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet94b2c762019-05-24 15:28:57 +02001780 goto copy;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001781 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001782 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001783 else if (type != HTX_BLK_DATA)
1784 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001785 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001786 v.len = vlen;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001787 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001788 goto copy;
1789 break;
1790
Christopher Faulet94b2c762019-05-24 15:28:57 +02001791 case H1_MSG_TRAILERS:
1792 if (type == HTX_BLK_EOM)
1793 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001794 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001795 goto error;
1796 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001797 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001798 /* If the message is not chunked, ignore
1799 * trailers. It may happen with H2 messages. */
1800 if (!(h1m->flags & H1_MF_CHNK))
1801 break;
1802
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001803 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001804 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet32188212018-11-20 18:21:43 +01001805 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001806 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001807 else { // HTX_BLK_TLR
1808 n = htx_get_blk_name(chn_htx, blk);
1809 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001810
1811 /* Try to adjust the case of the header name */
1812 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1813 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001814 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001815 goto copy;
1816 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001817 break;
1818
Christopher Faulet94b2c762019-05-24 15:28:57 +02001819 case H1_MSG_DONE:
1820 if (type != HTX_BLK_EOM)
1821 goto error;
1822 done:
1823 h1m->state = H1_MSG_DONE;
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001824 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1825 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1826 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1827 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001828 break;
1829
Christopher Faulet9768c262018-10-22 09:34:31 +02001830 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001831 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001832 /* Unexpected error during output processing */
1833 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001834 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001835 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001836 break;
1837 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001838
1839 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001840 total += vlen;
1841 count -= vlen;
1842 if (sz == vlen)
1843 blk = htx_remove_blk(chn_htx, blk);
1844 else {
1845 htx_cut_data_blk(chn_htx, blk, vlen);
1846 break;
1847 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001848 }
1849
Christopher Faulet9768c262018-10-22 09:34:31 +02001850 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001851 /* when the output buffer is empty, tmp shares the same area so that we
1852 * only have to update pointers and lengths.
1853 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001854 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1855 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001856 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001857 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001858
Willy Tarreau3815b222018-12-11 19:50:43 +01001859 htx_to_buf(chn_htx, buf);
1860 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001861 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001862 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001863 end:
1864 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001865}
1866
Christopher Faulet51dbc942018-09-13 09:05:15 +02001867/*********************************************************/
1868/* functions below are I/O callbacks from the connection */
1869/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001870static void h1_wake_stream_for_recv(struct h1s *h1s)
1871{
1872 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001873 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001874 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001875 h1s->recv_wait = NULL;
1876 }
1877}
1878static void h1_wake_stream_for_send(struct h1s *h1s)
1879{
1880 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001881 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001882 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001883 h1s->send_wait = NULL;
1884 }
1885}
1886
Christopher Faulet51dbc942018-09-13 09:05:15 +02001887/*
1888 * Attempt to read data, and subscribe if none available
1889 */
1890static int h1_recv(struct h1c *h1c)
1891{
1892 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001893 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001894 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001895 int rcvd = 0;
1896
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001897 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001898 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001899
Olivier Houchard75159a92018-12-03 18:46:09 +01001900 if (!h1_recv_allowed(h1c)) {
1901 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001902 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001903 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001904
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001905 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1906 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001907 goto end;
1908 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001909
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001910 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001911 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001912 h1_wake_stream_for_recv(h1s);
1913 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001914 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001915 }
1916
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001917 /*
1918 * If we only have a small amount of data, realign it,
1919 * it's probably cheaper than doing 2 recv() calls.
1920 */
1921 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1922 b_slow_realign(&h1c->ibuf, trash.area, 0);
1923
Willy Tarreau45f2b892018-12-05 07:59:27 +01001924 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001925 if (max) {
1926 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001927
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001928 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001929 if (!b_data(&h1c->ibuf)) {
1930 /* try to pre-align the buffer like the rxbufs will be
1931 * to optimize memory copies.
1932 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001933 h1c->ibuf.head = sizeof(struct htx);
1934 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001935 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001936 }
Christopher Faulet47365272018-10-31 17:40:50 +01001937 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001938 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001939 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001940 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001941 if (h1s->csinfo.t_idle == -1)
1942 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1943 }
Christopher Faulet47365272018-10-31 17:40:50 +01001944 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001945
Olivier Houchardcc3fec82019-07-26 15:11:11 +02001946 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001947 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001948 goto end;
1949 }
1950
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001951 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001952
Christopher Faulet9768c262018-10-22 09:34:31 +02001953 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001954 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1955 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001956
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001957 if (conn_xprt_read0_pending(conn) && h1s) {
1958 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001959 rcvd = 1;
1960 }
1961
Christopher Faulet51dbc942018-09-13 09:05:15 +02001962 if (!b_data(&h1c->ibuf))
1963 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001964 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001965 h1c->flags |= H1C_F_IN_FULL;
1966 return rcvd;
1967}
1968
1969
1970/*
1971 * Try to send data if possible
1972 */
1973static int h1_send(struct h1c *h1c)
1974{
1975 struct connection *conn = h1c->conn;
1976 unsigned int flags = 0;
1977 size_t ret;
1978 int sent = 0;
1979
1980 if (conn->flags & CO_FL_ERROR)
1981 return 0;
1982
1983 if (!b_data(&h1c->obuf))
1984 goto end;
1985
1986 if (h1c->flags & H1C_F_OUT_FULL)
1987 flags |= CO_SFL_MSG_MORE;
1988
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001989 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001990 if (ret > 0) {
1991 h1c->flags &= ~H1C_F_OUT_FULL;
1992 b_del(&h1c->obuf, ret);
1993 sent = 1;
1994 }
1995
Christopher Faulet145aa472018-12-06 10:56:20 +01001996 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1997 /* error or output closed, nothing to send, clear the buffer to release it */
1998 b_reset(&h1c->obuf);
1999 }
2000
Christopher Faulet51dbc942018-09-13 09:05:15 +02002001 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002002 if (!(h1c->flags & H1C_F_OUT_FULL))
2003 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002004
Christopher Faulet51dbc942018-09-13 09:05:15 +02002005 /* We're done, no more to send */
2006 if (!b_data(&h1c->obuf)) {
2007 h1_release_buf(h1c, &h1c->obuf);
2008 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01002009 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002010 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002011 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002012 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002013
2014 return sent;
2015}
2016
Christopher Faulet51dbc942018-09-13 09:05:15 +02002017
2018/* callback called on any event by the connection handler.
2019 * It applies changes and returns zero, or < 0 if it wants immediate
2020 * destruction of the connection.
2021 */
2022static int h1_process(struct h1c * h1c)
2023{
2024 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002025 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002026
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002027 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002028 return -1;
2029
Christopher Fauletfeb11742018-11-29 15:12:34 +01002030 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002031 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002032 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01002033 conn_xprt_read0_pending(conn))
2034 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002035 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002036 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002037 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002038 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002039 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002040 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002041 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002042 }
2043
Christopher Fauletfeb11742018-11-29 15:12:34 +01002044 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2045 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2046
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002047 if (conn_xprt_read0_pending(conn))
2048 h1s->flags |= H1S_F_REOS;
2049
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002050 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002051 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002052 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002053 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002054 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002055 h1s->cs->data_cb->wake(h1s->cs);
2056 }
Christopher Faulet47365272018-10-31 17:40:50 +01002057 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002058 if (h1c->task) {
2059 h1c->task->expire = TICK_ETERNITY;
2060 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002061 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 +01002062 ? h1c->shut_timeout
2063 : h1c->timeout));
2064 task_queue(h1c->task);
2065 }
2066 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002067 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002068
2069 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002070 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002071 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002072}
2073
2074static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2075{
2076 struct h1c *h1c = ctx;
2077 int ret = 0;
2078
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002079 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002080 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002081 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002082 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002083 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002084 h1_process(h1c);
2085 return NULL;
2086}
2087
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002088static void h1_reset(struct connection *conn)
2089{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002090
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002091}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002092
2093static int h1_wake(struct connection *conn)
2094{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002095 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002096 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002097
Christopher Faulet539e0292018-11-19 10:40:09 +01002098 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002099 ret = h1_process(h1c);
2100 if (ret == 0) {
2101 struct h1s *h1s = h1c->h1s;
2102
2103 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2104 ret = h1s->cs->data_cb->wake(h1s->cs);
2105 }
2106 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002107}
2108
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002109/* Connection timeout management. The principle is that if there's no receipt
2110 * nor sending for a certain amount of time, the connection is closed.
2111 */
2112static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2113{
2114 struct h1c *h1c = context;
2115 int expired = tick_is_expired(t->expire, now_ms);
2116
2117 if (!expired && h1c)
2118 return t;
2119
Olivier Houchard3f795f72019-04-17 22:51:06 +02002120 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002121
2122 if (!h1c) {
2123 /* resources were already deleted */
2124 return NULL;
2125 }
2126
2127 h1c->task = NULL;
2128 /* If a stream is still attached to the mux, just set an error and wait
2129 * for the stream's timeout. Otherwise, release the mux. This is only ok
2130 * because same timeouts are used.
2131 */
2132 if (h1c->h1s && h1c->h1s->cs)
2133 h1c->flags |= H1C_F_CS_ERROR;
2134 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002135 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002136 return NULL;
2137}
2138
Christopher Faulet51dbc942018-09-13 09:05:15 +02002139/*******************************************/
2140/* functions below are used by the streams */
2141/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002142
Christopher Faulet51dbc942018-09-13 09:05:15 +02002143/*
2144 * Attach a new stream to a connection
2145 * (Used for outgoing connections)
2146 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002147static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002148{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002149 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002150 struct conn_stream *cs = NULL;
2151 struct h1s *h1s;
2152
2153 if (h1c->flags & H1C_F_CS_ERROR)
2154 goto end;
2155
2156 cs = cs_new(h1c->conn);
2157 if (!cs)
2158 goto end;
2159
Olivier Houchardf502aca2018-12-14 19:42:40 +01002160 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002161 if (h1s == NULL)
2162 goto end;
2163
2164 return cs;
2165 end:
2166 cs_free(cs);
2167 return NULL;
2168}
2169
2170/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2171 * this mux, it's easy as we can only store a single conn_stream.
2172 */
2173static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2174{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002175 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002176 struct h1s *h1s = h1c->h1s;
2177
2178 if (h1s)
2179 return h1s->cs;
2180
2181 return NULL;
2182}
2183
Christopher Faulet73c12072019-04-08 11:23:22 +02002184static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002185{
Christopher Faulet73c12072019-04-08 11:23:22 +02002186 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002187
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002188 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002189 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002190}
2191
2192/*
2193 * Detach the stream from the connection and possibly release the connection.
2194 */
2195static void h1_detach(struct conn_stream *cs)
2196{
2197 struct h1s *h1s = cs->ctx;
2198 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002199 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002200 int has_keepalive;
2201 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002202
2203 cs->ctx = NULL;
2204 if (!h1s)
2205 return;
2206
Olivier Houchardf502aca2018-12-14 19:42:40 +01002207 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002208 h1c = h1s->h1c;
2209 h1s->cs = NULL;
2210
Olivier Houchard8a786902018-12-15 16:05:40 +01002211 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2212 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2213 h1s_destroy(h1s);
2214
2215 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002216 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002217 /* If there are any excess server data in the input buffer,
2218 * release it and close the connection ASAP (some data may
2219 * remain in the output buffer). This happens if a server sends
2220 * invalid responses. So in such case, we don't want to reuse
2221 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002222 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002223 if (b_data(&h1c->ibuf)) {
2224 h1_release_buf(h1c, &h1c->ibuf);
2225 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2226 goto release;
2227 }
Christopher Faulet03627242019-07-19 11:34:08 +02002228
Christopher Faulet9400a392018-11-23 23:10:39 +01002229 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002230 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002231 h1c->conn->flags |= CO_FL_PRIVATE;
2232
Olivier Houchard44d59142018-12-13 18:46:22 +01002233 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002234 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002235 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2236 h1c->conn->owner = NULL;
2237 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2238 /* The server doesn't want it, let's kill the connection right away */
2239 h1c->conn->mux->destroy(h1c->conn);
2240 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002241 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002242 return;
2243
2244 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002245 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002246 if (h1c->conn->owner == sess) {
2247 int ret = session_check_idle_conn(sess, h1c->conn);
2248 if (ret == -1)
2249 /* The connection got destroyed, let's leave */
2250 return;
2251 else if (ret == 1) {
2252 /* The connection was added to the server list,
2253 * wake the task so we can subscribe to events
2254 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002255 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002256 return;
2257 }
2258 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002259 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002260 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002261 struct server *srv = objt_server(h1c->conn->target);
2262
2263 if (srv) {
2264 if (h1c->conn->flags & CO_FL_PRIVATE)
2265 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002266 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002267 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2268 else
2269 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2270 }
2271 }
2272 }
2273
Christopher Fauletf1204b82019-07-19 14:51:06 +02002274 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002275 /* 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 +02002276 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2277 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2278 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2279 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002280 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002281 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002282 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002283 if (h1c->task) {
2284 h1c->task->expire = TICK_ETERNITY;
2285 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002286 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 +01002287 ? h1c->shut_timeout
2288 : h1c->timeout));
2289 task_queue(h1c->task);
2290 }
2291 }
2292 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002293}
2294
2295
2296static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2297{
2298 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002299 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002300
2301 if (!h1s)
2302 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002303 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002304
Christopher Faulet7f366362019-04-08 10:51:20 +02002305 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2306 goto do_shutr;
2307
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002308 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002309 return;
2310
Christopher Faulet7f366362019-04-08 10:51:20 +02002311 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002312 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2313 if (cs->flags & CS_FL_SHR)
2314 return;
2315 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002316 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2317 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002318 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 +02002319 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002320}
2321
2322static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2323{
2324 struct h1s *h1s = cs->ctx;
2325 struct h1c *h1c;
2326
2327 if (!h1s)
2328 return;
2329 h1c = h1s->h1c;
2330
Christopher Faulet7f366362019-04-08 10:51:20 +02002331 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2332 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002333
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002334 if ((h1c->flags & H1C_F_UPG_H2C) ||
2335 ((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 +02002336 return;
2337
Christopher Faulet7f366362019-04-08 10:51:20 +02002338 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002339 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2340 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2341 return;
2342
Christopher Faulet666a0c42019-01-08 11:12:04 +01002343 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002344}
2345
Christopher Faulet666a0c42019-01-08 11:12:04 +01002346static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002347{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002348 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002349
Christopher Faulet666a0c42019-01-08 11:12:04 +01002350 conn_xprt_shutw(conn);
2351 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2352 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2353 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002354}
2355
2356/* Called from the upper layer, to unsubscribe to events */
2357static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2358{
2359 struct wait_event *sw;
2360 struct h1s *h1s = cs->ctx;
2361
2362 if (!h1s)
2363 return 0;
2364
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002365 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002366 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002367 BUG_ON(h1s->recv_wait != sw);
2368 sw->events &= ~SUB_RETRY_RECV;
2369 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002370 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002371 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002372 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002373 BUG_ON(h1s->send_wait != sw);
2374 sw->events &= ~SUB_RETRY_SEND;
2375 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002376 }
2377 return 0;
2378}
2379
2380/* Called from the upper layer, to subscribe to events, such as being able to send */
2381static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2382{
2383 struct wait_event *sw;
2384 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002385 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002386
2387 if (!h1s)
2388 return -1;
2389
2390 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002391 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002392 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002393 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2394 sw->events |= SUB_RETRY_RECV;
2395 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002396 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002397 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002398 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002399 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2400 sw->events |= SUB_RETRY_SEND;
2401 h1s->send_wait = sw;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002402 /*
2403 * If the conn_stream attempt to subscribe, and the
2404 * mux isn't subscribed to the connection, then it
2405 * probably means the connection wasn't established
2406 * yet, so we have to subscribe.
2407 */
Christopher Faulet51bb1852019-09-04 10:22:34 +02002408 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002409 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2410 h1c->conn->xprt->subscribe(h1c->conn,
2411 h1c->conn->xprt_ctx,
2412 SUB_RETRY_SEND,
2413 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002414 return 0;
2415 default:
2416 break;
2417 }
2418 return -1;
2419}
2420
2421/* Called from the upper layer, to receive data */
2422static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2423{
2424 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002425 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002426 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002427 size_t ret = 0;
2428
Christopher Faulet539e0292018-11-19 10:40:09 +01002429 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002430 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002431
Christopher Faulete18777b2019-04-16 16:46:36 +02002432 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002433
2434 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2435 h1s->flags |= H1S_F_BUF_FLUSH;
2436 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002437 else {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002438 h1s->flags &= ~H1S_F_SPLICED_DATA;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002439 if (h1m->state != H1_MSG_DONE &&
2440 !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002441 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002442 }
2443 return ret;
2444}
2445
2446
2447/* Called from the upper layer, to send data */
2448static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2449{
2450 struct h1s *h1s = cs->ctx;
2451 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002452 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002453
2454 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002455 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002456
2457 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002458
2459 /* If we're not connected yet, or we're waiting for a handshake, stop
2460 * now, as we don't want to remove everything from the channel buffer
2461 * before we're sure we can send it.
2462 */
2463 if (!(h1c->conn->flags & CO_FL_CONNECTED) ||
2464 (h1c->conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002465 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002466
Christopher Fauletc31872f2019-06-04 22:09:36 +02002467 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002468 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002469
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002470 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2471 ret = h1_process_output(h1c, buf, count);
2472 if (!ret)
2473 break;
2474 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002475 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002476 if (!h1_send(h1c))
2477 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002478 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002479
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002480 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002481}
2482
Willy Tarreaue5733232019-05-22 19:24:06 +02002483#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002484/* Send and get, using splicing */
2485static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2486{
2487 struct h1s *h1s = cs->ctx;
2488 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2489 int ret = 0;
2490
Christopher Faulet1146f972019-05-29 14:35:24 +02002491 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002492 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002493 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2494 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002495 goto end;
2496 }
2497
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002498 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002499 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002500 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002501 }
2502
2503 h1s->flags &= ~H1S_F_BUF_FLUSH;
2504 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002505 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2506 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002507 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002508 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002509 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002510 if (!h1m->curr_len)
2511 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2512 }
2513
Christopher Faulet1be55f92018-10-02 15:59:23 +02002514 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002515 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002516 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002517 if (!pipe->data)
2518 cs->flags |= CS_FL_EOS;
2519 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002520 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002521}
2522
2523static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2524{
2525 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002526 int ret = 0;
2527
2528 if (b_data(&h1s->h1c->obuf))
2529 goto end;
2530
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002531 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002532 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002533 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002534 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002535 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002536 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002537 return ret;
2538}
2539#endif
2540
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002541/* for debugging with CLI's "show fd" command */
2542static void h1_show_fd(struct buffer *msg, struct connection *conn)
2543{
2544 struct h1c *h1c = conn->ctx;
2545 struct h1s *h1s = h1c->h1s;
2546
Christopher Fauletf376a312019-01-04 15:16:06 +01002547 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2548 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002549 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2550 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2551 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2552 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2553
2554 if (h1s) {
2555 char *method;
2556
2557 if (h1s->meth < HTTP_METH_OTHER)
2558 method = http_known_methods[h1s->meth].ptr;
2559 else
2560 method = "UNKNOWN";
2561 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2562 " .meth=%s status=%d",
2563 h1s, h1s->flags,
2564 h1m_state_str(h1s->req.state),
2565 h1m_state_str(h1s->res.state), method, h1s->status);
2566 if (h1s->cs)
2567 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2568 h1s->cs->flags, h1s->cs->data);
2569 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002570}
2571
2572
2573/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2574static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2575{
2576 struct h1_hdr_entry *entry;
2577
2578 /* Be sure there is a non-empty <to> */
2579 if (!strlen(to)) {
2580 memprintf(err, "expect <to>");
2581 return -1;
2582 }
2583
2584 /* Be sure only the case differs between <from> and <to> */
2585 if (strcasecmp(from, to)) {
2586 memprintf(err, "<from> and <to> must not differ execpt the case");
2587 return -1;
2588 }
2589
2590 /* Be sure <from> does not already existsin the tree */
2591 if (ebis_lookup(&hdrs_map.map, from)) {
2592 memprintf(err, "duplicate entry '%s'", from);
2593 return -1;
2594 }
2595
2596 /* Create the entry and insert it in the tree */
2597 entry = malloc(sizeof(*entry));
2598 if (!entry) {
2599 memprintf(err, "out of memory");
2600 return -1;
2601 }
2602
2603 entry->node.key = strdup(from);
2604 entry->name.ptr = strdup(to);
2605 entry->name.len = strlen(to);
2606 if (!entry->node.key || !entry->name.ptr) {
2607 free(entry->node.key);
2608 free(entry->name.ptr);
2609 free(entry);
2610 memprintf(err, "out of memory");
2611 return -1;
2612 }
2613 ebis_insert(&hdrs_map.map, &entry->node);
2614 return 0;
2615}
2616
2617static void h1_hdeaders_case_adjust_deinit()
2618{
2619 struct ebpt_node *node, *next;
2620 struct h1_hdr_entry *entry;
2621
2622 node = ebpt_first(&hdrs_map.map);
2623 while (node) {
2624 next = ebpt_next(node);
2625 ebpt_delete(node);
2626 entry = container_of(node, struct h1_hdr_entry, node);
2627 free(entry->node.key);
2628 free(entry->name.ptr);
2629 free(entry);
2630 node = next;
2631 }
2632 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002633}
2634
Christopher Faulet98fbe952019-07-22 16:18:24 +02002635static int cfg_h1_headers_case_adjust_postparser()
2636{
2637 FILE *file = NULL;
2638 char *c, *key_beg, *key_end, *value_beg, *value_end;
2639 char *err;
2640 int rc, line = 0, err_code = 0;
2641
2642 if (!hdrs_map.name)
2643 goto end;
2644
2645 file = fopen(hdrs_map.name, "r");
2646 if (!file) {
2647 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2648 hdrs_map.name);
2649 err_code |= ERR_ALERT | ERR_FATAL;
2650 goto end;
2651 }
2652
2653 /* now parse all lines. The file may contain only two header name per
2654 * line, separated by spaces. All heading and trailing spaces will be
2655 * ignored. Lines starting with a # are ignored.
2656 */
2657 while (fgets(trash.area, trash.size, file) != NULL) {
2658 line++;
2659 c = trash.area;
2660
2661 /* strip leading spaces and tabs */
2662 while (*c == ' ' || *c == '\t')
2663 c++;
2664
2665 /* ignore emptu lines, or lines beginning with a dash */
2666 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2667 continue;
2668
2669 /* look for the end of the key */
2670 key_beg = c;
2671 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2672 c++;
2673 key_end = c;
2674
2675 /* strip middle spaces and tabs */
2676 while (*c == ' ' || *c == '\t')
2677 c++;
2678
2679 /* look for the end of the value, it is the end of the line */
2680 value_beg = c;
2681 while (*c && *c != '\n' && *c != '\r')
2682 c++;
2683 value_end = c;
2684
2685 /* trim possibly trailing spaces and tabs */
2686 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2687 value_end--;
2688
2689 /* set final \0 and check entries */
2690 *key_end = '\0';
2691 *value_end = '\0';
2692
2693 err = NULL;
2694 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2695 if (rc < 0) {
2696 free(err);
2697 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2698 hdrs_map.name, err, line);
2699 err_code |= ERR_ALERT | ERR_FATAL;
2700 goto end;
2701 }
2702 if (rc > 0) {
2703 free(err);
2704 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2705 hdrs_map.name, err, line);
2706 err_code |= ERR_WARN;
2707 }
2708 }
2709
2710 end:
2711 if (file)
2712 fclose(file);
2713 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2714 return err_code;
2715}
2716
2717
2718/* config parser for global "h1-outgoing-header-case-adjust" */
2719static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2720 struct proxy *defpx, const char *file, int line,
2721 char **err)
2722{
2723 if (too_many_args(2, args, err, NULL))
2724 return -1;
2725 if (!*(args[1]) || !*(args[2])) {
2726 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2727 return -1;
2728 }
2729 return add_hdr_case_adjust(args[1], args[2], err);
2730}
2731
2732/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2733static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2734 struct proxy *defpx, const char *file, int line,
2735 char **err)
2736{
2737 if (too_many_args(1, args, err, NULL))
2738 return -1;
2739 if (!*(args[1])) {
2740 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2741 return -1;
2742 }
2743 free(hdrs_map.name);
2744 hdrs_map.name = strdup(args[1]);
2745 return 0;
2746}
2747
2748
2749/* config keyword parsers */
2750static struct cfg_kw_list cfg_kws = {{ }, {
2751 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2752 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2753 { 0, NULL, NULL },
2754 }
2755};
2756
2757INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2758REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2759
2760
Christopher Faulet51dbc942018-09-13 09:05:15 +02002761/****************************************/
2762/* MUX initialization and instanciation */
2763/****************************************/
2764
2765/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002766static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002767 .init = h1_init,
2768 .wake = h1_wake,
2769 .attach = h1_attach,
2770 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002771 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002772 .detach = h1_detach,
2773 .destroy = h1_destroy,
2774 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002775 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002776 .rcv_buf = h1_rcv_buf,
2777 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002778#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002779 .rcv_pipe = h1_rcv_pipe,
2780 .snd_pipe = h1_snd_pipe,
2781#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002782 .subscribe = h1_subscribe,
2783 .unsubscribe = h1_unsubscribe,
2784 .shutr = h1_shutr,
2785 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002786 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002787 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002788 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002789 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002790};
2791
2792
2793/* this mux registers default HTX proto */
2794static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02002795{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02002796
Willy Tarreau0108d902018-11-25 19:14:37 +01002797INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2798
Christopher Faulet51dbc942018-09-13 09:05:15 +02002799/*
2800 * Local variables:
2801 * c-indent-level: 8
2802 * c-basic-offset: 8
2803 * End:
2804 */