blob: f4243a2252884048c5d00e11302f915cc2959815 [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 Faulet98fbe952019-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 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +020072/* 0x00001000 .. 0x00002000 unused */
Christopher Fauletada34b62019-05-24 16:36:43 +020073#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020074
75/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020076struct h1c {
77 struct connection *conn;
78 struct proxy *px;
79 uint32_t flags; /* Connection flags: H1C_F_* */
80
81 struct buffer ibuf; /* Input buffer to store data before parsing */
82 struct buffer obuf; /* Output buffer to store data after reformatting */
83
84 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
85 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
86
87 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010088 struct task *task; /* timeout management task */
89 int timeout; /* idle timeout duration in ticks */
90 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020091};
92
93/* H1 stream descriptor */
94struct h1s {
95 struct h1c *h1c;
96 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010097 struct cs_info csinfo; /* CS info, only used for client connections */
98 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020099
Christopher Faulet51dbc942018-09-13 09:05:15 +0200100 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
101 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200102
Olivier Houchardf502aca2018-12-14 19:42:40 +0100103 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200104 struct h1m req;
105 struct h1m res;
106
107 enum http_meth_t meth; /* HTTP resquest method */
108 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200109};
110
Christopher Faulet98fbe952019-07-22 16:18:24 +0200111/* Map of headers used to convert outgoing headers */
112struct h1_hdrs_map {
113 char *name;
114 struct eb_root map;
115};
116
117/* An entry in a headers map */
118struct h1_hdr_entry {
119 struct ist name;
120 struct ebpt_node node;
121};
122
123/* Declare the headers map */
124static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
125
126
Christopher Faulet51dbc942018-09-13 09:05:15 +0200127/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100128DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
129DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200130
Christopher Faulet51dbc942018-09-13 09:05:15 +0200131static int h1_recv(struct h1c *h1c);
132static int h1_send(struct h1c *h1c);
133static int h1_process(struct h1c *h1c);
134static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100135static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100136static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200137
138/*****************************************************/
139/* functions below are for dynamic buffer management */
140/*****************************************************/
141/*
142 * Indicates whether or not the we may call the h1_recv() function to
143 * attempt to receive data into the buffer and/or parse pending data. The
144 * condition is a bit complex due to some API limits for now. The rules are the
145 * following :
146 * - if an error or a shutdown was detected on the connection and the buffer
147 * is empty, we must not attempt to receive
148 * - if the input buffer failed to be allocated, we must not try to receive
149 * and we know there is nothing pending
150 * - if no flag indicates a blocking condition, we may attempt to receive,
151 * regardless of whether the input buffer is full or not, so that only de
152 * receiving part decides whether or not to block. This is needed because
153 * the connection API indeed prevents us from re-enabling receipt that is
154 * already enabled in a polled state, so we must always immediately stop as
155 * soon as the mux can't proceed so as never to hit an end of read with data
156 * pending in the buffers.
157 * - otherwise must may not attempt to receive
158 */
159static inline int h1_recv_allowed(const struct h1c *h1c)
160{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100161 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200162 return 0;
163
Christopher Fauletcf56b992018-12-11 16:12:31 +0100164 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
165 return 0;
166
Christopher Fauletcb55f482018-12-10 11:56:47 +0100167 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200168 return 1;
169
170 return 0;
171}
172
173/*
174 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
175 * flags are used to figure what buffer was requested. It returns 1 if the
176 * allocation succeeds, in which case the connection is woken up, or 0 if it's
177 * impossible to wake up and we prefer to be woken up later.
178 */
179static int h1_buf_available(void *target)
180{
181 struct h1c *h1c = target;
182
183 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
184 h1c->flags &= ~H1C_F_IN_ALLOC;
185 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200186 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200187 return 1;
188 }
189
190 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
191 h1c->flags &= ~H1C_F_OUT_ALLOC;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200192 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200193 return 1;
194 }
195
Christopher Faulet51dbc942018-09-13 09:05:15 +0200196 return 0;
197}
198
199/*
200 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
201 */
202static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
203{
204 struct buffer *buf = NULL;
205
206 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
207 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
208 h1c->buf_wait.target = h1c;
209 h1c->buf_wait.wakeup_cb = h1_buf_available;
210 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
211 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
212 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
213 __conn_xprt_stop_recv(h1c->conn);
214 }
215 return buf;
216}
217
218/*
219 * Release a buffer, if any, and try to wake up entities waiting in the buffer
220 * wait queue.
221 */
222static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
223{
224 if (bptr->size) {
225 b_free(bptr);
226 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
227 }
228}
229
Willy Tarreau00f18a32019-01-26 12:19:01 +0100230/* returns the number of streams in use on a connection to figure if it's
231 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
232 * as the caller will want to know if it was the last one after a detach().
233 */
234static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200235{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100236 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200237
Willy Tarreau00f18a32019-01-26 12:19:01 +0100238 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200239}
240
Willy Tarreau00f18a32019-01-26 12:19:01 +0100241/* returns the number of streams still available on a connection */
242static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100243{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100244 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100245}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200246
Willy Tarreau00f18a32019-01-26 12:19:01 +0100247
Christopher Faulet51dbc942018-09-13 09:05:15 +0200248/*****************************************************************/
249/* functions below are dedicated to the mux setup and management */
250/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200251
252/* returns non-zero if there are input data pending for stream h1s. */
253static inline size_t h1s_data_pending(const struct h1s *h1s)
254{
255 const struct h1m *h1m;
256
257 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
258 if (h1m->state == H1_MSG_DONE)
259 return 0; // data not for this stream (e.g. pipelining)
260
261 return b_data(&h1s->h1c->ibuf);
262}
263
Christopher Faulet47365272018-10-31 17:40:50 +0100264static struct conn_stream *h1s_new_cs(struct h1s *h1s)
265{
266 struct conn_stream *cs;
267
268 cs = cs_new(h1s->h1c->conn);
269 if (!cs)
270 goto err;
271 h1s->cs = cs;
272 cs->ctx = h1s;
273
274 if (h1s->flags & H1S_F_NOT_FIRST)
275 cs->flags |= CS_FL_NOT_FIRST;
276
277 if (stream_create_from_cs(cs) < 0)
278 goto err;
279 return cs;
280
281 err:
282 cs_free(cs);
283 h1s->cs = NULL;
284 return NULL;
285}
286
Olivier Houchardf502aca2018-12-14 19:42:40 +0100287static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200288{
289 struct h1s *h1s;
290
291 h1s = pool_alloc(pool_head_h1s);
292 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100293 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200294
295 h1s->h1c = h1c;
296 h1c->h1s = h1s;
297
Olivier Houchardf502aca2018-12-14 19:42:40 +0100298 h1s->sess = sess;
299
Christopher Faulet51dbc942018-09-13 09:05:15 +0200300 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100301 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200302
303 h1s->recv_wait = NULL;
304 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200305
306 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100307 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200308
Christopher Faulet129817b2018-09-20 16:14:40 +0200309 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100310 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200311
312 h1s->status = 0;
313 h1s->meth = HTTP_METH_OTHER;
314
Christopher Faulet47365272018-10-31 17:40:50 +0100315 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
316 h1s->flags |= H1S_F_NOT_FIRST;
317 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
318
Christopher Faulet129817b2018-09-20 16:14:40 +0200319 if (!conn_is_back(h1c->conn)) {
320 if (h1c->px->options2 & PR_O2_REQBUG_OK)
321 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200322
323 /* For frontend connections we should always have a session */
324 if (!sess)
325 sess = h1c->conn->owner;
326
Dave Pirotte234740f2019-07-10 13:57:38 +0000327 /* Timers for subsequent sessions on the same HTTP 1.x connection
328 * measure from `now`, not from the connection accept time */
329 if (h1s->flags & H1S_F_NOT_FIRST) {
330 h1s->csinfo.create_date = date;
331 h1s->csinfo.tv_create = now;
332 h1s->csinfo.t_handshake = 0;
333 h1s->csinfo.t_idle = -1;
334 }
335 else {
336 h1s->csinfo.create_date = sess->accept_date;
337 h1s->csinfo.tv_create = sess->tv_accept;
338 h1s->csinfo.t_handshake = sess->t_handshake;
339 h1s->csinfo.t_idle = -1;
340 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200341 }
342 else {
343 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
344 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200345
Christopher Fauletfeb11742018-11-29 15:12:34 +0100346 h1s->csinfo.create_date = date;
347 h1s->csinfo.tv_create = now;
348 h1s->csinfo.t_handshake = 0;
349 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200350 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100351
Christopher Faulete9b70722019-04-08 10:46:02 +0200352 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
353 * create a new one.
354 */
355 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200356 cs->ctx = h1s;
357 h1s->cs = cs;
358 }
Christopher Faulet47365272018-10-31 17:40:50 +0100359 else {
360 cs = h1s_new_cs(h1s);
361 if (!cs)
362 goto fail;
363 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200364 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100365
366 fail:
367 pool_free(pool_head_h1s, h1s);
368 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200369}
370
371static void h1s_destroy(struct h1s *h1s)
372{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200373 if (h1s) {
374 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200375
Christopher Fauletf2824e62018-10-01 12:12:37 +0200376 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200377
Christopher Fauletf2824e62018-10-01 12:12:37 +0200378 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100379 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200380 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100381 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200382
Christopher Fauletcb55f482018-12-10 11:56:47 +0100383 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100384 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
385 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
386 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200387 pool_free(pool_head_h1s, h1s);
388 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200389}
390
Christopher Fauletfeb11742018-11-29 15:12:34 +0100391static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
392{
393 struct h1s *h1s = cs->ctx;
394
395 if (h1s && !conn_is_back(cs->conn))
396 return &h1s->csinfo;
397 return NULL;
398}
399
Christopher Faulet51dbc942018-09-13 09:05:15 +0200400/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200401 * Initialize the mux once it's attached. It is expected that conn->ctx points
402 * to the existing conn_stream (for outgoing connections or for incoming onces
403 * during a mux upgrade) or NULL (for incoming ones during the connexion
404 * establishment). <input> is always used as Input buffer and may contain
405 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
406 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200407 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200408static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
409 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200410{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200411 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100412 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200413
414 h1c = pool_alloc(pool_head_h1c);
415 if (!h1c)
416 goto fail_h1c;
417 h1c->conn = conn;
418 h1c->px = proxy;
419
420 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200421 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200422 h1c->obuf = BUF_NULL;
423 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200424 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200425
Christopher Faulet51dbc942018-09-13 09:05:15 +0200426 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200427 h1c->wait_event.tasklet = tasklet_new();
428 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200429 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200430 h1c->wait_event.tasklet->process = h1_io_cb;
431 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100432 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200433
Christopher Faulete9b70722019-04-08 10:46:02 +0200434 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100435 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
436 if (tick_isset(proxy->timeout.serverfin))
437 h1c->shut_timeout = proxy->timeout.serverfin;
438 } else {
439 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
440 if (tick_isset(proxy->timeout.clientfin))
441 h1c->shut_timeout = proxy->timeout.clientfin;
442 }
443 if (tick_isset(h1c->timeout)) {
444 t = task_new(tid_bit);
445 if (!t)
446 goto fail;
447
448 h1c->task = t;
449 t->process = h1_timeout_task;
450 t->context = h1c;
451 t->expire = tick_add(now_ms, h1c->timeout);
452 }
453
Olivier Houchard985234d2019-06-13 17:54:33 +0200454 if (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200455 h1c->flags |= H1C_F_CS_WAIT_CONN;
456
Christopher Fauletf2824e62018-10-01 12:12:37 +0200457 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100458 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200459 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200460
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100461 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200462
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100463
464 if (t)
465 task_queue(t);
466
Christopher Faulet51dbc942018-09-13 09:05:15 +0200467 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200468 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200469
470 /* mux->wake will be called soon to complete the operation */
471 return 0;
472
473 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200474 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200475 if (h1c->wait_event.tasklet)
476 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200477 pool_free(pool_head_h1c, h1c);
478 fail_h1c:
479 return -1;
480}
481
Christopher Faulet73c12072019-04-08 11:23:22 +0200482/* release function. This one should be called to free all resources allocated
483 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200484 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200485static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200486{
Christopher Faulet61840e72019-04-15 09:33:32 +0200487 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200488
Christopher Faulet51dbc942018-09-13 09:05:15 +0200489 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200490 /* The connection must be aattached to this mux to be released */
491 if (h1c->conn && h1c->conn->ctx == h1c)
492 conn = h1c->conn;
493
494 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200495 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200496 /* Make sure we're no longer subscribed to anything */
497 if (h1c->wait_event.events)
498 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
499 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200500 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200501 /* connection successfully upgraded to H2, this
502 * mux was already released */
503 return;
504 }
505 sess_log(conn->owner); /* Log if the upgrade failed */
506 }
507
Olivier Houchard45c44372019-06-11 14:06:23 +0200508
Christopher Faulet51dbc942018-09-13 09:05:15 +0200509 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
510 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
511 LIST_DEL(&h1c->buf_wait.list);
512 LIST_INIT(&h1c->buf_wait.list);
513 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
514 }
515
516 h1_release_buf(h1c, &h1c->ibuf);
517 h1_release_buf(h1c, &h1c->obuf);
518
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100519 if (h1c->task) {
520 h1c->task->context = NULL;
521 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
522 h1c->task = NULL;
523 }
524
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200525 if (h1c->wait_event.tasklet)
526 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200527
Christopher Fauletf2824e62018-10-01 12:12:37 +0200528 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200529 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100530 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200531 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200532 pool_free(pool_head_h1c, h1c);
533 }
534
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200535 if (conn) {
536 conn->mux = NULL;
537 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200538
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200539 conn_stop_tracking(conn);
540 conn_full_close(conn);
541 if (conn->destroy_cb)
542 conn->destroy_cb(conn);
543 conn_free(conn);
544 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200545}
546
547/******************************************************/
548/* functions below are for the H1 protocol processing */
549/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200550/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
551 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200552 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100553static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200554{
Christopher Faulet570d1612018-11-26 11:13:57 +0100555 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200556
Christopher Faulet570d1612018-11-26 11:13:57 +0100557 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200558 (*(p + 5) > '1' ||
559 (*(p + 5) == '1' && *(p + 7) >= '1')))
560 h1m->flags |= H1_MF_VER_11;
561}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200562
Christopher Faulet9768c262018-10-22 09:34:31 +0200563/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
564 * greater or equal to 1.1
565 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100566static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200567{
Christopher Faulet570d1612018-11-26 11:13:57 +0100568 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200569
Christopher Faulet570d1612018-11-26 11:13:57 +0100570 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200571 (*(p + 5) > '1' ||
572 (*(p + 5) == '1' && *(p + 7) >= '1')))
573 h1m->flags |= H1_MF_VER_11;
574}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200575
Christopher Faulet9768c262018-10-22 09:34:31 +0200576/*
577 * Check the validity of the request version. If the version is valid, it
578 * returns 1. Otherwise, it returns 0.
579 */
580static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
581{
582 struct h1c *h1c = h1s->h1c;
583
584 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
585 * exactly one digit "." one digit. This check may be disabled using
586 * option accept-invalid-http-request.
587 */
588 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
589 if (sl.rq.v.len != 8)
590 return 0;
591
592 if (*(sl.rq.v.ptr + 4) != '/' ||
593 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
594 *(sl.rq.v.ptr + 6) != '.' ||
595 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
596 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200597 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200598 else if (!sl.rq.v.len) {
599 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200600
Christopher Faulet9768c262018-10-22 09:34:31 +0200601 /* RFC 1945 allows only GET for HTTP/0.9 requests */
602 if (sl.rq.meth != HTTP_METH_GET)
603 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200604
Christopher Faulet9768c262018-10-22 09:34:31 +0200605 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
606 if (!sl.rq.u.len)
607 return 0;
608
609 /* Add HTTP version */
610 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100611 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200612 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100613
614 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100615 ((*(sl.rq.v.ptr + 5) > '1') ||
616 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100617 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200618 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200619}
620
Christopher Faulet9768c262018-10-22 09:34:31 +0200621/*
622 * Check the validity of the response version. If the version is valid, it
623 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200624 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200625static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200626{
Christopher Faulet9768c262018-10-22 09:34:31 +0200627 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200628
Christopher Faulet9768c262018-10-22 09:34:31 +0200629 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
630 * exactly one digit "." one digit. This check may be disabled using
631 * option accept-invalid-http-request.
632 */
633 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
634 if (sl.st.v.len != 8)
635 return 0;
636
637 if (*(sl.st.v.ptr + 4) != '/' ||
638 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
639 *(sl.st.v.ptr + 6) != '.' ||
640 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
641 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200642 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100643
644 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100645 ((*(sl.st.v.ptr + 5) > '1') ||
646 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100647 h1m->flags |= H1_MF_VER_11;
648
Christopher Faulet9768c262018-10-22 09:34:31 +0200649 return 1;
650}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200651
652/* Deduce the connection mode of the client connection, depending on the
653 * configuration and the H1 message flags. This function is called twice, the
654 * first time when the request is parsed and the second time when the response
655 * is parsed.
656 */
657static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
658{
659 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200660
661 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100662 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200663 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100664 h1s->status == 101) {
665 /* Either we've established an explicit tunnel, or we're
666 * switching the protocol. In both cases, we're very unlikely to
667 * understand the next protocols. We have to switch to tunnel
668 * mode, so that we transfer the request and responses then let
669 * this protocol pass unmodified. When we later implement
670 * specific parsers for such protocols, we'll want to check the
671 * Upgrade header which contains information about that protocol
672 * for responses with status 101 (eg: see RFC2817 about TLS).
673 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200674 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100675 }
676 else if (h1s->flags & H1S_F_WANT_KAL) {
677 /* By default the client is in KAL mode. CLOSE mode mean
678 * it is imposed by the client itself. So only change
679 * KAL mode here. */
680 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
681 /* no length known or explicit close => close */
682 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
683 }
684 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
685 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
686 /* no explict keep-alive and option httpclose => close */
687 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
688 }
689 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200690 }
691 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100692 /* Input direction: first pass */
693 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
694 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200695 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100696 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200697 }
698
699 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
700 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
701 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
702}
703
704/* Deduce the connection mode of the client connection, depending on the
705 * configuration and the H1 message flags. This function is called twice, the
706 * first time when the request is parsed and the second time when the response
707 * is parsed.
708 */
709static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
710{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100711 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100712 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100713 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200714
Christopher Fauletf2824e62018-10-01 12:12:37 +0200715 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100716 /* Input direction: second pass */
717
Christopher Fauletf2824e62018-10-01 12:12:37 +0200718 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100719 h1s->status == 101) {
720 /* Either we've established an explicit tunnel, or we're
721 * switching the protocol. In both cases, we're very unlikely to
722 * understand the next protocols. We have to switch to tunnel
723 * mode, so that we transfer the request and responses then let
724 * this protocol pass unmodified. When we later implement
725 * specific parsers for such protocols, we'll want to check the
726 * Upgrade header which contains information about that protocol
727 * for responses with status 101 (eg: see RFC2817 about TLS).
728 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200729 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100730 }
731 else if (h1s->flags & H1S_F_WANT_KAL) {
732 /* By default the server is in KAL mode. CLOSE mode mean
733 * it is imposed by haproxy itself. So only change KAL
734 * mode here. */
735 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
736 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
737 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
738 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
739 }
740 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200741 }
Christopher Faulet70033782018-12-05 13:50:11 +0100742 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100743 /* Output direction: first pass */
744 if (h1m->flags & H1_MF_CONN_CLO) {
745 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100746 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100747 }
748 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
749 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
750 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
751 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
752 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
753 /* no explicit keep-alive option httpclose/server-close => close */
754 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
755 }
Christopher Faulet70033782018-12-05 13:50:11 +0100756 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200757
758 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
759 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
760 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200761}
762
Christopher Fauletb992af02019-03-28 15:42:24 +0100763static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200764{
765 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200766
767 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
768 * token is found
769 */
770 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200771 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200772
773 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100774 if (!(h1m->flags & H1_MF_VER_11))
775 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200776 }
777 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100778 if (h1m->flags & H1_MF_VER_11)
779 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200780 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200781}
782
Christopher Fauletb992af02019-03-28 15:42:24 +0100783static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200784{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200785 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
786 * token is found
787 */
788 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200789 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200790
791 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100792 if (!(h1m->flags & H1_MF_VER_11) ||
793 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
794 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200795 }
796 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100797 if (h1m->flags & H1_MF_VER_11)
798 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200799 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200800}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200801
Christopher Fauletb992af02019-03-28 15:42:24 +0100802static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200803{
Christopher Fauletb992af02019-03-28 15:42:24 +0100804 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200805 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100806 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200807 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200808}
809
Christopher Fauletb992af02019-03-28 15:42:24 +0100810static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
811{
812 if (!conn_is_back(h1s->h1c->conn))
813 h1_set_cli_conn_mode(h1s, h1m);
814 else
815 h1_set_srv_conn_mode(h1s, h1m);
816
817 if (!(h1m->flags & H1_MF_RESP))
818 h1_update_req_conn_value(h1s, h1m, conn_val);
819 else
820 h1_update_res_conn_value(h1s, h1m, conn_val);
821}
Christopher Faulete44769b2018-11-29 23:01:45 +0100822
Christopher Faulet98fbe952019-07-22 16:18:24 +0200823/* Try to adjust the case of the message header name using the global map
824 * <hdrs_map>.
825 */
826static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
827{
828 struct ebpt_node *node;
829 struct h1_hdr_entry *entry;
830
831 /* No entry in the map, do nothing */
832 if (eb_is_empty(&hdrs_map.map))
833 return;
834
835 /* No conversion fo the request headers */
836 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
837 return;
838
839 /* No conversion fo the response headers */
840 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
841 return;
842
843 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
844 if (!node)
845 return;
846 entry = container_of(node, struct h1_hdr_entry, node);
847 name->ptr = entry->name.ptr;
848 name->len = entry->name.len;
849}
850
Christopher Faulete44769b2018-11-29 23:01:45 +0100851/* Append the description of what is present in error snapshot <es> into <out>.
852 * The description must be small enough to always fit in a buffer. The output
853 * buffer may be the trash so the trash must not be used inside this function.
854 */
855static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
856{
857 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100858 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
859 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
860 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
861 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
862 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
863 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100864}
865/*
866 * Capture a bad request or response and archive it in the proxy's structure.
867 * By default it tries to report the error position as h1m->err_pos. However if
868 * this one is not set, it will then report h1m->next, which is the last known
869 * parsing point. The function is able to deal with wrapping buffers. It always
870 * displays buffers as a contiguous area starting at buf->p. The direction is
871 * determined thanks to the h1m's flags.
872 */
873static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
874 struct h1m *h1m, struct buffer *buf)
875{
876 struct session *sess = h1c->conn->owner;
877 struct proxy *proxy = h1c->px;
878 struct proxy *other_end = sess->fe;
879 union error_snapshot_ctx ctx;
880
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100881 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100882 other_end = si_strm(h1s->cs->data)->be;
883
884 /* http-specific part now */
885 ctx.h1.state = h1m->state;
886 ctx.h1.c_flags = h1c->flags;
887 ctx.h1.s_flags = h1s->flags;
888 ctx.h1.m_flags = h1m->flags;
889 ctx.h1.m_clen = h1m->curr_len;
890 ctx.h1.m_blen = h1m->body_len;
891
892 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
893 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100894 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
895 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100896}
897
Christopher Fauletadb22202018-12-12 10:32:09 +0100898/* Emit the chunksize followed by a CRLF in front of data of the buffer
899 * <buf>. It goes backwards and starts with the byte before the buffer's
900 * head. The caller is responsible for ensuring there is enough room left before
901 * the buffer's head for the string.
902 */
903static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
904{
905 char *beg, *end;
906
907 beg = end = b_head(buf);
908 *--beg = '\n';
909 *--beg = '\r';
910 do {
911 *--beg = hextab[chksz & 0xF];
912 } while (chksz >>= 4);
913 buf->head -= (end - beg);
914 b_add(buf, end - beg);
915}
916
917/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
918 * ensuring there is enough room left in the buffer for the string. */
919static void h1_emit_chunk_crlf(struct buffer *buf)
920{
921 *(b_peek(buf, b_data(buf))) = '\r';
922 *(b_peek(buf, b_data(buf) + 1)) = '\n';
923 b_add(buf, 2);
924}
925
Christopher Faulet129817b2018-09-20 16:14:40 +0200926/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100927 * Switch the request to tunnel mode. This function must only be called for
928 * CONNECT requests. On the client side, the mux is mark as busy on input,
929 * waiting the response.
930 */
931static void h1_set_req_tunnel_mode(struct h1s *h1s)
932{
933 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
934 h1s->req.state = H1_MSG_TUNNEL;
935 if (!conn_is_back(h1s->h1c->conn))
936 h1s->h1c->flags |= H1C_F_IN_BUSY;
937}
938
939/*
940 * Switch the response to tunnel mode. This function must only be called on
941 * successfull replies to CONNECT requests or on protocol switching. On the
942 * server side, if the request is not finished, the mux is mark as busy on
943 * input. Otherwise the request is also switch to tunnel mode.
944 */
945static void h1_set_res_tunnel_mode(struct h1s *h1s)
946{
947 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
948 h1s->res.state = H1_MSG_TUNNEL;
949 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
950 h1s->h1c->flags |= H1C_F_IN_BUSY;
951 else {
952 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
953 h1s->req.state = H1_MSG_TUNNEL;
954 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
955 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200956 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100957 }
958 }
959}
960
Christopher Faulet82f01602019-05-24 16:50:16 +0200961/* Estimate the size of the HTX headers after the parsing, including the EOH. */
962static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
963{
964 size_t sz = 0;
965 int i;
966
967 for (i = 0; hdrs[i].n.len; i++)
968 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
969 sz += sizeof(struct htx_blk) + 1;
970 return sz;
971}
972
Christopher Faulet30db3d72019-05-17 15:35:33 +0200973/* Estimate the size of the HTX request after the parsing. */
974static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
975{
976 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200977
978 /* size of the HTX start-line */
979 sz = sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200980 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200981 return sz;
982}
983
984/* Estimate the size of the HTX response after the parsing. */
985static size_t h1_eval_htx_res_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 */
990 sz = sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.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
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100995/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200996 * Add the EOM in the HTX message and switch the message to the DONE state. It
997 * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This
998 * functions is responsible to update the parser state <h1m>. It also add the
999 * flag CS_FL_EOI on the CS.
1000 */
1001static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max)
1002{
1003 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
1004 return 0;
1005
1006 h1m->state = H1_MSG_DONE;
1007 h1s->cs->flags |= CS_FL_EOI;
1008 return (sizeof(struct htx_blk) + 1);
1009}
1010
1011/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001012 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001013 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1014 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001015 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001016 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001017static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001018 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001019{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001020 struct htx_sl *sl;
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001021 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001022 union h1_sl h1sl;
1023 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001024 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 int ret = 0;
1026
Christopher Faulet30db3d72019-05-17 15:35:33 +02001027 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001028 goto end;
1029
Christopher Faulet129817b2018-09-20 16:14:40 +02001030 /* Realing input buffer if necessary */
1031 if (b_head(buf) + b_data(buf) > b_wrap(buf))
1032 b_slow_realign(buf, trash.area, 0);
1033
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001034 if (!(h1m->flags & H1_MF_RESP)) {
1035 /* Try to match H2 preface before parsing the request headers. */
1036 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
1037 if (ret > 0)
1038 goto h2c_upgrade;
1039 }
1040
Christopher Faulet30db3d72019-05-17 15:35:33 +02001041 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001042 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +02001043 if (ret <= 0) {
1044 /* Incomplete or invalid message. If the buffer is full, it's an
1045 * error because headers are too large to be handled by the
1046 * parser. */
Christopher Faulete5438b72019-06-26 14:56:27 +02001047 if (ret < 0 || (!ret && !buf_room_for_htx_data(buf)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001048 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001049 goto end;
1050 }
1051
1052 /* messages headers fully parsed, do some checks to prepare the body
1053 * parsing.
1054 */
1055
Christopher Faulet9768c262018-10-22 09:34:31 +02001056 /* Save the request's method or the response's status, check if the body
1057 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001058 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001059 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001060
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001061 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001062 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001063
1064 if (h1s->meth == HTTP_METH_CONNECT) {
1065 /* Switch CONNECT requests to tunnel mode */
1066 h1_set_req_tunnel_mode(h1s);
1067 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001068
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001069 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1070 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001071 h1m->err_state = h1m->state;
1072 goto vsn_error;
1073 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001074 }
1075 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001076 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001077
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001078 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1079 h1s->status == 101) {
1080 /* Switch successfull replies to CONNECT requests and
1081 * protocol switching to tunnel mode. */
1082 h1_set_res_tunnel_mode(h1s);
1083 }
1084 else if ((h1s->meth == HTTP_METH_HEAD) ||
1085 (h1s->status >= 100 && h1s->status < 200) ||
1086 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001087 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001088 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1089 h1m->flags |= H1_MF_XFER_LEN;
1090 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001091 }
1092 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001093 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001094 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001095 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001096 else {
1097 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001098 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001099 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001100
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001101 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1102 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001103 h1m->err_state = h1m->state;
1104 goto vsn_error;
1105 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001106 }
1107
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001108 /* Set HTX start-line flags */
1109 if (h1m->flags & H1_MF_VER_11)
1110 flags |= HTX_SL_F_VER_11;
1111 if (h1m->flags & H1_MF_XFER_ENC)
1112 flags |= HTX_SL_F_XFER_ENC;
1113 if (h1m->flags & H1_MF_XFER_LEN) {
1114 flags |= HTX_SL_F_XFER_LEN;
1115 if (h1m->flags & H1_MF_CHNK)
1116 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001117 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001118 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001119 if (h1m->body_len == 0)
1120 flags |= HTX_SL_F_BODYLESS;
1121 }
1122 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001123 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001124 }
1125
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001126 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001127 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001128 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1129 if (htx_is_empty(htx))
1130 goto error;
1131 h1m_init_req(h1m);
1132 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1133 ret = 0;
1134 goto end;
1135 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001136
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001137 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1138 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001139 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001140 sl->info.req.meth = h1s->meth;
Christopher Faulet42993a82019-06-14 10:31:25 +02001141
1142 /* Check if the uri contains an explicit scheme and if it is
1143 * "http" or "https". */
1144 if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') {
1145 sl->flags |= HTX_SL_F_HAS_SCHM;
1146 if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h')
1147 sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
1148 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001149 }
1150 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001151 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1152 if (htx_is_empty(htx))
1153 goto error;
1154 h1m_init_res(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 flags |= HTX_SL_F_IS_RESP;
1161 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1162 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001163 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001164 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001165 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001166
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001167 /* Set bytes used in the HTX mesage for the headers now */
1168 sl->hdrs_bytes = htx_used_space(htx) - used;
1169
Christopher Fauletb992af02019-03-28 15:42:24 +01001170 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001171
1172 /* If body length cannot be determined, set htx->extra to
1173 * ULLONG_MAX. This value is impossible in other cases.
1174 */
1175 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001176 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001177 end:
1178 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001179
1180 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001181 h1m->err_state = h1m->state;
1182 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001183 vsn_error:
1184 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001185 h1s->cs->flags |= CS_FL_EOI;
1186 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001187 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001188 ret = 0;
1189 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001190
1191 h2c_upgrade:
1192 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001193 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001194 htx->flags |= HTX_FL_UPGRADE;
1195 ret = 0;
1196 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001197}
1198
1199/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001200 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1201 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001202 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001203 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001204 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001205static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001206 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001207 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001208{
1209 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001210 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001211
Christopher Faulet129817b2018-09-20 16:14:40 +02001212 if (h1m->flags & H1_MF_XFER_LEN) {
1213 if (h1m->flags & H1_MF_CLEN) {
1214 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001215 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001216 if ((uint64_t)ret > h1m->curr_len)
1217 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001218 if (ret > b_contig_data(buf, *ofs))
1219 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001220
Christopher Faulet9768c262018-10-22 09:34:31 +02001221 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001222 /* very often with large files we'll face the following
1223 * situation :
1224 * - htx is empty and points to <htxbuf>
1225 * - ret == buf->data
1226 * - buf->head == sizeof(struct htx)
1227 * => we can swap the buffers and place an htx header into
1228 * the target buffer instead
1229 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001230 int32_t try = ret;
1231
Willy Tarreau78f548f2018-12-05 10:02:39 +01001232 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1233 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1234 void *raw_area = buf->area;
1235 void *htx_area = htxbuf->area;
1236 struct htx_blk *blk;
1237
1238 buf->area = htx_area;
1239 htxbuf->area = raw_area;
1240 htx = (struct htx *)htxbuf->area;
1241 htx->size = htxbuf->size - sizeof(*htx);
1242 htx_reset(htx);
1243 b_set_data(htxbuf, b_size(htxbuf));
1244
1245 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1246 blk->info += ret;
1247 /* nothing else to do, the old buffer now contains an
1248 * empty pre-initialized HTX header
1249 */
1250 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001251 else {
1252 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1253 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001254 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001255 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001256 *ofs += ret;
1257 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001258 if (ret < try)
1259 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 }
1261
1262 if (!h1m->curr_len) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001263 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001264 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001265 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001266 }
1267 else if (h1m->flags & H1_MF_CHNK) {
1268 new_chunk:
1269 /* te:chunked : parse chunks */
1270 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001271 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001272 if (ret <= 0)
1273 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001274 h1m->state = H1_MSG_CHUNK_SIZE;
1275
Christopher Fauletf2824e62018-10-01 12:12:37 +02001276 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001277 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001278 }
1279
1280 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1281 unsigned int chksz;
1282
Christopher Faulet30db3d72019-05-17 15:35:33 +02001283 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001284 if (ret <= 0)
1285 goto end;
Christopher Faulet54b5e212019-06-04 10:08:28 +02001286 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
Christopher Faulet9768c262018-10-22 09:34:31 +02001287
Christopher Faulet129817b2018-09-20 16:14:40 +02001288 h1m->curr_len = chksz;
1289 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001290 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001291 total += ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001292
1293 if (!h1m->curr_len)
1294 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 }
1296
1297 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001298 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001299 if ((uint64_t)ret > h1m->curr_len)
1300 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001301 if (ret > b_contig_data(buf, *ofs))
1302 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001303
Christopher Faulet9768c262018-10-22 09:34:31 +02001304 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001305 int32_t try = ret;
1306
1307 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001308 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001309 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001310 *ofs += ret;
1311 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001312 if (ret < try)
1313 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001314 }
1315 if (!h1m->curr_len) {
1316 h1m->state = H1_MSG_CHUNK_CRLF;
1317 goto new_chunk;
1318 }
1319 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001320 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001321 }
1322 else {
1323 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1324 * is no body. Switch the message in DONE state
1325 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001326 if (!h1_process_eom(h1s, h1m, htx, max))
Christopher Faulet9768c262018-10-22 09:34:31 +02001327 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001328 }
1329 }
1330 else {
1331 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001332 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001333 if (ret > b_contig_data(buf, *ofs))
1334 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001335
Christopher Faulet9768c262018-10-22 09:34:31 +02001336 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001337 int32_t try = ret;
1338
1339 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001340
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001341 *ofs += ret;
1342 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001343 if (ret < try)
1344 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001345 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001346 }
1347
1348 end:
1349 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001350 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001351 h1s->cs->flags |= CS_FL_EOI;
1352 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001353 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001354 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001355 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001356 return 0;
1357 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001358 /* update htx->extra, only when the body length is known */
1359 if (h1m->flags & H1_MF_XFER_LEN)
1360 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001361 return total;
1362}
1363
1364/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001365 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1366 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1367 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1368 * responsible to update the parser state <h1m>.
1369 */
1370static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1371 struct buffer *buf, size_t *ofs, size_t max)
1372{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02001373 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001374 struct h1m tlr_h1m;
1375 int ret = 0;
1376
1377 if (!max || !b_data(buf))
1378 goto end;
1379
1380 /* Realing input buffer if necessary */
1381 if (b_peek(buf, *ofs) > b_tail(buf))
1382 b_slow_realign(buf, trash.area, 0);
1383
1384 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
1385 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
1386 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
1387 if (ret <= 0) {
1388 /* Incomplete or invalid trailers. If the buffer is full, it's
1389 * an error because traliers are too large to be handled by the
1390 * parser. */
1391 if (ret < 0 || (!ret && !buf_room_for_htx_data(buf)))
1392 goto error;
1393 goto end;
1394 }
1395
1396 /* messages trailers fully parsed. */
1397 if (h1_eval_htx_hdrs_size(hdrs) > max) {
1398 if (htx_is_empty(htx))
1399 goto error;
1400 ret = 0;
1401 goto end;
1402 }
1403
1404 if (!htx_add_all_trailers(htx, hdrs))
1405 goto error;
1406
1407 *ofs += ret;
1408 h1s->flags |= H1S_F_HAVE_I_TLR;
1409 end:
1410 return ret;
1411
1412 error:
1413 h1m->err_state = h1m->state;
1414 h1m->err_pos = h1m->next;
1415 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
1416 h1s->cs->flags |= CS_FL_EOI;
1417 htx->flags |= HTX_FL_PARSING_ERROR;
1418 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1419 ret = 0;
1420 goto end;
1421}
1422
1423/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001424 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001425 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1426 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001427 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001428static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001429{
Christopher Faulet539e0292018-11-19 10:40:09 +01001430 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001431 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001432 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001433 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001434 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001435 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001436
Christopher Faulet539e0292018-11-19 10:40:09 +01001437 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001438
Christopher Fauletf2824e62018-10-01 12:12:37 +02001439 if (!conn_is_back(h1c->conn)) {
1440 h1m = &h1s->req;
1441 errflag = H1S_F_REQ_ERROR;
1442 }
1443 else {
1444 h1m = &h1s->res;
1445 errflag = H1S_F_RES_ERROR;
1446 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001447
Christopher Faulet74027762019-02-26 14:45:05 +01001448 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001449 if (h1s->flags & errflag)
1450 goto end;
1451
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001452 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001453 size_t used = htx_used_space(htx);
1454
Christopher Faulet129817b2018-09-20 16:14:40 +02001455 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001456 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001457 if (!ret)
1458 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001459 if ((h1m->flags & H1_MF_RESP) &&
1460 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1461 h1m_init_res(&h1s->res);
1462 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1463 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001464 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001465 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001466 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001467 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001468 if (!ret)
1469 break;
1470 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001471 else if (h1m->state == H1_MSG_TRAILERS) {
1472 if (!(h1s->flags & H1S_F_HAVE_I_TLR)) {
1473 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1474 if (!ret)
1475 break;
1476 }
1477 if (!h1_process_eom(h1s, h1m, htx, count))
1478 break;
1479 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001480 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001481 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1482 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001483 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001484 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001485 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001486 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001487 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001488 if (!ret)
1489 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001490 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001491 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001492 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001493 break;
1494 }
1495
Christopher Faulet30db3d72019-05-17 15:35:33 +02001496 count -= htx_used_space(htx) - used;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001497 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001498
Christopher Faulet47365272018-10-31 17:40:50 +01001499 if (h1s->flags & errflag)
1500 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001501
Christopher Faulet539e0292018-11-19 10:40:09 +01001502 b_del(&h1c->ibuf, total);
1503
1504 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001505 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001506 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001507 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001508 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001509 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet47365272018-10-31 17:40:50 +01001510 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001511
Christopher Fauletcf56b992018-12-11 16:12:31 +01001512 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1513
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001514 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001515 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001516 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001517 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001518
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001519 if ((h1s->flags & H1S_F_REOS) && (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001520 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001521 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001522 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001523 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001524
Christopher Faulet30db3d72019-05-17 15:35:33 +02001525 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001526
1527 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001528 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001529 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001530 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001531}
1532
Christopher Faulet129817b2018-09-20 16:14:40 +02001533/*
1534 * Process outgoing data. It parses data and transfer them from the channel buffer into
1535 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1536 * 0 if it couldn't proceed.
1537 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001538static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1539{
Christopher Faulet129817b2018-09-20 16:14:40 +02001540 struct h1s *h1s = h1c->h1s;
1541 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001542 struct htx *chn_htx;
1543 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001544 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001545 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001546 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001547
Christopher Faulet47365272018-10-31 17:40:50 +01001548 if (!count)
1549 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001550
Christopher Faulet94b2c762019-05-24 15:28:57 +02001551 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001552 if (htx_is_empty(chn_htx))
1553 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001554
Christopher Faulet51dbc942018-09-13 09:05:15 +02001555 if (!h1_get_buf(h1c, &h1c->obuf)) {
1556 h1c->flags |= H1C_F_OUT_ALLOC;
1557 goto end;
1558 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001559
Christopher Fauletf2824e62018-10-01 12:12:37 +02001560 if (!conn_is_back(h1c->conn)) {
1561 h1m = &h1s->res;
1562 errflag = H1S_F_RES_ERROR;
1563 }
1564 else {
1565 h1m = &h1s->req;
1566 errflag = H1S_F_REQ_ERROR;
1567 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001568
Christopher Faulet0e54d542019-07-04 21:22:34 +02001569 if (h1s->flags & errflag)
1570 goto end;
1571
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001572 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001573 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001574
Willy Tarreau3815b222018-12-11 19:50:43 +01001575 /* Perform some optimizations to reduce the number of buffer copies.
1576 * First, if the mux's buffer is empty and the htx area contains
1577 * exactly one data block of the same size as the requested count,
1578 * then it's possible to simply swap the caller's buffer with the
1579 * mux's output buffer and adjust offsets and length to match the
1580 * entire DATA HTX block in the middle. In this case we perform a
1581 * true zero-copy operation from end-to-end. This is the situation
1582 * that happens all the time with large files. Second, if this is not
1583 * possible, but the mux's output buffer is empty, we still have an
1584 * opportunity to avoid the copy to the intermediary buffer, by making
1585 * the intermediary buffer's area point to the output buffer's area.
1586 * In this case we want to skip the HTX header to make sure that copies
1587 * remain aligned and that this operation remains possible all the
1588 * time. This goes for headers, data blocks and any data extracted from
1589 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001590 */
1591 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001592 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001593 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001594 htx_get_blk_value(chn_htx, blk).len == count) {
1595 void *old_area = h1c->obuf.area;
1596
1597 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001598 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001599 h1c->obuf.data = count;
1600
1601 buf->area = old_area;
1602 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001603
1604 /* The message is chunked. We need to emit the chunk
1605 * size. We have at least the size of the struct htx to
1606 * write the chunk envelope. It should be enough.
1607 */
1608 if (h1m->flags & H1_MF_CHNK) {
1609 h1_emit_chunk_size(&h1c->obuf, count);
1610 h1_emit_chunk_crlf(&h1c->obuf);
1611 }
1612
Willy Tarreau3815b222018-12-11 19:50:43 +01001613 total += count;
1614 goto out;
1615 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001616 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001617 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001618 else
1619 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001620
Christopher Fauletc2518a52019-06-25 21:41:02 +02001621 tmp.data = 0;
1622 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001623 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001624 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001625 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001626 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001627 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001628 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001629
Christopher Fauletb2e84162018-12-06 11:39:49 +01001630 vlen = sz;
1631 if (vlen > count) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001632 if (type != HTX_BLK_DATA)
Christopher Fauletb2e84162018-12-06 11:39:49 +01001633 goto copy;
1634 vlen = count;
1635 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001636
Christopher Faulet94b2c762019-05-24 15:28:57 +02001637 if (type == HTX_BLK_UNUSED)
1638 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001639
Christopher Faulet94b2c762019-05-24 15:28:57 +02001640 switch (h1m->state) {
1641 case H1_MSG_RQBEFORE:
1642 if (type != HTX_BLK_REQ_SL)
1643 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001645 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001646 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001647 if (!htx_reqline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001648 goto copy;
1649 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001650 if (sl->flags & HTX_SL_F_BODYLESS)
1651 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001652 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001653 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001654
Christopher Faulet94b2c762019-05-24 15:28:57 +02001655 case H1_MSG_RPBEFORE:
1656 if (type != HTX_BLK_RES_SL)
1657 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001658 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001659 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001660 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001661 if (!htx_stline_to_h1(sl, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001662 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001663 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001664 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001665 if (sl->info.res.status < 200 &&
1666 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001667 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001668 h1m->state = H1_MSG_HDR_FIRST;
1669 break;
1670
Christopher Faulet94b2c762019-05-24 15:28:57 +02001671 case H1_MSG_HDR_FIRST:
1672 case H1_MSG_HDR_NAME:
1673 case H1_MSG_HDR_L2_LWS:
1674 if (type == HTX_BLK_EOH)
1675 goto last_lf;
1676 if (type != HTX_BLK_HDR)
1677 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001678 h1m->state = H1_MSG_HDR_NAME;
1679 n = htx_get_blk_name(chn_htx, blk);
1680 v = htx_get_blk_value(chn_htx, blk);
1681
1682 if (isteqi(n, ist("transfer-encoding")))
1683 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001684 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001685 /* Only skip C-L header with invalid value. */
1686 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001687 goto skip_hdr;
1688 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001689 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001690 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001691 if (!v.len)
1692 goto skip_hdr;
1693 }
1694
Christopher Faulet98fbe952019-07-22 16:18:24 +02001695 /* Try to adjust the case of the header name */
1696 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1697 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001698 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001699 goto copy;
1700 skip_hdr:
1701 h1m->state = H1_MSG_HDR_L2_LWS;
1702 break;
1703
Christopher Faulet94b2c762019-05-24 15:28:57 +02001704 case H1_MSG_LAST_LF:
1705 if (type != HTX_BLK_EOH)
1706 goto error;
1707 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001708 h1m->state = H1_MSG_LAST_LF;
1709 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001710 /* There is no "Connection:" header and
1711 * it the conn_mode must be
1712 * processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001713 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001714 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001715 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001716 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001717 /* Try to adjust the case of the header name */
1718 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1719 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001720 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001721 goto copy;
1722 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001723 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001724 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001725
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001726 if ((h1s->meth != HTTP_METH_CONNECT &&
1727 (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 +01001728 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1729 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1730 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1731 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1732 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001733 /* chunking needed but header not seen */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001734 if (!chunk_memcat(&tmp, "transfer-encoding: chunked\r\n", 28))
Willy Tarreau4710d202019-01-03 17:39:54 +01001735 goto copy;
1736 h1m->flags |= H1_MF_CHNK;
1737 }
1738
Christopher Fauletc2518a52019-06-25 21:41:02 +02001739 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet9768c262018-10-22 09:34:31 +02001740 goto copy;
1741
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001742 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1743 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1744 h1_set_req_tunnel_mode(h1s);
1745 }
1746 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1747 /* a successfull reply to a CONNECT or a protocol switching is sent
1748 * to the client . Switch the response to tunnel mode. */
1749 h1_set_res_tunnel_mode(h1s);
1750 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001751 else if ((h1m->flags & H1_MF_RESP) &&
1752 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1753 h1m_init_res(&h1s->res);
1754 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001755 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001756 }
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001757 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD)
1758 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001759 else
1760 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001761 break;
1762
Christopher Faulet94b2c762019-05-24 15:28:57 +02001763 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001764 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001765 if (type == HTX_BLK_EOM) {
1766 /* Chunked message without explicit trailers */
1767 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001768 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001769 goto copy;
1770 }
1771 goto done;
1772 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001773 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001774 /* If the message is not chunked, never
1775 * add the last chunk. */
1776 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Faulet94b2c762019-05-24 15:28:57 +02001777 goto copy;
Christopher Faulet94b2c762019-05-24 15:28:57 +02001778 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001779 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001780 else if (type != HTX_BLK_DATA)
1781 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001782 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001783 v.len = vlen;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001784 if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001785 goto copy;
1786 break;
1787
Christopher Faulet94b2c762019-05-24 15:28:57 +02001788 case H1_MSG_TRAILERS:
1789 if (type == HTX_BLK_EOM)
1790 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001791 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001792 goto error;
1793 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001794 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001795 /* If the message is not chunked, ignore
1796 * trailers. It may happen with H2 messages. */
1797 if (!(h1m->flags & H1_MF_CHNK))
1798 break;
1799
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001800 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001801 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Faulet32188212018-11-20 18:21:43 +01001802 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001803 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001804 else { // HTX_BLK_TLR
1805 n = htx_get_blk_name(chn_htx, blk);
1806 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001807
1808 /* Try to adjust the case of the header name */
1809 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1810 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Fauletc2518a52019-06-25 21:41:02 +02001811 if (!htx_hdr_to_h1(n, v, &tmp))
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001812 goto copy;
1813 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001814 break;
1815
Christopher Faulet94b2c762019-05-24 15:28:57 +02001816 case H1_MSG_DONE:
1817 if (type != HTX_BLK_EOM)
1818 goto error;
1819 done:
1820 h1m->state = H1_MSG_DONE;
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001821 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1822 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1823 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1824 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001825 break;
1826
Christopher Faulet9768c262018-10-22 09:34:31 +02001827 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001828 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001829 /* Unexpected error during output processing */
1830 chn_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001831 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001832 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001833 break;
1834 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001835
1836 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001837 total += vlen;
1838 count -= vlen;
1839 if (sz == vlen)
1840 blk = htx_remove_blk(chn_htx, blk);
1841 else {
1842 htx_cut_data_blk(chn_htx, blk, vlen);
1843 break;
1844 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001845 }
1846
Christopher Faulet9768c262018-10-22 09:34:31 +02001847 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001848 /* when the output buffer is empty, tmp shares the same area so that we
1849 * only have to update pointers and lengths.
1850 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001851 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1852 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001853 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001854 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001855
Willy Tarreau3815b222018-12-11 19:50:43 +01001856 htx_to_buf(chn_htx, buf);
1857 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001858 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001859 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001860 end:
1861 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001862}
1863
Christopher Faulet51dbc942018-09-13 09:05:15 +02001864/*********************************************************/
1865/* functions below are I/O callbacks from the connection */
1866/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001867static void h1_wake_stream_for_recv(struct h1s *h1s)
1868{
1869 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001870 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001871 tasklet_wakeup(h1s->recv_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001872 h1s->recv_wait = NULL;
1873 }
1874}
1875static void h1_wake_stream_for_send(struct h1s *h1s)
1876{
1877 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001878 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001879 tasklet_wakeup(h1s->send_wait->tasklet);
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001880 h1s->send_wait = NULL;
1881 }
1882}
1883
Christopher Faulet51dbc942018-09-13 09:05:15 +02001884/*
1885 * Attempt to read data, and subscribe if none available
1886 */
1887static int h1_recv(struct h1c *h1c)
1888{
1889 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001890 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001891 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001892 int rcvd = 0;
1893
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001894 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001895 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001896
Olivier Houchard92d093d2019-06-11 16:36:33 +02001897 if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001898 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1899 return 0;
1900 }
1901
Olivier Houchard75159a92018-12-03 18:46:09 +01001902 if (!h1_recv_allowed(h1c)) {
1903 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001904 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001905 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001906
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001907 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1908 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001909 goto end;
1910 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001911
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001912 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001913 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001914 h1_wake_stream_for_recv(h1s);
1915 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001916 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001917 }
1918
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001919 /*
1920 * If we only have a small amount of data, realign it,
1921 * it's probably cheaper than doing 2 recv() calls.
1922 */
1923 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1924 b_slow_realign(&h1c->ibuf, trash.area, 0);
1925
Willy Tarreau45f2b892018-12-05 07:59:27 +01001926 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001927 if (max) {
1928 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001929
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001930 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001931 if (!b_data(&h1c->ibuf)) {
1932 /* try to pre-align the buffer like the rxbufs will be
1933 * to optimize memory copies.
1934 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001935 h1c->ibuf.head = sizeof(struct htx);
1936 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001937 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001938 }
Christopher Faulet47365272018-10-31 17:40:50 +01001939 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001940 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001941 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001942 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001943 if (h1s->csinfo.t_idle == -1)
1944 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1945 }
Christopher Faulet47365272018-10-31 17:40:50 +01001946 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001947
Christopher Fauletcf56b992018-12-11 16:12:31 +01001948 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001949 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001950 goto end;
1951 }
1952
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001953 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001954
Christopher Faulet9768c262018-10-22 09:34:31 +02001955 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001956 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1957 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001958
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001959 if (conn_xprt_read0_pending(conn) && h1s) {
1960 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001961 rcvd = 1;
1962 }
1963
Christopher Faulet51dbc942018-09-13 09:05:15 +02001964 if (!b_data(&h1c->ibuf))
1965 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001966 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001967 h1c->flags |= H1C_F_IN_FULL;
1968 return rcvd;
1969}
1970
1971
1972/*
1973 * Try to send data if possible
1974 */
1975static int h1_send(struct h1c *h1c)
1976{
1977 struct connection *conn = h1c->conn;
1978 unsigned int flags = 0;
1979 size_t ret;
1980 int sent = 0;
1981
1982 if (conn->flags & CO_FL_ERROR)
1983 return 0;
1984
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001985 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001986 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001987 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001988 return 0;
1989 }
1990
Christopher Faulet51dbc942018-09-13 09:05:15 +02001991 if (!b_data(&h1c->obuf))
1992 goto end;
1993
1994 if (h1c->flags & H1C_F_OUT_FULL)
1995 flags |= CO_SFL_MSG_MORE;
1996
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001997 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001998 if (ret > 0) {
1999 h1c->flags &= ~H1C_F_OUT_FULL;
2000 b_del(&h1c->obuf, ret);
2001 sent = 1;
2002 }
2003
Christopher Faulet145aa472018-12-06 10:56:20 +01002004 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
2005 /* error or output closed, nothing to send, clear the buffer to release it */
2006 b_reset(&h1c->obuf);
2007 }
2008
Christopher Faulet51dbc942018-09-13 09:05:15 +02002009 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002010 if (!(h1c->flags & H1C_F_OUT_FULL))
2011 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002012
Christopher Faulet51dbc942018-09-13 09:05:15 +02002013 /* We're done, no more to send */
2014 if (!b_data(&h1c->obuf)) {
2015 h1_release_buf(h1c, &h1c->obuf);
2016 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01002017 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002018 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002019 else 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 Faulet51dbc942018-09-13 09:05:15 +02002021
2022 return sent;
2023}
2024
Christopher Faulet51dbc942018-09-13 09:05:15 +02002025
2026/* callback called on any event by the connection handler.
2027 * It applies changes and returns zero, or < 0 if it wants immediate
2028 * destruction of the connection.
2029 */
2030static int h1_process(struct h1c * h1c)
2031{
2032 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002033 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002034
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002035 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002036 return -1;
2037
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002038 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Olivier Houchard690e0f02019-06-11 16:37:24 +02002039 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) ||
Olivier Houchard60630032019-06-13 17:37:00 +02002040 (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE)))
Christopher Faulet539e0292018-11-19 10:40:09 +01002041 goto end;
2042 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01002043 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002044 }
2045
Christopher Fauletfeb11742018-11-29 15:12:34 +01002046 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002047 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002048 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01002049 conn_xprt_read0_pending(conn))
2050 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002051 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002052 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002053 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002054 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002055 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002056 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002057 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002058 }
2059
Christopher Fauletfeb11742018-11-29 15:12:34 +01002060 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2061 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2062
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002063 if (conn_xprt_read0_pending(conn))
2064 h1s->flags |= H1S_F_REOS;
2065
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002066 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002067 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002068 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002069 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002070 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01002071 h1s->cs->data_cb->wake(h1s->cs);
2072 }
Christopher Faulet47365272018-10-31 17:40:50 +01002073 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002074 if (h1c->task) {
2075 h1c->task->expire = TICK_ETERNITY;
2076 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002077 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 +01002078 ? h1c->shut_timeout
2079 : h1c->timeout));
2080 task_queue(h1c->task);
2081 }
2082 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002083 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002084
2085 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002086 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01002087 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002088}
2089
2090static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2091{
2092 struct h1c *h1c = ctx;
2093 int ret = 0;
2094
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002095 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002096 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002097 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002098 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002099 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002100 h1_process(h1c);
2101 return NULL;
2102}
2103
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002104static void h1_reset(struct connection *conn)
2105{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002106 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002107
2108 /* Reset the flags, and let the mux know we're waiting for a connection */
2109 h1c->flags = H1C_F_CS_WAIT_CONN;
2110}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002111
2112static int h1_wake(struct connection *conn)
2113{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002114 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002115 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002116
Christopher Faulet539e0292018-11-19 10:40:09 +01002117 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002118 ret = h1_process(h1c);
2119 if (ret == 0) {
2120 struct h1s *h1s = h1c->h1s;
2121
2122 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2123 ret = h1s->cs->data_cb->wake(h1s->cs);
2124 }
2125 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002126}
2127
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002128/* Connection timeout management. The principle is that if there's no receipt
2129 * nor sending for a certain amount of time, the connection is closed.
2130 */
2131static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2132{
2133 struct h1c *h1c = context;
2134 int expired = tick_is_expired(t->expire, now_ms);
2135
2136 if (!expired && h1c)
2137 return t;
2138
Olivier Houchard3f795f72019-04-17 22:51:06 +02002139 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002140
2141 if (!h1c) {
2142 /* resources were already deleted */
2143 return NULL;
2144 }
2145
2146 h1c->task = NULL;
2147 /* If a stream is still attached to the mux, just set an error and wait
2148 * for the stream's timeout. Otherwise, release the mux. This is only ok
2149 * because same timeouts are used.
2150 */
2151 if (h1c->h1s && h1c->h1s->cs)
2152 h1c->flags |= H1C_F_CS_ERROR;
2153 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002154 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002155 return NULL;
2156}
2157
Christopher Faulet51dbc942018-09-13 09:05:15 +02002158/*******************************************/
2159/* functions below are used by the streams */
2160/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002161
Christopher Faulet51dbc942018-09-13 09:05:15 +02002162/*
2163 * Attach a new stream to a connection
2164 * (Used for outgoing connections)
2165 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002166static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002167{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002168 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002169 struct conn_stream *cs = NULL;
2170 struct h1s *h1s;
2171
2172 if (h1c->flags & H1C_F_CS_ERROR)
2173 goto end;
2174
2175 cs = cs_new(h1c->conn);
2176 if (!cs)
2177 goto end;
2178
Olivier Houchardf502aca2018-12-14 19:42:40 +01002179 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002180 if (h1s == NULL)
2181 goto end;
2182
2183 return cs;
2184 end:
2185 cs_free(cs);
2186 return NULL;
2187}
2188
2189/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2190 * this mux, it's easy as we can only store a single conn_stream.
2191 */
2192static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2193{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002194 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002195 struct h1s *h1s = h1c->h1s;
2196
2197 if (h1s)
2198 return h1s->cs;
2199
2200 return NULL;
2201}
2202
Christopher Faulet73c12072019-04-08 11:23:22 +02002203static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002204{
Christopher Faulet73c12072019-04-08 11:23:22 +02002205 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002206
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002207 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002208 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002209}
2210
2211/*
2212 * Detach the stream from the connection and possibly release the connection.
2213 */
2214static void h1_detach(struct conn_stream *cs)
2215{
2216 struct h1s *h1s = cs->ctx;
2217 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002218 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002219 int has_keepalive;
2220 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002221
2222 cs->ctx = NULL;
2223 if (!h1s)
2224 return;
2225
Olivier Houchardf502aca2018-12-14 19:42:40 +01002226 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002227 h1c = h1s->h1c;
2228 h1s->cs = NULL;
2229
Olivier Houchard8a786902018-12-15 16:05:40 +01002230 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2231 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2232 h1s_destroy(h1s);
2233
2234 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002235 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002236 /* If there are any excess server data in the input buffer,
2237 * release it and close the connection ASAP (some data may
2238 * remain in the output buffer). This happens if a server sends
2239 * invalid responses. So in such case, we don't want to reuse
2240 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002241 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002242 if (b_data(&h1c->ibuf)) {
2243 h1_release_buf(h1c, &h1c->ibuf);
2244 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2245 goto release;
2246 }
Christopher Faulet03627242019-07-19 11:34:08 +02002247
Christopher Faulet9400a392018-11-23 23:10:39 +01002248 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002249 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002250 h1c->conn->flags |= CO_FL_PRIVATE;
2251
Olivier Houchard44d59142018-12-13 18:46:22 +01002252 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002253 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002254 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2255 h1c->conn->owner = NULL;
2256 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2257 /* The server doesn't want it, let's kill the connection right away */
2258 h1c->conn->mux->destroy(h1c->conn);
2259 else
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002260 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houchard351411f2018-12-27 17:20:54 +01002261 return;
2262
2263 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002264 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002265 if (h1c->conn->owner == sess) {
2266 int ret = session_check_idle_conn(sess, h1c->conn);
2267 if (ret == -1)
2268 /* The connection got destroyed, let's leave */
2269 return;
2270 else if (ret == 1) {
2271 /* The connection was added to the server list,
2272 * wake the task so we can subscribe to events
2273 */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002274 tasklet_wakeup(h1c->wait_event.tasklet);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002275 return;
2276 }
2277 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002278 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002279 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002280 struct server *srv = objt_server(h1c->conn->target);
2281
2282 if (srv) {
2283 if (h1c->conn->flags & CO_FL_PRIVATE)
2284 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002285 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002286 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2287 else
2288 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2289 }
2290 }
2291 }
2292
Christopher Fauletf1204b82019-07-19 14:51:06 +02002293 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002294 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet37243bc2019-07-11 15:40:25 +02002295 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2296 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2297 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
2298 !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002299 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002300 else {
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002301 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002302 if (h1c->task) {
2303 h1c->task->expire = TICK_ETERNITY;
2304 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002305 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 +01002306 ? h1c->shut_timeout
2307 : h1c->timeout));
2308 task_queue(h1c->task);
2309 }
2310 }
2311 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002312}
2313
2314
2315static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2316{
2317 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002318 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002319
2320 if (!h1s)
2321 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002322 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002323
Christopher Faulet7f366362019-04-08 10:51:20 +02002324 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2325 goto do_shutr;
2326
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002327 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002328 return;
2329
Christopher Faulet7f366362019-04-08 10:51:20 +02002330 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002331 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2332 if (cs->flags & CS_FL_SHR)
2333 return;
2334 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002335 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2336 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002337 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 +02002338 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002339}
2340
2341static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2342{
2343 struct h1s *h1s = cs->ctx;
2344 struct h1c *h1c;
2345
2346 if (!h1s)
2347 return;
2348 h1c = h1s->h1c;
2349
Christopher Faulet7f366362019-04-08 10:51:20 +02002350 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2351 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002352
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002353 if ((h1c->flags & H1C_F_UPG_H2C) ||
2354 ((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 +02002355 return;
2356
Christopher Faulet7f366362019-04-08 10:51:20 +02002357 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002358 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2359 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2360 return;
2361
Christopher Faulet666a0c42019-01-08 11:12:04 +01002362 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002363}
2364
Christopher Faulet666a0c42019-01-08 11:12:04 +01002365static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002366{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002367 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002368
Christopher Faulet666a0c42019-01-08 11:12:04 +01002369 conn_xprt_shutw(conn);
2370 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2371 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2372 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002373}
2374
2375/* Called from the upper layer, to unsubscribe to events */
2376static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2377{
2378 struct wait_event *sw;
2379 struct h1s *h1s = cs->ctx;
2380
2381 if (!h1s)
2382 return 0;
2383
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002384 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002385 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002386 BUG_ON(h1s->recv_wait != sw);
2387 sw->events &= ~SUB_RETRY_RECV;
2388 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002389 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002390 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002391 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002392 BUG_ON(h1s->send_wait != sw);
2393 sw->events &= ~SUB_RETRY_SEND;
2394 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002395 }
2396 return 0;
2397}
2398
2399/* Called from the upper layer, to subscribe to events, such as being able to send */
2400static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2401{
2402 struct wait_event *sw;
2403 struct h1s *h1s = cs->ctx;
2404
2405 if (!h1s)
2406 return -1;
2407
2408 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002409 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002410 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002411 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2412 sw->events |= SUB_RETRY_RECV;
2413 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002414 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002415 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002416 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002417 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2418 sw->events |= SUB_RETRY_SEND;
2419 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002420 return 0;
2421 default:
2422 break;
2423 }
2424 return -1;
2425}
2426
2427/* Called from the upper layer, to receive data */
2428static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2429{
2430 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002431 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002432 size_t ret = 0;
2433
Christopher Faulet539e0292018-11-19 10:40:09 +01002434 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002435 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002436
Christopher Faulete18777b2019-04-16 16:46:36 +02002437 if (flags & CO_RFL_BUF_FLUSH) {
2438 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2439
2440 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2441 h1s->flags |= H1S_F_BUF_FLUSH;
2442 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002443 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2444 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002445 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002446 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002447 }
2448 return ret;
2449}
2450
2451
2452/* Called from the upper layer, to send data */
2453static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2454{
2455 struct h1s *h1s = cs->ctx;
2456 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002457 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002458
2459 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002460 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002461
2462 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002463 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2464 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002465
Christopher Fauletc31872f2019-06-04 22:09:36 +02002466 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002467 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002468
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002469 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2470 ret = h1_process_output(h1c, buf, count);
2471 if (!ret)
2472 break;
2473 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002474 count -= ret;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002475 if (!h1_send(h1c))
2476 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002477 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002478
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002479 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002480}
2481
Willy Tarreaue5733232019-05-22 19:24:06 +02002482#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002483/* Send and get, using splicing */
2484static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2485{
2486 struct h1s *h1s = cs->ctx;
2487 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2488 int ret = 0;
2489
Christopher Faulet1146f972019-05-29 14:35:24 +02002490 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002491 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002492 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2493 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002494 goto end;
2495 }
2496
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002497 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002498 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002499 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002500 }
2501
2502 h1s->flags &= ~H1S_F_BUF_FLUSH;
2503 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002504 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2505 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002506 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002507 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002508 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002509 if (!h1m->curr_len)
2510 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2511 }
2512
Christopher Faulet1be55f92018-10-02 15:59:23 +02002513 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002514 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002515 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002516 if (!pipe->data)
2517 cs->flags |= CS_FL_EOS;
2518 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002519 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002520}
2521
2522static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2523{
2524 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002525 int ret = 0;
2526
2527 if (b_data(&h1s->h1c->obuf))
2528 goto end;
2529
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002530 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002531 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002532 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002533 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002534 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002535 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002536 return ret;
2537}
2538#endif
2539
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002540/* for debugging with CLI's "show fd" command */
2541static void h1_show_fd(struct buffer *msg, struct connection *conn)
2542{
2543 struct h1c *h1c = conn->ctx;
2544 struct h1s *h1s = h1c->h1s;
2545
Christopher Fauletf376a312019-01-04 15:16:06 +01002546 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2547 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002548 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2549 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2550 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2551 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2552
2553 if (h1s) {
2554 char *method;
2555
2556 if (h1s->meth < HTTP_METH_OTHER)
2557 method = http_known_methods[h1s->meth].ptr;
2558 else
2559 method = "UNKNOWN";
2560 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2561 " .meth=%s status=%d",
2562 h1s, h1s->flags,
2563 h1m_state_str(h1s->req.state),
2564 h1m_state_str(h1s->res.state), method, h1s->status);
2565 if (h1s->cs)
2566 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2567 h1s->cs->flags, h1s->cs->data);
2568 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002569}
2570
2571
2572/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2573static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2574{
2575 struct h1_hdr_entry *entry;
2576
2577 /* Be sure there is a non-empty <to> */
2578 if (!strlen(to)) {
2579 memprintf(err, "expect <to>");
2580 return -1;
2581 }
2582
2583 /* Be sure only the case differs between <from> and <to> */
2584 if (strcasecmp(from, to)) {
2585 memprintf(err, "<from> and <to> must not differ execpt the case");
2586 return -1;
2587 }
2588
2589 /* Be sure <from> does not already existsin the tree */
2590 if (ebis_lookup(&hdrs_map.map, from)) {
2591 memprintf(err, "duplicate entry '%s'", from);
2592 return -1;
2593 }
2594
2595 /* Create the entry and insert it in the tree */
2596 entry = malloc(sizeof(*entry));
2597 if (!entry) {
2598 memprintf(err, "out of memory");
2599 return -1;
2600 }
2601
2602 entry->node.key = strdup(from);
2603 entry->name.ptr = strdup(to);
2604 entry->name.len = strlen(to);
2605 if (!entry->node.key || !entry->name.ptr) {
2606 free(entry->node.key);
2607 free(entry->name.ptr);
2608 free(entry);
2609 memprintf(err, "out of memory");
2610 return -1;
2611 }
2612 ebis_insert(&hdrs_map.map, &entry->node);
2613 return 0;
2614}
2615
2616static void h1_hdeaders_case_adjust_deinit()
2617{
2618 struct ebpt_node *node, *next;
2619 struct h1_hdr_entry *entry;
2620
2621 node = ebpt_first(&hdrs_map.map);
2622 while (node) {
2623 next = ebpt_next(node);
2624 ebpt_delete(node);
2625 entry = container_of(node, struct h1_hdr_entry, node);
2626 free(entry->node.key);
2627 free(entry->name.ptr);
2628 free(entry);
2629 node = next;
2630 }
2631 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002632}
2633
Christopher Faulet98fbe952019-07-22 16:18:24 +02002634static int cfg_h1_headers_case_adjust_postparser()
2635{
2636 FILE *file = NULL;
2637 char *c, *key_beg, *key_end, *value_beg, *value_end;
2638 char *err;
2639 int rc, line = 0, err_code = 0;
2640
2641 if (!hdrs_map.name)
2642 goto end;
2643
2644 file = fopen(hdrs_map.name, "r");
2645 if (!file) {
2646 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
2647 hdrs_map.name);
2648 err_code |= ERR_ALERT | ERR_FATAL;
2649 goto end;
2650 }
2651
2652 /* now parse all lines. The file may contain only two header name per
2653 * line, separated by spaces. All heading and trailing spaces will be
2654 * ignored. Lines starting with a # are ignored.
2655 */
2656 while (fgets(trash.area, trash.size, file) != NULL) {
2657 line++;
2658 c = trash.area;
2659
2660 /* strip leading spaces and tabs */
2661 while (*c == ' ' || *c == '\t')
2662 c++;
2663
2664 /* ignore emptu lines, or lines beginning with a dash */
2665 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
2666 continue;
2667
2668 /* look for the end of the key */
2669 key_beg = c;
2670 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2671 c++;
2672 key_end = c;
2673
2674 /* strip middle spaces and tabs */
2675 while (*c == ' ' || *c == '\t')
2676 c++;
2677
2678 /* look for the end of the value, it is the end of the line */
2679 value_beg = c;
2680 while (*c && *c != '\n' && *c != '\r')
2681 c++;
2682 value_end = c;
2683
2684 /* trim possibly trailing spaces and tabs */
2685 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2686 value_end--;
2687
2688 /* set final \0 and check entries */
2689 *key_end = '\0';
2690 *value_end = '\0';
2691
2692 err = NULL;
2693 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
2694 if (rc < 0) {
2695 free(err);
2696 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2697 hdrs_map.name, err, line);
2698 err_code |= ERR_ALERT | ERR_FATAL;
2699 goto end;
2700 }
2701 if (rc > 0) {
2702 free(err);
2703 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
2704 hdrs_map.name, err, line);
2705 err_code |= ERR_WARN;
2706 }
2707 }
2708
2709 end:
2710 if (file)
2711 fclose(file);
2712 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
2713 return err_code;
2714}
2715
2716
2717/* config parser for global "h1-outgoing-header-case-adjust" */
2718static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
2719 struct proxy *defpx, const char *file, int line,
2720 char **err)
2721{
2722 if (too_many_args(2, args, err, NULL))
2723 return -1;
2724 if (!*(args[1]) || !*(args[2])) {
2725 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
2726 return -1;
2727 }
2728 return add_hdr_case_adjust(args[1], args[2], err);
2729}
2730
2731/* config parser for global "h1-outgoing-headers-case-adjust-file" */
2732static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
2733 struct proxy *defpx, const char *file, int line,
2734 char **err)
2735{
2736 if (too_many_args(1, args, err, NULL))
2737 return -1;
2738 if (!*(args[1])) {
2739 memprintf(err, "'%s' expects <file> as argument.", args[0]);
2740 return -1;
2741 }
2742 free(hdrs_map.name);
2743 hdrs_map.name = strdup(args[1]);
2744 return 0;
2745}
2746
2747
2748/* config keyword parsers */
2749static struct cfg_kw_list cfg_kws = {{ }, {
2750 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
2751 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
2752 { 0, NULL, NULL },
2753 }
2754};
2755
2756INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2757REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
2758
2759
Christopher Faulet51dbc942018-09-13 09:05:15 +02002760/****************************************/
2761/* MUX initialization and instanciation */
2762/****************************************/
2763
2764/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002765static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002766 .init = h1_init,
2767 .wake = h1_wake,
2768 .attach = h1_attach,
2769 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002770 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002771 .detach = h1_detach,
2772 .destroy = h1_destroy,
2773 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002774 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002775 .rcv_buf = h1_rcv_buf,
2776 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002777#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002778 .rcv_pipe = h1_rcv_pipe,
2779 .snd_pipe = h1_snd_pipe,
2780#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002781 .subscribe = h1_subscribe,
2782 .unsubscribe = h1_unsubscribe,
2783 .shutr = h1_shutr,
2784 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002785 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002786 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002787 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002788 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002789};
2790
2791
2792/* this mux registers default HTX proto */
2793static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02002794{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02002795
Willy Tarreau0108d902018-11-25 19:14:37 +01002796INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2797
Christopher Faulet51dbc942018-09-13 09:05:15 +02002798/*
2799 * Local variables:
2800 * c-indent-level: 8
2801 * c-basic-offset: 8
2802 * End:
2803 */