blob: b36b29358a1b6362fb4f5df7bdb2a02bab1ee4be [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 Fauletf36e72b2019-12-05 11:18:31 +010045#define H1C_F_IN_BUSY 0x00000040 /* mux is blocked on input waiting the other side */
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 Fauletf36e72b2019-12-05 11:18:31 +010051#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down */
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/*
Christopher Faulet7caf1502019-12-05 12:30:55 +0100145 * Indicates whether or not we may receive data. The rules are the following :
146 * - if an error or a shutdown for reads was detected on the connection we
147 must not attempt to receive
148 * - if the input buffer failed to be allocated or is full , we must not try
149 * to receive
150 * - if he input processing is busy waiting for the output side, we may
151 * attemp to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200152 * - otherwise must may not attempt to receive
153 */
154static inline int h1_recv_allowed(const struct h1c *h1c)
155{
Christopher Faulet7caf1502019-12-05 12:30:55 +0100156 if (h1c->flags & H1C_F_CS_ERROR)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200157 return 0;
158
Christopher Faulet7caf1502019-12-05 12:30:55 +0100159 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH))
Christopher Fauletcf56b992018-12-11 16:12:31 +0100160 return 0;
161
Christopher Fauletcb55f482018-12-10 11:56:47 +0100162 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200163 return 1;
164
165 return 0;
166}
167
168/*
169 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
170 * flags are used to figure what buffer was requested. It returns 1 if the
171 * allocation succeeds, in which case the connection is woken up, or 0 if it's
172 * impossible to wake up and we prefer to be woken up later.
173 */
174static int h1_buf_available(void *target)
175{
176 struct h1c *h1c = target;
177
178 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
179 h1c->flags &= ~H1C_F_IN_ALLOC;
180 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200181 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200182 return 1;
183 }
184
185 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
186 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200187 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200188 return 1;
189 }
190
Christopher Faulet51dbc942018-09-13 09:05:15 +0200191 return 0;
192}
193
194/*
195 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
196 */
197static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
198{
199 struct buffer *buf = NULL;
200
201 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
202 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
203 h1c->buf_wait.target = h1c;
204 h1c->buf_wait.wakeup_cb = h1_buf_available;
205 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
206 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
207 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
208 __conn_xprt_stop_recv(h1c->conn);
209 }
210 return buf;
211}
212
213/*
214 * Release a buffer, if any, and try to wake up entities waiting in the buffer
215 * wait queue.
216 */
217static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
218{
219 if (bptr->size) {
220 b_free(bptr);
221 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
222 }
223}
224
Christopher Faulete31a0f12019-12-05 10:23:37 +0100225/* returns the number of streams in use on a connection to figure if it's idle
226 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
227 * not. This flag is only set when no H1S is attached and when the previous
228 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100229 */
230static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200231{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100232 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200233
Christopher Faulete31a0f12019-12-05 10:23:37 +0100234 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200235}
236
Willy Tarreau00f18a32019-01-26 12:19:01 +0100237/* returns the number of streams still available on a connection */
238static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100239{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100240 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100241}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200242
Christopher Fauletb5762062020-08-05 11:31:16 +0200243/* Refresh the h1c task timeout if necessary */
244static void h1_refresh_timeout(struct h1c *h1c)
245{
246 if (h1c->task) {
247 h1c->task->expire = TICK_ETERNITY;
Willy Tarreau08144582020-09-08 15:40:57 +0200248 if (h1c->flags & H1C_F_CS_SHUTDOWN) {
249 /* half-closed connections switch to clientfin/serverfin
250 * timeouts so that we don't hang too long on clients
251 * that have gone away (especially in tunnel mode).
252 */
253 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
254 task_queue(h1c->task);
255 } else if ((!h1c->h1s && !conn_is_back(h1c->conn)) || b_data(&h1c->obuf)) {
Christopher Fauletb5762062020-08-05 11:31:16 +0200256 /* front connections waiting for a stream, as well as any connection with
257 * pending data, need a timeout.
258 */
Willy Tarreau08144582020-09-08 15:40:57 +0200259 h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Fauletb5762062020-08-05 11:31:16 +0200260 ? h1c->shut_timeout
261 : h1c->timeout));
262 task_queue(h1c->task);
263 }
264 }
265}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200266/*****************************************************************/
267/* functions below are dedicated to the mux setup and management */
268/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200269
270/* returns non-zero if there are input data pending for stream h1s. */
271static inline size_t h1s_data_pending(const struct h1s *h1s)
272{
273 const struct h1m *h1m;
274
275 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
276 if (h1m->state == H1_MSG_DONE)
277 return 0; // data not for this stream (e.g. pipelining)
278
279 return b_data(&h1s->h1c->ibuf);
280}
281
Christopher Faulet47365272018-10-31 17:40:50 +0100282static struct conn_stream *h1s_new_cs(struct h1s *h1s)
283{
284 struct conn_stream *cs;
285
286 cs = cs_new(h1s->h1c->conn);
287 if (!cs)
288 goto err;
289 h1s->cs = cs;
290 cs->ctx = h1s;
291
292 if (h1s->flags & H1S_F_NOT_FIRST)
293 cs->flags |= CS_FL_NOT_FIRST;
294
Willy Tarreau7e9dd732020-01-17 16:19:34 +0100295 if (global.tune.options & GTUNE_USE_SPLICE)
296 cs->flags |= CS_FL_MAY_SPLICE;
297
Christopher Faulet47365272018-10-31 17:40:50 +0100298 if (stream_create_from_cs(cs) < 0)
299 goto err;
300 return cs;
301
302 err:
303 cs_free(cs);
304 h1s->cs = NULL;
305 return NULL;
306}
307
Olivier Houchardf502aca2018-12-14 19:42:40 +0100308static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200309{
310 struct h1s *h1s;
311
312 h1s = pool_alloc(pool_head_h1s);
313 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100314 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200315
316 h1s->h1c = h1c;
317 h1c->h1s = h1s;
318
Olivier Houchardf502aca2018-12-14 19:42:40 +0100319 h1s->sess = sess;
320
Christopher Faulet51dbc942018-09-13 09:05:15 +0200321 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100322 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200323
324 h1s->recv_wait = NULL;
325 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200326
327 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100328 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200329
Christopher Faulet129817b2018-09-20 16:14:40 +0200330 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100331 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200332
333 h1s->status = 0;
334 h1s->meth = HTTP_METH_OTHER;
335
Christopher Faulet47365272018-10-31 17:40:50 +0100336 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
337 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulete31a0f12019-12-05 10:23:37 +0100338 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100339
Christopher Faulet129817b2018-09-20 16:14:40 +0200340 if (!conn_is_back(h1c->conn)) {
341 if (h1c->px->options2 & PR_O2_REQBUG_OK)
342 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200343
344 /* For frontend connections we should always have a session */
345 if (!sess)
Christopher Faulet823613c2020-09-30 15:00:13 +0200346 h1s->sess = sess = h1c->conn->owner;
Christopher Faulete9b70722019-04-08 10:46:02 +0200347
Dave Pirottecf67ec52019-07-10 13:57:38 +0000348 /* Timers for subsequent sessions on the same HTTP 1.x connection
349 * measure from `now`, not from the connection accept time */
350 if (h1s->flags & H1S_F_NOT_FIRST) {
351 h1s->csinfo.create_date = date;
352 h1s->csinfo.tv_create = now;
353 h1s->csinfo.t_handshake = 0;
354 h1s->csinfo.t_idle = -1;
355 }
356 else {
357 h1s->csinfo.create_date = sess->accept_date;
358 h1s->csinfo.tv_create = sess->tv_accept;
359 h1s->csinfo.t_handshake = sess->t_handshake;
360 h1s->csinfo.t_idle = -1;
361 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200362 }
363 else {
364 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
365 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200366
Christopher Fauletfeb11742018-11-29 15:12:34 +0100367 h1s->csinfo.create_date = date;
368 h1s->csinfo.tv_create = now;
369 h1s->csinfo.t_handshake = 0;
370 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200371 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100372
Christopher Faulete9b70722019-04-08 10:46:02 +0200373 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
374 * create a new one.
375 */
376 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200377 cs->ctx = h1s;
378 h1s->cs = cs;
379 }
Christopher Faulet47365272018-10-31 17:40:50 +0100380 else {
381 cs = h1s_new_cs(h1s);
382 if (!cs)
383 goto fail;
384 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200385 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100386
387 fail:
388 pool_free(pool_head_h1s, h1s);
389 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200390}
391
392static void h1s_destroy(struct h1s *h1s)
393{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200394 if (h1s) {
395 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200396
Christopher Fauletf2824e62018-10-01 12:12:37 +0200397 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200398
Christopher Fauletf2824e62018-10-01 12:12:37 +0200399 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100400 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200401 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100402 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200403
Christopher Fauletcb55f482018-12-10 11:56:47 +0100404 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100405 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
406 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulete31a0f12019-12-05 10:23:37 +0100407
408 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
409 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
410 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
411 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
412 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
413 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200414 pool_free(pool_head_h1s, h1s);
415 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200416}
417
Christopher Fauletfeb11742018-11-29 15:12:34 +0100418static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
419{
420 struct h1s *h1s = cs->ctx;
421
422 if (h1s && !conn_is_back(cs->conn))
423 return &h1s->csinfo;
424 return NULL;
425}
426
Christopher Faulet51dbc942018-09-13 09:05:15 +0200427/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200428 * Initialize the mux once it's attached. It is expected that conn->ctx points
429 * to the existing conn_stream (for outgoing connections or for incoming onces
430 * during a mux upgrade) or NULL (for incoming ones during the connexion
431 * establishment). <input> is always used as Input buffer and may contain
432 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
433 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200434 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200435static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
436 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200437{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200438 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100439 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200440
441 h1c = pool_alloc(pool_head_h1c);
442 if (!h1c)
443 goto fail_h1c;
444 h1c->conn = conn;
445 h1c->px = proxy;
446
Christopher Faulete31a0f12019-12-05 10:23:37 +0100447 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200448 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200449 h1c->obuf = BUF_NULL;
450 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200451 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200452
Christopher Faulet51dbc942018-09-13 09:05:15 +0200453 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200454 h1c->wait_event.tasklet = tasklet_new();
455 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200456 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200457 h1c->wait_event.tasklet->process = h1_io_cb;
458 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100459 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200460
Christopher Faulete9b70722019-04-08 10:46:02 +0200461 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100462 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
463 if (tick_isset(proxy->timeout.serverfin))
464 h1c->shut_timeout = proxy->timeout.serverfin;
465 } else {
466 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
467 if (tick_isset(proxy->timeout.clientfin))
468 h1c->shut_timeout = proxy->timeout.clientfin;
469 }
470 if (tick_isset(h1c->timeout)) {
471 t = task_new(tid_bit);
472 if (!t)
473 goto fail;
474
475 h1c->task = t;
476 t->process = h1_timeout_task;
477 t->context = h1c;
478 t->expire = tick_add(now_ms, h1c->timeout);
479 }
480
Olivier Houchard985234d2019-06-13 17:54:33 +0200481 if (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200482 h1c->flags |= H1C_F_CS_WAIT_CONN;
483
Christopher Fauletf2824e62018-10-01 12:12:37 +0200484 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100485 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200486 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200487
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100488 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200489
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100490
491 if (t)
492 task_queue(t);
493
Christopher Faulet51dbc942018-09-13 09:05:15 +0200494 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200495 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200496
497 /* mux->wake will be called soon to complete the operation */
498 return 0;
499
500 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200501 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200502 if (h1c->wait_event.tasklet)
503 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200504 pool_free(pool_head_h1c, h1c);
505 fail_h1c:
506 return -1;
507}
508
Christopher Faulet73c12072019-04-08 11:23:22 +0200509/* release function. This one should be called to free all resources allocated
510 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200511 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200512static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200513{
Christopher Faulet61840e72019-04-15 09:33:32 +0200514 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200515
Christopher Faulet51dbc942018-09-13 09:05:15 +0200516 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200517 /* The connection must be aattached to this mux to be released */
518 if (h1c->conn && h1c->conn->ctx == h1c)
519 conn = h1c->conn;
520
521 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200522 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard13f556e2019-07-03 13:08:18 +0200523 /* Make sure we're no longer subscribed to anything */
524 if (h1c->wait_event.events)
525 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
526 h1c->wait_event.events, &h1c->wait_event);
Olivier Houchard45c44372019-06-11 14:06:23 +0200527 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200528 /* connection successfully upgraded to H2, this
529 * mux was already released */
530 return;
531 }
532 sess_log(conn->owner); /* Log if the upgrade failed */
533 }
534
Olivier Houchard45c44372019-06-11 14:06:23 +0200535
Christopher Faulet51dbc942018-09-13 09:05:15 +0200536 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
537 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
538 LIST_DEL(&h1c->buf_wait.list);
539 LIST_INIT(&h1c->buf_wait.list);
540 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
541 }
542
543 h1_release_buf(h1c, &h1c->ibuf);
544 h1_release_buf(h1c, &h1c->obuf);
545
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100546 if (h1c->task) {
547 h1c->task->context = NULL;
548 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
549 h1c->task = NULL;
550 }
551
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200552 if (h1c->wait_event.tasklet)
553 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200554
Christopher Fauletf2824e62018-10-01 12:12:37 +0200555 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200556 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100557 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200558 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200559 pool_free(pool_head_h1c, h1c);
560 }
561
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200562 if (conn) {
563 conn->mux = NULL;
564 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200565
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200566 conn_stop_tracking(conn);
567 conn_full_close(conn);
568 if (conn->destroy_cb)
569 conn->destroy_cb(conn);
570 conn_free(conn);
571 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200572}
573
574/******************************************************/
575/* functions below are for the H1 protocol processing */
576/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200577/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
578 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200579 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100580static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200581{
Christopher Faulet570d1612018-11-26 11:13:57 +0100582 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200583
Christopher Faulet570d1612018-11-26 11:13:57 +0100584 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200585 (*(p + 5) > '1' ||
586 (*(p + 5) == '1' && *(p + 7) >= '1')))
587 h1m->flags |= H1_MF_VER_11;
588}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200589
Christopher Faulet9768c262018-10-22 09:34:31 +0200590/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
591 * greater or equal to 1.1
592 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100593static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200594{
Christopher Faulet570d1612018-11-26 11:13:57 +0100595 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200596
Christopher Faulet570d1612018-11-26 11:13:57 +0100597 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200598 (*(p + 5) > '1' ||
599 (*(p + 5) == '1' && *(p + 7) >= '1')))
600 h1m->flags |= H1_MF_VER_11;
601}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200602
Christopher Faulet9768c262018-10-22 09:34:31 +0200603/*
604 * Check the validity of the request version. If the version is valid, it
605 * returns 1. Otherwise, it returns 0.
606 */
607static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
608{
609 struct h1c *h1c = h1s->h1c;
610
611 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
612 * exactly one digit "." one digit. This check may be disabled using
613 * option accept-invalid-http-request.
614 */
615 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
616 if (sl.rq.v.len != 8)
617 return 0;
618
619 if (*(sl.rq.v.ptr + 4) != '/' ||
620 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
621 *(sl.rq.v.ptr + 6) != '.' ||
622 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
623 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200624 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200625 else if (!sl.rq.v.len) {
626 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200627
Christopher Faulet9768c262018-10-22 09:34:31 +0200628 /* RFC 1945 allows only GET for HTTP/0.9 requests */
629 if (sl.rq.meth != HTTP_METH_GET)
630 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200631
Christopher Faulet9768c262018-10-22 09:34:31 +0200632 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
633 if (!sl.rq.u.len)
634 return 0;
635
636 /* Add HTTP version */
637 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100638 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200639 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100640
641 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100642 ((*(sl.rq.v.ptr + 5) > '1') ||
643 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100644 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200645 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200646}
647
Christopher Faulet9768c262018-10-22 09:34:31 +0200648/*
649 * Check the validity of the response version. If the version is valid, it
650 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200651 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200652static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200653{
Christopher Faulet9768c262018-10-22 09:34:31 +0200654 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200655
Christopher Faulet9768c262018-10-22 09:34:31 +0200656 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
657 * exactly one digit "." one digit. This check may be disabled using
658 * option accept-invalid-http-request.
659 */
660 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
661 if (sl.st.v.len != 8)
662 return 0;
663
664 if (*(sl.st.v.ptr + 4) != '/' ||
665 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
666 *(sl.st.v.ptr + 6) != '.' ||
667 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
668 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200669 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100670
671 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100672 ((*(sl.st.v.ptr + 5) > '1') ||
673 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100674 h1m->flags |= H1_MF_VER_11;
675
Christopher Faulet9768c262018-10-22 09:34:31 +0200676 return 1;
677}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200678
679/* Deduce the connection mode of the client connection, depending on the
680 * configuration and the H1 message flags. This function is called twice, the
681 * first time when the request is parsed and the second time when the response
682 * is parsed.
683 */
684static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
685{
686 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200687
688 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100689 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200690 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100691 h1s->status == 101) {
692 /* Either we've established an explicit tunnel, or we're
693 * switching the protocol. In both cases, we're very unlikely to
694 * understand the next protocols. We have to switch to tunnel
695 * mode, so that we transfer the request and responses then let
696 * this protocol pass unmodified. When we later implement
697 * specific parsers for such protocols, we'll want to check the
698 * Upgrade header which contains information about that protocol
699 * for responses with status 101 (eg: see RFC2817 about TLS).
700 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200701 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100702 }
703 else if (h1s->flags & H1S_F_WANT_KAL) {
704 /* By default the client is in KAL mode. CLOSE mode mean
705 * it is imposed by the client itself. So only change
706 * KAL mode here. */
707 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
708 /* no length known or explicit close => close */
709 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
710 }
711 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
712 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
713 /* no explict keep-alive and option httpclose => close */
714 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
715 }
716 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200717 }
718 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100719 /* Input direction: first pass */
720 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
721 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200722 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100723 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200724 }
725
726 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
727 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
728 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
729}
730
731/* Deduce the connection mode of the client connection, depending on the
732 * configuration and the H1 message flags. This function is called twice, the
733 * first time when the request is parsed and the second time when the response
734 * is parsed.
735 */
736static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
737{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100738 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100739 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100740 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200741
Christopher Fauletf2824e62018-10-01 12:12:37 +0200742 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100743 /* Input direction: second pass */
744
Christopher Fauletf2824e62018-10-01 12:12:37 +0200745 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100746 h1s->status == 101) {
747 /* Either we've established an explicit tunnel, or we're
748 * switching the protocol. In both cases, we're very unlikely to
749 * understand the next protocols. We have to switch to tunnel
750 * mode, so that we transfer the request and responses then let
751 * this protocol pass unmodified. When we later implement
752 * specific parsers for such protocols, we'll want to check the
753 * Upgrade header which contains information about that protocol
754 * for responses with status 101 (eg: see RFC2817 about TLS).
755 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200756 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100757 }
758 else if (h1s->flags & H1S_F_WANT_KAL) {
759 /* By default the server is in KAL mode. CLOSE mode mean
760 * it is imposed by haproxy itself. So only change KAL
761 * mode here. */
762 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
763 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
764 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
765 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
766 }
767 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200768 }
Christopher Faulet70033782018-12-05 13:50:11 +0100769 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100770 /* Output direction: first pass */
771 if (h1m->flags & H1_MF_CONN_CLO) {
772 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100773 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100774 }
775 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
776 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
777 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
778 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
779 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
780 /* no explicit keep-alive option httpclose/server-close => close */
781 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
782 }
Christopher Faulet70033782018-12-05 13:50:11 +0100783 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200784
785 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
786 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
787 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200788}
789
Christopher Fauletb992af02019-03-28 15:42:24 +0100790static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200791{
792 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200793
794 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
795 * token is found
796 */
797 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200798 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200799
800 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100801 if (!(h1m->flags & H1_MF_VER_11))
802 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200803 }
804 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100805 if (h1m->flags & H1_MF_VER_11)
806 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200807 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200808}
809
Christopher Fauletb992af02019-03-28 15:42:24 +0100810static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200811{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200812 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
813 * token is found
814 */
815 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200816 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200817
818 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100819 if (!(h1m->flags & H1_MF_VER_11) ||
820 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
821 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200822 }
823 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100824 if (h1m->flags & H1_MF_VER_11)
825 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200826 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200827}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200828
Christopher Fauletb992af02019-03-28 15:42:24 +0100829static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200830{
Christopher Fauletb992af02019-03-28 15:42:24 +0100831 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200832 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100833 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200834 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200835}
836
Christopher Fauletb992af02019-03-28 15:42:24 +0100837static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
838{
839 if (!conn_is_back(h1s->h1c->conn))
840 h1_set_cli_conn_mode(h1s, h1m);
841 else
842 h1_set_srv_conn_mode(h1s, h1m);
843
844 if (!(h1m->flags & H1_MF_RESP))
845 h1_update_req_conn_value(h1s, h1m, conn_val);
846 else
847 h1_update_res_conn_value(h1s, h1m, conn_val);
848}
Christopher Faulete44769b2018-11-29 23:01:45 +0100849
Christopher Fauleta99ff4d2019-07-22 16:18:24 +0200850/* Try to adjust the case of the message header name using the global map
851 * <hdrs_map>.
852 */
853static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
854{
855 struct ebpt_node *node;
856 struct h1_hdr_entry *entry;
857
858 /* No entry in the map, do nothing */
859 if (eb_is_empty(&hdrs_map.map))
860 return;
861
862 /* No conversion fo the request headers */
863 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
864 return;
865
866 /* No conversion fo the response headers */
867 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
868 return;
869
870 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
871 if (!node)
872 return;
873 entry = container_of(node, struct h1_hdr_entry, node);
874 name->ptr = entry->name.ptr;
875 name->len = entry->name.len;
876}
877
Christopher Faulete44769b2018-11-29 23:01:45 +0100878/* Append the description of what is present in error snapshot <es> into <out>.
879 * The description must be small enough to always fit in a buffer. The output
880 * buffer may be the trash so the trash must not be used inside this function.
881 */
882static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
883{
884 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100885 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
886 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
887 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
888 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
889 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
890 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100891}
892/*
893 * Capture a bad request or response and archive it in the proxy's structure.
894 * By default it tries to report the error position as h1m->err_pos. However if
895 * this one is not set, it will then report h1m->next, which is the last known
896 * parsing point. The function is able to deal with wrapping buffers. It always
897 * displays buffers as a contiguous area starting at buf->p. The direction is
898 * determined thanks to the h1m's flags.
899 */
900static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
901 struct h1m *h1m, struct buffer *buf)
902{
Christopher Faulet8ec3d242020-10-15 17:19:46 +0200903 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +0100904 struct proxy *proxy = h1c->px;
905 struct proxy *other_end = sess->fe;
906 union error_snapshot_ctx ctx;
907
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100908 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100909 other_end = si_strm(h1s->cs->data)->be;
910
911 /* http-specific part now */
912 ctx.h1.state = h1m->state;
913 ctx.h1.c_flags = h1c->flags;
914 ctx.h1.s_flags = h1s->flags;
915 ctx.h1.m_flags = h1m->flags;
916 ctx.h1.m_clen = h1m->curr_len;
917 ctx.h1.m_blen = h1m->body_len;
918
919 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
920 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100921 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
922 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100923}
924
Christopher Fauletadb22202018-12-12 10:32:09 +0100925/* Emit the chunksize followed by a CRLF in front of data of the buffer
926 * <buf>. It goes backwards and starts with the byte before the buffer's
927 * head. The caller is responsible for ensuring there is enough room left before
928 * the buffer's head for the string.
929 */
930static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
931{
932 char *beg, *end;
933
934 beg = end = b_head(buf);
935 *--beg = '\n';
936 *--beg = '\r';
937 do {
938 *--beg = hextab[chksz & 0xF];
939 } while (chksz >>= 4);
940 buf->head -= (end - beg);
941 b_add(buf, end - beg);
942}
943
944/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
945 * ensuring there is enough room left in the buffer for the string. */
946static void h1_emit_chunk_crlf(struct buffer *buf)
947{
948 *(b_peek(buf, b_data(buf))) = '\r';
949 *(b_peek(buf, b_data(buf) + 1)) = '\n';
950 b_add(buf, 2);
951}
952
Christopher Faulet129817b2018-09-20 16:14:40 +0200953/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100954 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletac4778c2019-11-15 11:14:23 +0100955 * CONNECT requests. On the client side, if the response is not finished, the
956 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100957 */
958static void h1_set_req_tunnel_mode(struct h1s *h1s)
959{
960 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
961 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100962 if (!conn_is_back(h1s->h1c->conn) && h1s->res.state < H1_MSG_DONE)
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100963 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100964 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
965 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
966 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
967 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100968}
969
970/*
971 * Switch the response to tunnel mode. This function must only be called on
Christopher Fauletac4778c2019-11-15 11:14:23 +0100972 * successfull replies to CONNECT requests or on protocol switching. In this
973 * last case, this function takes care to switch the request to tunnel mode if
974 * possible. On the server side, if the request is not finished, the mux is mark
975 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100976 */
977static void h1_set_res_tunnel_mode(struct h1s *h1s)
978{
Christopher Fauletac4778c2019-11-15 11:14:23 +0100979 /* On protocol switching, switch the request to tunnel mode if it is in
980 * DONE state. Otherwise we will wait the end of the request to switch
981 * it in tunnel mode.
982 */
983 if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
984 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
985 h1s->req.state = H1_MSG_TUNNEL;
986 }
987
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100988 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
989 h1s->res.state = H1_MSG_TUNNEL;
990 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
991 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100992 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
993 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
994 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100995 }
996}
997
Christopher Faulet82f01602019-05-24 16:50:16 +0200998/* Estimate the size of the HTX headers after the parsing, including the EOH. */
999static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
1000{
1001 size_t sz = 0;
1002 int i;
1003
1004 for (i = 0; hdrs[i].n.len; i++)
1005 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
1006 sz += sizeof(struct htx_blk) + 1;
1007 return sz;
1008}
1009
Christopher Faulet30db3d72019-05-17 15:35:33 +02001010/* Estimate the size of the HTX request after the parsing. */
1011static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
1012{
1013 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001014
1015 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +02001016 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 +02001017 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001018 return sz;
1019}
1020
1021/* Estimate the size of the HTX response after the parsing. */
1022static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
1023{
1024 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001025
1026 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +02001027 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 +02001028 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001029 return sz;
1030}
1031
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001032/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001033 * Add the EOM in the HTX message and switch the message to the DONE state. It
1034 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
1035 * functions is responsible to update the parser state <h1m>. It also add the
1036 * flag CS_FL_EOI on the CS.
1037 */
1038static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
1039{
Willy Tarreau62038152019-08-23 09:29:29 +02001040 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
1041 h1s->flags |= H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001042 return 0;
Willy Tarreau62038152019-08-23 09:29:29 +02001043 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001044
Willy Tarreau62038152019-08-23 09:29:29 +02001045 h1s->flags &= ~H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001046 h1m->state = H1_MSG_DONE;
Christopher Faulet1cf96cd2020-12-07 18:21:27 +01001047 /* Don't set EOI on the conn-stream for protocol upgrade requests, wait
1048 * the response to do so or not depending on the status code.
1049 */
1050 if (!(h1m->flags & H1_MF_CONN_UPG))
1051 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001052 return (sizeof(struct htx_blk) + 1);
1053}
1054
1055/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001056 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001057 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1058 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001059 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001060 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001061static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001062 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001063{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001064 struct htx_sl *sl;
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001065 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001066 union h1_sl h1sl;
1067 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001068 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +02001069 int ret = 0;
1070
Christopher Faulet30db3d72019-05-17 15:35:33 +02001071 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001072 goto end;
1073
Christopher Faulet129817b2018-09-20 16:14:40 +02001074 /* Realing input buffer if necessary */
1075 if (b_head(buf) + b_data(buf) > b_wrap(buf))
1076 b_slow_realign(buf, trash.area, 0);
1077
Christopher Faulet842f41f2019-09-25 09:10:46 +02001078 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001079 /* Try to match H2 preface before parsing the request headers. */
1080 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
1081 if (ret > 0)
1082 goto h2c_upgrade;
1083 }
1084
Christopher Faulet30db3d72019-05-17 15:35:33 +02001085 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001086 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +02001087 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001088 /* Incomplete or invalid message. If the input buffer only
1089 * contains headers and is full, which is detected by it being
1090 * full and the offset to be zero, it's an error because
1091 * headers are too large to be handled by the parser. */
1092 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001093 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001094 goto end;
1095 }
1096
Christopher Fauletb4bad502019-10-11 14:22:00 +02001097 if (h1m->err_pos >= 0) {
1098 /* Maybe we found an error during the parsing while we were
1099 * configured not to block on that, so we have to capture it
1100 * now.
1101 */
1102 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1103 }
1104
Christopher Faulet129817b2018-09-20 16:14:40 +02001105 /* messages headers fully parsed, do some checks to prepare the body
1106 * parsing.
1107 */
1108
Christopher Faulet9768c262018-10-22 09:34:31 +02001109 /* Save the request's method or the response's status, check if the body
1110 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001111 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001112 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001113
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001114 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001115 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001116
1117 if (h1s->meth == HTTP_METH_CONNECT) {
1118 /* Switch CONNECT requests to tunnel mode */
1119 h1_set_req_tunnel_mode(h1s);
1120 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001121
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001122 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1123 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001124 h1m->err_state = h1m->state;
1125 goto vsn_error;
1126 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001127 }
1128 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001129 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001130
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001131 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1132 h1s->status == 101) {
1133 /* Switch successfull replies to CONNECT requests and
1134 * protocol switching to tunnel mode. */
1135 h1_set_res_tunnel_mode(h1s);
1136 }
1137 else if ((h1s->meth == HTTP_METH_HEAD) ||
1138 (h1s->status >= 100 && h1s->status < 200) ||
1139 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001140 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001141 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1142 h1m->flags |= H1_MF_XFER_LEN;
1143 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001144 }
1145 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001146 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001147 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001148 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001149 else {
1150 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001151 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001152 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001153
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001154 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1155 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001156 h1m->err_state = h1m->state;
1157 goto vsn_error;
1158 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001159 }
1160
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001161 /* Set HTX start-line flags */
1162 if (h1m->flags & H1_MF_VER_11)
1163 flags |= HTX_SL_F_VER_11;
1164 if (h1m->flags & H1_MF_XFER_ENC)
1165 flags |= HTX_SL_F_XFER_ENC;
1166 if (h1m->flags & H1_MF_XFER_LEN) {
1167 flags |= HTX_SL_F_XFER_LEN;
1168 if (h1m->flags & H1_MF_CHNK)
1169 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001170 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001171 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001172 if (h1m->body_len == 0)
1173 flags |= HTX_SL_F_BODYLESS;
1174 }
1175 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001176 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001177 }
1178
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001179 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001180 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001181 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1182 if (htx_is_empty(htx))
1183 goto error;
1184 h1m_init_req(h1m);
1185 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1186 ret = 0;
1187 goto end;
1188 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001189
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001190 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1191 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001192 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001193 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001194
1195 /* Check if the uri contains an explicit scheme and if it is
1196 * "http" or "https". */
1197 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1198 sl->flags |= HTX_SL_F_HAS_SCHM;
1199 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1200 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1201 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001202 }
1203 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001204 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1205 if (htx_is_empty(htx))
1206 goto error;
1207 h1m_init_res(h1m);
1208 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1209 ret = 0;
1210 goto end;
1211 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001212
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001213 flags |= HTX_SL_F_IS_RESP;
1214 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1215 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001216 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001217 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001218 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001219
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001220 /* Set bytes used in the HTX mesage for the headers now */
1221 sl->hdrs_bytes = htx_used_space(htx) - used;
1222
Christopher Fauletb992af02019-03-28 15:42:24 +01001223 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001224
1225 /* If body length cannot be determined, set htx->extra to
1226 * ULLONG_MAX. This value is impossible in other cases.
1227 */
1228 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001229 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001230 end:
1231 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001232
1233 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001234 h1m->err_state = h1m->state;
1235 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 vsn_error:
1237 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001238 h1s->cs->flags |= CS_FL_EOI;
1239 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001240 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001241 ret = 0;
1242 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001243
1244 h2c_upgrade:
1245 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001246 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001247 htx->flags |= HTX_FL_UPGRADE;
1248 ret = 0;
1249 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001250}
1251
1252/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001253 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1254 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001255 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001256 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001257 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001258static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001259 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001260 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001261{
1262 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001263 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001264
Christopher Faulet129817b2018-09-20 16:14:40 +02001265 if (h1m->flags & H1_MF_XFER_LEN) {
1266 if (h1m->flags & H1_MF_CLEN) {
1267 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001268 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001269 if ((uint64_t)ret > h1m->curr_len)
1270 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001271 if (ret > b_contig_data(buf, *ofs))
1272 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001273
Christopher Faulet9768c262018-10-22 09:34:31 +02001274 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001275 /* very often with large files we'll face the following
1276 * situation :
1277 * - htx is empty and points to <htxbuf>
1278 * - ret == buf->data
1279 * - buf->head == sizeof(struct htx)
1280 * => we can swap the buffers and place an htx header into
1281 * the target buffer instead
1282 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001283 int32_t try = ret;
1284
Willy Tarreau78f548f2018-12-05 10:02:39 +01001285 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1286 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1287 void *raw_area = buf->area;
1288 void *htx_area = htxbuf->area;
1289 struct htx_blk *blk;
1290
1291 buf->area = htx_area;
1292 htxbuf->area = raw_area;
1293 htx = (struct htx *)htxbuf->area;
1294 htx->size = htxbuf->size - sizeof(*htx);
1295 htx_reset(htx);
1296 b_set_data(htxbuf, b_size(htxbuf));
1297
1298 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1299 blk->info += ret;
1300 /* nothing else to do, the old buffer now contains an
1301 * empty pre-initialized HTX header
1302 */
1303 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001304 else {
1305 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1306 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001307 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001308 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001309 *ofs += ret;
1310 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001311 if (ret < try)
1312 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001313 }
1314
1315 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001316 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001317 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001318 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001319 }
1320 else if (h1m->flags & H1_MF_CHNK) {
1321 new_chunk:
1322 /* te:chunked : parse chunks */
1323 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001324 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001325 if (ret <= 0)
1326 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001327 h1m->state = H1_MSG_CHUNK_SIZE;
1328
Christopher Fauletf2824e62018-10-01 12:12:37 +02001329 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001330 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001331 }
1332
1333 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1334 unsigned int chksz;
1335
Christopher Faulet30db3d72019-05-17 15:35:33 +02001336 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001337 if (ret <= 0)
1338 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001339 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001340
Christopher Faulet129817b2018-09-20 16:14:40 +02001341 h1m->curr_len = chksz;
1342 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001343 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001344 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001345
1346 if (!h1m->curr_len)
1347 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001348 }
1349
1350 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001351 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001352 if ((uint64_t)ret > h1m->curr_len)
1353 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001354 if (ret > b_contig_data(buf, *ofs))
1355 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001356
Christopher Faulet9768c262018-10-22 09:34:31 +02001357 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001358 int32_t try = ret;
1359
1360 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001361 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001362 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001363 *ofs += ret;
1364 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001365 if (ret < try)
1366 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001367 }
1368 if (!h1m->curr_len) {
1369 h1m->state = H1_MSG_CHUNK_CRLF;
1370 goto new_chunk;
1371 }
1372 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001373 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001374 }
1375 else {
1376 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1377 * is no body. Switch the message in DONE state
1378 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001379 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001380 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001381 }
1382 }
1383 else {
1384 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001385 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001386 if (ret > b_contig_data(buf, *ofs))
1387 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001388
Christopher Faulet9768c262018-10-22 09:34:31 +02001389 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001390 int32_t try = ret;
1391
1392 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001393
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001394 *ofs += ret;
1395 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001396 if (ret < try)
1397 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001398 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001399 }
1400
1401 end:
1402 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001403 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001404 h1s->cs->flags |= CS_FL_EOI;
1405 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001406 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001407 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001408 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001409 return 0;
1410 }
Willy Tarreau7e9dd732020-01-17 16:19:34 +01001411
Christopher Fauletafadc9a2020-07-03 14:51:15 +02001412 if (h1s->cs && !(h1m->flags & H1_MF_CHNK) &&
1413 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL)))
Willy Tarreau7e9dd732020-01-17 16:19:34 +01001414 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1415 else if (h1s->cs)
1416 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1417
Christopher Faulet9768c262018-10-22 09:34:31 +02001418 /* update htx->extra, only when the body length is known */
1419 if (h1m->flags & H1_MF_XFER_LEN)
1420 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001421 return total;
1422}
1423
1424/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001425 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1426 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1427 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1428 * responsible to update the parser state <h1m>.
1429 */
1430static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1431 struct buffer *buf, size_t *ofs, size_t max)
1432{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001433 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001434 struct h1m tlr_h1m;
1435 int ret = 0;
1436
1437 if (!max || !b_data(buf))
1438 goto end;
1439
1440 /* Realing input buffer if necessary */
1441 if (b_peek(buf, *ofs) > b_tail(buf))
1442 b_slow_realign(buf, trash.area, 0);
1443
1444 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1445 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1446 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1447 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001448 /* Incomplete or invalid trailers. If the input buffer only
1449 * contains trailers and is full, which is detected by it being
1450 * full and the offset to be zero, it's an error because
1451 * trailers are too large to be handled by the parser. */
1452 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001453 goto error;
1454 goto end;
1455 }
1456
1457 /* messages trailers fully parsed. */
1458 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1459 if (htx_is_empty(htx))
1460 goto error;
1461 ret = 0;
1462 goto end;
1463 }
1464
1465 if (!htx_add_all_trailers(htx, hdrs))
1466 goto error;
1467
1468 *ofs += ret;
1469 h1s->flags |= H1S_F_HAVE_I_TLR;
1470 end:
1471 return ret;
1472
1473 error:
1474 h1m->err_state = h1m->state;
1475 h1m->err_pos = h1m->next;
1476 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1477 h1s->cs->flags |= CS_FL_EOI;
1478 htx->flags |= HTX_FL_PARSING_ERROR;
1479 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1480 ret = 0;
1481 goto end;
1482}
1483
1484/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001485 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001486 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1487 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001488 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001489static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001490{
Christopher Faulet539e0292018-11-19 10:40:09 +01001491 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001492 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001493 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001494 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001495 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001496 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001497
Christopher Faulet539e0292018-11-19 10:40:09 +01001498 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001499
Christopher Fauletf2824e62018-10-01 12:12:37 +02001500 if (!conn_is_back(h1c->conn)) {
1501 h1m = &h1s->req;
1502 errflag = H1S_F_REQ_ERROR;
1503 }
1504 else {
1505 h1m = &h1s->res;
1506 errflag = H1S_F_RES_ERROR;
1507 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001508
Christopher Faulet74027762019-02-26 14:45:05 +01001509 data = htx->data;
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001510 if (h1s->flags & errflag)
1511 goto end;
1512
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001513 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001514 size_t used = htx_used_space(htx);
1515
Christopher Faulet129817b2018-09-20 16:14:40 +02001516 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001517 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001518 if (!ret)
1519 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001520 if ((h1m->flags & H1_MF_RESP) &&
1521 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1522 h1m_init_res(&h1s->res);
1523 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1524 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001525 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001526 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001527 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001528 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001529 if (!ret)
1530 break;
1531 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001532 else if (h1m->state == H1_MSG_TRAILERS) {
1533 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1534 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1535 if (!ret)
1536 break;
1537 }
Christopher Faulet1021e722019-09-03 21:55:14 +02001538 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001539 break;
1540 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001541 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletac4778c2019-11-15 11:14:23 +01001542 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1543 h1_set_req_tunnel_mode(h1s);
Christopher Fauletaffe0cb2020-07-10 10:01:26 +02001544 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001545 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletaffe0cb2020-07-10 10:01:26 +02001546 break;
1547 }
1548 else
1549 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001550 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001551 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001552 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001553 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 if (!ret)
1555 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001556 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001557 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001558 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001559 break;
1560 }
1561
Christopher Faulet30db3d72019-05-17 15:35:33 +02001562 count -= htx_used_space(htx) - used;
Christopher Faulet98e2bc22019-09-03 16:26:15 +02001563 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001564
Christopher Faulet47365272018-10-31 17:40:50 +01001565 if (h1s->flags & errflag)
1566 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001567
Christopher Faulet539e0292018-11-19 10:40:09 +01001568 b_del(&h1c->ibuf, total);
1569
1570 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001571 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001572 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001573 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001574 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001575 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001576 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001577
Christopher Fauletcf56b992018-12-11 16:12:31 +01001578 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1579
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001580 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001581 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauleta882a462019-12-06 15:59:05 +01001582
1583 if ((h1s_data_pending(h1s) && !htx_is_empty(htx)) || (h1s->flags & H1S_F_APPEND_EOM))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001584 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001585
Willy Tarreau62038152019-08-23 09:29:29 +02001586 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1587 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001588 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001589 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001590 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001591 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001592
Christopher Faulet30db3d72019-05-17 15:35:33 +02001593 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001594
1595 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001596 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001597 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001598 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001599}
1600
Christopher Faulet129817b2018-09-20 16:14:40 +02001601/*
1602 * Process outgoing data. It parses data and transfer them from the channel buffer into
1603 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1604 * 0 if it couldn't proceed.
1605 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001606static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1607{
Christopher Faulet129817b2018-09-20 16:14:40 +02001608 struct h1s *h1s = h1c->h1s;
1609 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001610 struct htx *chn_htx;
1611 struct htx_blk *blk;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001612 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001613 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001614 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001615
Christopher Faulet47365272018-10-31 17:40:50 +01001616 if (!count)
1617 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001618
Christopher Faulet94b2c762019-05-24 15:28:57 +02001619 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001620 if (htx_is_empty(chn_htx))
1621 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001622
Christopher Faulet51dbc942018-09-13 09:05:15 +02001623 if (!h1_get_buf(h1c, &h1c->obuf)) {
1624 h1c->flags |= H1C_F_OUT_ALLOC;
1625 goto end;
1626 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001627
Christopher Fauletf2824e62018-10-01 12:12:37 +02001628 if (!conn_is_back(h1c->conn)) {
1629 h1m = &h1s->res;
1630 errflag = H1S_F_RES_ERROR;
1631 }
1632 else {
1633 h1m = &h1s->req;
1634 errflag = H1S_F_REQ_ERROR;
1635 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001636
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001637 if (h1s->flags & errflag)
1638 goto end;
1639
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001640 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001641 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001642
Willy Tarreau3815b222018-12-11 19:50:43 +01001643 /* Perform some optimizations to reduce the number of buffer copies.
1644 * First, if the mux's buffer is empty and the htx area contains
1645 * exactly one data block of the same size as the requested count,
1646 * then it's possible to simply swap the caller's buffer with the
1647 * mux's output buffer and adjust offsets and length to match the
1648 * entire DATA HTX block in the middle. In this case we perform a
1649 * true zero-copy operation from end-to-end. This is the situation
1650 * that happens all the time with large files. Second, if this is not
1651 * possible, but the mux's output buffer is empty, we still have an
1652 * opportunity to avoid the copy to the intermediary buffer, by making
1653 * the intermediary buffer's area point to the output buffer's area.
1654 * In this case we want to skip the HTX header to make sure that copies
1655 * remain aligned and that this operation remains possible all the
1656 * time. This goes for headers, data blocks and any data extracted from
1657 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001658 */
1659 if (!b_data(&h1c->obuf)) {
Willy Tarreau3815b222018-12-11 19:50:43 +01001660 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001661 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001662 htx_get_blk_value(chn_htx, blk).len == count) {
1663 void *old_area = h1c->obuf.area;
1664
1665 h1c->obuf.area = buf->area;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001666 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001667 h1c->obuf.data = count;
1668
1669 buf->area = old_area;
1670 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001671
1672 /* The message is chunked. We need to emit the chunk
1673 * size. We have at least the size of the struct htx to
1674 * write the chunk envelope. It should be enough.
1675 */
1676 if (h1m->flags & H1_MF_CHNK) {
1677 h1_emit_chunk_size(&h1c->obuf, count);
1678 h1_emit_chunk_crlf(&h1c->obuf);
1679 }
1680
Willy Tarreau3815b222018-12-11 19:50:43 +01001681 total += count;
1682 goto out;
1683 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001684 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001685 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001686 else
1687 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001688
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001689 tmp.data = 0;
1690 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001691 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001692 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001693 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001694 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001695 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001696 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001697
Christopher Fauletb2e84162018-12-06 11:39:49 +01001698 vlen = sz;
1699 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001700 if (type != HTX_BLK_DATA)
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001701 goto full;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001702 vlen = count;
1703 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001704
Christopher Faulet94b2c762019-05-24 15:28:57 +02001705 if (type == HTX_BLK_UNUSED)
1706 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001707
Christopher Faulet94b2c762019-05-24 15:28:57 +02001708 switch (h1m->state) {
1709 case H1_MSG_RQBEFORE:
1710 if (type != HTX_BLK_REQ_SL)
1711 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001712 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001713 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001714 h1_parse_req_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001715 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001716 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001717 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001718 if (sl->flags & HTX_SL_F_BODYLESS)
1719 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001720 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001721 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001722
Christopher Faulet94b2c762019-05-24 15:28:57 +02001723 case H1_MSG_RPBEFORE:
1724 if (type != HTX_BLK_RES_SL)
1725 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001726 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001727 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001728 h1_parse_res_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001729 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001730 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001731 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001732 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001733 if (sl->info.res.status < 200 &&
1734 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001735 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001736 h1m->state = H1_MSG_HDR_FIRST;
1737 break;
1738
Christopher Faulet94b2c762019-05-24 15:28:57 +02001739 case H1_MSG_HDR_FIRST:
1740 case H1_MSG_HDR_NAME:
1741 case H1_MSG_HDR_L2_LWS:
1742 if (type == HTX_BLK_EOH)
1743 goto last_lf;
1744 if (type != HTX_BLK_HDR)
1745 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001746 h1m->state = H1_MSG_HDR_NAME;
1747 n = htx_get_blk_name(chn_htx, blk);
1748 v = htx_get_blk_value(chn_htx, blk);
1749
1750 if (isteqi(n, ist("transfer-encoding")))
1751 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001752 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001753 /* Only skip C-L header with invalid value. */
1754 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001755 goto skip_hdr;
1756 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001757 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001758 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001759 if (!v.len)
1760 goto skip_hdr;
1761 }
1762
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001763 /* Try to adjust the case of the header name */
1764 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1765 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001766 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001767 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001768 skip_hdr:
1769 h1m->state = H1_MSG_HDR_L2_LWS;
1770 break;
1771
Christopher Faulet94b2c762019-05-24 15:28:57 +02001772 case H1_MSG_LAST_LF:
1773 if (type != HTX_BLK_EOH)
1774 goto error;
1775 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001776 h1m->state = H1_MSG_LAST_LF;
1777 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001778 /* There is no "Connection:" header and
1779 * it the conn_mode must be
1780 * processed. So do it */
Christopher Faulet661bfc32019-06-17 14:07:46 +02001781 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001782 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001783 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001784 if (v.len) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001785 /* Try to adjust the case of the header name */
1786 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1787 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001788 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001789 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001790 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001791 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001792 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001793
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001794 if ((h1s->meth != HTTP_METH_CONNECT &&
1795 (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 +01001796 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1797 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1798 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1799 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1800 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001801 /* chunking needed but header not seen */
Christopher Faulete5addb02019-10-04 10:23:51 +02001802 n = ist("transfer-encoding");
1803 v = ist("chunked");
1804 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1805 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
1806 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001807 goto full;
Willy Tarreau4710d202019-01-03 17:39:54 +01001808 h1m->flags |= H1_MF_CHNK;
1809 }
1810
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001811 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001812 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001813
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001814 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1815 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1816 h1_set_req_tunnel_mode(h1s);
1817 }
Christopher Fauletac4778c2019-11-15 11:14:23 +01001818 else if ((h1m->flags & H1_MF_RESP) &&
1819 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001820 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletac4778c2019-11-15 11:14:23 +01001821 * to the client. Switch the response to tunnel mode.
1822 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001823 h1_set_res_tunnel_mode(h1s);
1824 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001825 else if ((h1m->flags & H1_MF_RESP) &&
1826 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1827 h1m_init_res(&h1s->res);
1828 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001829 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001830 }
Christopher Faulet33d58b52019-07-01 16:17:30 +02001831 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1832 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001833 else
1834 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001835 break;
1836
Christopher Faulet94b2c762019-05-24 15:28:57 +02001837 case H1_MSG_DATA:
Christopher Fauletaaa25062019-07-04 17:12:12 +02001838 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001839 if (type == HTX_BLK_EOM) {
1840 /* Chunked message without explicit trailers */
1841 if (h1m->flags & H1_MF_CHNK) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001842 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001843 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001844 }
1845 goto done;
1846 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001847 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet52131682019-06-27 17:40:14 +02001848 /* If the message is not chunked, never
1849 * add the last chunk. */
1850 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001851 goto full;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001852 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001853 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001854 else if (type != HTX_BLK_DATA)
1855 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001856 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001857 v.len = vlen;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001858 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001859 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001860 break;
1861
Christopher Faulet94b2c762019-05-24 15:28:57 +02001862 case H1_MSG_TRAILERS:
1863 if (type == HTX_BLK_EOM)
1864 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001865 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001866 goto error;
1867 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001868 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet52131682019-06-27 17:40:14 +02001869 /* If the message is not chunked, ignore
1870 * trailers. It may happen with H2 messages. */
1871 if (!(h1m->flags & H1_MF_CHNK))
1872 break;
1873
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001874 if (type == HTX_BLK_EOT) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001875 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001876 goto full;
Christopher Faulet32188212018-11-20 18:21:43 +01001877 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001878 else { // HTX_BLK_TLR
1879 n = htx_get_blk_name(chn_htx, blk);
1880 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001881
1882 /* Try to adjust the case of the header name */
1883 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1884 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001885 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001886 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001887 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001888 break;
1889
Christopher Faulet94b2c762019-05-24 15:28:57 +02001890 case H1_MSG_DONE:
1891 if (type != HTX_BLK_EOM)
1892 goto error;
1893 done:
1894 h1m->state = H1_MSG_DONE;
Christopher Fauletac4778c2019-11-15 11:14:23 +01001895 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1896 h1_set_req_tunnel_mode(h1s);
1897 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001898 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1899 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1900 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001901 break;
1902
Christopher Faulet9768c262018-10-22 09:34:31 +02001903 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001904 error:
Christopher Fauletc431df82019-06-26 15:16:28 +02001905 /* Unexpected error during output processing */
1906 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001907 h1s->flags |= errflag;
Christopher Fauletc431df82019-06-26 15:16:28 +02001908 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001909 break;
1910 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001911
1912 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001913 total += vlen;
1914 count -= vlen;
1915 if (sz == vlen)
1916 blk = htx_remove_blk(chn_htx, blk);
1917 else {
1918 htx_cut_data_blk(chn_htx, blk, vlen);
1919 break;
1920 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001921 }
1922
Christopher Faulet9768c262018-10-22 09:34:31 +02001923 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001924 /* when the output buffer is empty, tmp shares the same area so that we
1925 * only have to update pointers and lengths.
1926 */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001927 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1928 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001929 else
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001930 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001931
Willy Tarreau3815b222018-12-11 19:50:43 +01001932 htx_to_buf(chn_htx, buf);
1933 out:
Christopher Faulet1cf96cd2020-12-07 18:21:27 +01001934 /* Both the request and the response reached the DONE state. So set EOI
1935 * flag on the conn-stream. Most of time, the flag will already be set,
1936 * except for protocol upgrades.
1937 */
1938 if (h1s->cs && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1939 h1s->cs->flags |= CS_FL_EOI;
1940
Willy Tarreau45f2b892018-12-05 07:59:27 +01001941 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001942 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001943 end:
1944 return total;
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001945
1946 full:
1947 h1c->flags |= H1C_F_OUT_FULL;
1948 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001949}
1950
Christopher Faulet51dbc942018-09-13 09:05:15 +02001951/*********************************************************/
1952/* functions below are I/O callbacks from the connection */
1953/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001954static void h1_wake_stream_for_recv(struct h1s *h1s)
1955{
1956 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001957 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001958 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001959 h1s->recv_wait = NULL;
1960 }
1961}
1962static void h1_wake_stream_for_send(struct h1s *h1s)
1963{
1964 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001965 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001966 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001967 h1s->send_wait = NULL;
1968 }
1969}
1970
Christopher Faulet51dbc942018-09-13 09:05:15 +02001971/*
1972 * Attempt to read data, and subscribe if none available
1973 */
1974static int h1_recv(struct h1c *h1c)
1975{
1976 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001977 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001978 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001979 int rcvd = 0;
1980
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001981 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001982 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001983
Olivier Houchard5b06cb32019-08-13 15:21:59 +02001984 if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN)
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001985 return 0;
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001986
Olivier Houchard75159a92018-12-03 18:46:09 +01001987 if (!h1_recv_allowed(h1c)) {
1988 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001989 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001990 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001991
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001992 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1993 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001994 goto end;
1995 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001996
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001997 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001998 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001999 h1_wake_stream_for_recv(h1s);
2000 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02002001 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002002 }
2003
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002004 /*
2005 * If we only have a small amount of data, realign it,
2006 * it's probably cheaper than doing 2 recv() calls.
2007 */
2008 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2009 b_slow_realign(&h1c->ibuf, trash.area, 0);
2010
Willy Tarreau45f2b892018-12-05 07:59:27 +01002011 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002012 if (max) {
2013 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01002014
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002015 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002016 if (!b_data(&h1c->ibuf)) {
2017 /* try to pre-align the buffer like the rxbufs will be
2018 * to optimize memory copies.
2019 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002020 h1c->ibuf.head = sizeof(struct htx);
2021 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002022 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002023 }
Christopher Faulet47365272018-10-31 17:40:50 +01002024 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002025 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002026 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01002027 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01002028 if (h1s->csinfo.t_idle == -1)
2029 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2030 }
Christopher Faulet47365272018-10-31 17:40:50 +01002031 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002032
Christopher Fauletcf56b992018-12-11 16:12:31 +01002033 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002034 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002035 goto end;
2036 }
2037
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002038 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002039
Christopher Faulet9768c262018-10-22 09:34:31 +02002040 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002041 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2042 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002043
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002044 if (conn_xprt_read0_pending(conn) && h1s) {
2045 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002046 rcvd = 1;
2047 }
2048
Christopher Faulet51dbc942018-09-13 09:05:15 +02002049 if (!b_data(&h1c->ibuf))
2050 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01002051 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002052 h1c->flags |= H1C_F_IN_FULL;
2053 return rcvd;
2054}
2055
2056
2057/*
2058 * Try to send data if possible
2059 */
2060static int h1_send(struct h1c *h1c)
2061{
2062 struct connection *conn = h1c->conn;
2063 unsigned int flags = 0;
2064 size_t ret;
2065 int sent = 0;
2066
2067 if (conn->flags & CO_FL_ERROR)
2068 return 0;
2069
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002070 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002071 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002072 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002073 return 0;
2074 }
2075
Christopher Faulet51dbc942018-09-13 09:05:15 +02002076 if (!b_data(&h1c->obuf))
2077 goto end;
2078
2079 if (h1c->flags & H1C_F_OUT_FULL)
2080 flags |= CO_SFL_MSG_MORE;
2081
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002082 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002083 if (ret > 0) {
2084 h1c->flags &= ~H1C_F_OUT_FULL;
2085 b_del(&h1c->obuf, ret);
2086 sent = 1;
2087 }
2088
Christopher Faulet145aa472018-12-06 10:56:20 +01002089 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
2090 /* error or output closed, nothing to send, clear the buffer to release it */
2091 b_reset(&h1c->obuf);
2092 }
2093
Christopher Faulet51dbc942018-09-13 09:05:15 +02002094 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002095 if (!(h1c->flags & H1C_F_OUT_FULL))
2096 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002097
Christopher Faulet51dbc942018-09-13 09:05:15 +02002098 /* We're done, no more to send */
2099 if (!b_data(&h1c->obuf)) {
2100 h1_release_buf(h1c, &h1c->obuf);
2101 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01002102 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002103 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002104 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002105 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002106
2107 return sent;
2108}
2109
Christopher Faulet51dbc942018-09-13 09:05:15 +02002110
2111/* callback called on any event by the connection handler.
2112 * It applies changes and returns zero, or < 0 if it wants immediate
2113 * destruction of the connection.
2114 */
2115static int h1_process(struct h1c * h1c)
2116{
2117 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002118 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002119
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002120 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002121 return -1;
2122
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002123 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Olivier Houchard690e0f02019-06-11 16:37:24 +02002124 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) ||
Olivier Houchard60630032019-06-13 17:37:00 +02002125 (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE)))
Christopher Faulet539e0292018-11-19 10:40:09 +01002126 goto end;
2127 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01002128 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002129 }
2130
Christopher Fauletfeb11742018-11-29 15:12:34 +01002131 if (!h1s) {
Christopher Faulet470438c2019-07-11 15:40:25 +02002132 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Faulete31a0f12019-12-05 10:23:37 +01002133 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002134 goto release;
Christopher Faulete31a0f12019-12-05 10:23:37 +01002135 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002136 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002137 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002138 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002139 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002140 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002141 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002142 }
2143
Christopher Fauletfeb11742018-11-29 15:12:34 +01002144 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2145 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2146
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002147 if (conn_xprt_read0_pending(conn))
2148 h1s->flags |= H1S_F_REOS;
2149
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002150 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002151 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002152 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002153 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002154 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002155 h1s->cs->data_cb->wake(h1s->cs);
2156 }
Christopher Faulet47365272018-10-31 17:40:50 +01002157 end:
Christopher Fauletb5762062020-08-05 11:31:16 +02002158 h1_refresh_timeout(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002159 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002160
2161 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002162 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002163 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002164}
2165
2166static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2167{
2168 struct h1c *h1c = ctx;
2169 int ret = 0;
2170
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002171 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002172 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002173 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002174 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002175 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002176 h1_process(h1c);
2177 return NULL;
2178}
2179
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002180static void h1_reset(struct connection *conn)
2181{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002182 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002183
2184 /* Reset the flags, and let the mux know we're waiting for a connection */
2185 h1c->flags = H1C_F_CS_WAIT_CONN;
2186}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002187
2188static int h1_wake(struct connection *conn)
2189{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002190 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002191 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002192
Christopher Faulet539e0292018-11-19 10:40:09 +01002193 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002194 ret = h1_process(h1c);
2195 if (ret == 0) {
2196 struct h1s *h1s = h1c->h1s;
2197
2198 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2199 ret = h1s->cs->data_cb->wake(h1s->cs);
2200 }
2201 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002202}
2203
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002204/* Connection timeout management. The principle is that if there's no receipt
2205 * nor sending for a certain amount of time, the connection is closed.
2206 */
2207static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2208{
2209 struct h1c *h1c = context;
2210 int expired = tick_is_expired(t->expire, now_ms);
2211
2212 if (!expired && h1c)
2213 return t;
2214
Olivier Houchard3f795f72019-04-17 22:51:06 +02002215 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002216
2217 if (!h1c) {
2218 /* resources were already deleted */
2219 return NULL;
2220 }
2221
2222 h1c->task = NULL;
2223 /* If a stream is still attached to the mux, just set an error and wait
2224 * for the stream's timeout. Otherwise, release the mux. This is only ok
2225 * because same timeouts are used.
2226 */
2227 if (h1c->h1s && h1c->h1s->cs)
2228 h1c->flags |= H1C_F_CS_ERROR;
2229 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002230 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002231 return NULL;
2232}
2233
Christopher Faulet51dbc942018-09-13 09:05:15 +02002234/*******************************************/
2235/* functions below are used by the streams */
2236/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002237
Christopher Faulet51dbc942018-09-13 09:05:15 +02002238/*
2239 * Attach a new stream to a connection
2240 * (Used for outgoing connections)
2241 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002242static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002243{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002244 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002245 struct conn_stream *cs = NULL;
2246 struct h1s *h1s;
2247
2248 if (h1c->flags & H1C_F_CS_ERROR)
2249 goto end;
2250
2251 cs = cs_new(h1c->conn);
2252 if (!cs)
2253 goto end;
2254
Olivier Houchardf502aca2018-12-14 19:42:40 +01002255 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002256 if (h1s == NULL)
2257 goto end;
2258
2259 return cs;
2260 end:
2261 cs_free(cs);
2262 return NULL;
2263}
2264
2265/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2266 * this mux, it's easy as we can only store a single conn_stream.
2267 */
2268static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2269{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002270 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002271 struct h1s *h1s = h1c->h1s;
2272
2273 if (h1s)
2274 return h1s->cs;
2275
2276 return NULL;
2277}
2278
Christopher Faulet73c12072019-04-08 11:23:22 +02002279static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002280{
Christopher Faulet73c12072019-04-08 11:23:22 +02002281 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002282
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002283 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002284 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002285}
2286
2287/*
2288 * Detach the stream from the connection and possibly release the connection.
2289 */
2290static void h1_detach(struct conn_stream *cs)
2291{
2292 struct h1s *h1s = cs->ctx;
2293 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002294 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002295 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002296
2297 cs->ctx = NULL;
2298 if (!h1s)
2299 return;
2300
Olivier Houchardf502aca2018-12-14 19:42:40 +01002301 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002302 h1c = h1s->h1c;
2303 h1s->cs = NULL;
2304
Olivier Houchard8a786902018-12-15 16:05:40 +01002305 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2306 h1s_destroy(h1s);
2307
Christopher Faulete31a0f12019-12-05 10:23:37 +01002308 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002309 /* If there are any excess server data in the input buffer,
2310 * release it and close the connection ASAP (some data may
2311 * remain in the output buffer). This happens if a server sends
2312 * invalid responses. So in such case, we don't want to reuse
2313 * the connection
Christopher Faulet39e679b2019-07-19 11:34:08 +02002314 */
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002315 if (b_data(&h1c->ibuf)) {
2316 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulete31a0f12019-12-05 10:23:37 +01002317 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002318 goto release;
2319 }
Christopher Faulet39e679b2019-07-19 11:34:08 +02002320
Christopher Faulet9400a392018-11-23 23:10:39 +01002321 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002322 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002323 h1c->conn->flags |= CO_FL_PRIVATE;
2324
Olivier Houchard44d59142018-12-13 18:46:22 +01002325 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002326 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002327 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2328 h1c->conn->owner = NULL;
2329 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2330 /* The server doesn't want it, let's kill the connection right away */
Olivier Houchard109ba132020-02-14 13:23:45 +01002331 h1c->conn->mux->destroy(h1c);
Olivier Houchard351411f2018-12-27 17:20:54 +01002332 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002333 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002334 return;
Olivier Houchard351411f2018-12-27 17:20:54 +01002335 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002336 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002337 if (h1c->conn->owner == sess) {
2338 int ret = session_check_idle_conn(sess, h1c->conn);
2339 if (ret == -1)
2340 /* The connection got destroyed, let's leave */
2341 return;
2342 else if (ret == 1) {
2343 /* The connection was added to the server list,
2344 * wake the task so we can subscribe to events
2345 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002346 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002347 return;
2348 }
2349 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002350 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002351 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002352 struct server *srv = objt_server(h1c->conn->target);
2353
2354 if (srv) {
2355 if (h1c->conn->flags & CO_FL_PRIVATE)
2356 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002357 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002358 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2359 else
2360 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2361 }
2362 }
2363 }
2364
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002365 release:
Christopher Faulet9fa93f62019-06-28 17:41:42 +02002366 /* 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 +02002367 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2368 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2369 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2370 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002371 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002372 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002373 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb5762062020-08-05 11:31:16 +02002374 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002375 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002376}
2377
2378
2379static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2380{
2381 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002382 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002383
2384 if (!h1s)
2385 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002386 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002387
Christopher Faulet7f366362019-04-08 10:51:20 +02002388 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2389 goto do_shutr;
2390
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002391 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392 return;
2393
Christopher Faulet7f366362019-04-08 10:51:20 +02002394 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002395 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2396 if (cs->flags & CS_FL_SHR)
2397 return;
2398 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002399 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Fauletf36e72b2019-12-05 11:18:31 +01002400 (mode == CS_SHR_DRAIN));
Christopher Faulet51dbc942018-09-13 09:05:15 +02002401}
2402
2403static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2404{
2405 struct h1s *h1s = cs->ctx;
2406 struct h1c *h1c;
2407
2408 if (!h1s)
2409 return;
2410 h1c = h1s->h1c;
2411
Christopher Faulet7f366362019-04-08 10:51:20 +02002412 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2413 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002414
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002415 if ((h1c->flags & H1C_F_UPG_H2C) ||
2416 ((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 +02002417 return;
2418
Christopher Faulet7f366362019-04-08 10:51:20 +02002419 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002420 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2421 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2422 return;
2423
Christopher Faulet666a0c42019-01-08 11:12:04 +01002424 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002425}
2426
Christopher Faulet666a0c42019-01-08 11:12:04 +01002427static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002428{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002429 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002430
Christopher Faulet666a0c42019-01-08 11:12:04 +01002431 conn_xprt_shutw(conn);
2432 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Fauletf36e72b2019-12-05 11:18:31 +01002433 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002434}
2435
2436/* Called from the upper layer, to unsubscribe to events */
2437static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2438{
2439 struct wait_event *sw;
2440 struct h1s *h1s = cs->ctx;
2441
2442 if (!h1s)
2443 return 0;
2444
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002445 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002446 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002447 BUG_ON(h1s->recv_wait != sw);
2448 sw->events &= ~SUB_RETRY_RECV;
2449 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002450 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002451 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002452 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002453 BUG_ON(h1s->send_wait != sw);
2454 sw->events &= ~SUB_RETRY_SEND;
2455 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002456 }
2457 return 0;
2458}
2459
2460/* Called from the upper layer, to subscribe to events, such as being able to send */
2461static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2462{
2463 struct wait_event *sw;
2464 struct h1s *h1s = cs->ctx;
2465
2466 if (!h1s)
2467 return -1;
2468
2469 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002470 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002471 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002472 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2473 sw->events |= SUB_RETRY_RECV;
2474 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002475 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002476 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002477 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002478 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2479 sw->events |= SUB_RETRY_SEND;
2480 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002481 return 0;
2482 default:
2483 break;
2484 }
2485 return -1;
2486}
2487
2488/* Called from the upper layer, to receive data */
2489static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2490{
2491 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002492 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002493 size_t ret = 0;
2494
Christopher Faulet539e0292018-11-19 10:40:09 +01002495 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002496 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002497
Christopher Faulete18777b2019-04-16 16:46:36 +02002498 if (flags & CO_RFL_BUF_FLUSH) {
2499 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2500
Christopher Fauletafadc9a2020-07-03 14:51:15 +02002501 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
Christopher Faulete18777b2019-04-16 16:46:36 +02002502 h1s->flags |= H1S_F_BUF_FLUSH;
2503 }
Olivier Houchardcd5da7a2019-08-22 18:34:25 +02002504 else {
Christopher Faulet0528ae22020-07-03 15:12:00 +02002505 if (ret)
2506 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002507 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Fauletd2a762f2020-07-24 16:31:14 +02002508 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002509 }
2510 return ret;
2511}
2512
2513
2514/* Called from the upper layer, to send data */
2515static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2516{
2517 struct h1s *h1s = cs->ctx;
2518 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002519 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002520
2521 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002522 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002523
2524 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002525 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2526 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002527
Christopher Fauletc31872f2019-06-04 22:09:36 +02002528 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002529 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002530
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002531 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2532 ret = h1_process_output(h1c, buf, count);
2533 if (!ret)
2534 break;
2535 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002536 count -= ret;
Olivier Houcharda1012862020-01-15 19:13:32 +01002537 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002538 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002539 }
Christopher Fauletb5762062020-08-05 11:31:16 +02002540 h1_refresh_timeout(h1c);
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002541 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002542}
2543
Willy Tarreaue5733232019-05-22 19:24:06 +02002544#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002545/* Send and get, using splicing */
2546static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2547{
2548 struct h1s *h1s = cs->ctx;
2549 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2550 int ret = 0;
2551
Christopher Faulet2c411be2019-11-05 16:24:27 +01002552 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002553 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002554 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2555 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002556 goto end;
2557 }
2558
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002559 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002560 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002561 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002562 }
2563
2564 h1s->flags &= ~H1S_F_BUF_FLUSH;
2565 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet8e8168a2020-07-03 15:02:25 +02002566
2567 if (!h1_recv_allowed(h1s->h1c))
2568 goto end;
2569
Christopher Faulet1be55f92018-10-02 15:59:23 +02002570 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2571 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002572 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet290988a2020-07-24 16:13:12 +02002573 if (ret <= 0)
2574 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2575 else if (h1m->state == H1_MSG_DATA) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002576 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002577 if (!h1m->curr_len)
2578 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2579 }
2580
Christopher Faulet1be55f92018-10-02 15:59:23 +02002581 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002582 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002583 h1s->flags |= H1S_F_REOS;
Christopher Fauletac4778c2019-11-15 11:14:23 +01002584 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet038ad812019-04-17 11:03:22 +02002585 }
Willy Tarreau7e9dd732020-01-17 16:19:34 +01002586
Christopher Faulete0ca6ad2020-07-07 10:56:40 +02002587 if ((h1s->flags & H1S_F_REOS) ||
2588 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Fauletafadc9a2020-07-03 14:51:15 +02002589 (h1m->state == H1_MSG_DATA && !h1m->curr_len))
Willy Tarreau7e9dd732020-01-17 16:19:34 +01002590 cs->flags &= ~CS_FL_MAY_SPLICE;
2591
Christopher Faulet1be55f92018-10-02 15:59:23 +02002592 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002593}
2594
2595static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2596{
2597 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002598 int ret = 0;
2599
2600 if (b_data(&h1s->h1c->obuf))
2601 goto end;
2602
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002603 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002604 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002605 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002606 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002607 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002608 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002609 return ret;
2610}
2611#endif
2612
Olivier Houchard198d1292019-10-25 16:19:26 +02002613static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2614{
2615 int ret = 0;
2616 switch (mux_ctl) {
2617 case MUX_STATUS:
2618 if (conn->flags & CO_FL_CONNECTED)
2619 ret |= MUX_STATUS_READY;
2620 return ret;
2621 default:
2622 return -1;
2623 }
2624}
2625
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002626/* for debugging with CLI's "show fd" command */
2627static void h1_show_fd(struct buffer *msg, struct connection *conn)
2628{
2629 struct h1c *h1c = conn->ctx;
2630 struct h1s *h1s = h1c->h1s;
2631
Christopher Fauletf376a312019-01-04 15:16:06 +01002632 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2633 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002634 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2635 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2636 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2637 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2638
2639 if (h1s) {
2640 char *method;
2641
2642 if (h1s->meth < HTTP_METH_OTHER)
2643 method = http_known_methods[h1s->meth].ptr;
2644 else
2645 method = "UNKNOWN";
2646 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2647 " .meth=%s status=%d",
2648 h1s, h1s->flags,
2649 h1m_state_str(h1s->req.state),
2650 h1m_state_str(h1s->res.state), method, h1s->status);
2651 if (h1s->cs)
2652 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2653 h1s->cs->flags, h1s->cs->data);
2654 }
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002655}
2656
2657
2658/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2659static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2660{
2661 struct h1_hdr_entry *entry;
2662
2663 /* Be sure there is a non-empty <to> */
2664 if (!strlen(to)) {
2665 memprintf(err, "expect <to>");
2666 return -1;
2667 }
2668
2669 /* Be sure only the case differs between <from> and <to> */
2670 if (strcasecmp(from, to)) {
2671 memprintf(err, "<from> and <to> must not differ execpt the case");
2672 return -1;
2673 }
2674
2675 /* Be sure <from> does not already existsin the tree */
2676 if (ebis_lookup(&hdrs_map.map, from)) {
2677 memprintf(err, "duplicate entry '%s'", from);
2678 return -1;
2679 }
2680
2681 /* Create the entry and insert it in the tree */
2682 entry = malloc(sizeof(*entry));
2683 if (!entry) {
2684 memprintf(err, "out of memory");
2685 return -1;
2686 }
2687
2688 entry->node.key = strdup(from);
2689 entry->name.ptr = strdup(to);
2690 entry->name.len = strlen(to);
2691 if (!entry->node.key || !entry->name.ptr) {
2692 free(entry->node.key);
2693 free(entry->name.ptr);
2694 free(entry);
2695 memprintf(err, "out of memory");
2696 return -1;
2697 }
2698 ebis_insert(&hdrs_map.map, &entry->node);
2699 return 0;
2700}
2701
2702static void h1_hdeaders_case_adjust_deinit()
2703{
2704 struct ebpt_node *node, *next;
2705 struct h1_hdr_entry *entry;
2706
2707 node = ebpt_first(&hdrs_map.map);
2708 while (node) {
2709 next = ebpt_next(node);
2710 ebpt_delete(node);
2711 entry = container_of(node, struct h1_hdr_entry, node);
2712 free(entry->node.key);
2713 free(entry->name.ptr);
2714 free(entry);
2715 node = next;
2716 }
2717 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002718}
2719
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002720static int cfg_h1_headers_case_adjust_postparser()
2721{
2722 FILE *file = NULL;
2723 char *c, *key_beg, *key_end, *value_beg, *value_end;
2724 char *err;
2725 int rc, line = 0, err_code = 0;
2726
2727 if (!hdrs_map.name)
2728 goto end;
2729
2730 file = fopen(hdrs_map.name, "r");
2731 if (!file) {
2732 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2733 hdrs_map.name);
2734 err_code |= ERR_ALERT | ERR_FATAL;
2735 goto end;
2736 }
2737
2738 /* now parse all lines. The file may contain only two header name per
2739 * line, separated by spaces. All heading and trailing spaces will be
2740 * ignored. Lines starting with a # are ignored.
2741 */
2742 while (fgets(trash.area, trash.size, file) != NULL) {
2743 line++;
2744 c = trash.area;
2745
2746 /* strip leading spaces and tabs */
2747 while (*c == ' ' || *c == '\t')
2748 c++;
2749
2750 /* ignore emptu lines, or lines beginning with a dash */
2751 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2752 continue;
2753
2754 /* look for the end of the key */
2755 key_beg = c;
2756 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2757 c++;
2758 key_end = c;
2759
2760 /* strip middle spaces and tabs */
2761 while (*c == ' ' || *c == '\t')
2762 c++;
2763
2764 /* look for the end of the value, it is the end of the line */
2765 value_beg = c;
2766 while (*c && *c != '\n' && *c != '\r')
2767 c++;
2768 value_end = c;
2769
2770 /* trim possibly trailing spaces and tabs */
2771 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2772 value_end--;
2773
2774 /* set final \0 and check entries */
2775 *key_end = '\0';
2776 *value_end = '\0';
2777
2778 err = NULL;
2779 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2780 if (rc < 0) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002781 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2782 hdrs_map.name, err, line);
2783 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Faulet27f20172019-07-30 16:51:42 +02002784 free(err);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002785 goto end;
2786 }
2787 if (rc > 0) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002788 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2789 hdrs_map.name, err, line);
2790 err_code |= ERR_WARN;
Christopher Faulet27f20172019-07-30 16:51:42 +02002791 free(err);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002792 }
2793 }
2794
2795 end:
2796 if (file)
2797 fclose(file);
2798 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2799 return err_code;
2800}
2801
2802
2803/* config parser for global "h1-outgoing-header-case-adjust" */
2804static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2805 struct proxy *defpx, const char *file, int line,
2806 char **err)
2807{
2808 if (too_many_args(2, args, err, NULL))
2809 return -1;
2810 if (!*(args[1]) || !*(args[2])) {
2811 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2812 return -1;
2813 }
2814 return add_hdr_case_adjust(args[1], args[2], err);
2815}
2816
2817/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2818static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2819 struct proxy *defpx, const char *file, int line,
2820 char **err)
2821{
2822 if (too_many_args(1, args, err, NULL))
2823 return -1;
2824 if (!*(args[1])) {
2825 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2826 return -1;
2827 }
2828 free(hdrs_map.name);
2829 hdrs_map.name = strdup(args[1]);
2830 return 0;
2831}
2832
2833
2834/* config keyword parsers */
2835static struct cfg_kw_list cfg_kws = {{ }, {
2836 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2837 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2838 { 0, NULL, NULL },
2839 }
2840};
2841
2842INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2843REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2844
2845
Christopher Faulet51dbc942018-09-13 09:05:15 +02002846/****************************************/
2847/* MUX initialization and instanciation */
2848/****************************************/
2849
2850/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002851static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002852 .init = h1_init,
2853 .wake = h1_wake,
2854 .attach = h1_attach,
2855 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002856 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002857 .detach = h1_detach,
2858 .destroy = h1_destroy,
2859 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002860 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002861 .rcv_buf = h1_rcv_buf,
2862 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002863#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002864 .rcv_pipe = h1_rcv_pipe,
2865 .snd_pipe = h1_snd_pipe,
2866#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002867 .subscribe = h1_subscribe,
2868 .unsubscribe = h1_unsubscribe,
2869 .shutr = h1_shutr,
2870 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002871 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002872 .reset = h1_reset,
Olivier Houchard198d1292019-10-25 16:19:26 +02002873 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002874 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002875 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002876};
2877
2878
2879/* this mux registers default HTX proto */
2880static struct mux_proto_list mux_proto_htx =
2881{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2882
Willy Tarreau0108d902018-11-25 19:14:37 +01002883INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2884
Christopher Faulet51dbc942018-09-13 09:05:15 +02002885/*
2886 * Local variables:
2887 * c-indent-level: 8
2888 * c-basic-offset: 8
2889 * End:
2890 */