blob: 5ab1feac4a6c1ed3a3f48590bc22dd6c22bbb656 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <common/cfgparse.h>
13#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010014#include <common/h1.h>
Christopher Faulet0ef372a2019-04-08 10:57:20 +020015#include <common/h2.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010017#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020018
Christopher Fauleta99ff4d2019-07-22 16:18:24 +020019#include <ebistree.h>
20
Christopher Faulet1be55f92018-10-02 15:59:23 +020021#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020022#include <types/proxy.h>
23#include <types/session.h>
24
Christopher Faulet51dbc942018-09-13 09:05:15 +020025#include <proto/connection.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020026#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020027#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010028#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020029#include <proto/stream.h>
30#include <proto/stream_interface.h>
31
32/*
33 * H1 Connection flags (32 bits)
34 */
35#define H1C_F_NONE 0x00000000
36
37/* Flags indicating why writing output data are blocked */
38#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
39#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
40/* 0x00000004 - 0x00000008 unused */
41
42/* Flags indicating why reading input data are blocked. */
43#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
44#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010045#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010046/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020047
48#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
49#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010050#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020051#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020052
Christopher Fauletf2824e62018-10-01 12:12:37 +020053#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020054#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020055
Christopher Faulet51dbc942018-09-13 09:05:15 +020056/*
57 * H1 Stream flags (32 bits)
58 */
Christopher Faulet129817b2018-09-20 16:14:40 +020059#define H1S_F_NONE 0x00000000
60#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020061#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
62#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020063#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020064#define H1S_F_WANT_KAL 0x00000010
65#define H1S_F_WANT_TUN 0x00000020
66#define H1S_F_WANT_CLO 0x00000040
67#define H1S_F_WANT_MSK 0x00000070
68#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010069#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010070#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010071#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
Willy Tarreau62038152019-08-23 09:29:29 +020072#define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */
73/* 0x00002000 .. 0x00002000 unused */
Christopher Fauletada34b62019-05-24 16:36:43 +020074#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020075
76/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020077struct h1c {
78 struct connection *conn;
79 struct proxy *px;
80 uint32_t flags; /* Connection flags: H1C_F_* */
81
82 struct buffer ibuf; /* Input buffer to store data before parsing */
83 struct buffer obuf; /* Output buffer to store data after reformatting */
84
85 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
86 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
87
88 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010089 struct task *task; /* timeout management task */
90 int timeout; /* idle timeout duration in ticks */
91 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020092};
93
94/* H1 stream descriptor */
95struct h1s {
96 struct h1c *h1c;
97 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010098 struct cs_info csinfo; /* CS info, only used for client connections */
99 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200100
Christopher Faulet51dbc942018-09-13 09:05:15 +0200101 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
102 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200103
Olivier Houchardf502aca2018-12-14 19:42:40 +0100104 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200105 struct h1m req;
106 struct h1m res;
107
108 enum http_meth_t meth; /* HTTP resquest method */
109 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200110};
111
Christopher Fauleta99ff4d2019-07-22 16:18:24 +0200112/* Map of headers used to convert outgoing headers */
113struct h1_hdrs_map {
114 char *name;
115 struct eb_root map;
116};
117
118/* An entry in a headers map */
119struct h1_hdr_entry {
120 struct ist name;
121 struct ebpt_node node;
122};
123
124/* Declare the headers map */
125static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
126
127
Christopher Faulet51dbc942018-09-13 09:05:15 +0200128/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100129DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
130DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200131
Christopher Faulet51dbc942018-09-13 09:05:15 +0200132static int h1_recv(struct h1c *h1c);
133static int h1_send(struct h1c *h1c);
134static int h1_process(struct h1c *h1c);
135static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100136static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100137static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200138
139/*****************************************************/
140/* functions below are for dynamic buffer management */
141/*****************************************************/
142/*
143 * Indicates whether or not the we may call the h1_recv() function to
144 * attempt to receive data into the buffer and/or parse pending data. The
145 * condition is a bit complex due to some API limits for now. The rules are the
146 * following :
147 * - if an error or a shutdown was detected on the connection and the buffer
148 * is empty, we must not attempt to receive
149 * - if the input buffer failed to be allocated, we must not try to receive
150 * and we know there is nothing pending
151 * - if no flag indicates a blocking condition, we may attempt to receive,
152 * regardless of whether the input buffer is full or not, so that only de
153 * receiving part decides whether or not to block. This is needed because
154 * the connection API indeed prevents us from re-enabling receipt that is
155 * already enabled in a polled state, so we must always immediately stop as
156 * soon as the mux can't proceed so as never to hit an end of read with data
157 * pending in the buffers.
158 * - otherwise must may not attempt to receive
159 */
160static inline int h1_recv_allowed(const struct h1c *h1c)
161{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100162 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200163 return 0;
164
Christopher Fauletcf56b992018-12-11 16:12:31 +0100165 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
166 return 0;
167
Christopher Fauletcb55f482018-12-10 11:56:47 +0100168 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200169 return 1;
170
171 return 0;
172}
173
174/*
175 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
176 * flags are used to figure what buffer was requested. It returns 1 if the
177 * allocation succeeds, in which case the connection is woken up, or 0 if it's
178 * impossible to wake up and we prefer to be woken up later.
179 */
180static int h1_buf_available(void *target)
181{
182 struct h1c *h1c = target;
183
184 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
185 h1c->flags &= ~H1C_F_IN_ALLOC;
186 if (h1_recv_allowed(h1c))
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
191 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
192 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200193 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200194 return 1;
195 }
196
Christopher Faulet51dbc942018-09-13 09:05:15 +0200197 return 0;
198}
199
200/*
201 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
202 */
203static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
204{
205 struct buffer *buf = NULL;
206
207 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
208 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
209 h1c->buf_wait.target = h1c;
210 h1c->buf_wait.wakeup_cb = h1_buf_available;
211 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
212 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
213 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
214 __conn_xprt_stop_recv(h1c->conn);
215 }
216 return buf;
217}
218
219/*
220 * Release a buffer, if any, and try to wake up entities waiting in the buffer
221 * wait queue.
222 */
223static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
224{
225 if (bptr->size) {
226 b_free(bptr);
227 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
228 }
229}
230
Willy Tarreau00f18a32019-01-26 12:19:01 +0100231/* returns the number of streams in use on a connection to figure if it's
232 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
233 * as the caller will want to know if it was the last one after a detach().
234 */
235static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200236{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100237 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200238
Willy Tarreau00f18a32019-01-26 12:19:01 +0100239 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200240}
241
Willy Tarreau00f18a32019-01-26 12:19:01 +0100242/* returns the number of streams still available on a connection */
243static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100244{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100245 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100246}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200247
Willy Tarreau00f18a32019-01-26 12:19:01 +0100248
Christopher Faulet51dbc942018-09-13 09:05:15 +0200249/*****************************************************************/
250/* functions below are dedicated to the mux setup and management */
251/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200252
253/* returns non-zero if there are input data pending for stream h1s. */
254static inline size_t h1s_data_pending(const struct h1s *h1s)
255{
256 const struct h1m *h1m;
257
258 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
259 if (h1m->state == H1_MSG_DONE)
260 return 0; // data not for this stream (e.g. pipelining)
261
262 return b_data(&h1s->h1c->ibuf);
263}
264
Christopher Faulet47365272018-10-31 17:40:50 +0100265static struct conn_stream *h1s_new_cs(struct h1s *h1s)
266{
267 struct conn_stream *cs;
268
269 cs = cs_new(h1s->h1c->conn);
270 if (!cs)
271 goto err;
272 h1s->cs = cs;
273 cs->ctx = h1s;
274
275 if (h1s->flags & H1S_F_NOT_FIRST)
276 cs->flags |= CS_FL_NOT_FIRST;
277
278 if (stream_create_from_cs(cs) < 0)
279 goto err;
280 return cs;
281
282 err:
283 cs_free(cs);
284 h1s->cs = NULL;
285 return NULL;
286}
287
Olivier Houchardf502aca2018-12-14 19:42:40 +0100288static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200289{
290 struct h1s *h1s;
291
292 h1s = pool_alloc(pool_head_h1s);
293 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100294 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200295
296 h1s->h1c = h1c;
297 h1c->h1s = h1s;
298
Olivier Houchardf502aca2018-12-14 19:42:40 +0100299 h1s->sess = sess;
300
Christopher Faulet51dbc942018-09-13 09:05:15 +0200301 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100302 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200303
304 h1s->recv_wait = NULL;
305 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200306
307 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100308 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200309
Christopher Faulet129817b2018-09-20 16:14:40 +0200310 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100311 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200312
313 h1s->status = 0;
314 h1s->meth = HTTP_METH_OTHER;
315
Christopher Faulet47365272018-10-31 17:40:50 +0100316 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
317 h1s->flags |= H1S_F_NOT_FIRST;
318 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
319
Christopher Faulet129817b2018-09-20 16:14:40 +0200320 if (!conn_is_back(h1c->conn)) {
321 if (h1c->px->options2 & PR_O2_REQBUG_OK)
322 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200323
324 /* For frontend connections we should always have a session */
325 if (!sess)
326 sess = h1c->conn->owner;
327
Dave Pirottecf67ec52019-07-10 13:57:38 +0000328 /* Timers for subsequent sessions on the same HTTP 1.x connection
329 * measure from `now`, not from the connection accept time */
330 if (h1s->flags & H1S_F_NOT_FIRST) {
331 h1s->csinfo.create_date = date;
332 h1s->csinfo.tv_create = now;
333 h1s->csinfo.t_handshake = 0;
334 h1s->csinfo.t_idle = -1;
335 }
336 else {
337 h1s->csinfo.create_date = sess->accept_date;
338 h1s->csinfo.tv_create = sess->tv_accept;
339 h1s->csinfo.t_handshake = sess->t_handshake;
340 h1s->csinfo.t_idle = -1;
341 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200342 }
343 else {
344 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
345 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200346
Christopher Fauletfeb11742018-11-29 15:12:34 +0100347 h1s->csinfo.create_date = date;
348 h1s->csinfo.tv_create = now;
349 h1s->csinfo.t_handshake = 0;
350 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200351 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100352
Christopher Faulete9b70722019-04-08 10:46:02 +0200353 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
354 * create a new one.
355 */
356 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200357 cs->ctx = h1s;
358 h1s->cs = cs;
359 }
Christopher Faulet47365272018-10-31 17:40:50 +0100360 else {
361 cs = h1s_new_cs(h1s);
362 if (!cs)
363 goto fail;
364 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200365 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100366
367 fail:
368 pool_free(pool_head_h1s, h1s);
369 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200370}
371
372static void h1s_destroy(struct h1s *h1s)
373{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200374 if (h1s) {
375 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200376
Christopher Fauletf2824e62018-10-01 12:12:37 +0200377 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200378
Christopher Fauletf2824e62018-10-01 12:12:37 +0200379 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100380 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200381 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100382 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200383
Christopher Fauletcb55f482018-12-10 11:56:47 +0100384 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100385 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
386 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
387 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200388 pool_free(pool_head_h1s, h1s);
389 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200390}
391
Christopher Fauletfeb11742018-11-29 15:12:34 +0100392static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
393{
394 struct h1s *h1s = cs->ctx;
395
396 if (h1s && !conn_is_back(cs->conn))
397 return &h1s->csinfo;
398 return NULL;
399}
400
Christopher Faulet51dbc942018-09-13 09:05:15 +0200401/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200402 * Initialize the mux once it's attached. It is expected that conn->ctx points
403 * to the existing conn_stream (for outgoing connections or for incoming onces
404 * during a mux upgrade) or NULL (for incoming ones during the connexion
405 * establishment). <input> is always used as Input buffer and may contain
406 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
407 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200408 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200409static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
410 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200411{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200412 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100413 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200414
415 h1c = pool_alloc(pool_head_h1c);
416 if (!h1c)
417 goto fail_h1c;
418 h1c->conn = conn;
419 h1c->px = proxy;
420
421 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200422 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200423 h1c->obuf = BUF_NULL;
424 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200425 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200426
Christopher Faulet51dbc942018-09-13 09:05:15 +0200427 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200428 h1c->wait_event.tasklet = tasklet_new();
429 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200430 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200431 h1c->wait_event.tasklet->process = h1_io_cb;
432 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100433 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200434
Christopher Faulete9b70722019-04-08 10:46:02 +0200435 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100436 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
437 if (tick_isset(proxy->timeout.serverfin))
438 h1c->shut_timeout = proxy->timeout.serverfin;
439 } else {
440 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
441 if (tick_isset(proxy->timeout.clientfin))
442 h1c->shut_timeout = proxy->timeout.clientfin;
443 }
444 if (tick_isset(h1c->timeout)) {
445 t = task_new(tid_bit);
446 if (!t)
447 goto fail;
448
449 h1c->task = t;
450 t->process = h1_timeout_task;
451 t->context = h1c;
452 t->expire = tick_add(now_ms, h1c->timeout);
453 }
454
Olivier Houchard985234d2019-06-13 17:54:33 +0200455 if (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200456 h1c->flags |= H1C_F_CS_WAIT_CONN;
457
Christopher Fauletf2824e62018-10-01 12:12:37 +0200458 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100459 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200460 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200461
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100462 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200463
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100464
465 if (t)
466 task_queue(t);
467
Christopher Faulet51dbc942018-09-13 09:05:15 +0200468 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200469 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200470
471 /* mux->wake will be called soon to complete the operation */
472 return 0;
473
474 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200475 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200476 if (h1c->wait_event.tasklet)
477 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200478 pool_free(pool_head_h1c, h1c);
479 fail_h1c:
480 return -1;
481}
482
Christopher Faulet73c12072019-04-08 11:23:22 +0200483/* release function. This one should be called to free all resources allocated
484 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200485 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200486static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200487{
Christopher Faulet61840e72019-04-15 09:33:32 +0200488 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200489
Christopher Faulet51dbc942018-09-13 09:05:15 +0200490 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200491 /* The connection must be aattached to this mux to be released */
492 if (h1c->conn && h1c->conn->ctx == h1c)
493 conn = h1c->conn;
494
495 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200496 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard13f556e2019-07-03 13:08:18 +0200497 /* Make sure we're no longer subscribed to anything */
498 if (h1c->wait_event.events)
499 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
500 h1c->wait_event.events, &h1c->wait_event);
Olivier Houchard45c44372019-06-11 14:06:23 +0200501 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200502 /* connection successfully upgraded to H2, this
503 * mux was already released */
504 return;
505 }
506 sess_log(conn->owner); /* Log if the upgrade failed */
507 }
508
Olivier Houchard45c44372019-06-11 14:06:23 +0200509
Christopher Faulet51dbc942018-09-13 09:05:15 +0200510 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
511 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
512 LIST_DEL(&h1c->buf_wait.list);
513 LIST_INIT(&h1c->buf_wait.list);
514 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
515 }
516
517 h1_release_buf(h1c, &h1c->ibuf);
518 h1_release_buf(h1c, &h1c->obuf);
519
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100520 if (h1c->task) {
521 h1c->task->context = NULL;
522 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
523 h1c->task = NULL;
524 }
525
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200526 if (h1c->wait_event.tasklet)
527 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200528
Christopher Fauletf2824e62018-10-01 12:12:37 +0200529 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200530 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100531 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200532 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200533 pool_free(pool_head_h1c, h1c);
534 }
535
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200536 if (conn) {
537 conn->mux = NULL;
538 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200539
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200540 conn_stop_tracking(conn);
541 conn_full_close(conn);
542 if (conn->destroy_cb)
543 conn->destroy_cb(conn);
544 conn_free(conn);
545 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200546}
547
548/******************************************************/
549/* functions below are for the H1 protocol processing */
550/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200551/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
552 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200553 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100554static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200555{
Christopher Faulet570d1612018-11-26 11:13:57 +0100556 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200557
Christopher Faulet570d1612018-11-26 11:13:57 +0100558 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200559 (*(p + 5) > '1' ||
560 (*(p + 5) == '1' && *(p + 7) >= '1')))
561 h1m->flags |= H1_MF_VER_11;
562}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200563
Christopher Faulet9768c262018-10-22 09:34:31 +0200564/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
565 * greater or equal to 1.1
566 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100567static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200568{
Christopher Faulet570d1612018-11-26 11:13:57 +0100569 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200570
Christopher Faulet570d1612018-11-26 11:13:57 +0100571 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200572 (*(p + 5) > '1' ||
573 (*(p + 5) == '1' && *(p + 7) >= '1')))
574 h1m->flags |= H1_MF_VER_11;
575}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200576
Christopher Faulet9768c262018-10-22 09:34:31 +0200577/*
578 * Check the validity of the request version. If the version is valid, it
579 * returns 1. Otherwise, it returns 0.
580 */
581static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
582{
583 struct h1c *h1c = h1s->h1c;
584
585 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
586 * exactly one digit "." one digit. This check may be disabled using
587 * option accept-invalid-http-request.
588 */
589 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
590 if (sl.rq.v.len != 8)
591 return 0;
592
593 if (*(sl.rq.v.ptr + 4) != '/' ||
594 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
595 *(sl.rq.v.ptr + 6) != '.' ||
596 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
597 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200598 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200599 else if (!sl.rq.v.len) {
600 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200601
Christopher Faulet9768c262018-10-22 09:34:31 +0200602 /* RFC 1945 allows only GET for HTTP/0.9 requests */
603 if (sl.rq.meth != HTTP_METH_GET)
604 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200605
Christopher Faulet9768c262018-10-22 09:34:31 +0200606 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
607 if (!sl.rq.u.len)
608 return 0;
609
610 /* Add HTTP version */
611 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100612 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200613 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100614
615 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100616 ((*(sl.rq.v.ptr + 5) > '1') ||
617 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100618 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200619 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200620}
621
Christopher Faulet9768c262018-10-22 09:34:31 +0200622/*
623 * Check the validity of the response version. If the version is valid, it
624 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200625 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200626static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200627{
Christopher Faulet9768c262018-10-22 09:34:31 +0200628 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200629
Christopher Faulet9768c262018-10-22 09:34:31 +0200630 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
631 * exactly one digit "." one digit. This check may be disabled using
632 * option accept-invalid-http-request.
633 */
634 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
635 if (sl.st.v.len != 8)
636 return 0;
637
638 if (*(sl.st.v.ptr + 4) != '/' ||
639 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
640 *(sl.st.v.ptr + 6) != '.' ||
641 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
642 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200643 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100644
645 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100646 ((*(sl.st.v.ptr + 5) > '1') ||
647 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100648 h1m->flags |= H1_MF_VER_11;
649
Christopher Faulet9768c262018-10-22 09:34:31 +0200650 return 1;
651}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200652
653/* Deduce the connection mode of the client connection, depending on the
654 * configuration and the H1 message flags. This function is called twice, the
655 * first time when the request is parsed and the second time when the response
656 * is parsed.
657 */
658static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
659{
660 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200661
662 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100663 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200664 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100665 h1s->status == 101) {
666 /* Either we've established an explicit tunnel, or we're
667 * switching the protocol. In both cases, we're very unlikely to
668 * understand the next protocols. We have to switch to tunnel
669 * mode, so that we transfer the request and responses then let
670 * this protocol pass unmodified. When we later implement
671 * specific parsers for such protocols, we'll want to check the
672 * Upgrade header which contains information about that protocol
673 * for responses with status 101 (eg: see RFC2817 about TLS).
674 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200675 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100676 }
677 else if (h1s->flags & H1S_F_WANT_KAL) {
678 /* By default the client is in KAL mode. CLOSE mode mean
679 * it is imposed by the client itself. So only change
680 * KAL mode here. */
681 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
682 /* no length known or explicit close => close */
683 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
684 }
685 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
686 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
687 /* no explict keep-alive and option httpclose => close */
688 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
689 }
690 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200691 }
692 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100693 /* Input direction: first pass */
694 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
695 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200696 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100697 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200698 }
699
700 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
701 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
702 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
703}
704
705/* Deduce the connection mode of the client connection, depending on the
706 * configuration and the H1 message flags. This function is called twice, the
707 * first time when the request is parsed and the second time when the response
708 * is parsed.
709 */
710static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
711{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100712 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100713 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100714 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200715
Christopher Fauletf2824e62018-10-01 12:12:37 +0200716 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100717 /* Input direction: second pass */
718
Christopher Fauletf2824e62018-10-01 12:12:37 +0200719 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100720 h1s->status == 101) {
721 /* Either we've established an explicit tunnel, or we're
722 * switching the protocol. In both cases, we're very unlikely to
723 * understand the next protocols. We have to switch to tunnel
724 * mode, so that we transfer the request and responses then let
725 * this protocol pass unmodified. When we later implement
726 * specific parsers for such protocols, we'll want to check the
727 * Upgrade header which contains information about that protocol
728 * for responses with status 101 (eg: see RFC2817 about TLS).
729 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200730 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100731 }
732 else if (h1s->flags & H1S_F_WANT_KAL) {
733 /* By default the server is in KAL mode. CLOSE mode mean
734 * it is imposed by haproxy itself. So only change KAL
735 * mode here. */
736 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
737 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
738 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
739 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
740 }
741 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200742 }
Christopher Faulet70033782018-12-05 13:50:11 +0100743 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100744 /* Output direction: first pass */
745 if (h1m->flags & H1_MF_CONN_CLO) {
746 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100747 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100748 }
749 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
750 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
751 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
752 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
753 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
754 /* no explicit keep-alive option httpclose/server-close => close */
755 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
756 }
Christopher Faulet70033782018-12-05 13:50:11 +0100757 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200758
759 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
760 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
761 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200762}
763
Christopher Fauletb992af02019-03-28 15:42:24 +0100764static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200765{
766 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200767
768 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
769 * token is found
770 */
771 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200772 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200773
774 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100775 if (!(h1m->flags & H1_MF_VER_11))
776 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200777 }
778 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100779 if (h1m->flags & H1_MF_VER_11)
780 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200781 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200782}
783
Christopher Fauletb992af02019-03-28 15:42:24 +0100784static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200785{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200786 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
787 * token is found
788 */
789 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200790 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200791
792 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100793 if (!(h1m->flags & H1_MF_VER_11) ||
794 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
795 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200796 }
797 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100798 if (h1m->flags & H1_MF_VER_11)
799 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200800 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200801}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200802
Christopher Fauletb992af02019-03-28 15:42:24 +0100803static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200804{
Christopher Fauletb992af02019-03-28 15:42:24 +0100805 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200806 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100807 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200808 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200809}
810
Christopher Fauletb992af02019-03-28 15:42:24 +0100811static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
812{
813 if (!conn_is_back(h1s->h1c->conn))
814 h1_set_cli_conn_mode(h1s, h1m);
815 else
816 h1_set_srv_conn_mode(h1s, h1m);
817
818 if (!(h1m->flags & H1_MF_RESP))
819 h1_update_req_conn_value(h1s, h1m, conn_val);
820 else
821 h1_update_res_conn_value(h1s, h1m, conn_val);
822}
Christopher Faulete44769b2018-11-29 23:01:45 +0100823
Christopher Fauleta99ff4d2019-07-22 16:18:24 +0200824/* Try to adjust the case of the message header name using the global map
825 * <hdrs_map>.
826 */
827static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
828{
829 struct ebpt_node *node;
830 struct h1_hdr_entry *entry;
831
832 /* No entry in the map, do nothing */
833 if (eb_is_empty(&hdrs_map.map))
834 return;
835
836 /* No conversion fo the request headers */
837 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
838 return;
839
840 /* No conversion fo the response headers */
841 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
842 return;
843
844 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
845 if (!node)
846 return;
847 entry = container_of(node, struct h1_hdr_entry, node);
848 name->ptr = entry->name.ptr;
849 name->len = entry->name.len;
850}
851
Christopher Faulete44769b2018-11-29 23:01:45 +0100852/* Append the description of what is present in error snapshot <es> into <out>.
853 * The description must be small enough to always fit in a buffer. The output
854 * buffer may be the trash so the trash must not be used inside this function.
855 */
856static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
857{
858 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100859 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
860 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
861 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
862 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
863 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
864 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100865}
866/*
867 * Capture a bad request or response and archive it in the proxy's structure.
868 * By default it tries to report the error position as h1m->err_pos. However if
869 * this one is not set, it will then report h1m->next, which is the last known
870 * parsing point. The function is able to deal with wrapping buffers. It always
871 * displays buffers as a contiguous area starting at buf->p. The direction is
872 * determined thanks to the h1m's flags.
873 */
874static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
875 struct h1m *h1m, struct buffer *buf)
876{
877 struct session *sess = h1c->conn->owner;
878 struct proxy *proxy = h1c->px;
879 struct proxy *other_end = sess->fe;
880 union error_snapshot_ctx ctx;
881
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100882 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100883 other_end = si_strm(h1s->cs->data)->be;
884
885 /* http-specific part now */
886 ctx.h1.state = h1m->state;
887 ctx.h1.c_flags = h1c->flags;
888 ctx.h1.s_flags = h1s->flags;
889 ctx.h1.m_flags = h1m->flags;
890 ctx.h1.m_clen = h1m->curr_len;
891 ctx.h1.m_blen = h1m->body_len;
892
893 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
894 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100895 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
896 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100897}
898
Christopher Fauletadb22202018-12-12 10:32:09 +0100899/* Emit the chunksize followed by a CRLF in front of data of the buffer
900 * <buf>. It goes backwards and starts with the byte before the buffer's
901 * head. The caller is responsible for ensuring there is enough room left before
902 * the buffer's head for the string.
903 */
904static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
905{
906 char *beg, *end;
907
908 beg = end = b_head(buf);
909 *--beg = '\n';
910 *--beg = '\r';
911 do {
912 *--beg = hextab[chksz & 0xF];
913 } while (chksz >>= 4);
914 buf->head -= (end - beg);
915 b_add(buf, end - beg);
916}
917
918/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
919 * ensuring there is enough room left in the buffer for the string. */
920static void h1_emit_chunk_crlf(struct buffer *buf)
921{
922 *(b_peek(buf, b_data(buf))) = '\r';
923 *(b_peek(buf, b_data(buf) + 1)) = '\n';
924 b_add(buf, 2);
925}
926
Christopher Faulet129817b2018-09-20 16:14:40 +0200927/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100928 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletac4778c2019-11-15 11:14:23 +0100929 * CONNECT requests. On the client side, if the response is not finished, the
930 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100931 */
932static void h1_set_req_tunnel_mode(struct h1s *h1s)
933{
934 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
935 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100936 if (!conn_is_back(h1s->h1c->conn) && h1s->res.state < H1_MSG_DONE)
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100937 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100938 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
939 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
940 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
941 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100942}
943
944/*
945 * Switch the response to tunnel mode. This function must only be called on
Christopher Fauletac4778c2019-11-15 11:14:23 +0100946 * successfull replies to CONNECT requests or on protocol switching. In this
947 * last case, this function takes care to switch the request to tunnel mode if
948 * possible. On the server side, if the request is not finished, the mux is mark
949 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100950 */
951static void h1_set_res_tunnel_mode(struct h1s *h1s)
952{
Christopher Fauletac4778c2019-11-15 11:14:23 +0100953 /* On protocol switching, switch the request to tunnel mode if it is in
954 * DONE state. Otherwise we will wait the end of the request to switch
955 * it in tunnel mode.
956 */
957 if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
958 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
959 h1s->req.state = H1_MSG_TUNNEL;
960 }
961
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100962 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
963 h1s->res.state = H1_MSG_TUNNEL;
964 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
965 h1s->h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletac4778c2019-11-15 11:14:23 +0100966 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
967 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
968 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100969 }
970}
971
Christopher Faulet82f01602019-05-24 16:50:16 +0200972/* Estimate the size of the HTX headers after the parsing, including the EOH. */
973static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
974{
975 size_t sz = 0;
976 int i;
977
978 for (i = 0; hdrs[i].n.len; i++)
979 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
980 sz += sizeof(struct htx_blk) + 1;
981 return sz;
982}
983
Christopher Faulet30db3d72019-05-17 15:35:33 +0200984/* Estimate the size of the HTX request after the parsing. */
985static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
986{
987 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200988
989 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +0200990 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 +0200991 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200992 return sz;
993}
994
995/* Estimate the size of the HTX response after the parsing. */
996static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
997{
998 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200999
1000 /* size of the HTX start-line */
Christopher Faulet1d76cef2019-09-03 16:16:50 +02001001 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 +02001002 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001003 return sz;
1004}
1005
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001006/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001007 * Add the EOM in the HTX message and switch the message to the DONE state. It
1008 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
1009 * functions is responsible to update the parser state <h1m>. It also add the
1010 * flag CS_FL_EOI on the CS.
1011 */
1012static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
1013{
Willy Tarreau62038152019-08-23 09:29:29 +02001014 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
1015 h1s->flags |= H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001016 return 0;
Willy Tarreau62038152019-08-23 09:29:29 +02001017 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001018
Willy Tarreau62038152019-08-23 09:29:29 +02001019 h1s->flags &= ~H1S_F_APPEND_EOM;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001020 h1m->state = H1_MSG_DONE;
1021 h1s->cs->flags |= CS_FL_EOI;
1022 return (sizeof(struct htx_blk) + 1);
1023}
1024
1025/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001026 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001027 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1028 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001029 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001030 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001031static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001032 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001033{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001034 struct htx_sl *sl;
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001035 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001036 union h1_sl h1sl;
1037 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001038 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +02001039 int ret = 0;
1040
Christopher Faulet30db3d72019-05-17 15:35:33 +02001041 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001042 goto end;
1043
Christopher Faulet129817b2018-09-20 16:14:40 +02001044 /* Realing input buffer if necessary */
1045 if (b_head(buf) + b_data(buf) > b_wrap(buf))
1046 b_slow_realign(buf, trash.area, 0);
1047
Christopher Faulet842f41f2019-09-25 09:10:46 +02001048 if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001049 /* Try to match H2 preface before parsing the request headers. */
1050 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
1051 if (ret > 0)
1052 goto h2c_upgrade;
1053 }
1054
Christopher Faulet30db3d72019-05-17 15:35:33 +02001055 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001056 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +02001057 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001058 /* Incomplete or invalid message. If the input buffer only
1059 * contains headers and is full, which is detected by it being
1060 * full and the offset to be zero, it's an error because
1061 * headers are too large to be handled by the parser. */
1062 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001063 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001064 goto end;
1065 }
1066
Christopher Fauletb4bad502019-10-11 14:22:00 +02001067 if (h1m->err_pos >= 0) {
1068 /* Maybe we found an error during the parsing while we were
1069 * configured not to block on that, so we have to capture it
1070 * now.
1071 */
1072 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1073 }
1074
Christopher Faulet129817b2018-09-20 16:14:40 +02001075 /* messages headers fully parsed, do some checks to prepare the body
1076 * parsing.
1077 */
1078
Christopher Faulet9768c262018-10-22 09:34:31 +02001079 /* Save the request's method or the response's status, check if the body
1080 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001081 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001082 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001083
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001084 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001085 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001086
1087 if (h1s->meth == HTTP_METH_CONNECT) {
1088 /* Switch CONNECT requests to tunnel mode */
1089 h1_set_req_tunnel_mode(h1s);
1090 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001091
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001092 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1093 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001094 h1m->err_state = h1m->state;
1095 goto vsn_error;
1096 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001097 }
1098 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001099 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001100
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001101 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1102 h1s->status == 101) {
1103 /* Switch successfull replies to CONNECT requests and
1104 * protocol switching to tunnel mode. */
1105 h1_set_res_tunnel_mode(h1s);
1106 }
1107 else if ((h1s->meth == HTTP_METH_HEAD) ||
1108 (h1s->status >= 100 && h1s->status < 200) ||
1109 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001110 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001111 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1112 h1m->flags |= H1_MF_XFER_LEN;
1113 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001114 }
1115 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001116 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001117 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001118 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001119 else {
1120 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001121 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001122 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001123
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001124 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1125 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001126 h1m->err_state = h1m->state;
1127 goto vsn_error;
1128 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001129 }
1130
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001131 /* Set HTX start-line flags */
1132 if (h1m->flags & H1_MF_VER_11)
1133 flags |= HTX_SL_F_VER_11;
1134 if (h1m->flags & H1_MF_XFER_ENC)
1135 flags |= HTX_SL_F_XFER_ENC;
1136 if (h1m->flags & H1_MF_XFER_LEN) {
1137 flags |= HTX_SL_F_XFER_LEN;
1138 if (h1m->flags & H1_MF_CHNK)
1139 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001140 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001141 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001142 if (h1m->body_len == 0)
1143 flags |= HTX_SL_F_BODYLESS;
1144 }
1145 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001146 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001147 }
1148
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001149 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001150 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001151 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1152 if (htx_is_empty(htx))
1153 goto error;
1154 h1m_init_req(h1m);
1155 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1156 ret = 0;
1157 goto end;
1158 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001159
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001160 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1161 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001162 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001163 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001164
1165 /* Check if the uri contains an explicit scheme and if it is
1166 * "http" or "https". */
1167 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1168 sl->flags |= HTX_SL_F_HAS_SCHM;
1169 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1170 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1171 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001172 }
1173 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001174 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1175 if (htx_is_empty(htx))
1176 goto error;
1177 h1m_init_res(h1m);
1178 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1179 ret = 0;
1180 goto end;
1181 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001182
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001183 flags |= HTX_SL_F_IS_RESP;
1184 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1185 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001186 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001187 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001188 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001189
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001190 /* Set bytes used in the HTX mesage for the headers now */
1191 sl->hdrs_bytes = htx_used_space(htx) - used;
1192
Christopher Fauletb992af02019-03-28 15:42:24 +01001193 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001194
1195 /* If body length cannot be determined, set htx->extra to
1196 * ULLONG_MAX. This value is impossible in other cases.
1197 */
1198 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001199 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001200 end:
1201 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001202
1203 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001204 h1m->err_state = h1m->state;
1205 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001206 vsn_error:
1207 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001208 h1s->cs->flags |= CS_FL_EOI;
1209 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001210 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001211 ret = 0;
1212 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001213
1214 h2c_upgrade:
1215 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001216 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001217 htx->flags |= HTX_FL_UPGRADE;
1218 ret = 0;
1219 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001220}
1221
1222/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001223 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1224 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001225 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001226 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001227 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001228static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001229 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001230 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001231{
1232 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001233 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001234
Christopher Faulet129817b2018-09-20 16:14:40 +02001235 if (h1m->flags & H1_MF_XFER_LEN) {
1236 if (h1m->flags & H1_MF_CLEN) {
1237 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001238 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001239 if ((uint64_t)ret > h1m->curr_len)
1240 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001241 if (ret > b_contig_data(buf, *ofs))
1242 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001243
Christopher Faulet9768c262018-10-22 09:34:31 +02001244 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001245 /* very often with large files we'll face the following
1246 * situation :
1247 * - htx is empty and points to <htxbuf>
1248 * - ret == buf->data
1249 * - buf->head == sizeof(struct htx)
1250 * => we can swap the buffers and place an htx header into
1251 * the target buffer instead
1252 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001253 int32_t try = ret;
1254
Willy Tarreau78f548f2018-12-05 10:02:39 +01001255 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1256 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1257 void *raw_area = buf->area;
1258 void *htx_area = htxbuf->area;
1259 struct htx_blk *blk;
1260
1261 buf->area = htx_area;
1262 htxbuf->area = raw_area;
1263 htx = (struct htx *)htxbuf->area;
1264 htx->size = htxbuf->size - sizeof(*htx);
1265 htx_reset(htx);
1266 b_set_data(htxbuf, b_size(htxbuf));
1267
1268 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1269 blk->info += ret;
1270 /* nothing else to do, the old buffer now contains an
1271 * empty pre-initialized HTX header
1272 */
1273 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001274 else {
1275 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1276 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001277 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001278 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001279 *ofs += ret;
1280 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001281 if (ret < try)
1282 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001283 }
1284
1285 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001286 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001287 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001288 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001289 }
1290 else if (h1m->flags & H1_MF_CHNK) {
1291 new_chunk:
1292 /* te:chunked : parse chunks */
1293 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001294 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 if (ret <= 0)
1296 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001297 h1m->state = H1_MSG_CHUNK_SIZE;
1298
Christopher Fauletf2824e62018-10-01 12:12:37 +02001299 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001300 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001301 }
1302
1303 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1304 unsigned int chksz;
1305
Christopher Faulet30db3d72019-05-17 15:35:33 +02001306 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001307 if (ret <= 0)
1308 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001309 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001310
Christopher Faulet129817b2018-09-20 16:14:40 +02001311 h1m->curr_len = chksz;
1312 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001313 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001314 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001315
1316 if (!h1m->curr_len)
1317 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001318 }
1319
1320 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001321 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001322 if ((uint64_t)ret > h1m->curr_len)
1323 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001324 if (ret > b_contig_data(buf, *ofs))
1325 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001326
Christopher Faulet9768c262018-10-22 09:34:31 +02001327 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001328 int32_t try = ret;
1329
1330 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001331 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001332 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001333 *ofs += ret;
1334 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001335 if (ret < try)
1336 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001337 }
1338 if (!h1m->curr_len) {
1339 h1m->state = H1_MSG_CHUNK_CRLF;
1340 goto new_chunk;
1341 }
1342 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001343 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001344 }
1345 else {
1346 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1347 * is no body. Switch the message in DONE state
1348 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001349 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001350 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001351 }
1352 }
1353 else {
1354 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001355 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001356 if (ret > b_contig_data(buf, *ofs))
1357 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001358
Christopher Faulet9768c262018-10-22 09:34:31 +02001359 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001360 int32_t try = ret;
1361
1362 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001363
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001364 *ofs += ret;
1365 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001366 if (ret < try)
1367 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001368 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001369 }
1370
1371 end:
1372 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001373 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001374 h1s->cs->flags |= CS_FL_EOI;
1375 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001376 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001377 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001378 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001379 return 0;
1380 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001381 /* update htx->extra, only when the body length is known */
1382 if (h1m->flags & H1_MF_XFER_LEN)
1383 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001384 return total;
1385}
1386
1387/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001388 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1389 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1390 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1391 * responsible to update the parser state <h1m>.
1392 */
1393static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1394 struct buffer *buf, size_t *ofs, size_t max)
1395{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001396 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001397 struct h1m tlr_h1m;
1398 int ret = 0;
1399
1400 if (!max || !b_data(buf))
1401 goto end;
1402
1403 /* Realing input buffer if necessary */
1404 if (b_peek(buf, *ofs) > b_tail(buf))
1405 b_slow_realign(buf, trash.area, 0);
1406
1407 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1408 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1409 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1410 if (ret <= 0) {
Willy Tarreau9a408ab2019-08-23 08:11:36 +02001411 /* Incomplete or invalid trailers. If the input buffer only
1412 * contains trailers and is full, which is detected by it being
1413 * full and the offset to be zero, it's an error because
1414 * trailers are too large to be handled by the parser. */
1415 if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf)))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001416 goto error;
1417 goto end;
1418 }
1419
1420 /* messages trailers fully parsed. */
1421 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1422 if (htx_is_empty(htx))
1423 goto error;
1424 ret = 0;
1425 goto end;
1426 }
1427
1428 if (!htx_add_all_trailers(htx, hdrs))
1429 goto error;
1430
1431 *ofs += ret;
1432 h1s->flags |= H1S_F_HAVE_I_TLR;
1433 end:
1434 return ret;
1435
1436 error:
1437 h1m->err_state = h1m->state;
1438 h1m->err_pos = h1m->next;
1439 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1440 h1s->cs->flags |= CS_FL_EOI;
1441 htx->flags |= HTX_FL_PARSING_ERROR;
1442 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1443 ret = 0;
1444 goto end;
1445}
1446
1447/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001448 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001449 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1450 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001451 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001452static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001453{
Christopher Faulet539e0292018-11-19 10:40:09 +01001454 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001455 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001456 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001457 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001458 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001459 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001460
Christopher Faulet539e0292018-11-19 10:40:09 +01001461 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001462
Christopher Fauletf2824e62018-10-01 12:12:37 +02001463 if (!conn_is_back(h1c->conn)) {
1464 h1m = &h1s->req;
1465 errflag = H1S_F_REQ_ERROR;
1466 }
1467 else {
1468 h1m = &h1s->res;
1469 errflag = H1S_F_RES_ERROR;
1470 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001471
Christopher Faulet74027762019-02-26 14:45:05 +01001472 data = htx->data;
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001473 if (h1s->flags & errflag)
1474 goto end;
1475
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001476 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001477 size_t used = htx_used_space(htx);
1478
Christopher Faulet129817b2018-09-20 16:14:40 +02001479 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001480 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001481 if (!ret)
1482 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001483 if ((h1m->flags & H1_MF_RESP) &&
1484 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1485 h1m_init_res(&h1s->res);
1486 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1487 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001488 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001489 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001490 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001491 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001492 if (!ret)
1493 break;
1494 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001495 else if (h1m->state == H1_MSG_TRAILERS) {
1496 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1497 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1498 if (!ret)
1499 break;
1500 }
Christopher Faulet1021e722019-09-03 21:55:14 +02001501 else if (!h1_process_eom(h1s, h1m, htx, count))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001502 break;
1503 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001504 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletac4778c2019-11-15 11:14:23 +01001505 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1506 h1_set_req_tunnel_mode(h1s);
1507 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001508 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001509 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001510 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001511 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001512 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001513 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001514 if (!ret)
1515 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001516 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001517 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001518 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001519 break;
1520 }
1521
Christopher Faulet30db3d72019-05-17 15:35:33 +02001522 count -= htx_used_space(htx) - used;
Christopher Faulet98e2bc22019-09-03 16:26:15 +02001523 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001524
Christopher Faulet47365272018-10-31 17:40:50 +01001525 if (h1s->flags & errflag)
1526 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001527
Christopher Faulet539e0292018-11-19 10:40:09 +01001528 b_del(&h1c->ibuf, total);
1529
1530 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001531 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001532 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001533 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001534 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001535 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001536 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001537
Christopher Fauletcf56b992018-12-11 16:12:31 +01001538 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1539
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001540 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001541 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001542 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001543 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001544
Willy Tarreau62038152019-08-23 09:29:29 +02001545 if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) &&
1546 (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001547 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001548 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001549 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001550 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001551
Christopher Faulet30db3d72019-05-17 15:35:33 +02001552 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001553
1554 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001555 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001556 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001557 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001558}
1559
Christopher Faulet129817b2018-09-20 16:14:40 +02001560/*
1561 * Process outgoing data. It parses data and transfer them from the channel buffer into
1562 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1563 * 0 if it couldn't proceed.
1564 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001565static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1566{
Christopher Faulet129817b2018-09-20 16:14:40 +02001567 struct h1s *h1s = h1c->h1s;
1568 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001569 struct htx *chn_htx;
1570 struct htx_blk *blk;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001571 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001572 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001573 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001574
Christopher Faulet47365272018-10-31 17:40:50 +01001575 if (!count)
1576 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001577
Christopher Faulet94b2c762019-05-24 15:28:57 +02001578 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001579 if (htx_is_empty(chn_htx))
1580 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001581
Christopher Faulet51dbc942018-09-13 09:05:15 +02001582 if (!h1_get_buf(h1c, &h1c->obuf)) {
1583 h1c->flags |= H1C_F_OUT_ALLOC;
1584 goto end;
1585 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001586
Christopher Fauletf2824e62018-10-01 12:12:37 +02001587 if (!conn_is_back(h1c->conn)) {
1588 h1m = &h1s->res;
1589 errflag = H1S_F_RES_ERROR;
1590 }
1591 else {
1592 h1m = &h1s->req;
1593 errflag = H1S_F_REQ_ERROR;
1594 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001595
Christopher Fauletcd9f6a72019-07-04 21:22:34 +02001596 if (h1s->flags & errflag)
1597 goto end;
1598
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001599 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001600 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001601
Willy Tarreau3815b222018-12-11 19:50:43 +01001602 /* Perform some optimizations to reduce the number of buffer copies.
1603 * First, if the mux's buffer is empty and the htx area contains
1604 * exactly one data block of the same size as the requested count,
1605 * then it's possible to simply swap the caller's buffer with the
1606 * mux's output buffer and adjust offsets and length to match the
1607 * entire DATA HTX block in the middle. In this case we perform a
1608 * true zero-copy operation from end-to-end. This is the situation
1609 * that happens all the time with large files. Second, if this is not
1610 * possible, but the mux's output buffer is empty, we still have an
1611 * opportunity to avoid the copy to the intermediary buffer, by making
1612 * the intermediary buffer's area point to the output buffer's area.
1613 * In this case we want to skip the HTX header to make sure that copies
1614 * remain aligned and that this operation remains possible all the
1615 * time. This goes for headers, data blocks and any data extracted from
1616 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001617 */
1618 if (!b_data(&h1c->obuf)) {
Willy Tarreau3815b222018-12-11 19:50:43 +01001619 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001620 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001621 htx_get_blk_value(chn_htx, blk).len == count) {
1622 void *old_area = h1c->obuf.area;
1623
1624 h1c->obuf.area = buf->area;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001625 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001626 h1c->obuf.data = count;
1627
1628 buf->area = old_area;
1629 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001630
1631 /* The message is chunked. We need to emit the chunk
1632 * size. We have at least the size of the struct htx to
1633 * write the chunk envelope. It should be enough.
1634 */
1635 if (h1m->flags & H1_MF_CHNK) {
1636 h1_emit_chunk_size(&h1c->obuf, count);
1637 h1_emit_chunk_crlf(&h1c->obuf);
1638 }
1639
Willy Tarreau3815b222018-12-11 19:50:43 +01001640 total += count;
1641 goto out;
1642 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001643 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001644 }
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001645 else
1646 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001647
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001648 tmp.data = 0;
1649 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001650 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001651 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001652 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001653 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001654 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001655 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001656
Christopher Fauletb2e84162018-12-06 11:39:49 +01001657 vlen = sz;
1658 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001659 if (type != HTX_BLK_DATA)
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001660 goto full;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001661 vlen = count;
1662 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001663
Christopher Faulet94b2c762019-05-24 15:28:57 +02001664 if (type == HTX_BLK_UNUSED)
1665 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001666
Christopher Faulet94b2c762019-05-24 15:28:57 +02001667 switch (h1m->state) {
1668 case H1_MSG_RQBEFORE:
1669 if (type != HTX_BLK_REQ_SL)
1670 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001671 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001672 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001673 h1_parse_req_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001674 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001675 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001676 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001677 if (sl->flags & HTX_SL_F_BODYLESS)
1678 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001679 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001680 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001681
Christopher Faulet94b2c762019-05-24 15:28:57 +02001682 case H1_MSG_RPBEFORE:
1683 if (type != HTX_BLK_RES_SL)
1684 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001685 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001686 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 h1_parse_res_vsn(h1m, sl);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001688 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001689 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001690 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001691 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001692 if (sl->info.res.status < 200 &&
1693 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001694 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001695 h1m->state = H1_MSG_HDR_FIRST;
1696 break;
1697
Christopher Faulet94b2c762019-05-24 15:28:57 +02001698 case H1_MSG_HDR_FIRST:
1699 case H1_MSG_HDR_NAME:
1700 case H1_MSG_HDR_L2_LWS:
1701 if (type == HTX_BLK_EOH)
1702 goto last_lf;
1703 if (type != HTX_BLK_HDR)
1704 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001705 h1m->state = H1_MSG_HDR_NAME;
1706 n = htx_get_blk_name(chn_htx, blk);
1707 v = htx_get_blk_value(chn_htx, blk);
1708
1709 if (isteqi(n, ist("transfer-encoding")))
1710 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001711 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001712 /* Only skip C-L header with invalid value. */
1713 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001714 goto skip_hdr;
1715 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001716 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001717 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001718 if (!v.len)
1719 goto skip_hdr;
1720 }
1721
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001722 /* Try to adjust the case of the header name */
1723 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1724 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001725 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001726 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001727 skip_hdr:
1728 h1m->state = H1_MSG_HDR_L2_LWS;
1729 break;
1730
Christopher Faulet94b2c762019-05-24 15:28:57 +02001731 case H1_MSG_LAST_LF:
1732 if (type != HTX_BLK_EOH)
1733 goto error;
1734 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001735 h1m->state = H1_MSG_LAST_LF;
1736 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001737 /* There is no "Connection:" header and
1738 * it the conn_mode must be
1739 * processed. So do it */
Christopher Faulet661bfc32019-06-17 14:07:46 +02001740 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001741 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001742 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001743 if (v.len) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001744 /* Try to adjust the case of the header name */
1745 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1746 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001747 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001748 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001749 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001750 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001751 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001752
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001753 if ((h1s->meth != HTTP_METH_CONNECT &&
1754 (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 +01001755 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1756 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1757 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1758 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1759 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001760 /* chunking needed but header not seen */
Christopher Faulete5addb02019-10-04 10:23:51 +02001761 n = ist("transfer-encoding");
1762 v = ist("chunked");
1763 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1764 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
1765 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001766 goto full;
Willy Tarreau4710d202019-01-03 17:39:54 +01001767 h1m->flags |= H1_MF_CHNK;
1768 }
1769
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001770 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001771 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001772
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001773 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1774 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1775 h1_set_req_tunnel_mode(h1s);
1776 }
Christopher Fauletac4778c2019-11-15 11:14:23 +01001777 else if ((h1m->flags & H1_MF_RESP) &&
1778 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001779 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletac4778c2019-11-15 11:14:23 +01001780 * to the client. Switch the response to tunnel mode.
1781 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001782 h1_set_res_tunnel_mode(h1s);
1783 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001784 else if ((h1m->flags & H1_MF_RESP) &&
1785 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1786 h1m_init_res(&h1s->res);
1787 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001788 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001789 }
Christopher Faulet33d58b52019-07-01 16:17:30 +02001790 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1791 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001792 else
1793 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001794 break;
1795
Christopher Faulet94b2c762019-05-24 15:28:57 +02001796 case H1_MSG_DATA:
Christopher Fauletaaa25062019-07-04 17:12:12 +02001797 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001798 if (type == HTX_BLK_EOM) {
1799 /* Chunked message without explicit trailers */
1800 if (h1m->flags & H1_MF_CHNK) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001801 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001802 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001803 }
1804 goto done;
1805 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001806 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet52131682019-06-27 17:40:14 +02001807 /* If the message is not chunked, never
1808 * add the last chunk. */
1809 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001810 goto full;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001811 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001812 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001813 else if (type != HTX_BLK_DATA)
1814 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001815 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001816 v.len = vlen;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001817 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001818 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001819 break;
1820
Christopher Faulet94b2c762019-05-24 15:28:57 +02001821 case H1_MSG_TRAILERS:
1822 if (type == HTX_BLK_EOM)
1823 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001824 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001825 goto error;
1826 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001827 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet52131682019-06-27 17:40:14 +02001828 /* If the message is not chunked, ignore
1829 * trailers. It may happen with H2 messages. */
1830 if (!(h1m->flags & H1_MF_CHNK))
1831 break;
1832
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001833 if (type == HTX_BLK_EOT) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001834 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001835 goto full;
Christopher Faulet32188212018-11-20 18:21:43 +01001836 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001837 else { // HTX_BLK_TLR
1838 n = htx_get_blk_name(chn_htx, blk);
1839 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001840
1841 /* Try to adjust the case of the header name */
1842 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1843 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001844 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001845 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001846 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001847 break;
1848
Christopher Faulet94b2c762019-05-24 15:28:57 +02001849 case H1_MSG_DONE:
1850 if (type != HTX_BLK_EOM)
1851 goto error;
1852 done:
1853 h1m->state = H1_MSG_DONE;
Christopher Fauletac4778c2019-11-15 11:14:23 +01001854 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1855 h1_set_req_tunnel_mode(h1s);
1856 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001857 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1858 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1859 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001860 break;
1861
Christopher Faulet9768c262018-10-22 09:34:31 +02001862 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001863 error:
Christopher Fauletc431df82019-06-26 15:16:28 +02001864 /* Unexpected error during output processing */
1865 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001866 h1s->flags |= errflag;
Christopher Fauletc431df82019-06-26 15:16:28 +02001867 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001868 break;
1869 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001870
1871 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001872 total += vlen;
1873 count -= vlen;
1874 if (sz == vlen)
1875 blk = htx_remove_blk(chn_htx, blk);
1876 else {
1877 htx_cut_data_blk(chn_htx, blk, vlen);
1878 break;
1879 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001880 }
1881
Christopher Faulet9768c262018-10-22 09:34:31 +02001882 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001883 /* when the output buffer is empty, tmp shares the same area so that we
1884 * only have to update pointers and lengths.
1885 */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001886 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1887 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001888 else
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001889 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001890
Willy Tarreau3815b222018-12-11 19:50:43 +01001891 htx_to_buf(chn_htx, buf);
1892 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001893 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001894 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001895 end:
1896 return total;
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001897
1898 full:
1899 h1c->flags |= H1C_F_OUT_FULL;
1900 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001901}
1902
Christopher Faulet51dbc942018-09-13 09:05:15 +02001903/*********************************************************/
1904/* functions below are I/O callbacks from the connection */
1905/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001906static void h1_wake_stream_for_recv(struct h1s *h1s)
1907{
1908 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001909 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001910 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001911 h1s->recv_wait = NULL;
1912 }
1913}
1914static void h1_wake_stream_for_send(struct h1s *h1s)
1915{
1916 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001917 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001918 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001919 h1s->send_wait = NULL;
1920 }
1921}
1922
Christopher Faulet51dbc942018-09-13 09:05:15 +02001923/*
1924 * Attempt to read data, and subscribe if none available
1925 */
1926static int h1_recv(struct h1c *h1c)
1927{
1928 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001929 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001930 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001931 int rcvd = 0;
1932
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001933 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001934 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001935
Olivier Houchard5b06cb32019-08-13 15:21:59 +02001936 if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN)
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001937 return 0;
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001938
Olivier Houchard75159a92018-12-03 18:46:09 +01001939 if (!h1_recv_allowed(h1c)) {
1940 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001941 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001942 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001943
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001944 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1945 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001946 goto end;
1947 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001948
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001949 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001950 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001951 h1_wake_stream_for_recv(h1s);
1952 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001953 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001954 }
1955
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001956 /*
1957 * If we only have a small amount of data, realign it,
1958 * it's probably cheaper than doing 2 recv() calls.
1959 */
1960 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1961 b_slow_realign(&h1c->ibuf, trash.area, 0);
1962
Willy Tarreau45f2b892018-12-05 07:59:27 +01001963 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001964 if (max) {
1965 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001966
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001967 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001968 if (!b_data(&h1c->ibuf)) {
1969 /* try to pre-align the buffer like the rxbufs will be
1970 * to optimize memory copies.
1971 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001972 h1c->ibuf.head = sizeof(struct htx);
1973 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001974 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001975 }
Christopher Faulet47365272018-10-31 17:40:50 +01001976 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001977 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001978 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001979 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001980 if (h1s->csinfo.t_idle == -1)
1981 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1982 }
Christopher Faulet47365272018-10-31 17:40:50 +01001983 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001984
Christopher Fauletcf56b992018-12-11 16:12:31 +01001985 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001986 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001987 goto end;
1988 }
1989
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001990 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001991
Christopher Faulet9768c262018-10-22 09:34:31 +02001992 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001993 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1994 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001995
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001996 if (conn_xprt_read0_pending(conn) && h1s) {
1997 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001998 rcvd = 1;
1999 }
2000
Christopher Faulet51dbc942018-09-13 09:05:15 +02002001 if (!b_data(&h1c->ibuf))
2002 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01002003 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002004 h1c->flags |= H1C_F_IN_FULL;
2005 return rcvd;
2006}
2007
2008
2009/*
2010 * Try to send data if possible
2011 */
2012static int h1_send(struct h1c *h1c)
2013{
2014 struct connection *conn = h1c->conn;
2015 unsigned int flags = 0;
2016 size_t ret;
2017 int sent = 0;
2018
2019 if (conn->flags & CO_FL_ERROR)
2020 return 0;
2021
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002022 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002023 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002024 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002025 return 0;
2026 }
2027
Christopher Faulet51dbc942018-09-13 09:05:15 +02002028 if (!b_data(&h1c->obuf))
2029 goto end;
2030
2031 if (h1c->flags & H1C_F_OUT_FULL)
2032 flags |= CO_SFL_MSG_MORE;
2033
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002034 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002035 if (ret > 0) {
2036 h1c->flags &= ~H1C_F_OUT_FULL;
2037 b_del(&h1c->obuf, ret);
2038 sent = 1;
2039 }
2040
Christopher Faulet145aa472018-12-06 10:56:20 +01002041 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
2042 /* error or output closed, nothing to send, clear the buffer to release it */
2043 b_reset(&h1c->obuf);
2044 }
2045
Christopher Faulet51dbc942018-09-13 09:05:15 +02002046 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002047 if (!(h1c->flags & H1C_F_OUT_FULL))
2048 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002049
Christopher Faulet51dbc942018-09-13 09:05:15 +02002050 /* We're done, no more to send */
2051 if (!b_data(&h1c->obuf)) {
2052 h1_release_buf(h1c, &h1c->obuf);
2053 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01002054 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002055 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002056 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002057 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002058
2059 return sent;
2060}
2061
Christopher Faulet51dbc942018-09-13 09:05:15 +02002062
2063/* callback called on any event by the connection handler.
2064 * It applies changes and returns zero, or < 0 if it wants immediate
2065 * destruction of the connection.
2066 */
2067static int h1_process(struct h1c * h1c)
2068{
2069 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002070 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002071
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002072 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002073 return -1;
2074
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002075 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Olivier Houchard690e0f02019-06-11 16:37:24 +02002076 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) ||
Olivier Houchard60630032019-06-13 17:37:00 +02002077 (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE)))
Christopher Faulet539e0292018-11-19 10:40:09 +01002078 goto end;
2079 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01002080 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002081 }
2082
Christopher Fauletfeb11742018-11-29 15:12:34 +01002083 if (!h1s) {
Christopher Faulet470438c2019-07-11 15:40:25 +02002084 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002085 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01002086 conn_xprt_read0_pending(conn))
2087 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002088 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002089 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002090 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002091 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002092 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002093 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002094 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002095 }
2096
Christopher Fauletfeb11742018-11-29 15:12:34 +01002097 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2098 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2099
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002100 if (conn_xprt_read0_pending(conn))
2101 h1s->flags |= H1S_F_REOS;
2102
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002103 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002104 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002105 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002106 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002107 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002108 h1s->cs->data_cb->wake(h1s->cs);
2109 }
Christopher Faulet47365272018-10-31 17:40:50 +01002110 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002111 if (h1c->task) {
2112 h1c->task->expire = TICK_ETERNITY;
2113 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002114 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 +01002115 ? h1c->shut_timeout
2116 : h1c->timeout));
2117 task_queue(h1c->task);
2118 }
2119 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002120 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002121
2122 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002123 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002124 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002125}
2126
2127static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2128{
2129 struct h1c *h1c = ctx;
2130 int ret = 0;
2131
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002132 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002133 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002134 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002135 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002136 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002137 h1_process(h1c);
2138 return NULL;
2139}
2140
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002141static void h1_reset(struct connection *conn)
2142{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002143 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002144
2145 /* Reset the flags, and let the mux know we're waiting for a connection */
2146 h1c->flags = H1C_F_CS_WAIT_CONN;
2147}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002148
2149static int h1_wake(struct connection *conn)
2150{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002151 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002152 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002153
Christopher Faulet539e0292018-11-19 10:40:09 +01002154 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002155 ret = h1_process(h1c);
2156 if (ret == 0) {
2157 struct h1s *h1s = h1c->h1s;
2158
2159 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2160 ret = h1s->cs->data_cb->wake(h1s->cs);
2161 }
2162 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002163}
2164
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002165/* Connection timeout management. The principle is that if there's no receipt
2166 * nor sending for a certain amount of time, the connection is closed.
2167 */
2168static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2169{
2170 struct h1c *h1c = context;
2171 int expired = tick_is_expired(t->expire, now_ms);
2172
2173 if (!expired && h1c)
2174 return t;
2175
Olivier Houchard3f795f72019-04-17 22:51:06 +02002176 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002177
2178 if (!h1c) {
2179 /* resources were already deleted */
2180 return NULL;
2181 }
2182
2183 h1c->task = NULL;
2184 /* If a stream is still attached to the mux, just set an error and wait
2185 * for the stream's timeout. Otherwise, release the mux. This is only ok
2186 * because same timeouts are used.
2187 */
2188 if (h1c->h1s && h1c->h1s->cs)
2189 h1c->flags |= H1C_F_CS_ERROR;
2190 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002191 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002192 return NULL;
2193}
2194
Christopher Faulet51dbc942018-09-13 09:05:15 +02002195/*******************************************/
2196/* functions below are used by the streams */
2197/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002198
Christopher Faulet51dbc942018-09-13 09:05:15 +02002199/*
2200 * Attach a new stream to a connection
2201 * (Used for outgoing connections)
2202 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002203static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002204{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002205 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002206 struct conn_stream *cs = NULL;
2207 struct h1s *h1s;
2208
2209 if (h1c->flags & H1C_F_CS_ERROR)
2210 goto end;
2211
2212 cs = cs_new(h1c->conn);
2213 if (!cs)
2214 goto end;
2215
Olivier Houchardf502aca2018-12-14 19:42:40 +01002216 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002217 if (h1s == NULL)
2218 goto end;
2219
2220 return cs;
2221 end:
2222 cs_free(cs);
2223 return NULL;
2224}
2225
2226/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2227 * this mux, it's easy as we can only store a single conn_stream.
2228 */
2229static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2230{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002231 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002232 struct h1s *h1s = h1c->h1s;
2233
2234 if (h1s)
2235 return h1s->cs;
2236
2237 return NULL;
2238}
2239
Christopher Faulet73c12072019-04-08 11:23:22 +02002240static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002241{
Christopher Faulet73c12072019-04-08 11:23:22 +02002242 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002243
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002244 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002245 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002246}
2247
2248/*
2249 * Detach the stream from the connection and possibly release the connection.
2250 */
2251static void h1_detach(struct conn_stream *cs)
2252{
2253 struct h1s *h1s = cs->ctx;
2254 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002255 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002256 int has_keepalive;
2257 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002258
2259 cs->ctx = NULL;
2260 if (!h1s)
2261 return;
2262
Olivier Houchardf502aca2018-12-14 19:42:40 +01002263 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002264 h1c = h1s->h1c;
2265 h1s->cs = NULL;
2266
Olivier Houchard8a786902018-12-15 16:05:40 +01002267 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2268 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2269 h1s_destroy(h1s);
2270
2271 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002272 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002273 /* If there are any excess server data in the input buffer,
2274 * release it and close the connection ASAP (some data may
2275 * remain in the output buffer). This happens if a server sends
2276 * invalid responses. So in such case, we don't want to reuse
2277 * the connection
Christopher Faulet39e679b2019-07-19 11:34:08 +02002278 */
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002279 if (b_data(&h1c->ibuf)) {
2280 h1_release_buf(h1c, &h1c->ibuf);
2281 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2282 goto release;
2283 }
Christopher Faulet39e679b2019-07-19 11:34:08 +02002284
Christopher Faulet9400a392018-11-23 23:10:39 +01002285 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002286 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002287 h1c->conn->flags |= CO_FL_PRIVATE;
2288
Olivier Houchard44d59142018-12-13 18:46:22 +01002289 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002290 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002291 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2292 h1c->conn->owner = NULL;
2293 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2294 /* The server doesn't want it, let's kill the connection right away */
2295 h1c->conn->mux->destroy(h1c->conn);
2296 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002297 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002298 return;
2299
2300 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002301 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002302 if (h1c->conn->owner == sess) {
2303 int ret = session_check_idle_conn(sess, h1c->conn);
2304 if (ret == -1)
2305 /* The connection got destroyed, let's leave */
2306 return;
2307 else if (ret == 1) {
2308 /* The connection was added to the server list,
2309 * wake the task so we can subscribe to events
2310 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002311 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002312 return;
2313 }
2314 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002315 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002316 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002317 struct server *srv = objt_server(h1c->conn->target);
2318
2319 if (srv) {
2320 if (h1c->conn->flags & CO_FL_PRIVATE)
2321 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002322 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002323 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2324 else
2325 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2326 }
2327 }
2328 }
2329
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002330 release:
Christopher Faulet9fa93f62019-06-28 17:41:42 +02002331 /* 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 +02002332 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2333 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2334 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2335 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002336 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002337 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002338 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002339 if (h1c->task) {
2340 h1c->task->expire = TICK_ETERNITY;
2341 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002342 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 +01002343 ? h1c->shut_timeout
2344 : h1c->timeout));
2345 task_queue(h1c->task);
2346 }
2347 }
2348 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002349}
2350
2351
2352static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2353{
2354 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002355 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002356
2357 if (!h1s)
2358 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002359 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002360
Christopher Faulet7f366362019-04-08 10:51:20 +02002361 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2362 goto do_shutr;
2363
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002364 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002365 return;
2366
Christopher Faulet7f366362019-04-08 10:51:20 +02002367 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002368 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2369 if (cs->flags & CS_FL_SHR)
2370 return;
2371 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002372 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2373 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002374 if ((cs->conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet7f366362019-04-08 10:51:20 +02002375 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002376}
2377
2378static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2379{
2380 struct h1s *h1s = cs->ctx;
2381 struct h1c *h1c;
2382
2383 if (!h1s)
2384 return;
2385 h1c = h1s->h1c;
2386
Christopher Faulet7f366362019-04-08 10:51:20 +02002387 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2388 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002389
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002390 if ((h1c->flags & H1C_F_UPG_H2C) ||
2391 ((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 +02002392 return;
2393
Christopher Faulet7f366362019-04-08 10:51:20 +02002394 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002395 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2396 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2397 return;
2398
Christopher Faulet666a0c42019-01-08 11:12:04 +01002399 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002400}
2401
Christopher Faulet666a0c42019-01-08 11:12:04 +01002402static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002403{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002404 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002405
Christopher Faulet666a0c42019-01-08 11:12:04 +01002406 conn_xprt_shutw(conn);
2407 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2408 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2409 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002410}
2411
2412/* Called from the upper layer, to unsubscribe to events */
2413static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2414{
2415 struct wait_event *sw;
2416 struct h1s *h1s = cs->ctx;
2417
2418 if (!h1s)
2419 return 0;
2420
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002421 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002422 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002423 BUG_ON(h1s->recv_wait != sw);
2424 sw->events &= ~SUB_RETRY_RECV;
2425 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002426 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002427 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002428 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002429 BUG_ON(h1s->send_wait != sw);
2430 sw->events &= ~SUB_RETRY_SEND;
2431 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002432 }
2433 return 0;
2434}
2435
2436/* Called from the upper layer, to subscribe to events, such as being able to send */
2437static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2438{
2439 struct wait_event *sw;
2440 struct h1s *h1s = cs->ctx;
2441
2442 if (!h1s)
2443 return -1;
2444
2445 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002446 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002447 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002448 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2449 sw->events |= SUB_RETRY_RECV;
2450 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002451 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002452 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002453 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002454 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2455 sw->events |= SUB_RETRY_SEND;
2456 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002457 return 0;
2458 default:
2459 break;
2460 }
2461 return -1;
2462}
2463
2464/* Called from the upper layer, to receive data */
2465static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2466{
2467 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002468 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002469 size_t ret = 0;
2470
Christopher Faulet539e0292018-11-19 10:40:09 +01002471 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002472 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002473
Christopher Faulete18777b2019-04-16 16:46:36 +02002474 if (flags & CO_RFL_BUF_FLUSH) {
2475 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2476
2477 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2478 h1s->flags |= H1S_F_BUF_FLUSH;
2479 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002480 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2481 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002482 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002483 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002484 }
2485 return ret;
2486}
2487
2488
2489/* Called from the upper layer, to send data */
2490static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2491{
2492 struct h1s *h1s = cs->ctx;
2493 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002494 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002495
2496 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002497 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002498
2499 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002500 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2501 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002502
Christopher Fauletc31872f2019-06-04 22:09:36 +02002503 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002504 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002505
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002506 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2507 ret = h1_process_output(h1c, buf, count);
2508 if (!ret)
2509 break;
2510 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002511 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002512 if (!h1_send(h1c))
2513 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002514 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002515
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002516 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002517}
2518
Willy Tarreaue5733232019-05-22 19:24:06 +02002519#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002520/* Send and get, using splicing */
2521static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2522{
2523 struct h1s *h1s = cs->ctx;
2524 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2525 int ret = 0;
2526
Christopher Faulet2c411be2019-11-05 16:24:27 +01002527 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002528 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002529 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2530 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002531 goto end;
2532 }
2533
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002534 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002535 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002536 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002537 }
2538
2539 h1s->flags &= ~H1S_F_BUF_FLUSH;
2540 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002541 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2542 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002543 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002544 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002545 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002546 if (!h1m->curr_len)
2547 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2548 }
2549
Christopher Faulet1be55f92018-10-02 15:59:23 +02002550 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002551 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002552 h1s->flags |= H1S_F_REOS;
Christopher Fauletac4778c2019-11-15 11:14:23 +01002553 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet038ad812019-04-17 11:03:22 +02002554 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002555 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002556}
2557
2558static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2559{
2560 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002561 int ret = 0;
2562
2563 if (b_data(&h1s->h1c->obuf))
2564 goto end;
2565
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002566 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002567 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002568 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002569 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002570 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002571 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002572 return ret;
2573}
2574#endif
2575
Olivier Houchard198d1292019-10-25 16:19:26 +02002576static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2577{
2578 int ret = 0;
2579 switch (mux_ctl) {
2580 case MUX_STATUS:
2581 if (conn->flags & CO_FL_CONNECTED)
2582 ret |= MUX_STATUS_READY;
2583 return ret;
2584 default:
2585 return -1;
2586 }
2587}
2588
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002589/* for debugging with CLI's "show fd" command */
2590static void h1_show_fd(struct buffer *msg, struct connection *conn)
2591{
2592 struct h1c *h1c = conn->ctx;
2593 struct h1s *h1s = h1c->h1s;
2594
Christopher Fauletf376a312019-01-04 15:16:06 +01002595 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2596 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002597 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2598 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2599 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2600 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2601
2602 if (h1s) {
2603 char *method;
2604
2605 if (h1s->meth < HTTP_METH_OTHER)
2606 method = http_known_methods[h1s->meth].ptr;
2607 else
2608 method = "UNKNOWN";
2609 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2610 " .meth=%s status=%d",
2611 h1s, h1s->flags,
2612 h1m_state_str(h1s->req.state),
2613 h1m_state_str(h1s->res.state), method, h1s->status);
2614 if (h1s->cs)
2615 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2616 h1s->cs->flags, h1s->cs->data);
2617 }
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002618}
2619
2620
2621/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2622static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2623{
2624 struct h1_hdr_entry *entry;
2625
2626 /* Be sure there is a non-empty <to> */
2627 if (!strlen(to)) {
2628 memprintf(err, "expect <to>");
2629 return -1;
2630 }
2631
2632 /* Be sure only the case differs between <from> and <to> */
2633 if (strcasecmp(from, to)) {
2634 memprintf(err, "<from> and <to> must not differ execpt the case");
2635 return -1;
2636 }
2637
2638 /* Be sure <from> does not already existsin the tree */
2639 if (ebis_lookup(&hdrs_map.map, from)) {
2640 memprintf(err, "duplicate entry '%s'", from);
2641 return -1;
2642 }
2643
2644 /* Create the entry and insert it in the tree */
2645 entry = malloc(sizeof(*entry));
2646 if (!entry) {
2647 memprintf(err, "out of memory");
2648 return -1;
2649 }
2650
2651 entry->node.key = strdup(from);
2652 entry->name.ptr = strdup(to);
2653 entry->name.len = strlen(to);
2654 if (!entry->node.key || !entry->name.ptr) {
2655 free(entry->node.key);
2656 free(entry->name.ptr);
2657 free(entry);
2658 memprintf(err, "out of memory");
2659 return -1;
2660 }
2661 ebis_insert(&hdrs_map.map, &entry->node);
2662 return 0;
2663}
2664
2665static void h1_hdeaders_case_adjust_deinit()
2666{
2667 struct ebpt_node *node, *next;
2668 struct h1_hdr_entry *entry;
2669
2670 node = ebpt_first(&hdrs_map.map);
2671 while (node) {
2672 next = ebpt_next(node);
2673 ebpt_delete(node);
2674 entry = container_of(node, struct h1_hdr_entry, node);
2675 free(entry->node.key);
2676 free(entry->name.ptr);
2677 free(entry);
2678 node = next;
2679 }
2680 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002681}
2682
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002683static int cfg_h1_headers_case_adjust_postparser()
2684{
2685 FILE *file = NULL;
2686 char *c, *key_beg, *key_end, *value_beg, *value_end;
2687 char *err;
2688 int rc, line = 0, err_code = 0;
2689
2690 if (!hdrs_map.name)
2691 goto end;
2692
2693 file = fopen(hdrs_map.name, "r");
2694 if (!file) {
2695 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2696 hdrs_map.name);
2697 err_code |= ERR_ALERT | ERR_FATAL;
2698 goto end;
2699 }
2700
2701 /* now parse all lines. The file may contain only two header name per
2702 * line, separated by spaces. All heading and trailing spaces will be
2703 * ignored. Lines starting with a # are ignored.
2704 */
2705 while (fgets(trash.area, trash.size, file) != NULL) {
2706 line++;
2707 c = trash.area;
2708
2709 /* strip leading spaces and tabs */
2710 while (*c == ' ' || *c == '\t')
2711 c++;
2712
2713 /* ignore emptu lines, or lines beginning with a dash */
2714 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2715 continue;
2716
2717 /* look for the end of the key */
2718 key_beg = c;
2719 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2720 c++;
2721 key_end = c;
2722
2723 /* strip middle spaces and tabs */
2724 while (*c == ' ' || *c == '\t')
2725 c++;
2726
2727 /* look for the end of the value, it is the end of the line */
2728 value_beg = c;
2729 while (*c && *c != '\n' && *c != '\r')
2730 c++;
2731 value_end = c;
2732
2733 /* trim possibly trailing spaces and tabs */
2734 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2735 value_end--;
2736
2737 /* set final \0 and check entries */
2738 *key_end = '\0';
2739 *value_end = '\0';
2740
2741 err = NULL;
2742 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2743 if (rc < 0) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002744 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2745 hdrs_map.name, err, line);
2746 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Faulet27f20172019-07-30 16:51:42 +02002747 free(err);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002748 goto end;
2749 }
2750 if (rc > 0) {
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002751 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2752 hdrs_map.name, err, line);
2753 err_code |= ERR_WARN;
Christopher Faulet27f20172019-07-30 16:51:42 +02002754 free(err);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002755 }
2756 }
2757
2758 end:
2759 if (file)
2760 fclose(file);
2761 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2762 return err_code;
2763}
2764
2765
2766/* config parser for global "h1-outgoing-header-case-adjust" */
2767static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2768 struct proxy *defpx, const char *file, int line,
2769 char **err)
2770{
2771 if (too_many_args(2, args, err, NULL))
2772 return -1;
2773 if (!*(args[1]) || !*(args[2])) {
2774 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2775 return -1;
2776 }
2777 return add_hdr_case_adjust(args[1], args[2], err);
2778}
2779
2780/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2781static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2782 struct proxy *defpx, const char *file, int line,
2783 char **err)
2784{
2785 if (too_many_args(1, args, err, NULL))
2786 return -1;
2787 if (!*(args[1])) {
2788 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2789 return -1;
2790 }
2791 free(hdrs_map.name);
2792 hdrs_map.name = strdup(args[1]);
2793 return 0;
2794}
2795
2796
2797/* config keyword parsers */
2798static struct cfg_kw_list cfg_kws = {{ }, {
2799 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2800 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2801 { 0, NULL, NULL },
2802 }
2803};
2804
2805INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2806REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2807
2808
Christopher Faulet51dbc942018-09-13 09:05:15 +02002809/****************************************/
2810/* MUX initialization and instanciation */
2811/****************************************/
2812
2813/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002814static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002815 .init = h1_init,
2816 .wake = h1_wake,
2817 .attach = h1_attach,
2818 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002819 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002820 .detach = h1_detach,
2821 .destroy = h1_destroy,
2822 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002823 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002824 .rcv_buf = h1_rcv_buf,
2825 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002826#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002827 .rcv_pipe = h1_rcv_pipe,
2828 .snd_pipe = h1_snd_pipe,
2829#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002830 .subscribe = h1_subscribe,
2831 .unsubscribe = h1_unsubscribe,
2832 .shutr = h1_shutr,
2833 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002834 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002835 .reset = h1_reset,
Olivier Houchard198d1292019-10-25 16:19:26 +02002836 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002837 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002838 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002839};
2840
2841
2842/* this mux registers default HTX proto */
2843static struct mux_proto_list mux_proto_htx =
2844{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2845
Willy Tarreau0108d902018-11-25 19:14:37 +01002846INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2847
Christopher Faulet51dbc942018-09-13 09:05:15 +02002848/*
2849 * Local variables:
2850 * c-indent-level: 8
2851 * c-basic-offset: 8
2852 * End:
2853 */