blob: 1254f2c158b09c4a5310268921e7b0b735cda097 [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
Willy Tarreau00f18a32019-01-26 12:19:01 +0100243
Christopher Faulet51dbc942018-09-13 09:05:15 +0200244/*****************************************************************/
245/* functions below are dedicated to the mux setup and management */
246/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200247
248/* returns non-zero if there are input data pending for stream h1s. */
249static inline size_t h1s_data_pending(const struct h1s *h1s)
250{
251 const struct h1m *h1m;
252
253 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
254 if (h1m->state == H1_MSG_DONE)
255 return 0; // data not for this stream (e.g. pipelining)
256
257 return b_data(&h1s->h1c->ibuf);
258}
259
Christopher Faulet47365272018-10-31 17:40:50 +0100260static struct conn_stream *h1s_new_cs(struct h1s *h1s)
261{
262 struct conn_stream *cs;
263
264 cs = cs_new(h1s->h1c->conn);
265 if (!cs)
266 goto err;
267 h1s->cs = cs;
268 cs->ctx = h1s;
269
270 if (h1s->flags & H1S_F_NOT_FIRST)
271 cs->flags |= CS_FL_NOT_FIRST;
272
Willy Tarreau7e9dd732020-01-17 16:19:34 +0100273 if (global.tune.options & GTUNE_USE_SPLICE)
274 cs->flags |= CS_FL_MAY_SPLICE;
275
Christopher Faulet47365272018-10-31 17:40:50 +0100276 if (stream_create_from_cs(cs) < 0)
277 goto err;
278 return cs;
279
280 err:
281 cs_free(cs);
282 h1s->cs = NULL;
283 return NULL;
284}
285
Olivier Houchardf502aca2018-12-14 19:42:40 +0100286static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200287{
288 struct h1s *h1s;
289
290 h1s = pool_alloc(pool_head_h1s);
291 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100292 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200293
294 h1s->h1c = h1c;
295 h1c->h1s = h1s;
296
Olivier Houchardf502aca2018-12-14 19:42:40 +0100297 h1s->sess = sess;
298
Christopher Faulet51dbc942018-09-13 09:05:15 +0200299 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100300 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200301
302 h1s->recv_wait = NULL;
303 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200304
305 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100306 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200307
Christopher Faulet129817b2018-09-20 16:14:40 +0200308 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100309 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200310
311 h1s->status = 0;
312 h1s->meth = HTTP_METH_OTHER;
313
Christopher Faulet47365272018-10-31 17:40:50 +0100314 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
315 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulete31a0f12019-12-05 10:23:37 +0100316 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100317
Christopher Faulet129817b2018-09-20 16:14:40 +0200318 if (!conn_is_back(h1c->conn)) {
319 if (h1c->px->options2 & PR_O2_REQBUG_OK)
320 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200321
322 /* For frontend connections we should always have a session */
323 if (!sess)
324 sess = h1c->conn->owner;
325
Dave Pirottecf67ec52019-07-10 13:57:38 +0000326 /* Timers for subsequent sessions on the same HTTP 1.x connection
327 * measure from `now`, not from the connection accept time */
328 if (h1s->flags & H1S_F_NOT_FIRST) {
329 h1s->csinfo.create_date = date;
330 h1s->csinfo.tv_create = now;
331 h1s->csinfo.t_handshake = 0;
332 h1s->csinfo.t_idle = -1;
333 }
334 else {
335 h1s->csinfo.create_date = sess->accept_date;
336 h1s->csinfo.tv_create = sess->tv_accept;
337 h1s->csinfo.t_handshake = sess->t_handshake;
338 h1s->csinfo.t_idle = -1;
339 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200340 }
341 else {
342 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
343 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200344
Christopher Fauletfeb11742018-11-29 15:12:34 +0100345 h1s->csinfo.create_date = date;
346 h1s->csinfo.tv_create = now;
347 h1s->csinfo.t_handshake = 0;
348 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200349 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100350
Christopher Faulete9b70722019-04-08 10:46:02 +0200351 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
352 * create a new one.
353 */
354 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200355 cs->ctx = h1s;
356 h1s->cs = cs;
357 }
Christopher Faulet47365272018-10-31 17:40:50 +0100358 else {
359 cs = h1s_new_cs(h1s);
360 if (!cs)
361 goto fail;
362 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200363 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100364
365 fail:
366 pool_free(pool_head_h1s, h1s);
367 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200368}
369
370static void h1s_destroy(struct h1s *h1s)
371{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200372 if (h1s) {
373 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200374
Christopher Fauletf2824e62018-10-01 12:12:37 +0200375 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200376
Christopher Fauletf2824e62018-10-01 12:12:37 +0200377 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100378 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200379 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100380 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200381
Christopher Fauletcb55f482018-12-10 11:56:47 +0100382 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100383 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
384 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulete31a0f12019-12-05 10:23:37 +0100385
386 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
387 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
388 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
389 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
390 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
391 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200392 pool_free(pool_head_h1s, h1s);
393 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200394}
395
Christopher Fauletfeb11742018-11-29 15:12:34 +0100396static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
397{
398 struct h1s *h1s = cs->ctx;
399
400 if (h1s && !conn_is_back(cs->conn))
401 return &h1s->csinfo;
402 return NULL;
403}
404
Christopher Faulet51dbc942018-09-13 09:05:15 +0200405/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200406 * Initialize the mux once it's attached. It is expected that conn->ctx points
407 * to the existing conn_stream (for outgoing connections or for incoming onces
408 * during a mux upgrade) or NULL (for incoming ones during the connexion
409 * establishment). <input> is always used as Input buffer and may contain
410 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
411 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200412 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200413static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
414 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200415{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200416 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100417 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200418
419 h1c = pool_alloc(pool_head_h1c);
420 if (!h1c)
421 goto fail_h1c;
422 h1c->conn = conn;
423 h1c->px = proxy;
424
Christopher Faulete31a0f12019-12-05 10:23:37 +0100425 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200426 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200427 h1c->obuf = BUF_NULL;
428 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200429 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200430
Christopher Faulet51dbc942018-09-13 09:05:15 +0200431 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200432 h1c->wait_event.tasklet = tasklet_new();
433 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200434 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200435 h1c->wait_event.tasklet->process = h1_io_cb;
436 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100437 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200438
Christopher Faulete9b70722019-04-08 10:46:02 +0200439 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100440 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
441 if (tick_isset(proxy->timeout.serverfin))
442 h1c->shut_timeout = proxy->timeout.serverfin;
443 } else {
444 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
445 if (tick_isset(proxy->timeout.clientfin))
446 h1c->shut_timeout = proxy->timeout.clientfin;
447 }
448 if (tick_isset(h1c->timeout)) {
449 t = task_new(tid_bit);
450 if (!t)
451 goto fail;
452
453 h1c->task = t;
454 t->process = h1_timeout_task;
455 t->context = h1c;
456 t->expire = tick_add(now_ms, h1c->timeout);
457 }
458
Olivier Houchard985234d2019-06-13 17:54:33 +0200459 if (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200460 h1c->flags |= H1C_F_CS_WAIT_CONN;
461
Christopher Fauletf2824e62018-10-01 12:12:37 +0200462 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100463 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200464 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200465
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100466 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200467
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100468
469 if (t)
470 task_queue(t);
471
Christopher Faulet51dbc942018-09-13 09:05:15 +0200472 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200473 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200474
475 /* mux->wake will be called soon to complete the operation */
476 return 0;
477
478 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200479 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200480 if (h1c->wait_event.tasklet)
481 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200482 pool_free(pool_head_h1c, h1c);
483 fail_h1c:
484 return -1;
485}
486
Christopher Faulet73c12072019-04-08 11:23:22 +0200487/* release function. This one should be called to free all resources allocated
488 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200489 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200490static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200491{
Christopher Faulet61840e72019-04-15 09:33:32 +0200492 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200493
Christopher Faulet51dbc942018-09-13 09:05:15 +0200494 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200495 /* The connection must be aattached to this mux to be released */
496 if (h1c->conn && h1c->conn->ctx == h1c)
497 conn = h1c->conn;
498
499 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200500 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard13f556e2019-07-03 13:08:18 +0200501 /* Make sure we're no longer subscribed to anything */
502 if (h1c->wait_event.events)
503 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
504 h1c->wait_event.events, &h1c->wait_event);
Olivier Houchard45c44372019-06-11 14:06:23 +0200505 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200506 /* connection successfully upgraded to H2, this
507 * mux was already released */
508 return;
509 }
510 sess_log(conn->owner); /* Log if the upgrade failed */
511 }
512
Olivier Houchard45c44372019-06-11 14:06:23 +0200513
Christopher Faulet51dbc942018-09-13 09:05:15 +0200514 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
515 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
516 LIST_DEL(&h1c->buf_wait.list);
517 LIST_INIT(&h1c->buf_wait.list);
518 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
519 }
520
521 h1_release_buf(h1c, &h1c->ibuf);
522 h1_release_buf(h1c, &h1c->obuf);
523
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100524 if (h1c->task) {
525 h1c->task->context = NULL;
526 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
527 h1c->task = NULL;
528 }
529
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200530 if (h1c->wait_event.tasklet)
531 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200532
Christopher Fauletf2824e62018-10-01 12:12:37 +0200533 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200534 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100535 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200536 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200537 pool_free(pool_head_h1c, h1c);
538 }
539
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200540 if (conn) {
541 conn->mux = NULL;
542 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200543
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200544 conn_stop_tracking(conn);
545 conn_full_close(conn);
546 if (conn->destroy_cb)
547 conn->destroy_cb(conn);
548 conn_free(conn);
549 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200550}
551
552/******************************************************/
553/* functions below are for the H1 protocol processing */
554/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200555/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
556 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200557 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100558static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200559{
Christopher Faulet570d1612018-11-26 11:13:57 +0100560 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200561
Christopher Faulet570d1612018-11-26 11:13:57 +0100562 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200563 (*(p + 5) > '1' ||
564 (*(p + 5) == '1' && *(p + 7) >= '1')))
565 h1m->flags |= H1_MF_VER_11;
566}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200567
Christopher Faulet9768c262018-10-22 09:34:31 +0200568/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
569 * greater or equal to 1.1
570 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100571static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200572{
Christopher Faulet570d1612018-11-26 11:13:57 +0100573 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200574
Christopher Faulet570d1612018-11-26 11:13:57 +0100575 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200576 (*(p + 5) > '1' ||
577 (*(p + 5) == '1' && *(p + 7) >= '1')))
578 h1m->flags |= H1_MF_VER_11;
579}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200580
Christopher Faulet9768c262018-10-22 09:34:31 +0200581/*
582 * Check the validity of the request version. If the version is valid, it
583 * returns 1. Otherwise, it returns 0.
584 */
585static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
586{
587 struct h1c *h1c = h1s->h1c;
588
589 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
590 * exactly one digit "." one digit. This check may be disabled using
591 * option accept-invalid-http-request.
592 */
593 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
594 if (sl.rq.v.len != 8)
595 return 0;
596
597 if (*(sl.rq.v.ptr + 4) != '/' ||
598 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
599 *(sl.rq.v.ptr + 6) != '.' ||
600 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
601 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200602 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200603 else if (!sl.rq.v.len) {
604 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200605
Christopher Faulet9768c262018-10-22 09:34:31 +0200606 /* RFC 1945 allows only GET for HTTP/0.9 requests */
607 if (sl.rq.meth != HTTP_METH_GET)
608 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200609
Christopher Faulet9768c262018-10-22 09:34:31 +0200610 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
611 if (!sl.rq.u.len)
612 return 0;
613
614 /* Add HTTP version */
615 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100616 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200617 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100618
619 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100620 ((*(sl.rq.v.ptr + 5) > '1') ||
621 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100622 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200623 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200624}
625
Christopher Faulet9768c262018-10-22 09:34:31 +0200626/*
627 * Check the validity of the response version. If the version is valid, it
628 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200629 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200630static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200631{
Christopher Faulet9768c262018-10-22 09:34:31 +0200632 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200633
Christopher Faulet9768c262018-10-22 09:34:31 +0200634 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
635 * exactly one digit "." one digit. This check may be disabled using
636 * option accept-invalid-http-request.
637 */
638 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
639 if (sl.st.v.len != 8)
640 return 0;
641
642 if (*(sl.st.v.ptr + 4) != '/' ||
643 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
644 *(sl.st.v.ptr + 6) != '.' ||
645 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
646 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200647 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100648
649 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100650 ((*(sl.st.v.ptr + 5) > '1') ||
651 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100652 h1m->flags |= H1_MF_VER_11;
653
Christopher Faulet9768c262018-10-22 09:34:31 +0200654 return 1;
655}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200656
657/* Deduce the connection mode of the client connection, depending on the
658 * configuration and the H1 message flags. This function is called twice, the
659 * first time when the request is parsed and the second time when the response
660 * is parsed.
661 */
662static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
663{
664 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200665
666 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100667 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200668 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100669 h1s->status == 101) {
670 /* Either we've established an explicit tunnel, or we're
671 * switching the protocol. In both cases, we're very unlikely to
672 * understand the next protocols. We have to switch to tunnel
673 * mode, so that we transfer the request and responses then let
674 * this protocol pass unmodified. When we later implement
675 * specific parsers for such protocols, we'll want to check the
676 * Upgrade header which contains information about that protocol
677 * for responses with status 101 (eg: see RFC2817 about TLS).
678 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200679 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100680 }
681 else if (h1s->flags & H1S_F_WANT_KAL) {
682 /* By default the client is in KAL mode. CLOSE mode mean
683 * it is imposed by the client itself. So only change
684 * KAL mode here. */
685 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
686 /* no length known or explicit close => close */
687 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
688 }
689 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
690 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
691 /* no explict keep-alive and option httpclose => close */
692 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
693 }
694 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200695 }
696 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100697 /* Input direction: first pass */
698 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
699 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200700 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100701 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200702 }
703
704 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
705 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
706 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
707}
708
709/* Deduce the connection mode of the client connection, depending on the
710 * configuration and the H1 message flags. This function is called twice, the
711 * first time when the request is parsed and the second time when the response
712 * is parsed.
713 */
714static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
715{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100716 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100717 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100718 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200719
Christopher Fauletf2824e62018-10-01 12:12:37 +0200720 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100721 /* Input direction: second pass */
722
Christopher Fauletf2824e62018-10-01 12:12:37 +0200723 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100724 h1s->status == 101) {
725 /* Either we've established an explicit tunnel, or we're
726 * switching the protocol. In both cases, we're very unlikely to
727 * understand the next protocols. We have to switch to tunnel
728 * mode, so that we transfer the request and responses then let
729 * this protocol pass unmodified. When we later implement
730 * specific parsers for such protocols, we'll want to check the
731 * Upgrade header which contains information about that protocol
732 * for responses with status 101 (eg: see RFC2817 about TLS).
733 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200734 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100735 }
736 else if (h1s->flags & H1S_F_WANT_KAL) {
737 /* By default the server is in KAL mode. CLOSE mode mean
738 * it is imposed by haproxy itself. So only change KAL
739 * mode here. */
740 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
741 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
742 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
743 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
744 }
745 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200746 }
Christopher Faulet70033782018-12-05 13:50:11 +0100747 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100748 /* Output direction: first pass */
749 if (h1m->flags & H1_MF_CONN_CLO) {
750 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100751 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100752 }
753 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
754 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
755 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
756 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
757 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
758 /* no explicit keep-alive option httpclose/server-close => close */
759 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
760 }
Christopher Faulet70033782018-12-05 13:50:11 +0100761 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200762
763 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
764 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
765 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200766}
767
Christopher Fauletb992af02019-03-28 15:42:24 +0100768static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200769{
770 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200771
772 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
773 * token is found
774 */
775 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200776 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200777
778 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100779 if (!(h1m->flags & H1_MF_VER_11))
780 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200781 }
782 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100783 if (h1m->flags & H1_MF_VER_11)
784 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200785 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200786}
787
Christopher Fauletb992af02019-03-28 15:42:24 +0100788static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200789{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200790 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
791 * token is found
792 */
793 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200794 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200795
796 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100797 if (!(h1m->flags & H1_MF_VER_11) ||
798 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
799 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200800 }
801 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100802 if (h1m->flags & H1_MF_VER_11)
803 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200804 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200805}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200806
Christopher Fauletb992af02019-03-28 15:42:24 +0100807static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200808{
Christopher Fauletb992af02019-03-28 15:42:24 +0100809 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200810 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100811 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200812 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200813}
814
Christopher Fauletb992af02019-03-28 15:42:24 +0100815static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
816{
817 if (!conn_is_back(h1s->h1c->conn))
818 h1_set_cli_conn_mode(h1s, h1m);
819 else
820 h1_set_srv_conn_mode(h1s, h1m);
821
822 if (!(h1m->flags & H1_MF_RESP))
823 h1_update_req_conn_value(h1s, h1m, conn_val);
824 else
825 h1_update_res_conn_value(h1s, h1m, conn_val);
826}
Christopher Faulete44769b2018-11-29 23:01:45 +0100827
Christopher Fauleta99ff4d2019-07-22 16:18:24 +0200828/* Try to adjust the case of the message header name using the global map
829 * <hdrs_map>.
830 */
831static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
832{
833 struct ebpt_node *node;
834 struct h1_hdr_entry *entry;
835
836 /* No entry in the map, do nothing */
837 if (eb_is_empty(&hdrs_map.map))
838 return;
839
840 /* No conversion fo the request headers */
841 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
842 return;
843
844 /* No conversion fo the response headers */
845 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
846 return;
847
848 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
849 if (!node)
850 return;
851 entry = container_of(node, struct h1_hdr_entry, node);
852 name->ptr = entry->name.ptr;
853 name->len = entry->name.len;
854}
855
Christopher Faulete44769b2018-11-29 23:01:45 +0100856/* Append the description of what is present in error snapshot <es> into <out>.
857 * The description must be small enough to always fit in a buffer. The output
858 * buffer may be the trash so the trash must not be used inside this function.
859 */
860static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
861{
862 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100863 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
864 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
865 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
866 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
867 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
868 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100869}
870/*
871 * Capture a bad request or response and archive it in the proxy's structure.
872 * By default it tries to report the error position as h1m->err_pos. However if
873 * this one is not set, it will then report h1m->next, which is the last known
874 * parsing point. The function is able to deal with wrapping buffers. It always
875 * displays buffers as a contiguous area starting at buf->p. The direction is
876 * determined thanks to the h1m's flags.
877 */
878static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
879 struct h1m *h1m, struct buffer *buf)
880{
881 struct session *sess = h1c->conn->owner;
882 struct proxy *proxy = h1c->px;
883 struct proxy *other_end = sess->fe;
884 union error_snapshot_ctx ctx;
885
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100886 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100887 other_end = si_strm(h1s->cs->data)->be;
888
889 /* http-specific part now */
890 ctx.h1.state = h1m->state;
891 ctx.h1.c_flags = h1c->flags;
892 ctx.h1.s_flags = h1s->flags;
893 ctx.h1.m_flags = h1m->flags;
894 ctx.h1.m_clen = h1m->curr_len;
895 ctx.h1.m_blen = h1m->body_len;
896
897 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
898 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100899 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
900 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100901}
902
Christopher Fauletadb22202018-12-12 10:32:09 +0100903/* Emit the chunksize followed by a CRLF in front of data of the buffer
904 * <buf>. It goes backwards and starts with the byte before the buffer's
905 * head. The caller is responsible for ensuring there is enough room left before
906 * the buffer's head for the string.
907 */
908static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
909{
910 char *beg, *end;
911
912 beg = end = b_head(buf);
913 *--beg = '\n';
914 *--beg = '\r';
915 do {
916 *--beg = hextab[chksz & 0xF];
917 } while (chksz >>= 4);
918 buf->head -= (end - beg);
919 b_add(buf, end - beg);
920}
921
922/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
923 * ensuring there is enough room left in the buffer for the string. */
924static void h1_emit_chunk_crlf(struct buffer *buf)
925{
926 *(b_peek(buf, b_data(buf))) = '\r';
927 *(b_peek(buf, b_data(buf) + 1)) = '\n';
928 b_add(buf, 2);
929}
930
Christopher Faulet129817b2018-09-20 16:14:40 +0200931/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100932 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletac4778c2019-11-15 11:14:23 +0100933 * CONNECT requests. On the client side, if the response is not finished, the
934 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100935 */
936static void h1_set_req_tunnel_mode(struct h1s *h1s)
937{
938 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
939 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100940 if (!conn_is_back(h1s->h1c->conn) && h1s->res.state < H1_MSG_DONE)
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100941 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100942 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
943 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
944 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
945 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100946}
947
948/*
949 * Switch the response to tunnel mode. This function must only be called on
Christopher Fauletac4778c2019-11-15 11:14:23 +0100950 * successfull replies to CONNECT requests or on protocol switching. In this
951 * last case, this function takes care to switch the request to tunnel mode if
952 * possible. On the server side, if the request is not finished, the mux is mark
953 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100954 */
955static void h1_set_res_tunnel_mode(struct h1s *h1s)
956{
Christopher Fauletac4778c2019-11-15 11:14:23 +0100957 /* On protocol switching, switch the request to tunnel mode if it is in
958 * DONE state. Otherwise we will wait the end of the request to switch
959 * it in tunnel mode.
960 */
961 if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
962 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
963 h1s->req.state = H1_MSG_TUNNEL;
964 }
965
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100966 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
967 h1s->res.state = H1_MSG_TUNNEL;
968 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
969 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100970 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
971 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
972 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100973 }
974}
975
Christopher Faulet82f01602019-05-24 16:50:16 +0200976/* Estimate the size of the HTX headers after the parsing, including the EOH. */
977static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
978{
979 size_t sz = 0;
980 int i;
981
982 for (i = 0; hdrs[i].n.len; i++)
983 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
984 sz += sizeof(struct htx_blk) + 1;
985 return sz;
986}
987
Christopher Faulet30db3d72019-05-17 15:35:33 +0200988/* Estimate the size of the HTX request after the parsing. */
989static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
990{
991 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200992
993 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +0200994 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 +0200995 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200996 return sz;
997}
998
999/* Estimate the size of the HTX response after the parsing. */
1000static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
1001{
1002 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001003
1004 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +02001005 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 +02001006 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001007 return sz;
1008}
1009
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001010/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001011 * Add the EOM in the HTX message and switch the message to the DONE state. It
1012 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
1013 * functions is responsible to update the parser state <h1m>. It also add the
1014 * flag CS_FL_EOI on the CS.
1015 */
1016static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
1017{
Willy Tarreau62038152019-08-23 09:29:29 +02001018 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
1019 h1s->flags |= H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001020 return 0;
Willy Tarreau62038152019-08-23 09:29:29 +02001021 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001022
Willy Tarreau62038152019-08-23 09:29:29 +02001023 h1s->flags &= ~H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001024 h1m->state = H1_MSG_DONE;
1025 h1s->cs->flags |= CS_FL_EOI;
1026 return (sizeof(struct htx_blk) + 1);
1027}
1028
1029/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001030 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001031 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1032 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001033 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001034 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001035static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001036 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001037{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001038 struct htx_sl *sl;
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001039 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001040 union h1_sl h1sl;
1041 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001042 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +02001043 int ret = 0;
1044
Christopher Faulet30db3d72019-05-17 15:35:33 +02001045 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001046 goto end;
1047
Christopher Faulet129817b2018-09-20 16:14:40 +02001048 /* Realing input buffer if necessary */
1049 if (b_head(buf) + b_data(buf) > b_wrap(buf))
1050 b_slow_realign(buf, trash.area, 0);
1051
Christopher Faulet842f41f2019-09-25 09:10:46 +02001052 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001053 /* Try to match H2 preface before parsing the request headers. */
1054 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
1055 if (ret > 0)
1056 goto h2c_upgrade;
1057 }
1058
Christopher Faulet30db3d72019-05-17 15:35:33 +02001059 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001060 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +02001061 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001062 /* Incomplete or invalid message. If the input buffer only
1063 * contains headers and is full, which is detected by it being
1064 * full and the offset to be zero, it's an error because
1065 * headers are too large to be handled by the parser. */
1066 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001067 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001068 goto end;
1069 }
1070
Christopher Fauletb4bad502019-10-11 14:22:00 +02001071 if (h1m->err_pos >= 0) {
1072 /* Maybe we found an error during the parsing while we were
1073 * configured not to block on that, so we have to capture it
1074 * now.
1075 */
1076 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1077 }
1078
Christopher Faulet129817b2018-09-20 16:14:40 +02001079 /* messages headers fully parsed, do some checks to prepare the body
1080 * parsing.
1081 */
1082
Christopher Faulet9768c262018-10-22 09:34:31 +02001083 /* Save the request's method or the response's status, check if the body
1084 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001085 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001086 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001087
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001088 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001089 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001090
1091 if (h1s->meth == HTTP_METH_CONNECT) {
1092 /* Switch CONNECT requests to tunnel mode */
1093 h1_set_req_tunnel_mode(h1s);
1094 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001095
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001096 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1097 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001098 h1m->err_state = h1m->state;
1099 goto vsn_error;
1100 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001101 }
1102 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001103 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001104
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001105 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1106 h1s->status == 101) {
1107 /* Switch successfull replies to CONNECT requests and
1108 * protocol switching to tunnel mode. */
1109 h1_set_res_tunnel_mode(h1s);
1110 }
1111 else if ((h1s->meth == HTTP_METH_HEAD) ||
1112 (h1s->status >= 100 && h1s->status < 200) ||
1113 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001114 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001115 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1116 h1m->flags |= H1_MF_XFER_LEN;
1117 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001118 }
1119 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001120 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001121 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001122 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001123 else {
1124 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001125 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001126 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001127
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001128 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1129 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001130 h1m->err_state = h1m->state;
1131 goto vsn_error;
1132 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001133 }
1134
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001135 /* Set HTX start-line flags */
1136 if (h1m->flags & H1_MF_VER_11)
1137 flags |= HTX_SL_F_VER_11;
1138 if (h1m->flags & H1_MF_XFER_ENC)
1139 flags |= HTX_SL_F_XFER_ENC;
1140 if (h1m->flags & H1_MF_XFER_LEN) {
1141 flags |= HTX_SL_F_XFER_LEN;
1142 if (h1m->flags & H1_MF_CHNK)
1143 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001144 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001145 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001146 if (h1m->body_len == 0)
1147 flags |= HTX_SL_F_BODYLESS;
1148 }
1149 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001150 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001151 }
1152
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001153 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001154 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001155 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1156 if (htx_is_empty(htx))
1157 goto error;
1158 h1m_init_req(h1m);
1159 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1160 ret = 0;
1161 goto end;
1162 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001163
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001164 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1165 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001166 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001167 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001168
1169 /* Check if the uri contains an explicit scheme and if it is
1170 * "http" or "https". */
1171 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1172 sl->flags |= HTX_SL_F_HAS_SCHM;
1173 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1174 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1175 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001176 }
1177 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001178 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1179 if (htx_is_empty(htx))
1180 goto error;
1181 h1m_init_res(h1m);
1182 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1183 ret = 0;
1184 goto end;
1185 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001186
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001187 flags |= HTX_SL_F_IS_RESP;
1188 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1189 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001190 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001191 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001192 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001193
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001194 /* Set bytes used in the HTX mesage for the headers now */
1195 sl->hdrs_bytes = htx_used_space(htx) - used;
1196
Christopher Fauletb992af02019-03-28 15:42:24 +01001197 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001198
1199 /* If body length cannot be determined, set htx->extra to
1200 * ULLONG_MAX. This value is impossible in other cases.
1201 */
1202 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001203 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001204 end:
1205 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001206
1207 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001208 h1m->err_state = h1m->state;
1209 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001210 vsn_error:
1211 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001212 h1s->cs->flags |= CS_FL_EOI;
1213 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001214 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001215 ret = 0;
1216 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001217
1218 h2c_upgrade:
1219 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001220 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001221 htx->flags |= HTX_FL_UPGRADE;
1222 ret = 0;
1223 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001224}
1225
1226/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001227 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1228 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001229 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001230 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001231 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001232static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001233 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001234 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001235{
1236 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001237 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001238
Christopher Faulet129817b2018-09-20 16:14:40 +02001239 if (h1m->flags & H1_MF_XFER_LEN) {
1240 if (h1m->flags & H1_MF_CLEN) {
1241 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001242 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001243 if ((uint64_t)ret > h1m->curr_len)
1244 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001245 if (ret > b_contig_data(buf, *ofs))
1246 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001247
Christopher Faulet9768c262018-10-22 09:34:31 +02001248 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001249 /* very often with large files we'll face the following
1250 * situation :
1251 * - htx is empty and points to <htxbuf>
1252 * - ret == buf->data
1253 * - buf->head == sizeof(struct htx)
1254 * => we can swap the buffers and place an htx header into
1255 * the target buffer instead
1256 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001257 int32_t try = ret;
1258
Willy Tarreau78f548f2018-12-05 10:02:39 +01001259 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1260 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1261 void *raw_area = buf->area;
1262 void *htx_area = htxbuf->area;
1263 struct htx_blk *blk;
1264
1265 buf->area = htx_area;
1266 htxbuf->area = raw_area;
1267 htx = (struct htx *)htxbuf->area;
1268 htx->size = htxbuf->size - sizeof(*htx);
1269 htx_reset(htx);
1270 b_set_data(htxbuf, b_size(htxbuf));
1271
1272 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1273 blk->info += ret;
1274 /* nothing else to do, the old buffer now contains an
1275 * empty pre-initialized HTX header
1276 */
1277 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001278 else {
1279 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1280 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001281 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001282 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001283 *ofs += ret;
1284 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001285 if (ret < try)
1286 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001287 }
1288
1289 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001290 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001291 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001292 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001293 }
1294 else if (h1m->flags & H1_MF_CHNK) {
1295 new_chunk:
1296 /* te:chunked : parse chunks */
1297 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001298 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001299 if (ret <= 0)
1300 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001301 h1m->state = H1_MSG_CHUNK_SIZE;
1302
Christopher Fauletf2824e62018-10-01 12:12:37 +02001303 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001304 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001305 }
1306
1307 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1308 unsigned int chksz;
1309
Christopher Faulet30db3d72019-05-17 15:35:33 +02001310 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001311 if (ret <= 0)
1312 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001313 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001314
Christopher Faulet129817b2018-09-20 16:14:40 +02001315 h1m->curr_len = chksz;
1316 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001317 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001318 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001319
1320 if (!h1m->curr_len)
1321 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001322 }
1323
1324 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001325 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001326 if ((uint64_t)ret > h1m->curr_len)
1327 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001328 if (ret > b_contig_data(buf, *ofs))
1329 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001330
Christopher Faulet9768c262018-10-22 09:34:31 +02001331 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001332 int32_t try = ret;
1333
1334 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001335 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001336 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001337 *ofs += ret;
1338 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001339 if (ret < try)
1340 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001341 }
1342 if (!h1m->curr_len) {
1343 h1m->state = H1_MSG_CHUNK_CRLF;
1344 goto new_chunk;
1345 }
1346 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001347 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001348 }
1349 else {
1350 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1351 * is no body. Switch the message in DONE state
1352 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001353 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001354 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001355 }
1356 }
1357 else {
1358 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001359 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001360 if (ret > b_contig_data(buf, *ofs))
1361 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001362
Christopher Faulet9768c262018-10-22 09:34:31 +02001363 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001364 int32_t try = ret;
1365
1366 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001367
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001368 *ofs += ret;
1369 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001370 if (ret < try)
1371 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001372 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001373 }
1374
1375 end:
1376 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001377 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001378 h1s->cs->flags |= CS_FL_EOI;
1379 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001380 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001381 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001382 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001383 return 0;
1384 }
Willy Tarreau7e9dd732020-01-17 16:19:34 +01001385
Christopher Fauletafadc9a2020-07-03 14:51:15 +02001386 if (h1s->cs && !(h1m->flags & H1_MF_CHNK) &&
1387 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL)))
Willy Tarreau7e9dd732020-01-17 16:19:34 +01001388 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1389 else if (h1s->cs)
1390 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1391
Christopher Faulet9768c262018-10-22 09:34:31 +02001392 /* update htx->extra, only when the body length is known */
1393 if (h1m->flags & H1_MF_XFER_LEN)
1394 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001395 return total;
1396}
1397
1398/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001399 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1400 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1401 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1402 * responsible to update the parser state <h1m>.
1403 */
1404static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1405 struct buffer *buf, size_t *ofs, size_t max)
1406{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001407 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001408 struct h1m tlr_h1m;
1409 int ret = 0;
1410
1411 if (!max || !b_data(buf))
1412 goto end;
1413
1414 /* Realing input buffer if necessary */
1415 if (b_peek(buf, *ofs) > b_tail(buf))
1416 b_slow_realign(buf, trash.area, 0);
1417
1418 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1419 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1420 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1421 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001422 /* Incomplete or invalid trailers. If the input buffer only
1423 * contains trailers and is full, which is detected by it being
1424 * full and the offset to be zero, it's an error because
1425 * trailers are too large to be handled by the parser. */
1426 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001427 goto error;
1428 goto end;
1429 }
1430
1431 /* messages trailers fully parsed. */
1432 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1433 if (htx_is_empty(htx))
1434 goto error;
1435 ret = 0;
1436 goto end;
1437 }
1438
1439 if (!htx_add_all_trailers(htx, hdrs))
1440 goto error;
1441
1442 *ofs += ret;
1443 h1s->flags |= H1S_F_HAVE_I_TLR;
1444 end:
1445 return ret;
1446
1447 error:
1448 h1m->err_state = h1m->state;
1449 h1m->err_pos = h1m->next;
1450 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1451 h1s->cs->flags |= CS_FL_EOI;
1452 htx->flags |= HTX_FL_PARSING_ERROR;
1453 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1454 ret = 0;
1455 goto end;
1456}
1457
1458/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001459 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001460 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1461 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001462 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001463static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001464{
Christopher Faulet539e0292018-11-19 10:40:09 +01001465 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001466 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001467 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001468 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001469 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001470 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001471
Christopher Faulet539e0292018-11-19 10:40:09 +01001472 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001473
Christopher Fauletf2824e62018-10-01 12:12:37 +02001474 if (!conn_is_back(h1c->conn)) {
1475 h1m = &h1s->req;
1476 errflag = H1S_F_REQ_ERROR;
1477 }
1478 else {
1479 h1m = &h1s->res;
1480 errflag = H1S_F_RES_ERROR;
1481 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001482
Christopher Faulet74027762019-02-26 14:45:05 +01001483 data = htx->data;
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001484 if (h1s->flags & errflag)
1485 goto end;
1486
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001487 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001488 size_t used = htx_used_space(htx);
1489
Christopher Faulet129817b2018-09-20 16:14:40 +02001490 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001491 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001492 if (!ret)
1493 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001494 if ((h1m->flags & H1_MF_RESP) &&
1495 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1496 h1m_init_res(&h1s->res);
1497 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1498 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001499 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001500 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001501 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001502 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001503 if (!ret)
1504 break;
1505 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001506 else if (h1m->state == H1_MSG_TRAILERS) {
1507 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1508 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1509 if (!ret)
1510 break;
1511 }
Christopher Faulet1021e722019-09-03 21:55:14 +02001512 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001513 break;
1514 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001515 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletac4778c2019-11-15 11:14:23 +01001516 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1517 h1_set_req_tunnel_mode(h1s);
1518 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001519 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001520 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001521 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001522 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001523 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001524 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001525 if (!ret)
1526 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001527 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001528 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001529 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001530 break;
1531 }
1532
Christopher Faulet30db3d72019-05-17 15:35:33 +02001533 count -= htx_used_space(htx) - used;
Christopher Faulet98e2bc22019-09-03 16:26:15 +02001534 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001535
Christopher Faulet47365272018-10-31 17:40:50 +01001536 if (h1s->flags & errflag)
1537 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001538
Christopher Faulet539e0292018-11-19 10:40:09 +01001539 b_del(&h1c->ibuf, total);
1540
1541 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001542 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001543 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001544 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001545 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001546 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001547 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001548
Christopher Fauletcf56b992018-12-11 16:12:31 +01001549 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1550
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001551 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001552 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauleta882a462019-12-06 15:59:05 +01001553
1554 if ((h1s_data_pending(h1s) && !htx_is_empty(htx)) || (h1s->flags & H1S_F_APPEND_EOM))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001555 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001556
Willy Tarreau62038152019-08-23 09:29:29 +02001557 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1558 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001559 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001560 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001561 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001562 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001563
Christopher Faulet30db3d72019-05-17 15:35:33 +02001564 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001565
1566 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001567 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001568 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001569 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001570}
1571
Christopher Faulet129817b2018-09-20 16:14:40 +02001572/*
1573 * Process outgoing data. It parses data and transfer them from the channel buffer into
1574 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1575 * 0 if it couldn't proceed.
1576 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001577static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1578{
Christopher Faulet129817b2018-09-20 16:14:40 +02001579 struct h1s *h1s = h1c->h1s;
1580 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001581 struct htx *chn_htx;
1582 struct htx_blk *blk;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001583 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001584 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001585 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001586
Christopher Faulet47365272018-10-31 17:40:50 +01001587 if (!count)
1588 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001589
Christopher Faulet94b2c762019-05-24 15:28:57 +02001590 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001591 if (htx_is_empty(chn_htx))
1592 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001593
Christopher Faulet51dbc942018-09-13 09:05:15 +02001594 if (!h1_get_buf(h1c, &h1c->obuf)) {
1595 h1c->flags |= H1C_F_OUT_ALLOC;
1596 goto end;
1597 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001598
Christopher Fauletf2824e62018-10-01 12:12:37 +02001599 if (!conn_is_back(h1c->conn)) {
1600 h1m = &h1s->res;
1601 errflag = H1S_F_RES_ERROR;
1602 }
1603 else {
1604 h1m = &h1s->req;
1605 errflag = H1S_F_REQ_ERROR;
1606 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001607
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001608 if (h1s->flags & errflag)
1609 goto end;
1610
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001611 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001612 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001613
Willy Tarreau3815b222018-12-11 19:50:43 +01001614 /* Perform some optimizations to reduce the number of buffer copies.
1615 * First, if the mux's buffer is empty and the htx area contains
1616 * exactly one data block of the same size as the requested count,
1617 * then it's possible to simply swap the caller's buffer with the
1618 * mux's output buffer and adjust offsets and length to match the
1619 * entire DATA HTX block in the middle. In this case we perform a
1620 * true zero-copy operation from end-to-end. This is the situation
1621 * that happens all the time with large files. Second, if this is not
1622 * possible, but the mux's output buffer is empty, we still have an
1623 * opportunity to avoid the copy to the intermediary buffer, by making
1624 * the intermediary buffer's area point to the output buffer's area.
1625 * In this case we want to skip the HTX header to make sure that copies
1626 * remain aligned and that this operation remains possible all the
1627 * time. This goes for headers, data blocks and any data extracted from
1628 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001629 */
1630 if (!b_data(&h1c->obuf)) {
Willy Tarreau3815b222018-12-11 19:50:43 +01001631 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001632 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001633 htx_get_blk_value(chn_htx, blk).len == count) {
1634 void *old_area = h1c->obuf.area;
1635
1636 h1c->obuf.area = buf->area;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001637 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001638 h1c->obuf.data = count;
1639
1640 buf->area = old_area;
1641 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001642
1643 /* The message is chunked. We need to emit the chunk
1644 * size. We have at least the size of the struct htx to
1645 * write the chunk envelope. It should be enough.
1646 */
1647 if (h1m->flags & H1_MF_CHNK) {
1648 h1_emit_chunk_size(&h1c->obuf, count);
1649 h1_emit_chunk_crlf(&h1c->obuf);
1650 }
1651
Willy Tarreau3815b222018-12-11 19:50:43 +01001652 total += count;
1653 goto out;
1654 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001655 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001656 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001657 else
1658 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001659
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001660 tmp.data = 0;
1661 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001662 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001663 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001664 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001665 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001666 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001667 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001668
Christopher Fauletb2e84162018-12-06 11:39:49 +01001669 vlen = sz;
1670 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001671 if (type != HTX_BLK_DATA)
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001672 goto full;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001673 vlen = count;
1674 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001675
Christopher Faulet94b2c762019-05-24 15:28:57 +02001676 if (type == HTX_BLK_UNUSED)
1677 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001678
Christopher Faulet94b2c762019-05-24 15:28:57 +02001679 switch (h1m->state) {
1680 case H1_MSG_RQBEFORE:
1681 if (type != HTX_BLK_REQ_SL)
1682 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001683 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001684 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001685 h1_parse_req_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001686 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001687 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001688 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001689 if (sl->flags & HTX_SL_F_BODYLESS)
1690 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001691 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001692 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001693
Christopher Faulet94b2c762019-05-24 15:28:57 +02001694 case H1_MSG_RPBEFORE:
1695 if (type != HTX_BLK_RES_SL)
1696 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001697 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001698 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001699 h1_parse_res_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001700 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001701 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001702 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001703 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001704 if (sl->info.res.status < 200 &&
1705 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001706 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001707 h1m->state = H1_MSG_HDR_FIRST;
1708 break;
1709
Christopher Faulet94b2c762019-05-24 15:28:57 +02001710 case H1_MSG_HDR_FIRST:
1711 case H1_MSG_HDR_NAME:
1712 case H1_MSG_HDR_L2_LWS:
1713 if (type == HTX_BLK_EOH)
1714 goto last_lf;
1715 if (type != HTX_BLK_HDR)
1716 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001717 h1m->state = H1_MSG_HDR_NAME;
1718 n = htx_get_blk_name(chn_htx, blk);
1719 v = htx_get_blk_value(chn_htx, blk);
1720
1721 if (isteqi(n, ist("transfer-encoding")))
1722 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001723 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001724 /* Only skip C-L header with invalid value. */
1725 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001726 goto skip_hdr;
1727 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001728 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001729 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001730 if (!v.len)
1731 goto skip_hdr;
1732 }
1733
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001734 /* Try to adjust the case of the header name */
1735 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1736 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001737 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001738 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001739 skip_hdr:
1740 h1m->state = H1_MSG_HDR_L2_LWS;
1741 break;
1742
Christopher Faulet94b2c762019-05-24 15:28:57 +02001743 case H1_MSG_LAST_LF:
1744 if (type != HTX_BLK_EOH)
1745 goto error;
1746 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001747 h1m->state = H1_MSG_LAST_LF;
1748 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001749 /* There is no "Connection:" header and
1750 * it the conn_mode must be
1751 * processed. So do it */
Christopher Faulet661bfc32019-06-17 14:07:46 +02001752 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001753 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001754 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001755 if (v.len) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001756 /* Try to adjust the case of the header name */
1757 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1758 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001759 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001760 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001761 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001762 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001763 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001764
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001765 if ((h1s->meth != HTTP_METH_CONNECT &&
1766 (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 +01001767 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1768 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1769 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1770 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1771 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001772 /* chunking needed but header not seen */
Christopher Faulete5addb02019-10-04 10:23:51 +02001773 n = ist("transfer-encoding");
1774 v = ist("chunked");
1775 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1776 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
1777 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001778 goto full;
Willy Tarreau4710d202019-01-03 17:39:54 +01001779 h1m->flags |= H1_MF_CHNK;
1780 }
1781
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001782 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001783 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001784
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001785 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1786 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1787 h1_set_req_tunnel_mode(h1s);
1788 }
Christopher Fauletac4778c2019-11-15 11:14:23 +01001789 else if ((h1m->flags & H1_MF_RESP) &&
1790 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001791 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletac4778c2019-11-15 11:14:23 +01001792 * to the client. Switch the response to tunnel mode.
1793 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001794 h1_set_res_tunnel_mode(h1s);
1795 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001796 else if ((h1m->flags & H1_MF_RESP) &&
1797 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1798 h1m_init_res(&h1s->res);
1799 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001800 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001801 }
Christopher Faulet33d58b52019-07-01 16:17:30 +02001802 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1803 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001804 else
1805 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001806 break;
1807
Christopher Faulet94b2c762019-05-24 15:28:57 +02001808 case H1_MSG_DATA:
Christopher Fauletaaa25062019-07-04 17:12:12 +02001809 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001810 if (type == HTX_BLK_EOM) {
1811 /* Chunked message without explicit trailers */
1812 if (h1m->flags & H1_MF_CHNK) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001813 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001814 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001815 }
1816 goto done;
1817 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001818 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet52131682019-06-27 17:40:14 +02001819 /* If the message is not chunked, never
1820 * add the last chunk. */
1821 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001822 goto full;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001823 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001824 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001825 else if (type != HTX_BLK_DATA)
1826 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001827 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001828 v.len = vlen;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001829 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001830 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001831 break;
1832
Christopher Faulet94b2c762019-05-24 15:28:57 +02001833 case H1_MSG_TRAILERS:
1834 if (type == HTX_BLK_EOM)
1835 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001836 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001837 goto error;
1838 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001839 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet52131682019-06-27 17:40:14 +02001840 /* If the message is not chunked, ignore
1841 * trailers. It may happen with H2 messages. */
1842 if (!(h1m->flags & H1_MF_CHNK))
1843 break;
1844
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001845 if (type == HTX_BLK_EOT) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001846 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001847 goto full;
Christopher Faulet32188212018-11-20 18:21:43 +01001848 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001849 else { // HTX_BLK_TLR
1850 n = htx_get_blk_name(chn_htx, blk);
1851 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001852
1853 /* Try to adjust the case of the header name */
1854 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1855 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001856 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001857 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001858 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001859 break;
1860
Christopher Faulet94b2c762019-05-24 15:28:57 +02001861 case H1_MSG_DONE:
1862 if (type != HTX_BLK_EOM)
1863 goto error;
1864 done:
1865 h1m->state = H1_MSG_DONE;
Christopher Fauletac4778c2019-11-15 11:14:23 +01001866 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1867 h1_set_req_tunnel_mode(h1s);
1868 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001869 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1870 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1871 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001872 break;
1873
Christopher Faulet9768c262018-10-22 09:34:31 +02001874 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001875 error:
Christopher Fauletc431df82019-06-26 15:16:28 +02001876 /* Unexpected error during output processing */
1877 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001878 h1s->flags |= errflag;
Christopher Fauletc431df82019-06-26 15:16:28 +02001879 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001880 break;
1881 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001882
1883 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001884 total += vlen;
1885 count -= vlen;
1886 if (sz == vlen)
1887 blk = htx_remove_blk(chn_htx, blk);
1888 else {
1889 htx_cut_data_blk(chn_htx, blk, vlen);
1890 break;
1891 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001892 }
1893
Christopher Faulet9768c262018-10-22 09:34:31 +02001894 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001895 /* when the output buffer is empty, tmp shares the same area so that we
1896 * only have to update pointers and lengths.
1897 */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001898 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1899 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001900 else
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001901 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001902
Willy Tarreau3815b222018-12-11 19:50:43 +01001903 htx_to_buf(chn_htx, buf);
1904 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001905 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001906 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001907 end:
1908 return total;
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001909
1910 full:
1911 h1c->flags |= H1C_F_OUT_FULL;
1912 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001913}
1914
Christopher Faulet51dbc942018-09-13 09:05:15 +02001915/*********************************************************/
1916/* functions below are I/O callbacks from the connection */
1917/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001918static void h1_wake_stream_for_recv(struct h1s *h1s)
1919{
1920 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001921 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001922 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001923 h1s->recv_wait = NULL;
1924 }
1925}
1926static void h1_wake_stream_for_send(struct h1s *h1s)
1927{
1928 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001929 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001930 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001931 h1s->send_wait = NULL;
1932 }
1933}
1934
Christopher Faulet51dbc942018-09-13 09:05:15 +02001935/*
1936 * Attempt to read data, and subscribe if none available
1937 */
1938static int h1_recv(struct h1c *h1c)
1939{
1940 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001941 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001942 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001943 int rcvd = 0;
1944
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001945 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001946 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001947
Olivier Houchard5b06cb32019-08-13 15:21:59 +02001948 if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN)
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001949 return 0;
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001950
Olivier Houchard75159a92018-12-03 18:46:09 +01001951 if (!h1_recv_allowed(h1c)) {
1952 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001953 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001954 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001955
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001956 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1957 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001958 goto end;
1959 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001960
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001961 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001962 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001963 h1_wake_stream_for_recv(h1s);
1964 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001965 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001966 }
1967
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001968 /*
1969 * If we only have a small amount of data, realign it,
1970 * it's probably cheaper than doing 2 recv() calls.
1971 */
1972 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1973 b_slow_realign(&h1c->ibuf, trash.area, 0);
1974
Willy Tarreau45f2b892018-12-05 07:59:27 +01001975 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001976 if (max) {
1977 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001978
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001979 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001980 if (!b_data(&h1c->ibuf)) {
1981 /* try to pre-align the buffer like the rxbufs will be
1982 * to optimize memory copies.
1983 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001984 h1c->ibuf.head = sizeof(struct htx);
1985 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001986 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001987 }
Christopher Faulet47365272018-10-31 17:40:50 +01001988 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001989 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001990 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001991 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001992 if (h1s->csinfo.t_idle == -1)
1993 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1994 }
Christopher Faulet47365272018-10-31 17:40:50 +01001995 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001996
Christopher Fauletcf56b992018-12-11 16:12:31 +01001997 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001998 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001999 goto end;
2000 }
2001
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002002 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002003
Christopher Faulet9768c262018-10-22 09:34:31 +02002004 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002005 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2006 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002007
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002008 if (conn_xprt_read0_pending(conn) && h1s) {
2009 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002010 rcvd = 1;
2011 }
2012
Christopher Faulet51dbc942018-09-13 09:05:15 +02002013 if (!b_data(&h1c->ibuf))
2014 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01002015 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002016 h1c->flags |= H1C_F_IN_FULL;
2017 return rcvd;
2018}
2019
2020
2021/*
2022 * Try to send data if possible
2023 */
2024static int h1_send(struct h1c *h1c)
2025{
2026 struct connection *conn = h1c->conn;
2027 unsigned int flags = 0;
2028 size_t ret;
2029 int sent = 0;
2030
2031 if (conn->flags & CO_FL_ERROR)
2032 return 0;
2033
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002034 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002035 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002036 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002037 return 0;
2038 }
2039
Christopher Faulet51dbc942018-09-13 09:05:15 +02002040 if (!b_data(&h1c->obuf))
2041 goto end;
2042
2043 if (h1c->flags & H1C_F_OUT_FULL)
2044 flags |= CO_SFL_MSG_MORE;
2045
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002046 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002047 if (ret > 0) {
2048 h1c->flags &= ~H1C_F_OUT_FULL;
2049 b_del(&h1c->obuf, ret);
2050 sent = 1;
2051 }
2052
Christopher Faulet145aa472018-12-06 10:56:20 +01002053 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
2054 /* error or output closed, nothing to send, clear the buffer to release it */
2055 b_reset(&h1c->obuf);
2056 }
2057
Christopher Faulet51dbc942018-09-13 09:05:15 +02002058 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002059 if (!(h1c->flags & H1C_F_OUT_FULL))
2060 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002061
Christopher Faulet51dbc942018-09-13 09:05:15 +02002062 /* We're done, no more to send */
2063 if (!b_data(&h1c->obuf)) {
2064 h1_release_buf(h1c, &h1c->obuf);
2065 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01002066 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002067 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002068 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002069 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002070
2071 return sent;
2072}
2073
Christopher Faulet51dbc942018-09-13 09:05:15 +02002074
2075/* callback called on any event by the connection handler.
2076 * It applies changes and returns zero, or < 0 if it wants immediate
2077 * destruction of the connection.
2078 */
2079static int h1_process(struct h1c * h1c)
2080{
2081 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002082 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002083
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002084 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002085 return -1;
2086
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002087 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Olivier Houchard690e0f02019-06-11 16:37:24 +02002088 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) ||
Olivier Houchard60630032019-06-13 17:37:00 +02002089 (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE)))
Christopher Faulet539e0292018-11-19 10:40:09 +01002090 goto end;
2091 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01002092 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002093 }
2094
Christopher Fauletfeb11742018-11-29 15:12:34 +01002095 if (!h1s) {
Christopher Faulet470438c2019-07-11 15:40:25 +02002096 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Faulete31a0f12019-12-05 10:23:37 +01002097 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002098 goto release;
Christopher Faulete31a0f12019-12-05 10:23:37 +01002099 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002100 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002101 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002102 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002103 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002104 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002105 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002106 }
2107
Christopher Fauletfeb11742018-11-29 15:12:34 +01002108 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2109 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2110
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002111 if (conn_xprt_read0_pending(conn))
2112 h1s->flags |= H1S_F_REOS;
2113
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002114 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002115 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002116 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002117 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002118 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002119 h1s->cs->data_cb->wake(h1s->cs);
2120 }
Christopher Faulet47365272018-10-31 17:40:50 +01002121 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002122 if (h1c->task) {
2123 h1c->task->expire = TICK_ETERNITY;
2124 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002125 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002126 ? h1c->shut_timeout
2127 : h1c->timeout));
2128 task_queue(h1c->task);
2129 }
2130 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002131 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002132
2133 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002134 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002135 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002136}
2137
2138static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2139{
2140 struct h1c *h1c = ctx;
2141 int ret = 0;
2142
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002143 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002144 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002145 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002146 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002147 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002148 h1_process(h1c);
2149 return NULL;
2150}
2151
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002152static void h1_reset(struct connection *conn)
2153{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002154 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002155
2156 /* Reset the flags, and let the mux know we're waiting for a connection */
2157 h1c->flags = H1C_F_CS_WAIT_CONN;
2158}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002159
2160static int h1_wake(struct connection *conn)
2161{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002162 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002163 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002164
Christopher Faulet539e0292018-11-19 10:40:09 +01002165 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002166 ret = h1_process(h1c);
2167 if (ret == 0) {
2168 struct h1s *h1s = h1c->h1s;
2169
2170 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2171 ret = h1s->cs->data_cb->wake(h1s->cs);
2172 }
2173 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002174}
2175
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002176/* Connection timeout management. The principle is that if there's no receipt
2177 * nor sending for a certain amount of time, the connection is closed.
2178 */
2179static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2180{
2181 struct h1c *h1c = context;
2182 int expired = tick_is_expired(t->expire, now_ms);
2183
2184 if (!expired && h1c)
2185 return t;
2186
Olivier Houchard3f795f72019-04-17 22:51:06 +02002187 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002188
2189 if (!h1c) {
2190 /* resources were already deleted */
2191 return NULL;
2192 }
2193
2194 h1c->task = NULL;
2195 /* If a stream is still attached to the mux, just set an error and wait
2196 * for the stream's timeout. Otherwise, release the mux. This is only ok
2197 * because same timeouts are used.
2198 */
2199 if (h1c->h1s && h1c->h1s->cs)
2200 h1c->flags |= H1C_F_CS_ERROR;
2201 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002202 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002203 return NULL;
2204}
2205
Christopher Faulet51dbc942018-09-13 09:05:15 +02002206/*******************************************/
2207/* functions below are used by the streams */
2208/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002209
Christopher Faulet51dbc942018-09-13 09:05:15 +02002210/*
2211 * Attach a new stream to a connection
2212 * (Used for outgoing connections)
2213 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002214static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002215{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002216 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002217 struct conn_stream *cs = NULL;
2218 struct h1s *h1s;
2219
2220 if (h1c->flags & H1C_F_CS_ERROR)
2221 goto end;
2222
2223 cs = cs_new(h1c->conn);
2224 if (!cs)
2225 goto end;
2226
Olivier Houchardf502aca2018-12-14 19:42:40 +01002227 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002228 if (h1s == NULL)
2229 goto end;
2230
2231 return cs;
2232 end:
2233 cs_free(cs);
2234 return NULL;
2235}
2236
2237/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2238 * this mux, it's easy as we can only store a single conn_stream.
2239 */
2240static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2241{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002242 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002243 struct h1s *h1s = h1c->h1s;
2244
2245 if (h1s)
2246 return h1s->cs;
2247
2248 return NULL;
2249}
2250
Christopher Faulet73c12072019-04-08 11:23:22 +02002251static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002252{
Christopher Faulet73c12072019-04-08 11:23:22 +02002253 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002254
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002255 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002256 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002257}
2258
2259/*
2260 * Detach the stream from the connection and possibly release the connection.
2261 */
2262static void h1_detach(struct conn_stream *cs)
2263{
2264 struct h1s *h1s = cs->ctx;
2265 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002266 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002267 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002268
2269 cs->ctx = NULL;
2270 if (!h1s)
2271 return;
2272
Olivier Houchardf502aca2018-12-14 19:42:40 +01002273 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002274 h1c = h1s->h1c;
2275 h1s->cs = NULL;
2276
Olivier Houchard8a786902018-12-15 16:05:40 +01002277 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2278 h1s_destroy(h1s);
2279
Christopher Faulete31a0f12019-12-05 10:23:37 +01002280 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002281 /* If there are any excess server data in the input buffer,
2282 * release it and close the connection ASAP (some data may
2283 * remain in the output buffer). This happens if a server sends
2284 * invalid responses. So in such case, we don't want to reuse
2285 * the connection
Christopher Faulet39e679b2019-07-19 11:34:08 +02002286 */
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002287 if (b_data(&h1c->ibuf)) {
2288 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulete31a0f12019-12-05 10:23:37 +01002289 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002290 goto release;
2291 }
Christopher Faulet39e679b2019-07-19 11:34:08 +02002292
Christopher Faulet9400a392018-11-23 23:10:39 +01002293 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002294 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002295 h1c->conn->flags |= CO_FL_PRIVATE;
2296
Olivier Houchard44d59142018-12-13 18:46:22 +01002297 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002298 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002299 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2300 h1c->conn->owner = NULL;
2301 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2302 /* The server doesn't want it, let's kill the connection right away */
Olivier Houchard109ba132020-02-14 13:23:45 +01002303 h1c->conn->mux->destroy(h1c);
Olivier Houchard351411f2018-12-27 17:20:54 +01002304 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002305 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002306 return;
Olivier Houchard351411f2018-12-27 17:20:54 +01002307 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002308 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002309 if (h1c->conn->owner == sess) {
2310 int ret = session_check_idle_conn(sess, h1c->conn);
2311 if (ret == -1)
2312 /* The connection got destroyed, let's leave */
2313 return;
2314 else if (ret == 1) {
2315 /* The connection was added to the server list,
2316 * wake the task so we can subscribe to events
2317 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002318 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002319 return;
2320 }
2321 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002322 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002323 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002324 struct server *srv = objt_server(h1c->conn->target);
2325
2326 if (srv) {
2327 if (h1c->conn->flags & CO_FL_PRIVATE)
2328 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002329 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002330 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2331 else
2332 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2333 }
2334 }
2335 }
2336
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002337 release:
Christopher Faulet9fa93f62019-06-28 17:41:42 +02002338 /* 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 +02002339 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2340 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2341 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2342 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002343 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002344 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002345 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002346 if (h1c->task) {
2347 h1c->task->expire = TICK_ETERNITY;
2348 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002349 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002350 ? h1c->shut_timeout
2351 : h1c->timeout));
2352 task_queue(h1c->task);
2353 }
2354 }
2355 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002356}
2357
2358
2359static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2360{
2361 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002362 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002363
2364 if (!h1s)
2365 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002366 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002367
Christopher Faulet7f366362019-04-08 10:51:20 +02002368 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2369 goto do_shutr;
2370
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002371 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002372 return;
2373
Christopher Faulet7f366362019-04-08 10:51:20 +02002374 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002375 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2376 if (cs->flags & CS_FL_SHR)
2377 return;
2378 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002379 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Fauletf36e72b2019-12-05 11:18:31 +01002380 (mode == CS_SHR_DRAIN));
Christopher Faulet51dbc942018-09-13 09:05:15 +02002381}
2382
2383static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2384{
2385 struct h1s *h1s = cs->ctx;
2386 struct h1c *h1c;
2387
2388 if (!h1s)
2389 return;
2390 h1c = h1s->h1c;
2391
Christopher Faulet7f366362019-04-08 10:51:20 +02002392 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2393 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002394
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002395 if ((h1c->flags & H1C_F_UPG_H2C) ||
2396 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002397 return;
2398
Christopher Faulet7f366362019-04-08 10:51:20 +02002399 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002400 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2401 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2402 return;
2403
Christopher Faulet666a0c42019-01-08 11:12:04 +01002404 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002405}
2406
Christopher Faulet666a0c42019-01-08 11:12:04 +01002407static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002408{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002409 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002410
Christopher Faulet666a0c42019-01-08 11:12:04 +01002411 conn_xprt_shutw(conn);
2412 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Fauletf36e72b2019-12-05 11:18:31 +01002413 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002414}
2415
2416/* Called from the upper layer, to unsubscribe to events */
2417static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2418{
2419 struct wait_event *sw;
2420 struct h1s *h1s = cs->ctx;
2421
2422 if (!h1s)
2423 return 0;
2424
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002425 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002426 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002427 BUG_ON(h1s->recv_wait != sw);
2428 sw->events &= ~SUB_RETRY_RECV;
2429 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002430 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002431 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002432 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002433 BUG_ON(h1s->send_wait != sw);
2434 sw->events &= ~SUB_RETRY_SEND;
2435 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002436 }
2437 return 0;
2438}
2439
2440/* Called from the upper layer, to subscribe to events, such as being able to send */
2441static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2442{
2443 struct wait_event *sw;
2444 struct h1s *h1s = cs->ctx;
2445
2446 if (!h1s)
2447 return -1;
2448
2449 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002450 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002451 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002452 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2453 sw->events |= SUB_RETRY_RECV;
2454 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002455 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002456 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002457 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002458 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2459 sw->events |= SUB_RETRY_SEND;
2460 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002461 return 0;
2462 default:
2463 break;
2464 }
2465 return -1;
2466}
2467
2468/* Called from the upper layer, to receive data */
2469static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2470{
2471 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002472 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002473 size_t ret = 0;
2474
Christopher Faulet539e0292018-11-19 10:40:09 +01002475 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002476 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002477
Christopher Faulete18777b2019-04-16 16:46:36 +02002478 if (flags & CO_RFL_BUF_FLUSH) {
2479 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2480
Christopher Fauletafadc9a2020-07-03 14:51:15 +02002481 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
Christopher Faulete18777b2019-04-16 16:46:36 +02002482 h1s->flags |= H1S_F_BUF_FLUSH;
2483 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002484 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
Christopher Faulet0528ae22020-07-03 15:12:00 +02002485 if (ret)
2486 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002487 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet39bb2272020-07-03 15:38:11 +02002488 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002489 }
2490 return ret;
2491}
2492
2493
2494/* Called from the upper layer, to send data */
2495static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2496{
2497 struct h1s *h1s = cs->ctx;
2498 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002499 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002500
2501 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002502 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002503
2504 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002505 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2506 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002507
Christopher Fauletc31872f2019-06-04 22:09:36 +02002508 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002509 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002510
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002511 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2512 ret = h1_process_output(h1c, buf, count);
2513 if (!ret)
2514 break;
2515 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002516 count -= ret;
Olivier Houcharda1012862020-01-15 19:13:32 +01002517 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002518 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002519 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002520
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002521 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002522}
2523
Willy Tarreaue5733232019-05-22 19:24:06 +02002524#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002525/* Send and get, using splicing */
2526static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2527{
2528 struct h1s *h1s = cs->ctx;
2529 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2530 int ret = 0;
2531
Christopher Faulet2c411be2019-11-05 16:24:27 +01002532 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002533 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002534 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2535 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002536 goto end;
2537 }
2538
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002539 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002540 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002541 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002542 }
2543
2544 h1s->flags &= ~H1S_F_BUF_FLUSH;
2545 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet8e8168a2020-07-03 15:02:25 +02002546
2547 if (!h1_recv_allowed(h1s->h1c))
2548 goto end;
2549
Christopher Faulet1be55f92018-10-02 15:59:23 +02002550 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2551 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002552 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002553 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002554 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002555 if (!h1m->curr_len)
2556 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2557 }
2558
Christopher Faulet1be55f92018-10-02 15:59:23 +02002559 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002560 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002561 h1s->flags |= H1S_F_REOS;
Christopher Fauletac4778c2019-11-15 11:14:23 +01002562 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet038ad812019-04-17 11:03:22 +02002563 }
Willy Tarreau7e9dd732020-01-17 16:19:34 +01002564
Christopher Faulete0ca6ad2020-07-07 10:56:40 +02002565 if ((h1s->flags & H1S_F_REOS) ||
2566 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Fauletafadc9a2020-07-03 14:51:15 +02002567 (h1m->state == H1_MSG_DATA && !h1m->curr_len))
Willy Tarreau7e9dd732020-01-17 16:19:34 +01002568 cs->flags &= ~CS_FL_MAY_SPLICE;
2569
Christopher Faulet1be55f92018-10-02 15:59:23 +02002570 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002571}
2572
2573static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2574{
2575 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002576 int ret = 0;
2577
2578 if (b_data(&h1s->h1c->obuf))
2579 goto end;
2580
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002581 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002582 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002583 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002584 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002585 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002586 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002587 return ret;
2588}
2589#endif
2590
Olivier Houchard198d1292019-10-25 16:19:26 +02002591static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2592{
2593 int ret = 0;
2594 switch (mux_ctl) {
2595 case MUX_STATUS:
2596 if (conn->flags & CO_FL_CONNECTED)
2597 ret |= MUX_STATUS_READY;
2598 return ret;
2599 default:
2600 return -1;
2601 }
2602}
2603
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002604/* for debugging with CLI's "show fd" command */
2605static void h1_show_fd(struct buffer *msg, struct connection *conn)
2606{
2607 struct h1c *h1c = conn->ctx;
2608 struct h1s *h1s = h1c->h1s;
2609
Christopher Fauletf376a312019-01-04 15:16:06 +01002610 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2611 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002612 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2613 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2614 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2615 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2616
2617 if (h1s) {
2618 char *method;
2619
2620 if (h1s->meth < HTTP_METH_OTHER)
2621 method = http_known_methods[h1s->meth].ptr;
2622 else
2623 method = "UNKNOWN";
2624 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2625 " .meth=%s status=%d",
2626 h1s, h1s->flags,
2627 h1m_state_str(h1s->req.state),
2628 h1m_state_str(h1s->res.state), method, h1s->status);
2629 if (h1s->cs)
2630 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2631 h1s->cs->flags, h1s->cs->data);
2632 }
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002633}
2634
2635
2636/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2637static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2638{
2639 struct h1_hdr_entry *entry;
2640
2641 /* Be sure there is a non-empty <to> */
2642 if (!strlen(to)) {
2643 memprintf(err, "expect <to>");
2644 return -1;
2645 }
2646
2647 /* Be sure only the case differs between <from> and <to> */
2648 if (strcasecmp(from, to)) {
2649 memprintf(err, "<from> and <to> must not differ execpt the case");
2650 return -1;
2651 }
2652
2653 /* Be sure <from> does not already existsin the tree */
2654 if (ebis_lookup(&hdrs_map.map, from)) {
2655 memprintf(err, "duplicate entry '%s'", from);
2656 return -1;
2657 }
2658
2659 /* Create the entry and insert it in the tree */
2660 entry = malloc(sizeof(*entry));
2661 if (!entry) {
2662 memprintf(err, "out of memory");
2663 return -1;
2664 }
2665
2666 entry->node.key = strdup(from);
2667 entry->name.ptr = strdup(to);
2668 entry->name.len = strlen(to);
2669 if (!entry->node.key || !entry->name.ptr) {
2670 free(entry->node.key);
2671 free(entry->name.ptr);
2672 free(entry);
2673 memprintf(err, "out of memory");
2674 return -1;
2675 }
2676 ebis_insert(&hdrs_map.map, &entry->node);
2677 return 0;
2678}
2679
2680static void h1_hdeaders_case_adjust_deinit()
2681{
2682 struct ebpt_node *node, *next;
2683 struct h1_hdr_entry *entry;
2684
2685 node = ebpt_first(&hdrs_map.map);
2686 while (node) {
2687 next = ebpt_next(node);
2688 ebpt_delete(node);
2689 entry = container_of(node, struct h1_hdr_entry, node);
2690 free(entry->node.key);
2691 free(entry->name.ptr);
2692 free(entry);
2693 node = next;
2694 }
2695 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002696}
2697
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002698static int cfg_h1_headers_case_adjust_postparser()
2699{
2700 FILE *file = NULL;
2701 char *c, *key_beg, *key_end, *value_beg, *value_end;
2702 char *err;
2703 int rc, line = 0, err_code = 0;
2704
2705 if (!hdrs_map.name)
2706 goto end;
2707
2708 file = fopen(hdrs_map.name, "r");
2709 if (!file) {
2710 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2711 hdrs_map.name);
2712 err_code |= ERR_ALERT | ERR_FATAL;
2713 goto end;
2714 }
2715
2716 /* now parse all lines. The file may contain only two header name per
2717 * line, separated by spaces. All heading and trailing spaces will be
2718 * ignored. Lines starting with a # are ignored.
2719 */
2720 while (fgets(trash.area, trash.size, file) != NULL) {
2721 line++;
2722 c = trash.area;
2723
2724 /* strip leading spaces and tabs */
2725 while (*c == ' ' || *c == '\t')
2726 c++;
2727
2728 /* ignore emptu lines, or lines beginning with a dash */
2729 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2730 continue;
2731
2732 /* look for the end of the key */
2733 key_beg = c;
2734 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2735 c++;
2736 key_end = c;
2737
2738 /* strip middle spaces and tabs */
2739 while (*c == ' ' || *c == '\t')
2740 c++;
2741
2742 /* look for the end of the value, it is the end of the line */
2743 value_beg = c;
2744 while (*c && *c != '\n' && *c != '\r')
2745 c++;
2746 value_end = c;
2747
2748 /* trim possibly trailing spaces and tabs */
2749 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2750 value_end--;
2751
2752 /* set final \0 and check entries */
2753 *key_end = '\0';
2754 *value_end = '\0';
2755
2756 err = NULL;
2757 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2758 if (rc < 0) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002759 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2760 hdrs_map.name, err, line);
2761 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Faulet27f20172019-07-30 16:51:42 +02002762 free(err);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002763 goto end;
2764 }
2765 if (rc > 0) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002766 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2767 hdrs_map.name, err, line);
2768 err_code |= ERR_WARN;
Christopher Faulet27f20172019-07-30 16:51:42 +02002769 free(err);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002770 }
2771 }
2772
2773 end:
2774 if (file)
2775 fclose(file);
2776 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2777 return err_code;
2778}
2779
2780
2781/* config parser for global "h1-outgoing-header-case-adjust" */
2782static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2783 struct proxy *defpx, const char *file, int line,
2784 char **err)
2785{
2786 if (too_many_args(2, args, err, NULL))
2787 return -1;
2788 if (!*(args[1]) || !*(args[2])) {
2789 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2790 return -1;
2791 }
2792 return add_hdr_case_adjust(args[1], args[2], err);
2793}
2794
2795/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2796static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2797 struct proxy *defpx, const char *file, int line,
2798 char **err)
2799{
2800 if (too_many_args(1, args, err, NULL))
2801 return -1;
2802 if (!*(args[1])) {
2803 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2804 return -1;
2805 }
2806 free(hdrs_map.name);
2807 hdrs_map.name = strdup(args[1]);
2808 return 0;
2809}
2810
2811
2812/* config keyword parsers */
2813static struct cfg_kw_list cfg_kws = {{ }, {
2814 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2815 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2816 { 0, NULL, NULL },
2817 }
2818};
2819
2820INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2821REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2822
2823
Christopher Faulet51dbc942018-09-13 09:05:15 +02002824/****************************************/
2825/* MUX initialization and instanciation */
2826/****************************************/
2827
2828/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002829static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002830 .init = h1_init,
2831 .wake = h1_wake,
2832 .attach = h1_attach,
2833 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002834 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002835 .detach = h1_detach,
2836 .destroy = h1_destroy,
2837 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002838 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002839 .rcv_buf = h1_rcv_buf,
2840 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002841#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002842 .rcv_pipe = h1_rcv_pipe,
2843 .snd_pipe = h1_snd_pipe,
2844#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002845 .subscribe = h1_subscribe,
2846 .unsubscribe = h1_unsubscribe,
2847 .shutr = h1_shutr,
2848 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002849 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002850 .reset = h1_reset,
Olivier Houchard198d1292019-10-25 16:19:26 +02002851 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002852 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002853 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002854};
2855
2856
2857/* this mux registers default HTX proto */
2858static struct mux_proto_list mux_proto_htx =
2859{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2860
Willy Tarreau0108d902018-11-25 19:14:37 +01002861INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2862
Christopher Faulet51dbc942018-09-13 09:05:15 +02002863/*
2864 * Local variables:
2865 * c-indent-level: 8
2866 * c-basic-offset: 8
2867 * End:
2868 */