blob: c8031a20aefa9017d845b262d3e4bd01b9ca86b5 [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 Fauleta99ff4d2019-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 Faulete31a0f12019-12-05 10:23:37 +010046/* 0x00000040 - 0x00000400 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020047
Christopher Faulete31a0f12019-12-05 10:23:37 +010048#define H1C_F_CS_WAIT_CONN 0x00000800 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020049#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
50#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010051#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Faulete31a0f12019-12-05 10:23:37 +010052#define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused
53 * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */
Christopher Faulet51dbc942018-09-13 09:05:15 +020054
Christopher Fauletf2824e62018-10-01 12:12:37 +020055#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 +020056#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020057
Christopher Faulet51dbc942018-09-13 09:05:15 +020058/*
59 * H1 Stream flags (32 bits)
60 */
Christopher Faulet129817b2018-09-20 16:14:40 +020061#define H1S_F_NONE 0x00000000
62#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020063#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
64#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020065#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020066#define H1S_F_WANT_KAL 0x00000010
67#define H1S_F_WANT_TUN 0x00000020
68#define H1S_F_WANT_CLO 0x00000040
69#define H1S_F_WANT_MSK 0x00000070
70#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010071#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010072#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010073#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
Willy Tarreau62038152019-08-23 09:29:29 +020074#define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */
75/* 0x00002000 .. 0x00002000 unused */
Christopher Fauletada34b62019-05-24 16:36:43 +020076#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020077
78/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020079struct h1c {
80 struct connection *conn;
81 struct proxy *px;
82 uint32_t flags; /* Connection flags: H1C_F_* */
83
84 struct buffer ibuf; /* Input buffer to store data before parsing */
85 struct buffer obuf; /* Output buffer to store data after reformatting */
86
87 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
88 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
89
90 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010091 struct task *task; /* timeout management task */
92 int timeout; /* idle timeout duration in ticks */
93 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020094};
95
96/* H1 stream descriptor */
97struct h1s {
98 struct h1c *h1c;
99 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100100 struct cs_info csinfo; /* CS info, only used for client connections */
101 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200102
Christopher Faulet51dbc942018-09-13 09:05:15 +0200103 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
104 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200105
Olivier Houchardf502aca2018-12-14 19:42:40 +0100106 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200107 struct h1m req;
108 struct h1m res;
109
110 enum http_meth_t meth; /* HTTP resquest method */
111 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200112};
113
Christopher Fauleta99ff4d2019-07-22 16:18:24 +0200114/* Map of headers used to convert outgoing headers */
115struct h1_hdrs_map {
116 char *name;
117 struct eb_root map;
118};
119
120/* An entry in a headers map */
121struct h1_hdr_entry {
122 struct ist name;
123 struct ebpt_node node;
124};
125
126/* Declare the headers map */
127static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
128
129
Christopher Faulet51dbc942018-09-13 09:05:15 +0200130/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100131DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
132DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200133
Christopher Faulet51dbc942018-09-13 09:05:15 +0200134static int h1_recv(struct h1c *h1c);
135static int h1_send(struct h1c *h1c);
136static int h1_process(struct h1c *h1c);
137static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100138static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100139static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200140
141/*****************************************************/
142/* functions below are for dynamic buffer management */
143/*****************************************************/
144/*
145 * Indicates whether or not the we may call the h1_recv() function to
146 * attempt to receive data into the buffer and/or parse pending data. The
147 * condition is a bit complex due to some API limits for now. The rules are the
148 * following :
149 * - if an error or a shutdown was detected on the connection and the buffer
150 * is empty, we must not attempt to receive
151 * - if the input buffer failed to be allocated, we must not try to receive
152 * and we know there is nothing pending
153 * - if no flag indicates a blocking condition, we may attempt to receive,
154 * regardless of whether the input buffer is full or not, so that only de
155 * receiving part decides whether or not to block. This is needed because
156 * the connection API indeed prevents us from re-enabling receipt that is
157 * already enabled in a polled state, so we must always immediately stop as
158 * soon as the mux can't proceed so as never to hit an end of read with data
159 * pending in the buffers.
160 * - otherwise must may not attempt to receive
161 */
162static inline int h1_recv_allowed(const struct h1c *h1c)
163{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100164 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200165 return 0;
166
Christopher Fauletcf56b992018-12-11 16:12:31 +0100167 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
168 return 0;
169
Christopher Fauletcb55f482018-12-10 11:56:47 +0100170 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200171 return 1;
172
173 return 0;
174}
175
176/*
177 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
178 * flags are used to figure what buffer was requested. It returns 1 if the
179 * allocation succeeds, in which case the connection is woken up, or 0 if it's
180 * impossible to wake up and we prefer to be woken up later.
181 */
182static int h1_buf_available(void *target)
183{
184 struct h1c *h1c = target;
185
186 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
187 h1c->flags &= ~H1C_F_IN_ALLOC;
188 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200189 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200190 return 1;
191 }
192
193 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
194 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200195 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200196 return 1;
197 }
198
Christopher Faulet51dbc942018-09-13 09:05:15 +0200199 return 0;
200}
201
202/*
203 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
204 */
205static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
206{
207 struct buffer *buf = NULL;
208
209 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
210 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
211 h1c->buf_wait.target = h1c;
212 h1c->buf_wait.wakeup_cb = h1_buf_available;
213 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
214 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
215 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
216 __conn_xprt_stop_recv(h1c->conn);
217 }
218 return buf;
219}
220
221/*
222 * Release a buffer, if any, and try to wake up entities waiting in the buffer
223 * wait queue.
224 */
225static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
226{
227 if (bptr->size) {
228 b_free(bptr);
229 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
230 }
231}
232
Christopher Faulete31a0f12019-12-05 10:23:37 +0100233/* returns the number of streams in use on a connection to figure if it's idle
234 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
235 * not. This flag is only set when no H1S is attached and when the previous
236 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100237 */
238static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200239{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100240 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200241
Christopher Faulete31a0f12019-12-05 10:23:37 +0100242 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200243}
244
Willy Tarreau00f18a32019-01-26 12:19:01 +0100245/* returns the number of streams still available on a connection */
246static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100247{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100248 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100249}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200250
Willy Tarreau00f18a32019-01-26 12:19:01 +0100251
Christopher Faulet51dbc942018-09-13 09:05:15 +0200252/*****************************************************************/
253/* functions below are dedicated to the mux setup and management */
254/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200255
256/* returns non-zero if there are input data pending for stream h1s. */
257static inline size_t h1s_data_pending(const struct h1s *h1s)
258{
259 const struct h1m *h1m;
260
261 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
262 if (h1m->state == H1_MSG_DONE)
263 return 0; // data not for this stream (e.g. pipelining)
264
265 return b_data(&h1s->h1c->ibuf);
266}
267
Christopher Faulet47365272018-10-31 17:40:50 +0100268static struct conn_stream *h1s_new_cs(struct h1s *h1s)
269{
270 struct conn_stream *cs;
271
272 cs = cs_new(h1s->h1c->conn);
273 if (!cs)
274 goto err;
275 h1s->cs = cs;
276 cs->ctx = h1s;
277
278 if (h1s->flags & H1S_F_NOT_FIRST)
279 cs->flags |= CS_FL_NOT_FIRST;
280
281 if (stream_create_from_cs(cs) < 0)
282 goto err;
283 return cs;
284
285 err:
286 cs_free(cs);
287 h1s->cs = NULL;
288 return NULL;
289}
290
Olivier Houchardf502aca2018-12-14 19:42:40 +0100291static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200292{
293 struct h1s *h1s;
294
295 h1s = pool_alloc(pool_head_h1s);
296 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100297 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200298
299 h1s->h1c = h1c;
300 h1c->h1s = h1s;
301
Olivier Houchardf502aca2018-12-14 19:42:40 +0100302 h1s->sess = sess;
303
Christopher Faulet51dbc942018-09-13 09:05:15 +0200304 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100305 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200306
307 h1s->recv_wait = NULL;
308 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200309
310 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100311 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200312
Christopher Faulet129817b2018-09-20 16:14:40 +0200313 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100314 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200315
316 h1s->status = 0;
317 h1s->meth = HTTP_METH_OTHER;
318
Christopher Faulet47365272018-10-31 17:40:50 +0100319 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
320 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulete31a0f12019-12-05 10:23:37 +0100321 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100322
Christopher Faulet129817b2018-09-20 16:14:40 +0200323 if (!conn_is_back(h1c->conn)) {
324 if (h1c->px->options2 & PR_O2_REQBUG_OK)
325 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200326
327 /* For frontend connections we should always have a session */
328 if (!sess)
329 sess = h1c->conn->owner;
330
Dave Pirottecf67ec52019-07-10 13:57:38 +0000331 /* Timers for subsequent sessions on the same HTTP 1.x connection
332 * measure from `now`, not from the connection accept time */
333 if (h1s->flags & H1S_F_NOT_FIRST) {
334 h1s->csinfo.create_date = date;
335 h1s->csinfo.tv_create = now;
336 h1s->csinfo.t_handshake = 0;
337 h1s->csinfo.t_idle = -1;
338 }
339 else {
340 h1s->csinfo.create_date = sess->accept_date;
341 h1s->csinfo.tv_create = sess->tv_accept;
342 h1s->csinfo.t_handshake = sess->t_handshake;
343 h1s->csinfo.t_idle = -1;
344 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200345 }
346 else {
347 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
348 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200349
Christopher Fauletfeb11742018-11-29 15:12:34 +0100350 h1s->csinfo.create_date = date;
351 h1s->csinfo.tv_create = now;
352 h1s->csinfo.t_handshake = 0;
353 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200354 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100355
Christopher Faulete9b70722019-04-08 10:46:02 +0200356 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
357 * create a new one.
358 */
359 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200360 cs->ctx = h1s;
361 h1s->cs = cs;
362 }
Christopher Faulet47365272018-10-31 17:40:50 +0100363 else {
364 cs = h1s_new_cs(h1s);
365 if (!cs)
366 goto fail;
367 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200368 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100369
370 fail:
371 pool_free(pool_head_h1s, h1s);
372 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200373}
374
375static void h1s_destroy(struct h1s *h1s)
376{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200377 if (h1s) {
378 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200379
Christopher Fauletf2824e62018-10-01 12:12:37 +0200380 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200381
Christopher Fauletf2824e62018-10-01 12:12:37 +0200382 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100383 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200384 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100385 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200386
Christopher Fauletcb55f482018-12-10 11:56:47 +0100387 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100388 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
389 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulete31a0f12019-12-05 10:23:37 +0100390
391 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
392 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
393 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
394 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
395 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
396 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200397 pool_free(pool_head_h1s, h1s);
398 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200399}
400
Christopher Fauletfeb11742018-11-29 15:12:34 +0100401static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
402{
403 struct h1s *h1s = cs->ctx;
404
405 if (h1s && !conn_is_back(cs->conn))
406 return &h1s->csinfo;
407 return NULL;
408}
409
Christopher Faulet51dbc942018-09-13 09:05:15 +0200410/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200411 * Initialize the mux once it's attached. It is expected that conn->ctx points
412 * to the existing conn_stream (for outgoing connections or for incoming onces
413 * during a mux upgrade) or NULL (for incoming ones during the connexion
414 * establishment). <input> is always used as Input buffer and may contain
415 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
416 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200417 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200418static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
419 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200420{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200421 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100422 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200423
424 h1c = pool_alloc(pool_head_h1c);
425 if (!h1c)
426 goto fail_h1c;
427 h1c->conn = conn;
428 h1c->px = proxy;
429
Christopher Faulete31a0f12019-12-05 10:23:37 +0100430 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200431 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200432 h1c->obuf = BUF_NULL;
433 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200434 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200435
Christopher Faulet51dbc942018-09-13 09:05:15 +0200436 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200437 h1c->wait_event.tasklet = tasklet_new();
438 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200439 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200440 h1c->wait_event.tasklet->process = h1_io_cb;
441 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100442 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200443
Christopher Faulete9b70722019-04-08 10:46:02 +0200444 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100445 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
446 if (tick_isset(proxy->timeout.serverfin))
447 h1c->shut_timeout = proxy->timeout.serverfin;
448 } else {
449 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
450 if (tick_isset(proxy->timeout.clientfin))
451 h1c->shut_timeout = proxy->timeout.clientfin;
452 }
453 if (tick_isset(h1c->timeout)) {
454 t = task_new(tid_bit);
455 if (!t)
456 goto fail;
457
458 h1c->task = t;
459 t->process = h1_timeout_task;
460 t->context = h1c;
461 t->expire = tick_add(now_ms, h1c->timeout);
462 }
463
Olivier Houchard985234d2019-06-13 17:54:33 +0200464 if (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200465 h1c->flags |= H1C_F_CS_WAIT_CONN;
466
Christopher Fauletf2824e62018-10-01 12:12:37 +0200467 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100468 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200469 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200470
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100471 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200472
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100473
474 if (t)
475 task_queue(t);
476
Christopher Faulet51dbc942018-09-13 09:05:15 +0200477 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200478 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200479
480 /* mux->wake will be called soon to complete the operation */
481 return 0;
482
483 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200484 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200485 if (h1c->wait_event.tasklet)
486 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200487 pool_free(pool_head_h1c, h1c);
488 fail_h1c:
489 return -1;
490}
491
Christopher Faulet73c12072019-04-08 11:23:22 +0200492/* release function. This one should be called to free all resources allocated
493 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200494 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200495static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200496{
Christopher Faulet61840e72019-04-15 09:33:32 +0200497 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200498
Christopher Faulet51dbc942018-09-13 09:05:15 +0200499 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200500 /* The connection must be aattached to this mux to be released */
501 if (h1c->conn && h1c->conn->ctx == h1c)
502 conn = h1c->conn;
503
504 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200505 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard13f556e2019-07-03 13:08:18 +0200506 /* Make sure we're no longer subscribed to anything */
507 if (h1c->wait_event.events)
508 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
509 h1c->wait_event.events, &h1c->wait_event);
Olivier Houchard45c44372019-06-11 14:06:23 +0200510 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200511 /* connection successfully upgraded to H2, this
512 * mux was already released */
513 return;
514 }
515 sess_log(conn->owner); /* Log if the upgrade failed */
516 }
517
Olivier Houchard45c44372019-06-11 14:06:23 +0200518
Christopher Faulet51dbc942018-09-13 09:05:15 +0200519 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
520 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
521 LIST_DEL(&h1c->buf_wait.list);
522 LIST_INIT(&h1c->buf_wait.list);
523 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
524 }
525
526 h1_release_buf(h1c, &h1c->ibuf);
527 h1_release_buf(h1c, &h1c->obuf);
528
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100529 if (h1c->task) {
530 h1c->task->context = NULL;
531 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
532 h1c->task = NULL;
533 }
534
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200535 if (h1c->wait_event.tasklet)
536 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200537
Christopher Fauletf2824e62018-10-01 12:12:37 +0200538 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200539 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100540 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200541 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200542 pool_free(pool_head_h1c, h1c);
543 }
544
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200545 if (conn) {
546 conn->mux = NULL;
547 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200548
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200549 conn_stop_tracking(conn);
550 conn_full_close(conn);
551 if (conn->destroy_cb)
552 conn->destroy_cb(conn);
553 conn_free(conn);
554 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200555}
556
557/******************************************************/
558/* functions below are for the H1 protocol processing */
559/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200560/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
561 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200562 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100563static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200564{
Christopher Faulet570d1612018-11-26 11:13:57 +0100565 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200566
Christopher Faulet570d1612018-11-26 11:13:57 +0100567 if ((HTX_SL_REQ_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/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
574 * greater or equal to 1.1
575 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100576static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200577{
Christopher Faulet570d1612018-11-26 11:13:57 +0100578 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200579
Christopher Faulet570d1612018-11-26 11:13:57 +0100580 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200581 (*(p + 5) > '1' ||
582 (*(p + 5) == '1' && *(p + 7) >= '1')))
583 h1m->flags |= H1_MF_VER_11;
584}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200585
Christopher Faulet9768c262018-10-22 09:34:31 +0200586/*
587 * Check the validity of the request version. If the version is valid, it
588 * returns 1. Otherwise, it returns 0.
589 */
590static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
591{
592 struct h1c *h1c = h1s->h1c;
593
594 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
595 * exactly one digit "." one digit. This check may be disabled using
596 * option accept-invalid-http-request.
597 */
598 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
599 if (sl.rq.v.len != 8)
600 return 0;
601
602 if (*(sl.rq.v.ptr + 4) != '/' ||
603 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
604 *(sl.rq.v.ptr + 6) != '.' ||
605 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
606 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200607 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200608 else if (!sl.rq.v.len) {
609 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200610
Christopher Faulet9768c262018-10-22 09:34:31 +0200611 /* RFC 1945 allows only GET for HTTP/0.9 requests */
612 if (sl.rq.meth != HTTP_METH_GET)
613 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200614
Christopher Faulet9768c262018-10-22 09:34:31 +0200615 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
616 if (!sl.rq.u.len)
617 return 0;
618
619 /* Add HTTP version */
620 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100621 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200622 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100623
624 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100625 ((*(sl.rq.v.ptr + 5) > '1') ||
626 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100627 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200628 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200629}
630
Christopher Faulet9768c262018-10-22 09:34:31 +0200631/*
632 * Check the validity of the response version. If the version is valid, it
633 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200634 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200635static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200636{
Christopher Faulet9768c262018-10-22 09:34:31 +0200637 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200638
Christopher Faulet9768c262018-10-22 09:34:31 +0200639 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
640 * exactly one digit "." one digit. This check may be disabled using
641 * option accept-invalid-http-request.
642 */
643 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
644 if (sl.st.v.len != 8)
645 return 0;
646
647 if (*(sl.st.v.ptr + 4) != '/' ||
648 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
649 *(sl.st.v.ptr + 6) != '.' ||
650 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
651 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200652 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100653
654 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100655 ((*(sl.st.v.ptr + 5) > '1') ||
656 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100657 h1m->flags |= H1_MF_VER_11;
658
Christopher Faulet9768c262018-10-22 09:34:31 +0200659 return 1;
660}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200661
662/* Deduce the connection mode of the client connection, depending on the
663 * configuration and the H1 message flags. This function is called twice, the
664 * first time when the request is parsed and the second time when the response
665 * is parsed.
666 */
667static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
668{
669 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200670
671 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100672 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200673 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100674 h1s->status == 101) {
675 /* Either we've established an explicit tunnel, or we're
676 * switching the protocol. In both cases, we're very unlikely to
677 * understand the next protocols. We have to switch to tunnel
678 * mode, so that we transfer the request and responses then let
679 * this protocol pass unmodified. When we later implement
680 * specific parsers for such protocols, we'll want to check the
681 * Upgrade header which contains information about that protocol
682 * for responses with status 101 (eg: see RFC2817 about TLS).
683 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200684 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100685 }
686 else if (h1s->flags & H1S_F_WANT_KAL) {
687 /* By default the client is in KAL mode. CLOSE mode mean
688 * it is imposed by the client itself. So only change
689 * KAL mode here. */
690 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
691 /* no length known or explicit close => close */
692 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
693 }
694 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
695 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
696 /* no explict keep-alive and option httpclose => close */
697 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
698 }
699 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200700 }
701 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100702 /* Input direction: first pass */
703 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
704 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200705 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100706 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200707 }
708
709 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
710 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
711 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
712}
713
714/* Deduce the connection mode of the client connection, depending on the
715 * configuration and the H1 message flags. This function is called twice, the
716 * first time when the request is parsed and the second time when the response
717 * is parsed.
718 */
719static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
720{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100721 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100722 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100723 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200724
Christopher Fauletf2824e62018-10-01 12:12:37 +0200725 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100726 /* Input direction: second pass */
727
Christopher Fauletf2824e62018-10-01 12:12:37 +0200728 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100729 h1s->status == 101) {
730 /* Either we've established an explicit tunnel, or we're
731 * switching the protocol. In both cases, we're very unlikely to
732 * understand the next protocols. We have to switch to tunnel
733 * mode, so that we transfer the request and responses then let
734 * this protocol pass unmodified. When we later implement
735 * specific parsers for such protocols, we'll want to check the
736 * Upgrade header which contains information about that protocol
737 * for responses with status 101 (eg: see RFC2817 about TLS).
738 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200739 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100740 }
741 else if (h1s->flags & H1S_F_WANT_KAL) {
742 /* By default the server is in KAL mode. CLOSE mode mean
743 * it is imposed by haproxy itself. So only change KAL
744 * mode here. */
745 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
746 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
747 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
748 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
749 }
750 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200751 }
Christopher Faulet70033782018-12-05 13:50:11 +0100752 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100753 /* Output direction: first pass */
754 if (h1m->flags & H1_MF_CONN_CLO) {
755 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100756 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100757 }
758 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
759 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
760 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
761 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
762 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
763 /* no explicit keep-alive option httpclose/server-close => close */
764 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
765 }
Christopher Faulet70033782018-12-05 13:50:11 +0100766 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200767
768 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
769 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
770 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200771}
772
Christopher Fauletb992af02019-03-28 15:42:24 +0100773static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200774{
775 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200776
777 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
778 * token is found
779 */
780 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200781 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200782
783 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100784 if (!(h1m->flags & H1_MF_VER_11))
785 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200786 }
787 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100788 if (h1m->flags & H1_MF_VER_11)
789 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200790 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200791}
792
Christopher Fauletb992af02019-03-28 15:42:24 +0100793static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200794{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200795 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
796 * token is found
797 */
798 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200799 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200800
801 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100802 if (!(h1m->flags & H1_MF_VER_11) ||
803 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
804 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200805 }
806 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100807 if (h1m->flags & H1_MF_VER_11)
808 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200809 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200810}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200811
Christopher Fauletb992af02019-03-28 15:42:24 +0100812static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200813{
Christopher Fauletb992af02019-03-28 15:42:24 +0100814 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200815 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100816 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200817 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200818}
819
Christopher Fauletb992af02019-03-28 15:42:24 +0100820static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
821{
822 if (!conn_is_back(h1s->h1c->conn))
823 h1_set_cli_conn_mode(h1s, h1m);
824 else
825 h1_set_srv_conn_mode(h1s, h1m);
826
827 if (!(h1m->flags & H1_MF_RESP))
828 h1_update_req_conn_value(h1s, h1m, conn_val);
829 else
830 h1_update_res_conn_value(h1s, h1m, conn_val);
831}
Christopher Faulete44769b2018-11-29 23:01:45 +0100832
Christopher Fauleta99ff4d2019-07-22 16:18:24 +0200833/* Try to adjust the case of the message header name using the global map
834 * <hdrs_map>.
835 */
836static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
837{
838 struct ebpt_node *node;
839 struct h1_hdr_entry *entry;
840
841 /* No entry in the map, do nothing */
842 if (eb_is_empty(&hdrs_map.map))
843 return;
844
845 /* No conversion fo the request headers */
846 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
847 return;
848
849 /* No conversion fo the response headers */
850 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
851 return;
852
853 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
854 if (!node)
855 return;
856 entry = container_of(node, struct h1_hdr_entry, node);
857 name->ptr = entry->name.ptr;
858 name->len = entry->name.len;
859}
860
Christopher Faulete44769b2018-11-29 23:01:45 +0100861/* Append the description of what is present in error snapshot <es> into <out>.
862 * The description must be small enough to always fit in a buffer. The output
863 * buffer may be the trash so the trash must not be used inside this function.
864 */
865static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
866{
867 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100868 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
869 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
870 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
871 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
872 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
873 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100874}
875/*
876 * Capture a bad request or response and archive it in the proxy's structure.
877 * By default it tries to report the error position as h1m->err_pos. However if
878 * this one is not set, it will then report h1m->next, which is the last known
879 * parsing point. The function is able to deal with wrapping buffers. It always
880 * displays buffers as a contiguous area starting at buf->p. The direction is
881 * determined thanks to the h1m's flags.
882 */
883static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
884 struct h1m *h1m, struct buffer *buf)
885{
886 struct session *sess = h1c->conn->owner;
887 struct proxy *proxy = h1c->px;
888 struct proxy *other_end = sess->fe;
889 union error_snapshot_ctx ctx;
890
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100891 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100892 other_end = si_strm(h1s->cs->data)->be;
893
894 /* http-specific part now */
895 ctx.h1.state = h1m->state;
896 ctx.h1.c_flags = h1c->flags;
897 ctx.h1.s_flags = h1s->flags;
898 ctx.h1.m_flags = h1m->flags;
899 ctx.h1.m_clen = h1m->curr_len;
900 ctx.h1.m_blen = h1m->body_len;
901
902 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
903 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100904 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
905 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100906}
907
Christopher Fauletadb22202018-12-12 10:32:09 +0100908/* Emit the chunksize followed by a CRLF in front of data of the buffer
909 * <buf>. It goes backwards and starts with the byte before the buffer's
910 * head. The caller is responsible for ensuring there is enough room left before
911 * the buffer's head for the string.
912 */
913static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
914{
915 char *beg, *end;
916
917 beg = end = b_head(buf);
918 *--beg = '\n';
919 *--beg = '\r';
920 do {
921 *--beg = hextab[chksz & 0xF];
922 } while (chksz >>= 4);
923 buf->head -= (end - beg);
924 b_add(buf, end - beg);
925}
926
927/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
928 * ensuring there is enough room left in the buffer for the string. */
929static void h1_emit_chunk_crlf(struct buffer *buf)
930{
931 *(b_peek(buf, b_data(buf))) = '\r';
932 *(b_peek(buf, b_data(buf) + 1)) = '\n';
933 b_add(buf, 2);
934}
935
Christopher Faulet129817b2018-09-20 16:14:40 +0200936/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100937 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletac4778c2019-11-15 11:14:23 +0100938 * CONNECT requests. On the client side, if the response is not finished, the
939 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100940 */
941static void h1_set_req_tunnel_mode(struct h1s *h1s)
942{
943 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
944 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100945 if (!conn_is_back(h1s->h1c->conn) && h1s->res.state < H1_MSG_DONE)
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100946 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100947 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
948 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
949 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
950 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100951}
952
953/*
954 * Switch the response to tunnel mode. This function must only be called on
Christopher Fauletac4778c2019-11-15 11:14:23 +0100955 * successfull replies to CONNECT requests or on protocol switching. In this
956 * last case, this function takes care to switch the request to tunnel mode if
957 * possible. On the server side, if the request is not finished, the mux is mark
958 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100959 */
960static void h1_set_res_tunnel_mode(struct h1s *h1s)
961{
Christopher Fauletac4778c2019-11-15 11:14:23 +0100962 /* On protocol switching, switch the request to tunnel mode if it is in
963 * DONE state. Otherwise we will wait the end of the request to switch
964 * it in tunnel mode.
965 */
966 if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
967 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
968 h1s->req.state = H1_MSG_TUNNEL;
969 }
970
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100971 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
972 h1s->res.state = H1_MSG_TUNNEL;
973 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
974 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100975 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
976 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
977 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100978 }
979}
980
Christopher Faulet82f01602019-05-24 16:50:16 +0200981/* Estimate the size of the HTX headers after the parsing, including the EOH. */
982static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
983{
984 size_t sz = 0;
985 int i;
986
987 for (i = 0; hdrs[i].n.len; i++)
988 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
989 sz += sizeof(struct htx_blk) + 1;
990 return sz;
991}
992
Christopher Faulet30db3d72019-05-17 15:35:33 +0200993/* Estimate the size of the HTX request after the parsing. */
994static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
995{
996 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200997
998 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +0200999 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 +02001000 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001001 return sz;
1002}
1003
1004/* Estimate the size of the HTX response after the parsing. */
1005static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
1006{
1007 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001008
1009 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +02001010 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 +02001011 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001012 return sz;
1013}
1014
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001015/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001016 * Add the EOM in the HTX message and switch the message to the DONE state. It
1017 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
1018 * functions is responsible to update the parser state <h1m>. It also add the
1019 * flag CS_FL_EOI on the CS.
1020 */
1021static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
1022{
Willy Tarreau62038152019-08-23 09:29:29 +02001023 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
1024 h1s->flags |= H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001025 return 0;
Willy Tarreau62038152019-08-23 09:29:29 +02001026 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001027
Willy Tarreau62038152019-08-23 09:29:29 +02001028 h1s->flags &= ~H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001029 h1m->state = H1_MSG_DONE;
1030 h1s->cs->flags |= CS_FL_EOI;
1031 return (sizeof(struct htx_blk) + 1);
1032}
1033
1034/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001035 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001036 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1037 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001038 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001039 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001040static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001041 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001042{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001043 struct htx_sl *sl;
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001044 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001045 union h1_sl h1sl;
1046 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001047 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +02001048 int ret = 0;
1049
Christopher Faulet30db3d72019-05-17 15:35:33 +02001050 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001051 goto end;
1052
Christopher Faulet129817b2018-09-20 16:14:40 +02001053 /* Realing input buffer if necessary */
1054 if (b_head(buf) + b_data(buf) > b_wrap(buf))
1055 b_slow_realign(buf, trash.area, 0);
1056
Christopher Faulet842f41f2019-09-25 09:10:46 +02001057 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001058 /* Try to match H2 preface before parsing the request headers. */
1059 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
1060 if (ret > 0)
1061 goto h2c_upgrade;
1062 }
1063
Christopher Faulet30db3d72019-05-17 15:35:33 +02001064 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001065 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +02001066 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001067 /* Incomplete or invalid message. If the input buffer only
1068 * contains headers and is full, which is detected by it being
1069 * full and the offset to be zero, it's an error because
1070 * headers are too large to be handled by the parser. */
1071 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001072 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001073 goto end;
1074 }
1075
Christopher Fauletb4bad502019-10-11 14:22:00 +02001076 if (h1m->err_pos >= 0) {
1077 /* Maybe we found an error during the parsing while we were
1078 * configured not to block on that, so we have to capture it
1079 * now.
1080 */
1081 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1082 }
1083
Christopher Faulet129817b2018-09-20 16:14:40 +02001084 /* messages headers fully parsed, do some checks to prepare the body
1085 * parsing.
1086 */
1087
Christopher Faulet9768c262018-10-22 09:34:31 +02001088 /* Save the request's method or the response's status, check if the body
1089 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001090 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001091 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001092
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001093 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001094 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001095
1096 if (h1s->meth == HTTP_METH_CONNECT) {
1097 /* Switch CONNECT requests to tunnel mode */
1098 h1_set_req_tunnel_mode(h1s);
1099 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001100
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001101 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1102 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001103 h1m->err_state = h1m->state;
1104 goto vsn_error;
1105 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001106 }
1107 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001108 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001109
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001110 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1111 h1s->status == 101) {
1112 /* Switch successfull replies to CONNECT requests and
1113 * protocol switching to tunnel mode. */
1114 h1_set_res_tunnel_mode(h1s);
1115 }
1116 else if ((h1s->meth == HTTP_METH_HEAD) ||
1117 (h1s->status >= 100 && h1s->status < 200) ||
1118 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001119 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001120 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1121 h1m->flags |= H1_MF_XFER_LEN;
1122 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001123 }
1124 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001125 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001126 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001127 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001128 else {
1129 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001130 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001131 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001132
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001133 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1134 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001135 h1m->err_state = h1m->state;
1136 goto vsn_error;
1137 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001138 }
1139
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001140 /* Set HTX start-line flags */
1141 if (h1m->flags & H1_MF_VER_11)
1142 flags |= HTX_SL_F_VER_11;
1143 if (h1m->flags & H1_MF_XFER_ENC)
1144 flags |= HTX_SL_F_XFER_ENC;
1145 if (h1m->flags & H1_MF_XFER_LEN) {
1146 flags |= HTX_SL_F_XFER_LEN;
1147 if (h1m->flags & H1_MF_CHNK)
1148 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001149 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001150 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001151 if (h1m->body_len == 0)
1152 flags |= HTX_SL_F_BODYLESS;
1153 }
1154 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001155 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001156 }
1157
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001158 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001159 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001160 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1161 if (htx_is_empty(htx))
1162 goto error;
1163 h1m_init_req(h1m);
1164 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1165 ret = 0;
1166 goto end;
1167 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001168
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001169 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1170 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001171 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001172 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001173
1174 /* Check if the uri contains an explicit scheme and if it is
1175 * "http" or "https". */
1176 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1177 sl->flags |= HTX_SL_F_HAS_SCHM;
1178 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1179 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1180 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001181 }
1182 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001183 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1184 if (htx_is_empty(htx))
1185 goto error;
1186 h1m_init_res(h1m);
1187 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1188 ret = 0;
1189 goto end;
1190 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001191
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001192 flags |= HTX_SL_F_IS_RESP;
1193 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1194 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001195 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001196 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001197 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001198
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001199 /* Set bytes used in the HTX mesage for the headers now */
1200 sl->hdrs_bytes = htx_used_space(htx) - used;
1201
Christopher Fauletb992af02019-03-28 15:42:24 +01001202 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001203
1204 /* If body length cannot be determined, set htx->extra to
1205 * ULLONG_MAX. This value is impossible in other cases.
1206 */
1207 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001208 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 end:
1210 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001211
1212 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001213 h1m->err_state = h1m->state;
1214 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001215 vsn_error:
1216 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001217 h1s->cs->flags |= CS_FL_EOI;
1218 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001219 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001220 ret = 0;
1221 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001222
1223 h2c_upgrade:
1224 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001225 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001226 htx->flags |= HTX_FL_UPGRADE;
1227 ret = 0;
1228 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001229}
1230
1231/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001232 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1233 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001234 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001235 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001236 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001237static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001238 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001239 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001240{
1241 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001242 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001243
Christopher Faulet129817b2018-09-20 16:14:40 +02001244 if (h1m->flags & H1_MF_XFER_LEN) {
1245 if (h1m->flags & H1_MF_CLEN) {
1246 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001247 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001248 if ((uint64_t)ret > h1m->curr_len)
1249 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001250 if (ret > b_contig_data(buf, *ofs))
1251 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001252
Christopher Faulet9768c262018-10-22 09:34:31 +02001253 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001254 /* very often with large files we'll face the following
1255 * situation :
1256 * - htx is empty and points to <htxbuf>
1257 * - ret == buf->data
1258 * - buf->head == sizeof(struct htx)
1259 * => we can swap the buffers and place an htx header into
1260 * the target buffer instead
1261 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001262 int32_t try = ret;
1263
Willy Tarreau78f548f2018-12-05 10:02:39 +01001264 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1265 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1266 void *raw_area = buf->area;
1267 void *htx_area = htxbuf->area;
1268 struct htx_blk *blk;
1269
1270 buf->area = htx_area;
1271 htxbuf->area = raw_area;
1272 htx = (struct htx *)htxbuf->area;
1273 htx->size = htxbuf->size - sizeof(*htx);
1274 htx_reset(htx);
1275 b_set_data(htxbuf, b_size(htxbuf));
1276
1277 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1278 blk->info += ret;
1279 /* nothing else to do, the old buffer now contains an
1280 * empty pre-initialized HTX header
1281 */
1282 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001283 else {
1284 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1285 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001286 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001287 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001288 *ofs += ret;
1289 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001290 if (ret < try)
1291 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001292 }
1293
1294 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001295 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001296 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001297 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001298 }
1299 else if (h1m->flags & H1_MF_CHNK) {
1300 new_chunk:
1301 /* te:chunked : parse chunks */
1302 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001303 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001304 if (ret <= 0)
1305 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001306 h1m->state = H1_MSG_CHUNK_SIZE;
1307
Christopher Fauletf2824e62018-10-01 12:12:37 +02001308 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001309 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001310 }
1311
1312 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1313 unsigned int chksz;
1314
Christopher Faulet30db3d72019-05-17 15:35:33 +02001315 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001316 if (ret <= 0)
1317 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001318 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001319
Christopher Faulet129817b2018-09-20 16:14:40 +02001320 h1m->curr_len = chksz;
1321 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001322 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001323 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001324
1325 if (!h1m->curr_len)
1326 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001327 }
1328
1329 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001330 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001331 if ((uint64_t)ret > h1m->curr_len)
1332 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001333 if (ret > b_contig_data(buf, *ofs))
1334 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001335
Christopher Faulet9768c262018-10-22 09:34:31 +02001336 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001337 int32_t try = ret;
1338
1339 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001340 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001341 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001342 *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 }
1347 if (!h1m->curr_len) {
1348 h1m->state = H1_MSG_CHUNK_CRLF;
1349 goto new_chunk;
1350 }
1351 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001352 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001353 }
1354 else {
1355 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1356 * is no body. Switch the message in DONE state
1357 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001358 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001359 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001360 }
1361 }
1362 else {
1363 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001364 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001365 if (ret > b_contig_data(buf, *ofs))
1366 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001367
Christopher Faulet9768c262018-10-22 09:34:31 +02001368 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001369 int32_t try = ret;
1370
1371 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001372
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001373 *ofs += ret;
1374 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001375 if (ret < try)
1376 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001377 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001378 }
1379
1380 end:
1381 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001382 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001383 h1s->cs->flags |= CS_FL_EOI;
1384 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001385 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001386 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001387 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001388 return 0;
1389 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001390 /* update htx->extra, only when the body length is known */
1391 if (h1m->flags & H1_MF_XFER_LEN)
1392 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001393 return total;
1394}
1395
1396/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001397 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1398 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1399 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1400 * responsible to update the parser state <h1m>.
1401 */
1402static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1403 struct buffer *buf, size_t *ofs, size_t max)
1404{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001405 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001406 struct h1m tlr_h1m;
1407 int ret = 0;
1408
1409 if (!max || !b_data(buf))
1410 goto end;
1411
1412 /* Realing input buffer if necessary */
1413 if (b_peek(buf, *ofs) > b_tail(buf))
1414 b_slow_realign(buf, trash.area, 0);
1415
1416 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1417 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1418 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1419 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001420 /* Incomplete or invalid trailers. If the input buffer only
1421 * contains trailers and is full, which is detected by it being
1422 * full and the offset to be zero, it's an error because
1423 * trailers are too large to be handled by the parser. */
1424 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001425 goto error;
1426 goto end;
1427 }
1428
1429 /* messages trailers fully parsed. */
1430 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1431 if (htx_is_empty(htx))
1432 goto error;
1433 ret = 0;
1434 goto end;
1435 }
1436
1437 if (!htx_add_all_trailers(htx, hdrs))
1438 goto error;
1439
1440 *ofs += ret;
1441 h1s->flags |= H1S_F_HAVE_I_TLR;
1442 end:
1443 return ret;
1444
1445 error:
1446 h1m->err_state = h1m->state;
1447 h1m->err_pos = h1m->next;
1448 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1449 h1s->cs->flags |= CS_FL_EOI;
1450 htx->flags |= HTX_FL_PARSING_ERROR;
1451 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1452 ret = 0;
1453 goto end;
1454}
1455
1456/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001457 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001458 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1459 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001460 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001461static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001462{
Christopher Faulet539e0292018-11-19 10:40:09 +01001463 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001464 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001465 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001466 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001467 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001468 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001469
Christopher Faulet539e0292018-11-19 10:40:09 +01001470 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001471
Christopher Fauletf2824e62018-10-01 12:12:37 +02001472 if (!conn_is_back(h1c->conn)) {
1473 h1m = &h1s->req;
1474 errflag = H1S_F_REQ_ERROR;
1475 }
1476 else {
1477 h1m = &h1s->res;
1478 errflag = H1S_F_RES_ERROR;
1479 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001480
Christopher Faulet74027762019-02-26 14:45:05 +01001481 data = htx->data;
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001482 if (h1s->flags & errflag)
1483 goto end;
1484
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001485 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001486 size_t used = htx_used_space(htx);
1487
Christopher Faulet129817b2018-09-20 16:14:40 +02001488 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001489 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001490 if (!ret)
1491 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001492 if ((h1m->flags & H1_MF_RESP) &&
1493 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1494 h1m_init_res(&h1s->res);
1495 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1496 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001497 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001498 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001499 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001500 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001501 if (!ret)
1502 break;
1503 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001504 else if (h1m->state == H1_MSG_TRAILERS) {
1505 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1506 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1507 if (!ret)
1508 break;
1509 }
Christopher Faulet1021e722019-09-03 21:55:14 +02001510 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001511 break;
1512 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001513 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletac4778c2019-11-15 11:14:23 +01001514 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1515 h1_set_req_tunnel_mode(h1s);
1516 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001517 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001518 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001519 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001520 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001521 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001522 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001523 if (!ret)
1524 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001525 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001526 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001527 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001528 break;
1529 }
1530
Christopher Faulet30db3d72019-05-17 15:35:33 +02001531 count -= htx_used_space(htx) - used;
Christopher Faulet98e2bc22019-09-03 16:26:15 +02001532 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001533
Christopher Faulet47365272018-10-31 17:40:50 +01001534 if (h1s->flags & errflag)
1535 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001536
Christopher Faulet539e0292018-11-19 10:40:09 +01001537 b_del(&h1c->ibuf, total);
1538
1539 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001540 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001541 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001542 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001543 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001544 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001545 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001546
Christopher Fauletcf56b992018-12-11 16:12:31 +01001547 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1548
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001549 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001550 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001551 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001552 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001553
Willy Tarreau62038152019-08-23 09:29:29 +02001554 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1555 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001556 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001557 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001558 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001559 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001560
Christopher Faulet30db3d72019-05-17 15:35:33 +02001561 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001562
1563 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001564 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001565 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001566 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001567}
1568
Christopher Faulet129817b2018-09-20 16:14:40 +02001569/*
1570 * Process outgoing data. It parses data and transfer them from the channel buffer into
1571 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1572 * 0 if it couldn't proceed.
1573 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001574static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1575{
Christopher Faulet129817b2018-09-20 16:14:40 +02001576 struct h1s *h1s = h1c->h1s;
1577 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001578 struct htx *chn_htx;
1579 struct htx_blk *blk;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001580 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001581 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001582 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001583
Christopher Faulet47365272018-10-31 17:40:50 +01001584 if (!count)
1585 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001586
Christopher Faulet94b2c762019-05-24 15:28:57 +02001587 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001588 if (htx_is_empty(chn_htx))
1589 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001590
Christopher Faulet51dbc942018-09-13 09:05:15 +02001591 if (!h1_get_buf(h1c, &h1c->obuf)) {
1592 h1c->flags |= H1C_F_OUT_ALLOC;
1593 goto end;
1594 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001595
Christopher Fauletf2824e62018-10-01 12:12:37 +02001596 if (!conn_is_back(h1c->conn)) {
1597 h1m = &h1s->res;
1598 errflag = H1S_F_RES_ERROR;
1599 }
1600 else {
1601 h1m = &h1s->req;
1602 errflag = H1S_F_REQ_ERROR;
1603 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001604
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001605 if (h1s->flags & errflag)
1606 goto end;
1607
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001608 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001609 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001610
Willy Tarreau3815b222018-12-11 19:50:43 +01001611 /* Perform some optimizations to reduce the number of buffer copies.
1612 * First, if the mux's buffer is empty and the htx area contains
1613 * exactly one data block of the same size as the requested count,
1614 * then it's possible to simply swap the caller's buffer with the
1615 * mux's output buffer and adjust offsets and length to match the
1616 * entire DATA HTX block in the middle. In this case we perform a
1617 * true zero-copy operation from end-to-end. This is the situation
1618 * that happens all the time with large files. Second, if this is not
1619 * possible, but the mux's output buffer is empty, we still have an
1620 * opportunity to avoid the copy to the intermediary buffer, by making
1621 * the intermediary buffer's area point to the output buffer's area.
1622 * In this case we want to skip the HTX header to make sure that copies
1623 * remain aligned and that this operation remains possible all the
1624 * time. This goes for headers, data blocks and any data extracted from
1625 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001626 */
1627 if (!b_data(&h1c->obuf)) {
Willy Tarreau3815b222018-12-11 19:50:43 +01001628 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001629 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001630 htx_get_blk_value(chn_htx, blk).len == count) {
1631 void *old_area = h1c->obuf.area;
1632
1633 h1c->obuf.area = buf->area;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001634 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001635 h1c->obuf.data = count;
1636
1637 buf->area = old_area;
1638 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001639
1640 /* The message is chunked. We need to emit the chunk
1641 * size. We have at least the size of the struct htx to
1642 * write the chunk envelope. It should be enough.
1643 */
1644 if (h1m->flags & H1_MF_CHNK) {
1645 h1_emit_chunk_size(&h1c->obuf, count);
1646 h1_emit_chunk_crlf(&h1c->obuf);
1647 }
1648
Willy Tarreau3815b222018-12-11 19:50:43 +01001649 total += count;
1650 goto out;
1651 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001652 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001653 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001654 else
1655 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001656
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001657 tmp.data = 0;
1658 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001659 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001660 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001661 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001662 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001663 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001664 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001665
Christopher Fauletb2e84162018-12-06 11:39:49 +01001666 vlen = sz;
1667 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001668 if (type != HTX_BLK_DATA)
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001669 goto full;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001670 vlen = count;
1671 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001672
Christopher Faulet94b2c762019-05-24 15:28:57 +02001673 if (type == HTX_BLK_UNUSED)
1674 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001675
Christopher Faulet94b2c762019-05-24 15:28:57 +02001676 switch (h1m->state) {
1677 case H1_MSG_RQBEFORE:
1678 if (type != HTX_BLK_REQ_SL)
1679 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001680 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001681 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001682 h1_parse_req_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001683 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001684 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001685 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001686 if (sl->flags & HTX_SL_F_BODYLESS)
1687 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001688 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001689 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001690
Christopher Faulet94b2c762019-05-24 15:28:57 +02001691 case H1_MSG_RPBEFORE:
1692 if (type != HTX_BLK_RES_SL)
1693 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001694 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001695 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001696 h1_parse_res_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001697 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001698 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001699 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001700 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001701 if (sl->info.res.status < 200 &&
1702 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001703 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001704 h1m->state = H1_MSG_HDR_FIRST;
1705 break;
1706
Christopher Faulet94b2c762019-05-24 15:28:57 +02001707 case H1_MSG_HDR_FIRST:
1708 case H1_MSG_HDR_NAME:
1709 case H1_MSG_HDR_L2_LWS:
1710 if (type == HTX_BLK_EOH)
1711 goto last_lf;
1712 if (type != HTX_BLK_HDR)
1713 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001714 h1m->state = H1_MSG_HDR_NAME;
1715 n = htx_get_blk_name(chn_htx, blk);
1716 v = htx_get_blk_value(chn_htx, blk);
1717
1718 if (isteqi(n, ist("transfer-encoding")))
1719 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001720 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001721 /* Only skip C-L header with invalid value. */
1722 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001723 goto skip_hdr;
1724 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001725 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001726 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001727 if (!v.len)
1728 goto skip_hdr;
1729 }
1730
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001731 /* Try to adjust the case of the header name */
1732 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1733 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001734 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001735 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001736 skip_hdr:
1737 h1m->state = H1_MSG_HDR_L2_LWS;
1738 break;
1739
Christopher Faulet94b2c762019-05-24 15:28:57 +02001740 case H1_MSG_LAST_LF:
1741 if (type != HTX_BLK_EOH)
1742 goto error;
1743 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001744 h1m->state = H1_MSG_LAST_LF;
1745 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001746 /* There is no "Connection:" header and
1747 * it the conn_mode must be
1748 * processed. So do it */
Christopher Faulet661bfc32019-06-17 14:07:46 +02001749 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001750 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001751 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001752 if (v.len) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001753 /* Try to adjust the case of the header name */
1754 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1755 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001756 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001757 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001758 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001759 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001760 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001761
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001762 if ((h1s->meth != HTTP_METH_CONNECT &&
1763 (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 +01001764 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1765 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1766 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1767 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1768 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001769 /* chunking needed but header not seen */
Christopher Faulete5addb02019-10-04 10:23:51 +02001770 n = ist("transfer-encoding");
1771 v = ist("chunked");
1772 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1773 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
1774 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001775 goto full;
Willy Tarreau4710d202019-01-03 17:39:54 +01001776 h1m->flags |= H1_MF_CHNK;
1777 }
1778
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001779 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001780 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001781
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001782 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1783 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1784 h1_set_req_tunnel_mode(h1s);
1785 }
Christopher Fauletac4778c2019-11-15 11:14:23 +01001786 else if ((h1m->flags & H1_MF_RESP) &&
1787 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001788 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletac4778c2019-11-15 11:14:23 +01001789 * to the client. Switch the response to tunnel mode.
1790 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001791 h1_set_res_tunnel_mode(h1s);
1792 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001793 else if ((h1m->flags & H1_MF_RESP) &&
1794 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1795 h1m_init_res(&h1s->res);
1796 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001797 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001798 }
Christopher Faulet33d58b52019-07-01 16:17:30 +02001799 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1800 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001801 else
1802 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001803 break;
1804
Christopher Faulet94b2c762019-05-24 15:28:57 +02001805 case H1_MSG_DATA:
Christopher Fauletaaa25062019-07-04 17:12:12 +02001806 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001807 if (type == HTX_BLK_EOM) {
1808 /* Chunked message without explicit trailers */
1809 if (h1m->flags & H1_MF_CHNK) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001810 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001811 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001812 }
1813 goto done;
1814 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001815 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet52131682019-06-27 17:40:14 +02001816 /* If the message is not chunked, never
1817 * add the last chunk. */
1818 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001819 goto full;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001820 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001821 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001822 else if (type != HTX_BLK_DATA)
1823 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001824 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001825 v.len = vlen;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001826 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001827 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001828 break;
1829
Christopher Faulet94b2c762019-05-24 15:28:57 +02001830 case H1_MSG_TRAILERS:
1831 if (type == HTX_BLK_EOM)
1832 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001833 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001834 goto error;
1835 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001836 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet52131682019-06-27 17:40:14 +02001837 /* If the message is not chunked, ignore
1838 * trailers. It may happen with H2 messages. */
1839 if (!(h1m->flags & H1_MF_CHNK))
1840 break;
1841
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001842 if (type == HTX_BLK_EOT) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001843 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001844 goto full;
Christopher Faulet32188212018-11-20 18:21:43 +01001845 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001846 else { // HTX_BLK_TLR
1847 n = htx_get_blk_name(chn_htx, blk);
1848 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001849
1850 /* Try to adjust the case of the header name */
1851 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1852 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001853 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001854 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001855 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001856 break;
1857
Christopher Faulet94b2c762019-05-24 15:28:57 +02001858 case H1_MSG_DONE:
1859 if (type != HTX_BLK_EOM)
1860 goto error;
1861 done:
1862 h1m->state = H1_MSG_DONE;
Christopher Fauletac4778c2019-11-15 11:14:23 +01001863 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1864 h1_set_req_tunnel_mode(h1s);
1865 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001866 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1867 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1868 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001869 break;
1870
Christopher Faulet9768c262018-10-22 09:34:31 +02001871 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001872 error:
Christopher Fauletc431df82019-06-26 15:16:28 +02001873 /* Unexpected error during output processing */
1874 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001875 h1s->flags |= errflag;
Christopher Fauletc431df82019-06-26 15:16:28 +02001876 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001877 break;
1878 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001879
1880 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001881 total += vlen;
1882 count -= vlen;
1883 if (sz == vlen)
1884 blk = htx_remove_blk(chn_htx, blk);
1885 else {
1886 htx_cut_data_blk(chn_htx, blk, vlen);
1887 break;
1888 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001889 }
1890
Christopher Faulet9768c262018-10-22 09:34:31 +02001891 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001892 /* when the output buffer is empty, tmp shares the same area so that we
1893 * only have to update pointers and lengths.
1894 */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001895 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1896 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001897 else
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001898 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001899
Willy Tarreau3815b222018-12-11 19:50:43 +01001900 htx_to_buf(chn_htx, buf);
1901 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001902 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001903 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001904 end:
1905 return total;
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001906
1907 full:
1908 h1c->flags |= H1C_F_OUT_FULL;
1909 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001910}
1911
Christopher Faulet51dbc942018-09-13 09:05:15 +02001912/*********************************************************/
1913/* functions below are I/O callbacks from the connection */
1914/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001915static void h1_wake_stream_for_recv(struct h1s *h1s)
1916{
1917 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001918 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001919 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001920 h1s->recv_wait = NULL;
1921 }
1922}
1923static void h1_wake_stream_for_send(struct h1s *h1s)
1924{
1925 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001926 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001927 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001928 h1s->send_wait = NULL;
1929 }
1930}
1931
Christopher Faulet51dbc942018-09-13 09:05:15 +02001932/*
1933 * Attempt to read data, and subscribe if none available
1934 */
1935static int h1_recv(struct h1c *h1c)
1936{
1937 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001938 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001939 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001940 int rcvd = 0;
1941
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001942 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001943 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001944
Olivier Houchard5b06cb32019-08-13 15:21:59 +02001945 if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN)
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001946 return 0;
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001947
Olivier Houchard75159a92018-12-03 18:46:09 +01001948 if (!h1_recv_allowed(h1c)) {
1949 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001950 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001951 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001952
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001953 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1954 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001955 goto end;
1956 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001957
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001958 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001959 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001960 h1_wake_stream_for_recv(h1s);
1961 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001962 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001963 }
1964
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001965 /*
1966 * If we only have a small amount of data, realign it,
1967 * it's probably cheaper than doing 2 recv() calls.
1968 */
1969 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1970 b_slow_realign(&h1c->ibuf, trash.area, 0);
1971
Willy Tarreau45f2b892018-12-05 07:59:27 +01001972 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001973 if (max) {
1974 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001975
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001976 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001977 if (!b_data(&h1c->ibuf)) {
1978 /* try to pre-align the buffer like the rxbufs will be
1979 * to optimize memory copies.
1980 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001981 h1c->ibuf.head = sizeof(struct htx);
1982 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001983 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001984 }
Christopher Faulet47365272018-10-31 17:40:50 +01001985 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001987 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001988 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001989 if (h1s->csinfo.t_idle == -1)
1990 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1991 }
Christopher Faulet47365272018-10-31 17:40:50 +01001992 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001993
Christopher Fauletcf56b992018-12-11 16:12:31 +01001994 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001995 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001996 goto end;
1997 }
1998
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001999 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002000
Christopher Faulet9768c262018-10-22 09:34:31 +02002001 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002002 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2003 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002004
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002005 if (conn_xprt_read0_pending(conn) && h1s) {
2006 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002007 rcvd = 1;
2008 }
2009
Christopher Faulet51dbc942018-09-13 09:05:15 +02002010 if (!b_data(&h1c->ibuf))
2011 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01002012 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002013 h1c->flags |= H1C_F_IN_FULL;
2014 return rcvd;
2015}
2016
2017
2018/*
2019 * Try to send data if possible
2020 */
2021static int h1_send(struct h1c *h1c)
2022{
2023 struct connection *conn = h1c->conn;
2024 unsigned int flags = 0;
2025 size_t ret;
2026 int sent = 0;
2027
2028 if (conn->flags & CO_FL_ERROR)
2029 return 0;
2030
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002031 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002032 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002033 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002034 return 0;
2035 }
2036
Christopher Faulet51dbc942018-09-13 09:05:15 +02002037 if (!b_data(&h1c->obuf))
2038 goto end;
2039
2040 if (h1c->flags & H1C_F_OUT_FULL)
2041 flags |= CO_SFL_MSG_MORE;
2042
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002043 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002044 if (ret > 0) {
2045 h1c->flags &= ~H1C_F_OUT_FULL;
2046 b_del(&h1c->obuf, ret);
2047 sent = 1;
2048 }
2049
Christopher Faulet145aa472018-12-06 10:56:20 +01002050 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
2051 /* error or output closed, nothing to send, clear the buffer to release it */
2052 b_reset(&h1c->obuf);
2053 }
2054
Christopher Faulet51dbc942018-09-13 09:05:15 +02002055 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002056 if (!(h1c->flags & H1C_F_OUT_FULL))
2057 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002058
Christopher Faulet51dbc942018-09-13 09:05:15 +02002059 /* We're done, no more to send */
2060 if (!b_data(&h1c->obuf)) {
2061 h1_release_buf(h1c, &h1c->obuf);
2062 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01002063 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002064 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002065 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002066 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002067
2068 return sent;
2069}
2070
Christopher Faulet51dbc942018-09-13 09:05:15 +02002071
2072/* callback called on any event by the connection handler.
2073 * It applies changes and returns zero, or < 0 if it wants immediate
2074 * destruction of the connection.
2075 */
2076static int h1_process(struct h1c * h1c)
2077{
2078 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002079 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002080
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002081 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002082 return -1;
2083
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002084 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Olivier Houchard690e0f02019-06-11 16:37:24 +02002085 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) ||
Olivier Houchard60630032019-06-13 17:37:00 +02002086 (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE)))
Christopher Faulet539e0292018-11-19 10:40:09 +01002087 goto end;
2088 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01002089 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002090 }
2091
Christopher Fauletfeb11742018-11-29 15:12:34 +01002092 if (!h1s) {
Christopher Faulet470438c2019-07-11 15:40:25 +02002093 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Faulete31a0f12019-12-05 10:23:37 +01002094 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002095 goto release;
Christopher Faulete31a0f12019-12-05 10:23:37 +01002096 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002097 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002098 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002099 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002100 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002101 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002102 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002103 }
2104
Christopher Fauletfeb11742018-11-29 15:12:34 +01002105 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2106 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2107
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002108 if (conn_xprt_read0_pending(conn))
2109 h1s->flags |= H1S_F_REOS;
2110
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002111 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002112 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002113 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002114 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002115 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002116 h1s->cs->data_cb->wake(h1s->cs);
2117 }
Christopher Faulet47365272018-10-31 17:40:50 +01002118 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002119 if (h1c->task) {
2120 h1c->task->expire = TICK_ETERNITY;
2121 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002122 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 +01002123 ? h1c->shut_timeout
2124 : h1c->timeout));
2125 task_queue(h1c->task);
2126 }
2127 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002128 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002129
2130 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002131 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002132 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002133}
2134
2135static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2136{
2137 struct h1c *h1c = ctx;
2138 int ret = 0;
2139
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002140 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002141 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002142 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002143 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002144 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002145 h1_process(h1c);
2146 return NULL;
2147}
2148
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002149static void h1_reset(struct connection *conn)
2150{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002151 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002152
2153 /* Reset the flags, and let the mux know we're waiting for a connection */
2154 h1c->flags = H1C_F_CS_WAIT_CONN;
2155}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002156
2157static int h1_wake(struct connection *conn)
2158{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002159 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002160 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002161
Christopher Faulet539e0292018-11-19 10:40:09 +01002162 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002163 ret = h1_process(h1c);
2164 if (ret == 0) {
2165 struct h1s *h1s = h1c->h1s;
2166
2167 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2168 ret = h1s->cs->data_cb->wake(h1s->cs);
2169 }
2170 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002171}
2172
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002173/* Connection timeout management. The principle is that if there's no receipt
2174 * nor sending for a certain amount of time, the connection is closed.
2175 */
2176static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2177{
2178 struct h1c *h1c = context;
2179 int expired = tick_is_expired(t->expire, now_ms);
2180
2181 if (!expired && h1c)
2182 return t;
2183
Olivier Houchard3f795f72019-04-17 22:51:06 +02002184 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002185
2186 if (!h1c) {
2187 /* resources were already deleted */
2188 return NULL;
2189 }
2190
2191 h1c->task = NULL;
2192 /* If a stream is still attached to the mux, just set an error and wait
2193 * for the stream's timeout. Otherwise, release the mux. This is only ok
2194 * because same timeouts are used.
2195 */
2196 if (h1c->h1s && h1c->h1s->cs)
2197 h1c->flags |= H1C_F_CS_ERROR;
2198 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002199 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002200 return NULL;
2201}
2202
Christopher Faulet51dbc942018-09-13 09:05:15 +02002203/*******************************************/
2204/* functions below are used by the streams */
2205/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002206
Christopher Faulet51dbc942018-09-13 09:05:15 +02002207/*
2208 * Attach a new stream to a connection
2209 * (Used for outgoing connections)
2210 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002211static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002212{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002213 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002214 struct conn_stream *cs = NULL;
2215 struct h1s *h1s;
2216
2217 if (h1c->flags & H1C_F_CS_ERROR)
2218 goto end;
2219
2220 cs = cs_new(h1c->conn);
2221 if (!cs)
2222 goto end;
2223
Olivier Houchardf502aca2018-12-14 19:42:40 +01002224 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002225 if (h1s == NULL)
2226 goto end;
2227
2228 return cs;
2229 end:
2230 cs_free(cs);
2231 return NULL;
2232}
2233
2234/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2235 * this mux, it's easy as we can only store a single conn_stream.
2236 */
2237static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2238{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002239 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002240 struct h1s *h1s = h1c->h1s;
2241
2242 if (h1s)
2243 return h1s->cs;
2244
2245 return NULL;
2246}
2247
Christopher Faulet73c12072019-04-08 11:23:22 +02002248static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002249{
Christopher Faulet73c12072019-04-08 11:23:22 +02002250 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002251
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002252 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002253 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002254}
2255
2256/*
2257 * Detach the stream from the connection and possibly release the connection.
2258 */
2259static void h1_detach(struct conn_stream *cs)
2260{
2261 struct h1s *h1s = cs->ctx;
2262 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002263 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002264 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002265
2266 cs->ctx = NULL;
2267 if (!h1s)
2268 return;
2269
Olivier Houchardf502aca2018-12-14 19:42:40 +01002270 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002271 h1c = h1s->h1c;
2272 h1s->cs = NULL;
2273
Olivier Houchard8a786902018-12-15 16:05:40 +01002274 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2275 h1s_destroy(h1s);
2276
Christopher Faulete31a0f12019-12-05 10:23:37 +01002277 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002278 /* If there are any excess server data in the input buffer,
2279 * release it and close the connection ASAP (some data may
2280 * remain in the output buffer). This happens if a server sends
2281 * invalid responses. So in such case, we don't want to reuse
2282 * the connection
Christopher Faulet39e679b2019-07-19 11:34:08 +02002283 */
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002284 if (b_data(&h1c->ibuf)) {
2285 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulete31a0f12019-12-05 10:23:37 +01002286 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002287 goto release;
2288 }
Christopher Faulet39e679b2019-07-19 11:34:08 +02002289
Christopher Faulet9400a392018-11-23 23:10:39 +01002290 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002291 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002292 h1c->conn->flags |= CO_FL_PRIVATE;
2293
Olivier Houchard44d59142018-12-13 18:46:22 +01002294 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002295 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002296 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2297 h1c->conn->owner = NULL;
2298 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2299 /* The server doesn't want it, let's kill the connection right away */
2300 h1c->conn->mux->destroy(h1c->conn);
2301 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002302 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002303 return;
2304
2305 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002306 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002307 if (h1c->conn->owner == sess) {
2308 int ret = session_check_idle_conn(sess, h1c->conn);
2309 if (ret == -1)
2310 /* The connection got destroyed, let's leave */
2311 return;
2312 else if (ret == 1) {
2313 /* The connection was added to the server list,
2314 * wake the task so we can subscribe to events
2315 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002316 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002317 return;
2318 }
2319 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002320 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002321 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002322 struct server *srv = objt_server(h1c->conn->target);
2323
2324 if (srv) {
2325 if (h1c->conn->flags & CO_FL_PRIVATE)
2326 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002327 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002328 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2329 else
2330 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2331 }
2332 }
2333 }
2334
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002335 release:
Christopher Faulet9fa93f62019-06-28 17:41:42 +02002336 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet470438c2019-07-11 15:40:25 +02002337 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2338 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2339 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2340 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002341 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002342 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002343 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002344 if (h1c->task) {
2345 h1c->task->expire = TICK_ETERNITY;
2346 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002347 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 +01002348 ? h1c->shut_timeout
2349 : h1c->timeout));
2350 task_queue(h1c->task);
2351 }
2352 }
2353 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002354}
2355
2356
2357static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2358{
2359 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002360 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002361
2362 if (!h1s)
2363 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002364 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002365
Christopher Faulet7f366362019-04-08 10:51:20 +02002366 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2367 goto do_shutr;
2368
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002369 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002370 return;
2371
Christopher Faulet7f366362019-04-08 10:51:20 +02002372 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002373 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2374 if (cs->flags & CS_FL_SHR)
2375 return;
2376 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002377 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2378 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002379 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 +02002380 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002381}
2382
2383static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2384{
2385 struct h1s *h1s = cs->ctx;
2386 struct h1c *h1c;
2387
2388 if (!h1s)
2389 return;
2390 h1c = h1s->h1c;
2391
Christopher Faulet7f366362019-04-08 10:51:20 +02002392 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2393 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002394
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002395 if ((h1c->flags & H1C_F_UPG_H2C) ||
2396 ((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 +02002397 return;
2398
Christopher Faulet7f366362019-04-08 10:51:20 +02002399 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002400 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2401 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2402 return;
2403
Christopher Faulet666a0c42019-01-08 11:12:04 +01002404 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002405}
2406
Christopher Faulet666a0c42019-01-08 11:12:04 +01002407static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002408{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002409 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002410
Christopher Faulet666a0c42019-01-08 11:12:04 +01002411 conn_xprt_shutw(conn);
2412 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2413 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2414 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002415}
2416
2417/* Called from the upper layer, to unsubscribe to events */
2418static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2419{
2420 struct wait_event *sw;
2421 struct h1s *h1s = cs->ctx;
2422
2423 if (!h1s)
2424 return 0;
2425
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002426 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002427 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002428 BUG_ON(h1s->recv_wait != sw);
2429 sw->events &= ~SUB_RETRY_RECV;
2430 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002431 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002432 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002433 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002434 BUG_ON(h1s->send_wait != sw);
2435 sw->events &= ~SUB_RETRY_SEND;
2436 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002437 }
2438 return 0;
2439}
2440
2441/* Called from the upper layer, to subscribe to events, such as being able to send */
2442static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2443{
2444 struct wait_event *sw;
2445 struct h1s *h1s = cs->ctx;
2446
2447 if (!h1s)
2448 return -1;
2449
2450 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002451 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002452 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002453 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2454 sw->events |= SUB_RETRY_RECV;
2455 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002456 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002457 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002458 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002459 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2460 sw->events |= SUB_RETRY_SEND;
2461 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002462 return 0;
2463 default:
2464 break;
2465 }
2466 return -1;
2467}
2468
2469/* Called from the upper layer, to receive data */
2470static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2471{
2472 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002473 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002474 size_t ret = 0;
2475
Christopher Faulet539e0292018-11-19 10:40:09 +01002476 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002477 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002478
Christopher Faulete18777b2019-04-16 16:46:36 +02002479 if (flags & CO_RFL_BUF_FLUSH) {
2480 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2481
2482 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2483 h1s->flags |= H1S_F_BUF_FLUSH;
2484 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002485 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2486 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002487 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002488 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002489 }
2490 return ret;
2491}
2492
2493
2494/* Called from the upper layer, to send data */
2495static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2496{
2497 struct h1s *h1s = cs->ctx;
2498 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002499 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002500
2501 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002502 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002503
2504 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002505 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2506 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002507
Christopher Fauletc31872f2019-06-04 22:09:36 +02002508 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002509 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002510
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002511 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2512 ret = h1_process_output(h1c, buf, count);
2513 if (!ret)
2514 break;
2515 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002516 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002517 if (!h1_send(h1c))
2518 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002519 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002520
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002521 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002522}
2523
Willy Tarreaue5733232019-05-22 19:24:06 +02002524#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002525/* Send and get, using splicing */
2526static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2527{
2528 struct h1s *h1s = cs->ctx;
2529 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2530 int ret = 0;
2531
Christopher Faulet2c411be2019-11-05 16:24:27 +01002532 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002533 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002534 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2535 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002536 goto end;
2537 }
2538
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002539 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002540 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002541 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002542 }
2543
2544 h1s->flags &= ~H1S_F_BUF_FLUSH;
2545 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002546 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2547 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002548 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002549 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002550 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002551 if (!h1m->curr_len)
2552 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2553 }
2554
Christopher Faulet1be55f92018-10-02 15:59:23 +02002555 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002556 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002557 h1s->flags |= H1S_F_REOS;
Christopher Fauletac4778c2019-11-15 11:14:23 +01002558 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet038ad812019-04-17 11:03:22 +02002559 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002560 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002561}
2562
2563static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2564{
2565 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002566 int ret = 0;
2567
2568 if (b_data(&h1s->h1c->obuf))
2569 goto end;
2570
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002571 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002572 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002573 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002574 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002575 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002576 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002577 return ret;
2578}
2579#endif
2580
Olivier Houchard198d1292019-10-25 16:19:26 +02002581static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2582{
2583 int ret = 0;
2584 switch (mux_ctl) {
2585 case MUX_STATUS:
2586 if (conn->flags & CO_FL_CONNECTED)
2587 ret |= MUX_STATUS_READY;
2588 return ret;
2589 default:
2590 return -1;
2591 }
2592}
2593
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002594/* for debugging with CLI's "show fd" command */
2595static void h1_show_fd(struct buffer *msg, struct connection *conn)
2596{
2597 struct h1c *h1c = conn->ctx;
2598 struct h1s *h1s = h1c->h1s;
2599
Christopher Fauletf376a312019-01-04 15:16:06 +01002600 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2601 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002602 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2603 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2604 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2605 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2606
2607 if (h1s) {
2608 char *method;
2609
2610 if (h1s->meth < HTTP_METH_OTHER)
2611 method = http_known_methods[h1s->meth].ptr;
2612 else
2613 method = "UNKNOWN";
2614 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2615 " .meth=%s status=%d",
2616 h1s, h1s->flags,
2617 h1m_state_str(h1s->req.state),
2618 h1m_state_str(h1s->res.state), method, h1s->status);
2619 if (h1s->cs)
2620 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2621 h1s->cs->flags, h1s->cs->data);
2622 }
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002623}
2624
2625
2626/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2627static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2628{
2629 struct h1_hdr_entry *entry;
2630
2631 /* Be sure there is a non-empty <to> */
2632 if (!strlen(to)) {
2633 memprintf(err, "expect <to>");
2634 return -1;
2635 }
2636
2637 /* Be sure only the case differs between <from> and <to> */
2638 if (strcasecmp(from, to)) {
2639 memprintf(err, "<from> and <to> must not differ execpt the case");
2640 return -1;
2641 }
2642
2643 /* Be sure <from> does not already existsin the tree */
2644 if (ebis_lookup(&hdrs_map.map, from)) {
2645 memprintf(err, "duplicate entry '%s'", from);
2646 return -1;
2647 }
2648
2649 /* Create the entry and insert it in the tree */
2650 entry = malloc(sizeof(*entry));
2651 if (!entry) {
2652 memprintf(err, "out of memory");
2653 return -1;
2654 }
2655
2656 entry->node.key = strdup(from);
2657 entry->name.ptr = strdup(to);
2658 entry->name.len = strlen(to);
2659 if (!entry->node.key || !entry->name.ptr) {
2660 free(entry->node.key);
2661 free(entry->name.ptr);
2662 free(entry);
2663 memprintf(err, "out of memory");
2664 return -1;
2665 }
2666 ebis_insert(&hdrs_map.map, &entry->node);
2667 return 0;
2668}
2669
2670static void h1_hdeaders_case_adjust_deinit()
2671{
2672 struct ebpt_node *node, *next;
2673 struct h1_hdr_entry *entry;
2674
2675 node = ebpt_first(&hdrs_map.map);
2676 while (node) {
2677 next = ebpt_next(node);
2678 ebpt_delete(node);
2679 entry = container_of(node, struct h1_hdr_entry, node);
2680 free(entry->node.key);
2681 free(entry->name.ptr);
2682 free(entry);
2683 node = next;
2684 }
2685 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002686}
2687
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002688static int cfg_h1_headers_case_adjust_postparser()
2689{
2690 FILE *file = NULL;
2691 char *c, *key_beg, *key_end, *value_beg, *value_end;
2692 char *err;
2693 int rc, line = 0, err_code = 0;
2694
2695 if (!hdrs_map.name)
2696 goto end;
2697
2698 file = fopen(hdrs_map.name, "r");
2699 if (!file) {
2700 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2701 hdrs_map.name);
2702 err_code |= ERR_ALERT | ERR_FATAL;
2703 goto end;
2704 }
2705
2706 /* now parse all lines. The file may contain only two header name per
2707 * line, separated by spaces. All heading and trailing spaces will be
2708 * ignored. Lines starting with a # are ignored.
2709 */
2710 while (fgets(trash.area, trash.size, file) != NULL) {
2711 line++;
2712 c = trash.area;
2713
2714 /* strip leading spaces and tabs */
2715 while (*c == ' ' || *c == '\t')
2716 c++;
2717
2718 /* ignore emptu lines, or lines beginning with a dash */
2719 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2720 continue;
2721
2722 /* look for the end of the key */
2723 key_beg = c;
2724 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2725 c++;
2726 key_end = c;
2727
2728 /* strip middle spaces and tabs */
2729 while (*c == ' ' || *c == '\t')
2730 c++;
2731
2732 /* look for the end of the value, it is the end of the line */
2733 value_beg = c;
2734 while (*c && *c != '\n' && *c != '\r')
2735 c++;
2736 value_end = c;
2737
2738 /* trim possibly trailing spaces and tabs */
2739 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2740 value_end--;
2741
2742 /* set final \0 and check entries */
2743 *key_end = '\0';
2744 *value_end = '\0';
2745
2746 err = NULL;
2747 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2748 if (rc < 0) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002749 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2750 hdrs_map.name, err, line);
2751 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Faulet27f20172019-07-30 16:51:42 +02002752 free(err);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002753 goto end;
2754 }
2755 if (rc > 0) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002756 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2757 hdrs_map.name, err, line);
2758 err_code |= ERR_WARN;
Christopher Faulet27f20172019-07-30 16:51:42 +02002759 free(err);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002760 }
2761 }
2762
2763 end:
2764 if (file)
2765 fclose(file);
2766 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2767 return err_code;
2768}
2769
2770
2771/* config parser for global "h1-outgoing-header-case-adjust" */
2772static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2773 struct proxy *defpx, const char *file, int line,
2774 char **err)
2775{
2776 if (too_many_args(2, args, err, NULL))
2777 return -1;
2778 if (!*(args[1]) || !*(args[2])) {
2779 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2780 return -1;
2781 }
2782 return add_hdr_case_adjust(args[1], args[2], err);
2783}
2784
2785/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2786static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2787 struct proxy *defpx, const char *file, int line,
2788 char **err)
2789{
2790 if (too_many_args(1, args, err, NULL))
2791 return -1;
2792 if (!*(args[1])) {
2793 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2794 return -1;
2795 }
2796 free(hdrs_map.name);
2797 hdrs_map.name = strdup(args[1]);
2798 return 0;
2799}
2800
2801
2802/* config keyword parsers */
2803static struct cfg_kw_list cfg_kws = {{ }, {
2804 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2805 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2806 { 0, NULL, NULL },
2807 }
2808};
2809
2810INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2811REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2812
2813
Christopher Faulet51dbc942018-09-13 09:05:15 +02002814/****************************************/
2815/* MUX initialization and instanciation */
2816/****************************************/
2817
2818/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002819static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002820 .init = h1_init,
2821 .wake = h1_wake,
2822 .attach = h1_attach,
2823 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002824 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002825 .detach = h1_detach,
2826 .destroy = h1_destroy,
2827 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002828 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002829 .rcv_buf = h1_rcv_buf,
2830 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002831#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002832 .rcv_pipe = h1_rcv_pipe,
2833 .snd_pipe = h1_snd_pipe,
2834#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002835 .subscribe = h1_subscribe,
2836 .unsubscribe = h1_unsubscribe,
2837 .shutr = h1_shutr,
2838 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002839 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002840 .reset = h1_reset,
Olivier Houchard198d1292019-10-25 16:19:26 +02002841 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002842 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002843 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002844};
2845
2846
2847/* this mux registers default HTX proto */
2848static struct mux_proto_list mux_proto_htx =
2849{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2850
Willy Tarreau0108d902018-11-25 19:14:37 +01002851INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2852
Christopher Faulet51dbc942018-09-13 09:05:15 +02002853/*
2854 * Local variables:
2855 * c-indent-level: 8
2856 * c-basic-offset: 8
2857 * End:
2858 */