blob: b3b66f4cbc1e4bf265fb3c2ce7ea7e870699a3d8 [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 Faulet65fc1dc2019-06-25 21:41:02 +02001761 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001762 goto full;
Willy Tarreau4710d202019-01-03 17:39:54 +01001763 h1m->flags |= H1_MF_CHNK;
1764 }
1765
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001766 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001767 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001768
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001769 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1770 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1771 h1_set_req_tunnel_mode(h1s);
1772 }
Christopher Fauletac4778c2019-11-15 11:14:23 +01001773 else if ((h1m->flags & H1_MF_RESP) &&
1774 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001775 /* a successfull reply to a CONNECT or a protocol switching is sent
Christopher Fauletac4778c2019-11-15 11:14:23 +01001776 * to the client. Switch the response to tunnel mode.
1777 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001778 h1_set_res_tunnel_mode(h1s);
1779 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001780 else if ((h1m->flags & H1_MF_RESP) &&
1781 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1782 h1m_init_res(&h1s->res);
1783 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001784 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001785 }
Christopher Faulet33d58b52019-07-01 16:17:30 +02001786 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1787 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001788 else
1789 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001790 break;
1791
Christopher Faulet94b2c762019-05-24 15:28:57 +02001792 case H1_MSG_DATA:
Christopher Fauletaaa25062019-07-04 17:12:12 +02001793 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001794 if (type == HTX_BLK_EOM) {
1795 /* Chunked message without explicit trailers */
1796 if (h1m->flags & H1_MF_CHNK) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001797 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001798 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001799 }
1800 goto done;
1801 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001802 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet52131682019-06-27 17:40:14 +02001803 /* If the message is not chunked, never
1804 * add the last chunk. */
1805 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001806 goto full;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001807 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001808 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001809 else if (type != HTX_BLK_DATA)
1810 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001811 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001812 v.len = vlen;
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001813 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001814 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001815 break;
1816
Christopher Faulet94b2c762019-05-24 15:28:57 +02001817 case H1_MSG_TRAILERS:
1818 if (type == HTX_BLK_EOM)
1819 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001820 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001821 goto error;
1822 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001823 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet52131682019-06-27 17:40:14 +02001824 /* If the message is not chunked, ignore
1825 * trailers. It may happen with H2 messages. */
1826 if (!(h1m->flags & H1_MF_CHNK))
1827 break;
1828
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001829 if (type == HTX_BLK_EOT) {
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001830 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001831 goto full;
Christopher Faulet32188212018-11-20 18:21:43 +01001832 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001833 else { // HTX_BLK_TLR
1834 n = htx_get_blk_name(chn_htx, blk);
1835 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02001836
1837 /* Try to adjust the case of the header name */
1838 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1839 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001840 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001841 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001842 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001843 break;
1844
Christopher Faulet94b2c762019-05-24 15:28:57 +02001845 case H1_MSG_DONE:
1846 if (type != HTX_BLK_EOM)
1847 goto error;
1848 done:
1849 h1m->state = H1_MSG_DONE;
Christopher Fauletac4778c2019-11-15 11:14:23 +01001850 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1851 h1_set_req_tunnel_mode(h1s);
1852 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001853 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1854 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1855 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001856 break;
1857
Christopher Faulet9768c262018-10-22 09:34:31 +02001858 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001859 error:
Christopher Fauletc431df82019-06-26 15:16:28 +02001860 /* Unexpected error during output processing */
1861 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001862 h1s->flags |= errflag;
Christopher Fauletc431df82019-06-26 15:16:28 +02001863 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001864 break;
1865 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001866
1867 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001868 total += vlen;
1869 count -= vlen;
1870 if (sz == vlen)
1871 blk = htx_remove_blk(chn_htx, blk);
1872 else {
1873 htx_cut_data_blk(chn_htx, blk, vlen);
1874 break;
1875 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001876 }
1877
Christopher Faulet9768c262018-10-22 09:34:31 +02001878 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001879 /* when the output buffer is empty, tmp shares the same area so that we
1880 * only have to update pointers and lengths.
1881 */
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001882 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1883 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001884 else
Christopher Faulet65fc1dc2019-06-25 21:41:02 +02001885 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001886
Willy Tarreau3815b222018-12-11 19:50:43 +01001887 htx_to_buf(chn_htx, buf);
1888 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001889 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001890 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001891 end:
1892 return total;
Christopher Faulet85fc6ef2019-10-14 14:17:00 +02001893
1894 full:
1895 h1c->flags |= H1C_F_OUT_FULL;
1896 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001897}
1898
Christopher Faulet51dbc942018-09-13 09:05:15 +02001899/*********************************************************/
1900/* functions below are I/O callbacks from the connection */
1901/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001902static void h1_wake_stream_for_recv(struct h1s *h1s)
1903{
1904 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001905 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001906 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001907 h1s->recv_wait = NULL;
1908 }
1909}
1910static void h1_wake_stream_for_send(struct h1s *h1s)
1911{
1912 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001913 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001914 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001915 h1s->send_wait = NULL;
1916 }
1917}
1918
Christopher Faulet51dbc942018-09-13 09:05:15 +02001919/*
1920 * Attempt to read data, and subscribe if none available
1921 */
1922static int h1_recv(struct h1c *h1c)
1923{
1924 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001925 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001926 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001927 int rcvd = 0;
1928
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001929 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001930 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001931
Olivier Houchard5b06cb32019-08-13 15:21:59 +02001932 if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN)
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001933 return 0;
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001934
Olivier Houchard75159a92018-12-03 18:46:09 +01001935 if (!h1_recv_allowed(h1c)) {
1936 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001937 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001938 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001939
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001940 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1941 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001942 goto end;
1943 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001944
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001945 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001946 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001947 h1_wake_stream_for_recv(h1s);
1948 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001949 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001950 }
1951
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001952 /*
1953 * If we only have a small amount of data, realign it,
1954 * it's probably cheaper than doing 2 recv() calls.
1955 */
1956 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1957 b_slow_realign(&h1c->ibuf, trash.area, 0);
1958
Willy Tarreau45f2b892018-12-05 07:59:27 +01001959 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001960 if (max) {
1961 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001962
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001963 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001964 if (!b_data(&h1c->ibuf)) {
1965 /* try to pre-align the buffer like the rxbufs will be
1966 * to optimize memory copies.
1967 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001968 h1c->ibuf.head = sizeof(struct htx);
1969 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001970 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001971 }
Christopher Faulet47365272018-10-31 17:40:50 +01001972 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001973 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001974 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001975 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001976 if (h1s->csinfo.t_idle == -1)
1977 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1978 }
Christopher Faulet47365272018-10-31 17:40:50 +01001979 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001980
Christopher Fauletcf56b992018-12-11 16:12:31 +01001981 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001982 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001983 goto end;
1984 }
1985
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001986 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001987
Christopher Faulet9768c262018-10-22 09:34:31 +02001988 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001989 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1990 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001991
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001992 if (conn_xprt_read0_pending(conn) && h1s) {
1993 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001994 rcvd = 1;
1995 }
1996
Christopher Faulet51dbc942018-09-13 09:05:15 +02001997 if (!b_data(&h1c->ibuf))
1998 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001999 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002000 h1c->flags |= H1C_F_IN_FULL;
2001 return rcvd;
2002}
2003
2004
2005/*
2006 * Try to send data if possible
2007 */
2008static int h1_send(struct h1c *h1c)
2009{
2010 struct connection *conn = h1c->conn;
2011 unsigned int flags = 0;
2012 size_t ret;
2013 int sent = 0;
2014
2015 if (conn->flags & CO_FL_ERROR)
2016 return 0;
2017
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002018 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002019 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002020 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002021 return 0;
2022 }
2023
Christopher Faulet51dbc942018-09-13 09:05:15 +02002024 if (!b_data(&h1c->obuf))
2025 goto end;
2026
2027 if (h1c->flags & H1C_F_OUT_FULL)
2028 flags |= CO_SFL_MSG_MORE;
2029
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002030 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002031 if (ret > 0) {
2032 h1c->flags &= ~H1C_F_OUT_FULL;
2033 b_del(&h1c->obuf, ret);
2034 sent = 1;
2035 }
2036
Christopher Faulet145aa472018-12-06 10:56:20 +01002037 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
2038 /* error or output closed, nothing to send, clear the buffer to release it */
2039 b_reset(&h1c->obuf);
2040 }
2041
Christopher Faulet51dbc942018-09-13 09:05:15 +02002042 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002043 if (!(h1c->flags & H1C_F_OUT_FULL))
2044 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002045
Christopher Faulet51dbc942018-09-13 09:05:15 +02002046 /* We're done, no more to send */
2047 if (!b_data(&h1c->obuf)) {
2048 h1_release_buf(h1c, &h1c->obuf);
2049 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01002050 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002051 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002052 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002053 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002054
2055 return sent;
2056}
2057
Christopher Faulet51dbc942018-09-13 09:05:15 +02002058
2059/* callback called on any event by the connection handler.
2060 * It applies changes and returns zero, or < 0 if it wants immediate
2061 * destruction of the connection.
2062 */
2063static int h1_process(struct h1c * h1c)
2064{
2065 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002066 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002067
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002068 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002069 return -1;
2070
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002071 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Olivier Houchard690e0f02019-06-11 16:37:24 +02002072 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) ||
Olivier Houchard60630032019-06-13 17:37:00 +02002073 (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE)))
Christopher Faulet539e0292018-11-19 10:40:09 +01002074 goto end;
2075 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01002076 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002077 }
2078
Christopher Fauletfeb11742018-11-29 15:12:34 +01002079 if (!h1s) {
Christopher Faulet470438c2019-07-11 15:40:25 +02002080 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002081 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01002082 conn_xprt_read0_pending(conn))
2083 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002084 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002085 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002086 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002087 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002088 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002089 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002090 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002091 }
2092
Christopher Fauletfeb11742018-11-29 15:12:34 +01002093 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2094 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2095
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002096 if (conn_xprt_read0_pending(conn))
2097 h1s->flags |= H1S_F_REOS;
2098
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002099 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002100 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002101 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002102 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002103 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002104 h1s->cs->data_cb->wake(h1s->cs);
2105 }
Christopher Faulet47365272018-10-31 17:40:50 +01002106 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002107 if (h1c->task) {
2108 h1c->task->expire = TICK_ETERNITY;
2109 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002110 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 +01002111 ? h1c->shut_timeout
2112 : h1c->timeout));
2113 task_queue(h1c->task);
2114 }
2115 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002116 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002117
2118 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002119 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002120 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002121}
2122
2123static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2124{
2125 struct h1c *h1c = ctx;
2126 int ret = 0;
2127
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002128 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002129 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002130 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002131 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002132 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002133 h1_process(h1c);
2134 return NULL;
2135}
2136
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002137static void h1_reset(struct connection *conn)
2138{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002139 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002140
2141 /* Reset the flags, and let the mux know we're waiting for a connection */
2142 h1c->flags = H1C_F_CS_WAIT_CONN;
2143}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002144
2145static int h1_wake(struct connection *conn)
2146{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002147 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002148 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002149
Christopher Faulet539e0292018-11-19 10:40:09 +01002150 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002151 ret = h1_process(h1c);
2152 if (ret == 0) {
2153 struct h1s *h1s = h1c->h1s;
2154
2155 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2156 ret = h1s->cs->data_cb->wake(h1s->cs);
2157 }
2158 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002159}
2160
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002161/* Connection timeout management. The principle is that if there's no receipt
2162 * nor sending for a certain amount of time, the connection is closed.
2163 */
2164static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2165{
2166 struct h1c *h1c = context;
2167 int expired = tick_is_expired(t->expire, now_ms);
2168
2169 if (!expired && h1c)
2170 return t;
2171
Olivier Houchard3f795f72019-04-17 22:51:06 +02002172 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002173
2174 if (!h1c) {
2175 /* resources were already deleted */
2176 return NULL;
2177 }
2178
2179 h1c->task = NULL;
2180 /* If a stream is still attached to the mux, just set an error and wait
2181 * for the stream's timeout. Otherwise, release the mux. This is only ok
2182 * because same timeouts are used.
2183 */
2184 if (h1c->h1s && h1c->h1s->cs)
2185 h1c->flags |= H1C_F_CS_ERROR;
2186 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002187 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002188 return NULL;
2189}
2190
Christopher Faulet51dbc942018-09-13 09:05:15 +02002191/*******************************************/
2192/* functions below are used by the streams */
2193/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002194
Christopher Faulet51dbc942018-09-13 09:05:15 +02002195/*
2196 * Attach a new stream to a connection
2197 * (Used for outgoing connections)
2198 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002199static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002200{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002201 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002202 struct conn_stream *cs = NULL;
2203 struct h1s *h1s;
2204
2205 if (h1c->flags & H1C_F_CS_ERROR)
2206 goto end;
2207
2208 cs = cs_new(h1c->conn);
2209 if (!cs)
2210 goto end;
2211
Olivier Houchardf502aca2018-12-14 19:42:40 +01002212 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002213 if (h1s == NULL)
2214 goto end;
2215
2216 return cs;
2217 end:
2218 cs_free(cs);
2219 return NULL;
2220}
2221
2222/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2223 * this mux, it's easy as we can only store a single conn_stream.
2224 */
2225static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2226{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002227 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002228 struct h1s *h1s = h1c->h1s;
2229
2230 if (h1s)
2231 return h1s->cs;
2232
2233 return NULL;
2234}
2235
Christopher Faulet73c12072019-04-08 11:23:22 +02002236static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002237{
Christopher Faulet73c12072019-04-08 11:23:22 +02002238 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002239
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002240 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002241 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002242}
2243
2244/*
2245 * Detach the stream from the connection and possibly release the connection.
2246 */
2247static void h1_detach(struct conn_stream *cs)
2248{
2249 struct h1s *h1s = cs->ctx;
2250 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002251 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002252 int has_keepalive;
2253 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002254
2255 cs->ctx = NULL;
2256 if (!h1s)
2257 return;
2258
Olivier Houchardf502aca2018-12-14 19:42:40 +01002259 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002260 h1c = h1s->h1c;
2261 h1s->cs = NULL;
2262
Olivier Houchard8a786902018-12-15 16:05:40 +01002263 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2264 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2265 h1s_destroy(h1s);
2266
2267 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002268 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002269 /* If there are any excess server data in the input buffer,
2270 * release it and close the connection ASAP (some data may
2271 * remain in the output buffer). This happens if a server sends
2272 * invalid responses. So in such case, we don't want to reuse
2273 * the connection
Christopher Faulet39e679b2019-07-19 11:34:08 +02002274 */
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002275 if (b_data(&h1c->ibuf)) {
2276 h1_release_buf(h1c, &h1c->ibuf);
2277 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2278 goto release;
2279 }
Christopher Faulet39e679b2019-07-19 11:34:08 +02002280
Christopher Faulet9400a392018-11-23 23:10:39 +01002281 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002282 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002283 h1c->conn->flags |= CO_FL_PRIVATE;
2284
Olivier Houchard44d59142018-12-13 18:46:22 +01002285 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002286 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002287 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2288 h1c->conn->owner = NULL;
2289 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2290 /* The server doesn't want it, let's kill the connection right away */
2291 h1c->conn->mux->destroy(h1c->conn);
2292 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002293 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002294 return;
2295
2296 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002297 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002298 if (h1c->conn->owner == sess) {
2299 int ret = session_check_idle_conn(sess, h1c->conn);
2300 if (ret == -1)
2301 /* The connection got destroyed, let's leave */
2302 return;
2303 else if (ret == 1) {
2304 /* The connection was added to the server list,
2305 * wake the task so we can subscribe to events
2306 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002307 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002308 return;
2309 }
2310 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002311 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002312 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002313 struct server *srv = objt_server(h1c->conn->target);
2314
2315 if (srv) {
2316 if (h1c->conn->flags & CO_FL_PRIVATE)
2317 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002318 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002319 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2320 else
2321 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2322 }
2323 }
2324 }
2325
Christopher Faulet0bf28f82019-07-19 14:51:06 +02002326 release:
Christopher Faulet9fa93f62019-06-28 17:41:42 +02002327 /* 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 +02002328 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2329 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2330 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2331 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002332 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002333 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002334 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002335 if (h1c->task) {
2336 h1c->task->expire = TICK_ETERNITY;
2337 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002338 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 +01002339 ? h1c->shut_timeout
2340 : h1c->timeout));
2341 task_queue(h1c->task);
2342 }
2343 }
2344 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002345}
2346
2347
2348static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2349{
2350 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002351 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002352
2353 if (!h1s)
2354 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002355 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002356
Christopher Faulet7f366362019-04-08 10:51:20 +02002357 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2358 goto do_shutr;
2359
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002360 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002361 return;
2362
Christopher Faulet7f366362019-04-08 10:51:20 +02002363 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002364 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2365 if (cs->flags & CS_FL_SHR)
2366 return;
2367 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002368 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2369 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002370 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 +02002371 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002372}
2373
2374static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2375{
2376 struct h1s *h1s = cs->ctx;
2377 struct h1c *h1c;
2378
2379 if (!h1s)
2380 return;
2381 h1c = h1s->h1c;
2382
Christopher Faulet7f366362019-04-08 10:51:20 +02002383 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2384 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002385
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002386 if ((h1c->flags & H1C_F_UPG_H2C) ||
2387 ((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 +02002388 return;
2389
Christopher Faulet7f366362019-04-08 10:51:20 +02002390 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002391 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2392 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2393 return;
2394
Christopher Faulet666a0c42019-01-08 11:12:04 +01002395 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002396}
2397
Christopher Faulet666a0c42019-01-08 11:12:04 +01002398static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002399{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002400 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002401
Christopher Faulet666a0c42019-01-08 11:12:04 +01002402 conn_xprt_shutw(conn);
2403 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2404 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2405 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002406}
2407
2408/* Called from the upper layer, to unsubscribe to events */
2409static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2410{
2411 struct wait_event *sw;
2412 struct h1s *h1s = cs->ctx;
2413
2414 if (!h1s)
2415 return 0;
2416
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002417 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002418 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002419 BUG_ON(h1s->recv_wait != sw);
2420 sw->events &= ~SUB_RETRY_RECV;
2421 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002422 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002423 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002424 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002425 BUG_ON(h1s->send_wait != sw);
2426 sw->events &= ~SUB_RETRY_SEND;
2427 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002428 }
2429 return 0;
2430}
2431
2432/* Called from the upper layer, to subscribe to events, such as being able to send */
2433static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2434{
2435 struct wait_event *sw;
2436 struct h1s *h1s = cs->ctx;
2437
2438 if (!h1s)
2439 return -1;
2440
2441 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002442 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002443 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002444 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2445 sw->events |= SUB_RETRY_RECV;
2446 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002447 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002448 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002449 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002450 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2451 sw->events |= SUB_RETRY_SEND;
2452 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002453 return 0;
2454 default:
2455 break;
2456 }
2457 return -1;
2458}
2459
2460/* Called from the upper layer, to receive data */
2461static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2462{
2463 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002464 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002465 size_t ret = 0;
2466
Christopher Faulet539e0292018-11-19 10:40:09 +01002467 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002468 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002469
Christopher Faulete18777b2019-04-16 16:46:36 +02002470 if (flags & CO_RFL_BUF_FLUSH) {
2471 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2472
2473 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2474 h1s->flags |= H1S_F_BUF_FLUSH;
2475 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002476 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2477 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002478 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002479 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002480 }
2481 return ret;
2482}
2483
2484
2485/* Called from the upper layer, to send data */
2486static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2487{
2488 struct h1s *h1s = cs->ctx;
2489 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002490 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002491
2492 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002493 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002494
2495 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002496 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2497 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002498
Christopher Fauletc31872f2019-06-04 22:09:36 +02002499 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002500 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002501
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002502 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2503 ret = h1_process_output(h1c, buf, count);
2504 if (!ret)
2505 break;
2506 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002507 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002508 if (!h1_send(h1c))
2509 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002510 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002511
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002512 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002513}
2514
Willy Tarreaue5733232019-05-22 19:24:06 +02002515#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002516/* Send and get, using splicing */
2517static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2518{
2519 struct h1s *h1s = cs->ctx;
2520 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2521 int ret = 0;
2522
Christopher Faulet2c411be2019-11-05 16:24:27 +01002523 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002524 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002525 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2526 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002527 goto end;
2528 }
2529
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002530 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002531 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002532 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002533 }
2534
2535 h1s->flags &= ~H1S_F_BUF_FLUSH;
2536 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002537 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2538 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002539 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002540 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002541 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002542 if (!h1m->curr_len)
2543 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2544 }
2545
Christopher Faulet1be55f92018-10-02 15:59:23 +02002546 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002547 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002548 h1s->flags |= H1S_F_REOS;
Christopher Fauletac4778c2019-11-15 11:14:23 +01002549 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet038ad812019-04-17 11:03:22 +02002550 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002551 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002552}
2553
2554static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2555{
2556 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002557 int ret = 0;
2558
2559 if (b_data(&h1s->h1c->obuf))
2560 goto end;
2561
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002562 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002563 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002564 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002565 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002566 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002567 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002568 return ret;
2569}
2570#endif
2571
Olivier Houchard198d1292019-10-25 16:19:26 +02002572static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2573{
2574 int ret = 0;
2575 switch (mux_ctl) {
2576 case MUX_STATUS:
2577 if (conn->flags & CO_FL_CONNECTED)
2578 ret |= MUX_STATUS_READY;
2579 return ret;
2580 default:
2581 return -1;
2582 }
2583}
2584
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002585/* for debugging with CLI's "show fd" command */
2586static void h1_show_fd(struct buffer *msg, struct connection *conn)
2587{
2588 struct h1c *h1c = conn->ctx;
2589 struct h1s *h1s = h1c->h1s;
2590
Christopher Fauletf376a312019-01-04 15:16:06 +01002591 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2592 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002593 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2594 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2595 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2596 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2597
2598 if (h1s) {
2599 char *method;
2600
2601 if (h1s->meth < HTTP_METH_OTHER)
2602 method = http_known_methods[h1s->meth].ptr;
2603 else
2604 method = "UNKNOWN";
2605 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2606 " .meth=%s status=%d",
2607 h1s, h1s->flags,
2608 h1m_state_str(h1s->req.state),
2609 h1m_state_str(h1s->res.state), method, h1s->status);
2610 if (h1s->cs)
2611 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2612 h1s->cs->flags, h1s->cs->data);
2613 }
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002614}
2615
2616
2617/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2618static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2619{
2620 struct h1_hdr_entry *entry;
2621
2622 /* Be sure there is a non-empty <to> */
2623 if (!strlen(to)) {
2624 memprintf(err, "expect <to>");
2625 return -1;
2626 }
2627
2628 /* Be sure only the case differs between <from> and <to> */
2629 if (strcasecmp(from, to)) {
2630 memprintf(err, "<from> and <to> must not differ execpt the case");
2631 return -1;
2632 }
2633
2634 /* Be sure <from> does not already existsin the tree */
2635 if (ebis_lookup(&hdrs_map.map, from)) {
2636 memprintf(err, "duplicate entry '%s'", from);
2637 return -1;
2638 }
2639
2640 /* Create the entry and insert it in the tree */
2641 entry = malloc(sizeof(*entry));
2642 if (!entry) {
2643 memprintf(err, "out of memory");
2644 return -1;
2645 }
2646
2647 entry->node.key = strdup(from);
2648 entry->name.ptr = strdup(to);
2649 entry->name.len = strlen(to);
2650 if (!entry->node.key || !entry->name.ptr) {
2651 free(entry->node.key);
2652 free(entry->name.ptr);
2653 free(entry);
2654 memprintf(err, "out of memory");
2655 return -1;
2656 }
2657 ebis_insert(&hdrs_map.map, &entry->node);
2658 return 0;
2659}
2660
2661static void h1_hdeaders_case_adjust_deinit()
2662{
2663 struct ebpt_node *node, *next;
2664 struct h1_hdr_entry *entry;
2665
2666 node = ebpt_first(&hdrs_map.map);
2667 while (node) {
2668 next = ebpt_next(node);
2669 ebpt_delete(node);
2670 entry = container_of(node, struct h1_hdr_entry, node);
2671 free(entry->node.key);
2672 free(entry->name.ptr);
2673 free(entry);
2674 node = next;
2675 }
2676 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002677}
2678
Christopher Fauleta99ff4d2019-07-22 16:18:24 +02002679static int cfg_h1_headers_case_adjust_postparser()
2680{
2681 FILE *file = NULL;
2682 char *c, *key_beg, *key_end, *value_beg, *value_end;
2683 char *err;
2684 int rc, line = 0, err_code = 0;
2685
2686 if (!hdrs_map.name)
2687 goto end;
2688
2689 file = fopen(hdrs_map.name, "r");
2690 if (!file) {
2691 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2692 hdrs_map.name);
2693 err_code |= ERR_ALERT | ERR_FATAL;
2694 goto end;
2695 }
2696
2697 /* now parse all lines. The file may contain only two header name per
2698 * line, separated by spaces. All heading and trailing spaces will be
2699 * ignored. Lines starting with a # are ignored.
2700 */
2701 while (fgets(trash.area, trash.size, file) != NULL) {
2702 line++;
2703 c = trash.area;
2704
2705 /* strip leading spaces and tabs */
2706 while (*c == ' ' || *c == '\t')
2707 c++;
2708
2709 /* ignore emptu lines, or lines beginning with a dash */
2710 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2711 continue;
2712
2713 /* look for the end of the key */
2714 key_beg = c;
2715 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2716 c++;
2717 key_end = c;
2718
2719 /* strip middle spaces and tabs */
2720 while (*c == ' ' || *c == '\t')
2721 c++;
2722
2723 /* look for the end of the value, it is the end of the line */
2724 value_beg = c;
2725 while (*c && *c != '\n' && *c != '\r')
2726 c++;
2727 value_end = c;
2728
2729 /* trim possibly trailing spaces and tabs */
2730 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2731 value_end--;
2732
2733 /* set final \0 and check entries */
2734 *key_end = '\0';
2735 *value_end = '\0';
2736
2737 err = NULL;
2738 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2739 if (rc < 0) {
2740 free(err);
2741 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2742 hdrs_map.name, err, line);
2743 err_code |= ERR_ALERT | ERR_FATAL;
2744 goto end;
2745 }
2746 if (rc > 0) {
2747 free(err);
2748 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2749 hdrs_map.name, err, line);
2750 err_code |= ERR_WARN;
2751 }
2752 }
2753
2754 end:
2755 if (file)
2756 fclose(file);
2757 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2758 return err_code;
2759}
2760
2761
2762/* config parser for global "h1-outgoing-header-case-adjust" */
2763static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2764 struct proxy *defpx, const char *file, int line,
2765 char **err)
2766{
2767 if (too_many_args(2, args, err, NULL))
2768 return -1;
2769 if (!*(args[1]) || !*(args[2])) {
2770 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2771 return -1;
2772 }
2773 return add_hdr_case_adjust(args[1], args[2], err);
2774}
2775
2776/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2777static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2778 struct proxy *defpx, const char *file, int line,
2779 char **err)
2780{
2781 if (too_many_args(1, args, err, NULL))
2782 return -1;
2783 if (!*(args[1])) {
2784 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2785 return -1;
2786 }
2787 free(hdrs_map.name);
2788 hdrs_map.name = strdup(args[1]);
2789 return 0;
2790}
2791
2792
2793/* config keyword parsers */
2794static struct cfg_kw_list cfg_kws = {{ }, {
2795 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2796 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2797 { 0, NULL, NULL },
2798 }
2799};
2800
2801INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2802REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2803
2804
Christopher Faulet51dbc942018-09-13 09:05:15 +02002805/****************************************/
2806/* MUX initialization and instanciation */
2807/****************************************/
2808
2809/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002810static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002811 .init = h1_init,
2812 .wake = h1_wake,
2813 .attach = h1_attach,
2814 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002815 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002816 .detach = h1_detach,
2817 .destroy = h1_destroy,
2818 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002819 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002820 .rcv_buf = h1_rcv_buf,
2821 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002822#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002823 .rcv_pipe = h1_rcv_pipe,
2824 .snd_pipe = h1_snd_pipe,
2825#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002826 .subscribe = h1_subscribe,
2827 .unsubscribe = h1_unsubscribe,
2828 .shutr = h1_shutr,
2829 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002830 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002831 .reset = h1_reset,
Olivier Houchard198d1292019-10-25 16:19:26 +02002832 .ctl = h1_ctl,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002833 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002834 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002835};
2836
2837
2838/* this mux registers default HTX proto */
2839static struct mux_proto_list mux_proto_htx =
2840{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2841
Willy Tarreau0108d902018-11-25 19:14:37 +01002842INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2843
Christopher Faulet51dbc942018-09-13 09:05:15 +02002844/*
2845 * Local variables:
2846 * c-indent-level: 8
2847 * c-basic-offset: 8
2848 * End:
2849 */