blob: e89dfd3e22c82f65b89b705501a7d26a58e3cc15 [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 Faulet1be55f92018-10-02 15:59:23 +020019#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020020#include <types/proxy.h>
21#include <types/session.h>
22
Christopher Faulet51dbc942018-09-13 09:05:15 +020023#include <proto/connection.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020024#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020025#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010026#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020027#include <proto/stream.h>
28#include <proto/stream_interface.h>
29
30/*
31 * H1 Connection flags (32 bits)
32 */
33#define H1C_F_NONE 0x00000000
34
35/* Flags indicating why writing output data are blocked */
36#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
37#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
38/* 0x00000004 - 0x00000008 unused */
39
40/* Flags indicating why reading input data are blocked. */
41#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
42#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010043#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010044/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020045
46#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
47#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010048#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020049#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020050
Christopher Fauletf2824e62018-10-01 12:12:37 +020051#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 +020052#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020053
Christopher Faulet51dbc942018-09-13 09:05:15 +020054/*
55 * H1 Stream flags (32 bits)
56 */
Christopher Faulet129817b2018-09-20 16:14:40 +020057#define H1S_F_NONE 0x00000000
58#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020059#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
60#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020061#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020062#define H1S_F_WANT_KAL 0x00000010
63#define H1S_F_WANT_TUN 0x00000020
64#define H1S_F_WANT_CLO 0x00000040
65#define H1S_F_WANT_MSK 0x00000070
66#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010067#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010068#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010069#define H1S_F_HAVE_I_EOD 0x00000400 /* Set during input process to know the last empty chunk was processed */
70#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
71#define H1S_F_HAVE_O_EOD 0x00001000 /* Set during output process to know the last empty chunk was processed */
72#define H1S_F_HAVE_O_TLR 0x00002000 /* Set during output process to know the trailers were processed */
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
111/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100112DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
113DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200114
Christopher Faulet51dbc942018-09-13 09:05:15 +0200115static int h1_recv(struct h1c *h1c);
116static int h1_send(struct h1c *h1c);
117static int h1_process(struct h1c *h1c);
118static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100119static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100120static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200121
122/*****************************************************/
123/* functions below are for dynamic buffer management */
124/*****************************************************/
125/*
126 * Indicates whether or not the we may call the h1_recv() function to
127 * attempt to receive data into the buffer and/or parse pending data. The
128 * condition is a bit complex due to some API limits for now. The rules are the
129 * following :
130 * - if an error or a shutdown was detected on the connection and the buffer
131 * is empty, we must not attempt to receive
132 * - if the input buffer failed to be allocated, we must not try to receive
133 * and we know there is nothing pending
134 * - if no flag indicates a blocking condition, we may attempt to receive,
135 * regardless of whether the input buffer is full or not, so that only de
136 * receiving part decides whether or not to block. This is needed because
137 * the connection API indeed prevents us from re-enabling receipt that is
138 * already enabled in a polled state, so we must always immediately stop as
139 * soon as the mux can't proceed so as never to hit an end of read with data
140 * pending in the buffers.
141 * - otherwise must may not attempt to receive
142 */
143static inline int h1_recv_allowed(const struct h1c *h1c)
144{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100145 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200146 return 0;
147
Christopher Fauletcf56b992018-12-11 16:12:31 +0100148 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
149 return 0;
150
Christopher Fauletcb55f482018-12-10 11:56:47 +0100151 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200152 return 1;
153
154 return 0;
155}
156
157/*
158 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
159 * flags are used to figure what buffer was requested. It returns 1 if the
160 * allocation succeeds, in which case the connection is woken up, or 0 if it's
161 * impossible to wake up and we prefer to be woken up later.
162 */
163static int h1_buf_available(void *target)
164{
165 struct h1c *h1c = target;
166
167 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
168 h1c->flags &= ~H1C_F_IN_ALLOC;
169 if (h1_recv_allowed(h1c))
170 tasklet_wakeup(h1c->wait_event.task);
171 return 1;
172 }
173
174 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
175 h1c->flags &= ~H1C_F_OUT_ALLOC;
176 tasklet_wakeup(h1c->wait_event.task);
177 return 1;
178 }
179
Christopher Faulet51dbc942018-09-13 09:05:15 +0200180 return 0;
181}
182
183/*
184 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
185 */
186static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
187{
188 struct buffer *buf = NULL;
189
190 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
191 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
192 h1c->buf_wait.target = h1c;
193 h1c->buf_wait.wakeup_cb = h1_buf_available;
194 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
195 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
196 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
197 __conn_xprt_stop_recv(h1c->conn);
198 }
199 return buf;
200}
201
202/*
203 * Release a buffer, if any, and try to wake up entities waiting in the buffer
204 * wait queue.
205 */
206static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
207{
208 if (bptr->size) {
209 b_free(bptr);
210 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
211 }
212}
213
Willy Tarreau00f18a32019-01-26 12:19:01 +0100214/* returns the number of streams in use on a connection to figure if it's
215 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
216 * as the caller will want to know if it was the last one after a detach().
217 */
218static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200219{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100220 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200221
Willy Tarreau00f18a32019-01-26 12:19:01 +0100222 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200223}
224
Willy Tarreau00f18a32019-01-26 12:19:01 +0100225/* returns the number of streams still available on a connection */
226static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100227{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100228 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100229}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200230
Willy Tarreau00f18a32019-01-26 12:19:01 +0100231
Christopher Faulet51dbc942018-09-13 09:05:15 +0200232/*****************************************************************/
233/* functions below are dedicated to the mux setup and management */
234/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200235
236/* returns non-zero if there are input data pending for stream h1s. */
237static inline size_t h1s_data_pending(const struct h1s *h1s)
238{
239 const struct h1m *h1m;
240
241 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
242 if (h1m->state == H1_MSG_DONE)
243 return 0; // data not for this stream (e.g. pipelining)
244
245 return b_data(&h1s->h1c->ibuf);
246}
247
Christopher Faulet47365272018-10-31 17:40:50 +0100248static struct conn_stream *h1s_new_cs(struct h1s *h1s)
249{
250 struct conn_stream *cs;
251
252 cs = cs_new(h1s->h1c->conn);
253 if (!cs)
254 goto err;
255 h1s->cs = cs;
256 cs->ctx = h1s;
257
258 if (h1s->flags & H1S_F_NOT_FIRST)
259 cs->flags |= CS_FL_NOT_FIRST;
260
261 if (stream_create_from_cs(cs) < 0)
262 goto err;
263 return cs;
264
265 err:
266 cs_free(cs);
267 h1s->cs = NULL;
268 return NULL;
269}
270
Olivier Houchardf502aca2018-12-14 19:42:40 +0100271static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200272{
273 struct h1s *h1s;
274
275 h1s = pool_alloc(pool_head_h1s);
276 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100277 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200278
279 h1s->h1c = h1c;
280 h1c->h1s = h1s;
281
Olivier Houchardf502aca2018-12-14 19:42:40 +0100282 h1s->sess = sess;
283
Christopher Faulet51dbc942018-09-13 09:05:15 +0200284 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100285 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200286
287 h1s->recv_wait = NULL;
288 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200289
290 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100291 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200292
Christopher Faulet129817b2018-09-20 16:14:40 +0200293 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100294 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200295
296 h1s->status = 0;
297 h1s->meth = HTTP_METH_OTHER;
298
Christopher Faulet47365272018-10-31 17:40:50 +0100299 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
300 h1s->flags |= H1S_F_NOT_FIRST;
301 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
302
Christopher Faulet129817b2018-09-20 16:14:40 +0200303 if (!conn_is_back(h1c->conn)) {
304 if (h1c->px->options2 & PR_O2_REQBUG_OK)
305 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200306
307 /* For frontend connections we should always have a session */
308 if (!sess)
309 sess = h1c->conn->owner;
310
311 h1s->csinfo.create_date = sess->accept_date;
312 h1s->csinfo.tv_create = sess->tv_accept;
313 h1s->csinfo.t_handshake = sess->t_handshake;
314 h1s->csinfo.t_idle = -1;
Christopher Faulet129817b2018-09-20 16:14:40 +0200315 }
316 else {
317 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
318 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200319
Christopher Fauletfeb11742018-11-29 15:12:34 +0100320 h1s->csinfo.create_date = date;
321 h1s->csinfo.tv_create = now;
322 h1s->csinfo.t_handshake = 0;
323 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200324 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100325
Christopher Faulete9b70722019-04-08 10:46:02 +0200326 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
327 * create a new one.
328 */
329 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200330 cs->ctx = h1s;
331 h1s->cs = cs;
332 }
Christopher Faulet47365272018-10-31 17:40:50 +0100333 else {
334 cs = h1s_new_cs(h1s);
335 if (!cs)
336 goto fail;
337 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200338 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100339
340 fail:
341 pool_free(pool_head_h1s, h1s);
342 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200343}
344
345static void h1s_destroy(struct h1s *h1s)
346{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200347 if (h1s) {
348 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200349
Christopher Fauletf2824e62018-10-01 12:12:37 +0200350 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200351
Christopher Fauletf2824e62018-10-01 12:12:37 +0200352 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100353 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200354 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100355 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200356
Christopher Fauletcb55f482018-12-10 11:56:47 +0100357 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100358 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
359 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
360 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200361 pool_free(pool_head_h1s, h1s);
362 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200363}
364
Christopher Fauletfeb11742018-11-29 15:12:34 +0100365static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
366{
367 struct h1s *h1s = cs->ctx;
368
369 if (h1s && !conn_is_back(cs->conn))
370 return &h1s->csinfo;
371 return NULL;
372}
373
Christopher Faulet51dbc942018-09-13 09:05:15 +0200374/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200375 * Initialize the mux once it's attached. It is expected that conn->ctx points
376 * to the existing conn_stream (for outgoing connections or for incoming onces
377 * during a mux upgrade) or NULL (for incoming ones during the connexion
378 * establishment). <input> is always used as Input buffer and may contain
379 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
380 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200381 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200382static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
383 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200384{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200385 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100386 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200387
388 h1c = pool_alloc(pool_head_h1c);
389 if (!h1c)
390 goto fail_h1c;
391 h1c->conn = conn;
392 h1c->px = proxy;
393
394 h1c->flags = H1C_F_NONE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200395 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200396 h1c->obuf = BUF_NULL;
397 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200398 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200399
Christopher Faulet51dbc942018-09-13 09:05:15 +0200400 LIST_INIT(&h1c->buf_wait.list);
401 h1c->wait_event.task = tasklet_new();
402 if (!h1c->wait_event.task)
403 goto fail;
404 h1c->wait_event.task->process = h1_io_cb;
405 h1c->wait_event.task->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100406 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200407
Christopher Faulete9b70722019-04-08 10:46:02 +0200408 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100409 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
410 if (tick_isset(proxy->timeout.serverfin))
411 h1c->shut_timeout = proxy->timeout.serverfin;
412 } else {
413 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
414 if (tick_isset(proxy->timeout.clientfin))
415 h1c->shut_timeout = proxy->timeout.clientfin;
416 }
417 if (tick_isset(h1c->timeout)) {
418 t = task_new(tid_bit);
419 if (!t)
420 goto fail;
421
422 h1c->task = t;
423 t->process = h1_timeout_task;
424 t->context = h1c;
425 t->expire = tick_add(now_ms, h1c->timeout);
426 }
427
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200428 if (!(conn->flags & CO_FL_CONNECTED))
429 h1c->flags |= H1C_F_CS_WAIT_CONN;
430
Christopher Fauletf2824e62018-10-01 12:12:37 +0200431 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100432 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200433 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200434
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100435 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200436
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100437
438 if (t)
439 task_queue(t);
440
Christopher Faulet51dbc942018-09-13 09:05:15 +0200441 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard9b960a82019-01-04 16:51:40 +0100442 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200443
444 /* mux->wake will be called soon to complete the operation */
445 return 0;
446
447 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200448 task_destroy(t);
Christopher Faulet47365272018-10-31 17:40:50 +0100449 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200450 tasklet_free(h1c->wait_event.task);
451 pool_free(pool_head_h1c, h1c);
452 fail_h1c:
453 return -1;
454}
455
Christopher Faulet73c12072019-04-08 11:23:22 +0200456/* release function. This one should be called to free all resources allocated
457 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200458 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200459static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200460{
Christopher Faulet61840e72019-04-15 09:33:32 +0200461 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200462
Christopher Faulet51dbc942018-09-13 09:05:15 +0200463 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200464 /* The connection must be aattached to this mux to be released */
465 if (h1c->conn && h1c->conn->ctx == h1c)
466 conn = h1c->conn;
467
468 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200469 h1c->flags &= ~H1C_F_UPG_H2C;
470 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) {
471 /* connection successfully upgraded to H2, this
472 * mux was already released */
473 return;
474 }
475 sess_log(conn->owner); /* Log if the upgrade failed */
476 }
477
Christopher Faulet61840e72019-04-15 09:33:32 +0200478
Christopher Faulet51dbc942018-09-13 09:05:15 +0200479 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
480 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
481 LIST_DEL(&h1c->buf_wait.list);
482 LIST_INIT(&h1c->buf_wait.list);
483 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
484 }
485
486 h1_release_buf(h1c, &h1c->ibuf);
487 h1_release_buf(h1c, &h1c->obuf);
488
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100489 if (h1c->task) {
490 h1c->task->context = NULL;
491 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
492 h1c->task = NULL;
493 }
494
Christopher Faulet51dbc942018-09-13 09:05:15 +0200495 if (h1c->wait_event.task)
496 tasklet_free(h1c->wait_event.task);
497
Christopher Fauletf2824e62018-10-01 12:12:37 +0200498 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200499 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100500 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200501 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200502 pool_free(pool_head_h1c, h1c);
503 }
504
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200505 if (conn) {
506 conn->mux = NULL;
507 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200508
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200509 conn_stop_tracking(conn);
510 conn_full_close(conn);
511 if (conn->destroy_cb)
512 conn->destroy_cb(conn);
513 conn_free(conn);
514 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200515}
516
517/******************************************************/
518/* functions below are for the H1 protocol processing */
519/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200520/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
521 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200522 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100523static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200524{
Christopher Faulet570d1612018-11-26 11:13:57 +0100525 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200526
Christopher Faulet570d1612018-11-26 11:13:57 +0100527 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200528 (*(p + 5) > '1' ||
529 (*(p + 5) == '1' && *(p + 7) >= '1')))
530 h1m->flags |= H1_MF_VER_11;
531}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200532
Christopher Faulet9768c262018-10-22 09:34:31 +0200533/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
534 * greater or equal to 1.1
535 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100536static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200537{
Christopher Faulet570d1612018-11-26 11:13:57 +0100538 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200539
Christopher Faulet570d1612018-11-26 11:13:57 +0100540 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200541 (*(p + 5) > '1' ||
542 (*(p + 5) == '1' && *(p + 7) >= '1')))
543 h1m->flags |= H1_MF_VER_11;
544}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200545
Christopher Faulet9768c262018-10-22 09:34:31 +0200546/*
547 * Check the validity of the request version. If the version is valid, it
548 * returns 1. Otherwise, it returns 0.
549 */
550static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
551{
552 struct h1c *h1c = h1s->h1c;
553
554 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
555 * exactly one digit "." one digit. This check may be disabled using
556 * option accept-invalid-http-request.
557 */
558 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
559 if (sl.rq.v.len != 8)
560 return 0;
561
562 if (*(sl.rq.v.ptr + 4) != '/' ||
563 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
564 *(sl.rq.v.ptr + 6) != '.' ||
565 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
566 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200567 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200568 else if (!sl.rq.v.len) {
569 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200570
Christopher Faulet9768c262018-10-22 09:34:31 +0200571 /* RFC 1945 allows only GET for HTTP/0.9 requests */
572 if (sl.rq.meth != HTTP_METH_GET)
573 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200574
Christopher Faulet9768c262018-10-22 09:34:31 +0200575 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
576 if (!sl.rq.u.len)
577 return 0;
578
579 /* Add HTTP version */
580 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100581 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200582 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100583
584 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100585 ((*(sl.rq.v.ptr + 5) > '1') ||
586 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100587 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200588 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200589}
590
Christopher Faulet9768c262018-10-22 09:34:31 +0200591/*
592 * Check the validity of the response version. If the version is valid, it
593 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200594 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200595static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200596{
Christopher Faulet9768c262018-10-22 09:34:31 +0200597 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200598
Christopher Faulet9768c262018-10-22 09:34:31 +0200599 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
600 * exactly one digit "." one digit. This check may be disabled using
601 * option accept-invalid-http-request.
602 */
603 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
604 if (sl.st.v.len != 8)
605 return 0;
606
607 if (*(sl.st.v.ptr + 4) != '/' ||
608 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
609 *(sl.st.v.ptr + 6) != '.' ||
610 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
611 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200612 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100613
614 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100615 ((*(sl.st.v.ptr + 5) > '1') ||
616 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100617 h1m->flags |= H1_MF_VER_11;
618
Christopher Faulet9768c262018-10-22 09:34:31 +0200619 return 1;
620}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200621
622/* Deduce the connection mode of the client connection, depending on the
623 * configuration and the H1 message flags. This function is called twice, the
624 * first time when the request is parsed and the second time when the response
625 * is parsed.
626 */
627static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
628{
629 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200630
631 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100632 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200633 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100634 h1s->status == 101) {
635 /* Either we've established an explicit tunnel, or we're
636 * switching the protocol. In both cases, we're very unlikely to
637 * understand the next protocols. We have to switch to tunnel
638 * mode, so that we transfer the request and responses then let
639 * this protocol pass unmodified. When we later implement
640 * specific parsers for such protocols, we'll want to check the
641 * Upgrade header which contains information about that protocol
642 * for responses with status 101 (eg: see RFC2817 about TLS).
643 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200644 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100645 }
646 else if (h1s->flags & H1S_F_WANT_KAL) {
647 /* By default the client is in KAL mode. CLOSE mode mean
648 * it is imposed by the client itself. So only change
649 * KAL mode here. */
650 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
651 /* no length known or explicit close => close */
652 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
653 }
654 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
655 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
656 /* no explict keep-alive and option httpclose => close */
657 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
658 }
659 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200660 }
661 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100662 /* Input direction: first pass */
663 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
664 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200665 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100666 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200667 }
668
669 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
670 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
671 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
672}
673
674/* Deduce the connection mode of the client connection, depending on the
675 * configuration and the H1 message flags. This function is called twice, the
676 * first time when the request is parsed and the second time when the response
677 * is parsed.
678 */
679static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
680{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100681 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100682 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100683 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200684
Christopher Fauletf2824e62018-10-01 12:12:37 +0200685 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100686 /* Input direction: second pass */
687
Christopher Fauletf2824e62018-10-01 12:12:37 +0200688 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100689 h1s->status == 101) {
690 /* Either we've established an explicit tunnel, or we're
691 * switching the protocol. In both cases, we're very unlikely to
692 * understand the next protocols. We have to switch to tunnel
693 * mode, so that we transfer the request and responses then let
694 * this protocol pass unmodified. When we later implement
695 * specific parsers for such protocols, we'll want to check the
696 * Upgrade header which contains information about that protocol
697 * for responses with status 101 (eg: see RFC2817 about TLS).
698 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200699 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100700 }
701 else if (h1s->flags & H1S_F_WANT_KAL) {
702 /* By default the server is in KAL mode. CLOSE mode mean
703 * it is imposed by haproxy itself. So only change KAL
704 * mode here. */
705 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
706 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
707 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
708 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
709 }
710 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200711 }
Christopher Faulet70033782018-12-05 13:50:11 +0100712 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100713 /* Output direction: first pass */
714 if (h1m->flags & H1_MF_CONN_CLO) {
715 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100716 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100717 }
718 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
719 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
720 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
721 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
722 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
723 /* no explicit keep-alive option httpclose/server-close => close */
724 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
725 }
Christopher Faulet70033782018-12-05 13:50:11 +0100726 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200727
728 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
729 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
730 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200731}
732
Christopher Fauletb992af02019-03-28 15:42:24 +0100733static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200734{
735 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200736
737 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
738 * token is found
739 */
740 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200741 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200742
743 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100744 if (!(h1m->flags & H1_MF_VER_11))
745 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200746 }
747 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100748 if (h1m->flags & H1_MF_VER_11)
749 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200750 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200751}
752
Christopher Fauletb992af02019-03-28 15:42:24 +0100753static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200754{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200755 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
756 * token is found
757 */
758 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200759 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200760
761 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100762 if (!(h1m->flags & H1_MF_VER_11) ||
763 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
764 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200765 }
766 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100767 if (h1m->flags & H1_MF_VER_11)
768 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200769 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200770}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200771
Christopher Fauletb992af02019-03-28 15:42:24 +0100772static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200773{
Christopher Fauletb992af02019-03-28 15:42:24 +0100774 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200775 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100776 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200777 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200778}
779
Christopher Fauletb992af02019-03-28 15:42:24 +0100780static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
781{
782 if (!conn_is_back(h1s->h1c->conn))
783 h1_set_cli_conn_mode(h1s, h1m);
784 else
785 h1_set_srv_conn_mode(h1s, h1m);
786
787 if (!(h1m->flags & H1_MF_RESP))
788 h1_update_req_conn_value(h1s, h1m, conn_val);
789 else
790 h1_update_res_conn_value(h1s, h1m, conn_val);
791}
Christopher Faulete44769b2018-11-29 23:01:45 +0100792
793/* Append the description of what is present in error snapshot <es> into <out>.
794 * The description must be small enough to always fit in a buffer. The output
795 * buffer may be the trash so the trash must not be used inside this function.
796 */
797static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
798{
799 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100800 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
801 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
802 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
803 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
804 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
805 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100806}
807/*
808 * Capture a bad request or response and archive it in the proxy's structure.
809 * By default it tries to report the error position as h1m->err_pos. However if
810 * this one is not set, it will then report h1m->next, which is the last known
811 * parsing point. The function is able to deal with wrapping buffers. It always
812 * displays buffers as a contiguous area starting at buf->p. The direction is
813 * determined thanks to the h1m's flags.
814 */
815static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
816 struct h1m *h1m, struct buffer *buf)
817{
818 struct session *sess = h1c->conn->owner;
819 struct proxy *proxy = h1c->px;
820 struct proxy *other_end = sess->fe;
821 union error_snapshot_ctx ctx;
822
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100823 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100824 other_end = si_strm(h1s->cs->data)->be;
825
826 /* http-specific part now */
827 ctx.h1.state = h1m->state;
828 ctx.h1.c_flags = h1c->flags;
829 ctx.h1.s_flags = h1s->flags;
830 ctx.h1.m_flags = h1m->flags;
831 ctx.h1.m_clen = h1m->curr_len;
832 ctx.h1.m_blen = h1m->body_len;
833
834 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
835 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100836 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
837 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100838}
839
Christopher Fauletadb22202018-12-12 10:32:09 +0100840/* Emit the chunksize followed by a CRLF in front of data of the buffer
841 * <buf>. It goes backwards and starts with the byte before the buffer's
842 * head. The caller is responsible for ensuring there is enough room left before
843 * the buffer's head for the string.
844 */
845static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
846{
847 char *beg, *end;
848
849 beg = end = b_head(buf);
850 *--beg = '\n';
851 *--beg = '\r';
852 do {
853 *--beg = hextab[chksz & 0xF];
854 } while (chksz >>= 4);
855 buf->head -= (end - beg);
856 b_add(buf, end - beg);
857}
858
859/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
860 * ensuring there is enough room left in the buffer for the string. */
861static void h1_emit_chunk_crlf(struct buffer *buf)
862{
863 *(b_peek(buf, b_data(buf))) = '\r';
864 *(b_peek(buf, b_data(buf) + 1)) = '\n';
865 b_add(buf, 2);
866}
867
Christopher Faulet129817b2018-09-20 16:14:40 +0200868/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100869 * Switch the request to tunnel mode. This function must only be called for
870 * CONNECT requests. On the client side, the mux is mark as busy on input,
871 * waiting the response.
872 */
873static void h1_set_req_tunnel_mode(struct h1s *h1s)
874{
875 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
876 h1s->req.state = H1_MSG_TUNNEL;
877 if (!conn_is_back(h1s->h1c->conn))
878 h1s->h1c->flags |= H1C_F_IN_BUSY;
879}
880
881/*
882 * Switch the response to tunnel mode. This function must only be called on
883 * successfull replies to CONNECT requests or on protocol switching. On the
884 * server side, if the request is not finished, the mux is mark as busy on
885 * input. Otherwise the request is also switch to tunnel mode.
886 */
887static void h1_set_res_tunnel_mode(struct h1s *h1s)
888{
889 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
890 h1s->res.state = H1_MSG_TUNNEL;
891 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
892 h1s->h1c->flags |= H1C_F_IN_BUSY;
893 else {
894 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
895 h1s->req.state = H1_MSG_TUNNEL;
896 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
897 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
898 tasklet_wakeup(h1s->h1c->wait_event.task);
899 }
900 }
901}
902
Christopher Faulet82f01602019-05-24 16:50:16 +0200903/* Estimate the size of the HTX headers after the parsing, including the EOH. */
904static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs)
905{
906 size_t sz = 0;
907 int i;
908
909 for (i = 0; hdrs[i].n.len; i++)
910 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
911 sz += sizeof(struct htx_blk) + 1;
912 return sz;
913}
914
Christopher Faulet30db3d72019-05-17 15:35:33 +0200915/* Estimate the size of the HTX request after the parsing. */
916static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
917{
918 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200919
920 /* size of the HTX start-line */
921 sz = sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200922 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200923
924 /* size of the EOM */
925 if (h1m->state == H1_MSG_DONE)
926 sz += sizeof(struct htx_blk) + 1;
927
928 return sz;
929}
930
931/* Estimate the size of the HTX response after the parsing. */
932static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs)
933{
934 size_t sz;
Christopher Faulet30db3d72019-05-17 15:35:33 +0200935
936 /* size of the HTX start-line */
937 sz = sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.len;
Christopher Faulet82f01602019-05-24 16:50:16 +0200938 sz += h1_eval_htx_hdrs_size(hdrs);
Christopher Faulet30db3d72019-05-17 15:35:33 +0200939
940 /* size of the EOM */
941 if (h1m->state == H1_MSG_DONE)
942 sz += sizeof(struct htx_blk) + 1;
943
944 return sz;
945}
946
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100947/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200948 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200949 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
950 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800951 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200952 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200953static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200954 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200955{
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200956 struct htx_sl *sl;
Christopher Faulet129817b2018-09-20 16:14:40 +0200957 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100958 union h1_sl h1sl;
959 unsigned int flags = HTX_SL_F_NONE;
Christopher Fauleta39d8ad2019-05-15 15:54:39 +0200960 size_t used;
Christopher Faulet129817b2018-09-20 16:14:40 +0200961 int ret = 0;
962
Christopher Faulet30db3d72019-05-17 15:35:33 +0200963 if (!max || !b_data(buf))
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100964 goto end;
965
Christopher Faulet129817b2018-09-20 16:14:40 +0200966 /* Realing input buffer if necessary */
967 if (b_head(buf) + b_data(buf) > b_wrap(buf))
968 b_slow_realign(buf, trash.area, 0);
969
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200970 if (!(h1m->flags & H1_MF_RESP)) {
971 /* Try to match H2 preface before parsing the request headers. */
972 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
973 if (ret > 0)
974 goto h2c_upgrade;
975 }
976
Christopher Faulet30db3d72019-05-17 15:35:33 +0200977 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100978 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200979 if (ret <= 0) {
980 /* Incomplete or invalid message. If the buffer is full, it's an
981 * error because headers are too large to be handled by the
982 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200983 if (ret < 0 || (!ret && b_full(buf)))
984 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200985 goto end;
986 }
987
988 /* messages headers fully parsed, do some checks to prepare the body
989 * parsing.
990 */
991
Christopher Faulet9768c262018-10-22 09:34:31 +0200992 /* Save the request's method or the response's status, check if the body
993 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200994 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100995 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200996
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100997 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +0200998 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100999
1000 if (h1s->meth == HTTP_METH_CONNECT) {
1001 /* Switch CONNECT requests to tunnel mode */
1002 h1_set_req_tunnel_mode(h1s);
1003 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001004
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001005 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1006 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001007 h1m->err_state = h1m->state;
1008 goto vsn_error;
1009 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001010 }
1011 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001012 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001013
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001014 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1015 h1s->status == 101) {
1016 /* Switch successfull replies to CONNECT requests and
1017 * protocol switching to tunnel mode. */
1018 h1_set_res_tunnel_mode(h1s);
1019 }
1020 else if ((h1s->meth == HTTP_METH_HEAD) ||
1021 (h1s->status >= 100 && h1s->status < 200) ||
1022 (h1s->status == 204) || (h1s->status == 304)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001023 /* Responses known to have no body. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001024 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1025 h1m->flags |= H1_MF_XFER_LEN;
1026 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001027 }
1028 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001029 /* Responses with a known body length. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001030 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet129817b2018-09-20 16:14:40 +02001031 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001032 else {
1033 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001034 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001035 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001036
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001037 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1038 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001039 h1m->err_state = h1m->state;
1040 goto vsn_error;
1041 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001042 }
1043
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001044 /* Set HTX start-line flags */
1045 if (h1m->flags & H1_MF_VER_11)
1046 flags |= HTX_SL_F_VER_11;
1047 if (h1m->flags & H1_MF_XFER_ENC)
1048 flags |= HTX_SL_F_XFER_ENC;
1049 if (h1m->flags & H1_MF_XFER_LEN) {
1050 flags |= HTX_SL_F_XFER_LEN;
1051 if (h1m->flags & H1_MF_CHNK)
1052 flags |= HTX_SL_F_CHNK;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001053 else if (h1m->flags & H1_MF_CLEN) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001054 flags |= HTX_SL_F_CLEN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001055 if (h1m->body_len == 0)
1056 flags |= HTX_SL_F_BODYLESS;
1057 }
1058 else
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001059 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001060 }
1061
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001062 used = htx_used_space(htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001063 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001064 if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) {
1065 if (htx_is_empty(htx))
1066 goto error;
1067 h1m_init_req(h1m);
1068 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1069 ret = 0;
1070 goto end;
1071 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001072
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001073 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1074 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001075 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001076 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001077 }
1078 else {
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001079 if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) {
1080 if (htx_is_empty(htx))
1081 goto error;
1082 h1m_init_res(h1m);
1083 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1084 ret = 0;
1085 goto end;
1086 }
Christopher Faulet30db3d72019-05-17 15:35:33 +02001087
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001088 flags |= HTX_SL_F_IS_RESP;
1089 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1090 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001091 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001092 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001093 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001094
Christopher Fauleta39d8ad2019-05-15 15:54:39 +02001095 /* Set bytes used in the HTX mesage for the headers now */
1096 sl->hdrs_bytes = htx_used_space(htx) - used;
1097
Christopher Fauletb992af02019-03-28 15:42:24 +01001098 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001099
1100 /* If body length cannot be determined, set htx->extra to
1101 * ULLONG_MAX. This value is impossible in other cases.
1102 */
1103 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
Christopher Faulet9768c262018-10-22 09:34:31 +02001104 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001105 end:
1106 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001107
1108 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001109 h1m->err_state = h1m->state;
1110 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001111 vsn_error:
1112 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001113 h1s->cs->flags |= CS_FL_EOI;
1114 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulete44769b2018-11-29 23:01:45 +01001115 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001116 ret = 0;
1117 goto end;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001118
1119 h2c_upgrade:
1120 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001121 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001122 htx->flags |= HTX_FL_UPGRADE;
1123 ret = 0;
1124 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001125}
1126
1127/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001128 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1129 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001130 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001131 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001132 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001133static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001134 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001135 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001136{
1137 size_t total = 0;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001138 int32_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001139
Christopher Faulet129817b2018-09-20 16:14:40 +02001140 if (h1m->flags & H1_MF_XFER_LEN) {
1141 if (h1m->flags & H1_MF_CLEN) {
1142 /* content-length: read only h2m->body_len */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001143 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001144 if ((uint64_t)ret > h1m->curr_len)
1145 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001146 if (ret > b_contig_data(buf, *ofs))
1147 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001148
Christopher Faulet9768c262018-10-22 09:34:31 +02001149 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001150 /* very often with large files we'll face the following
1151 * situation :
1152 * - htx is empty and points to <htxbuf>
1153 * - ret == buf->data
1154 * - buf->head == sizeof(struct htx)
1155 * => we can swap the buffers and place an htx header into
1156 * the target buffer instead
1157 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001158 int32_t try = ret;
1159
Willy Tarreau78f548f2018-12-05 10:02:39 +01001160 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1161 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1162 void *raw_area = buf->area;
1163 void *htx_area = htxbuf->area;
1164 struct htx_blk *blk;
1165
1166 buf->area = htx_area;
1167 htxbuf->area = raw_area;
1168 htx = (struct htx *)htxbuf->area;
1169 htx->size = htxbuf->size - sizeof(*htx);
1170 htx_reset(htx);
1171 b_set_data(htxbuf, b_size(htxbuf));
1172
1173 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1174 blk->info += ret;
1175 /* nothing else to do, the old buffer now contains an
1176 * empty pre-initialized HTX header
1177 */
1178 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001179 else {
1180 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
1181 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001182 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001183 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001184 *ofs += ret;
1185 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001186 if (ret < try)
1187 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001188 }
1189
1190 if (!h1m->curr_len) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001191 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001192 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001193 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001194 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet9768c262018-10-22 09:34:31 +02001195 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001196 }
1197 else if (h1m->flags & H1_MF_CHNK) {
1198 new_chunk:
1199 /* te:chunked : parse chunks */
1200 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001201 ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf));
Christopher Faulet129817b2018-09-20 16:14:40 +02001202 if (ret <= 0)
1203 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001204 h1m->state = H1_MSG_CHUNK_SIZE;
1205
Christopher Fauletf2824e62018-10-01 12:12:37 +02001206 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001207 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001208 }
1209
1210 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1211 unsigned int chksz;
1212
Christopher Faulet30db3d72019-05-17 15:35:33 +02001213 ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001214 if (ret <= 0)
1215 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001216 if (!chksz) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001217 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOD))
Christopher Faulet9768c262018-10-22 09:34:31 +02001218 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001219 h1s->flags |= H1S_F_HAVE_I_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001220 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001221 max -= sizeof(struct htx_blk) + 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001222 }
1223 else
1224 h1m->state = H1_MSG_DATA;
1225
Christopher Faulet129817b2018-09-20 16:14:40 +02001226 h1m->curr_len = chksz;
1227 h1m->body_len += chksz;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001228 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001229 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001230 }
1231
1232 if (h1m->state == H1_MSG_DATA) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001233 ret = htx_get_max_blksz(htx, max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001234 if ((uint64_t)ret > h1m->curr_len)
1235 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 if (ret > b_contig_data(buf, *ofs))
1237 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001238
Christopher Faulet9768c262018-10-22 09:34:31 +02001239 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001240 int32_t try = ret;
1241
1242 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001243 h1m->curr_len -= ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001244 max -= sizeof(struct htx_blk) + ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001245 *ofs += ret;
1246 total += ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001247 if (ret < try)
1248 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001249 }
1250 if (!h1m->curr_len) {
1251 h1m->state = H1_MSG_CHUNK_CRLF;
1252 goto new_chunk;
1253 }
1254 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001255 }
1256
1257 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001258 /* Trailers were alread parsed, only the EOM
1259 * need to be added */
Willy Tarreau34d23482019-01-03 17:46:56 +01001260 if (h1s->flags & H1S_F_HAVE_I_TLR)
Christopher Faulet17276482018-11-22 11:44:35 +01001261 goto skip_tlr_parsing;
1262
Christopher Faulet30db3d72019-05-17 15:35:33 +02001263 ret = htx_get_max_blksz(htx, max);
1264 ret = h1_measure_trailers(buf, *ofs, ret);
1265 if (ret <= 0) {
1266 if (!ret && b_full(buf))
1267 ret = -1;
Christopher Faulet129817b2018-09-20 16:14:40 +02001268 goto end;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001269 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001270
1271 /* Realing input buffer if tailers wrap. For now
1272 * this is a workaroung. Because trailers are
1273 * not split on CRLF, like headers, there is no
1274 * way to know where to split it when trailers
1275 * wrap. This is a limitation of
1276 * h1_measure_trailers.
1277 */
1278 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1279 b_slow_realign(buf, trash.area, 0);
1280
1281 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1282 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001283 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001284 max -= sizeof(struct htx_blk) + ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001285 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001286 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001287
Christopher Faulet17276482018-11-22 11:44:35 +01001288 skip_tlr_parsing:
Christopher Faulet30db3d72019-05-17 15:35:33 +02001289 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001290 goto end;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001291 max -= sizeof(struct htx_blk) + 1;
Christopher Faulet129817b2018-09-20 16:14:40 +02001292 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001293 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001294 }
1295 }
1296 else {
1297 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1298 * is no body. Switch the message in DONE state
1299 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001300 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001301 goto end;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001302 max -= sizeof(struct htx_blk) + 1;
Christopher Faulet129817b2018-09-20 16:14:40 +02001303 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001304 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001305 }
1306 }
1307 else {
1308 /* no content length, read till SHUTW */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001309 ret = htx_get_max_blksz(htx, max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001310 if (ret > b_contig_data(buf, *ofs))
1311 ret = b_contig_data(buf, *ofs);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001312
Christopher Faulet9768c262018-10-22 09:34:31 +02001313 if (ret) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001314 int32_t try = ret;
1315
1316 ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try));
Christopher Faulet9768c262018-10-22 09:34:31 +02001317
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001318 *ofs += ret;
1319 total = ret;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001320 if (ret < try)
1321 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001322 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001323 }
1324
1325 end:
1326 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001327 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001328 h1s->cs->flags |= CS_FL_EOI;
1329 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001330 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001331 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001332 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001333 return 0;
1334 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001335 /* update htx->extra, only when the body length is known */
1336 if (h1m->flags & H1_MF_XFER_LEN)
1337 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001338 return total;
1339}
1340
1341/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001342 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001343 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1344 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001345 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001346static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001347{
Christopher Faulet539e0292018-11-19 10:40:09 +01001348 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001349 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001350 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001351 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001352 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001353 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001354
Christopher Faulet539e0292018-11-19 10:40:09 +01001355 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001356
Christopher Fauletf2824e62018-10-01 12:12:37 +02001357 if (!conn_is_back(h1c->conn)) {
1358 h1m = &h1s->req;
1359 errflag = H1S_F_REQ_ERROR;
1360 }
1361 else {
1362 h1m = &h1s->res;
1363 errflag = H1S_F_RES_ERROR;
1364 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001365
Christopher Faulet74027762019-02-26 14:45:05 +01001366 data = htx->data;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001367 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001368 size_t used = htx_used_space(htx);
1369
Christopher Faulet129817b2018-09-20 16:14:40 +02001370 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001371 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001372 if (!ret)
1373 break;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001374 if ((h1m->flags & H1_MF_RESP) &&
1375 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1376 h1m_init_res(&h1s->res);
1377 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1378 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001379 }
1380 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001381 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001382 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001383 if (!ret)
1384 break;
1385 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001386 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001387 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE)
1388 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001389 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001390 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001391 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001392 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001393 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001394 if (!ret)
1395 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001396 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001397 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001398 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001399 break;
1400 }
1401
Christopher Faulet30db3d72019-05-17 15:35:33 +02001402 count -= htx_used_space(htx) - used;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001403 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001404
Christopher Faulet47365272018-10-31 17:40:50 +01001405 if (h1s->flags & errflag)
1406 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001407
Christopher Faulet539e0292018-11-19 10:40:09 +01001408 b_del(&h1c->ibuf, total);
1409
1410 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001411 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001412 ret = htx->data - data;
Willy Tarreau45f2b892018-12-05 07:59:27 +01001413 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001414 h1c->flags &= ~H1C_F_IN_FULL;
1415 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001416 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001417
Christopher Fauletcf56b992018-12-11 16:12:31 +01001418 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1419
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001420 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001421 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001422 else if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001423 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001424
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001425 if ((h1s->flags & H1S_F_REOS) && (!h1s_data_pending(h1s) || htx_is_empty(htx))) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001426 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001427 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001428 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001429 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001430
Christopher Faulet30db3d72019-05-17 15:35:33 +02001431 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001432
1433 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001434 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001435 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001436 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001437}
1438
Christopher Faulet129817b2018-09-20 16:14:40 +02001439/*
1440 * Process outgoing data. It parses data and transfer them from the channel buffer into
1441 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1442 * 0 if it couldn't proceed.
1443 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001444static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1445{
Christopher Faulet129817b2018-09-20 16:14:40 +02001446 struct h1s *h1s = h1c->h1s;
1447 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001448 struct htx *chn_htx;
1449 struct htx_blk *blk;
1450 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001451 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001452 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001453
Christopher Faulet47365272018-10-31 17:40:50 +01001454 if (!count)
1455 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001456
Christopher Faulet94b2c762019-05-24 15:28:57 +02001457 chn_htx = htxbuf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001458 if (htx_is_empty(chn_htx))
1459 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001460
Christopher Faulet51dbc942018-09-13 09:05:15 +02001461 if (!h1_get_buf(h1c, &h1c->obuf)) {
1462 h1c->flags |= H1C_F_OUT_ALLOC;
1463 goto end;
1464 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001465
Christopher Fauletf2824e62018-10-01 12:12:37 +02001466 if (!conn_is_back(h1c->conn)) {
1467 h1m = &h1s->res;
1468 errflag = H1S_F_RES_ERROR;
1469 }
1470 else {
1471 h1m = &h1s->req;
1472 errflag = H1S_F_REQ_ERROR;
1473 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001474
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001475 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001476 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001477
1478 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001479
Willy Tarreau3815b222018-12-11 19:50:43 +01001480 /* Perform some optimizations to reduce the number of buffer copies.
1481 * First, if the mux's buffer is empty and the htx area contains
1482 * exactly one data block of the same size as the requested count,
1483 * then it's possible to simply swap the caller's buffer with the
1484 * mux's output buffer and adjust offsets and length to match the
1485 * entire DATA HTX block in the middle. In this case we perform a
1486 * true zero-copy operation from end-to-end. This is the situation
1487 * that happens all the time with large files. Second, if this is not
1488 * possible, but the mux's output buffer is empty, we still have an
1489 * opportunity to avoid the copy to the intermediary buffer, by making
1490 * the intermediary buffer's area point to the output buffer's area.
1491 * In this case we want to skip the HTX header to make sure that copies
1492 * remain aligned and that this operation remains possible all the
1493 * time. This goes for headers, data blocks and any data extracted from
1494 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001495 */
1496 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001497 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001498
1499 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001500 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001501 htx_get_blk_value(chn_htx, blk).len == count) {
1502 void *old_area = h1c->obuf.area;
1503
1504 h1c->obuf.area = buf->area;
1505 h1c->obuf.data = count;
1506
1507 buf->area = old_area;
1508 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001509
1510 /* The message is chunked. We need to emit the chunk
1511 * size. We have at least the size of the struct htx to
1512 * write the chunk envelope. It should be enough.
1513 */
1514 if (h1m->flags & H1_MF_CHNK) {
1515 h1_emit_chunk_size(&h1c->obuf, count);
1516 h1_emit_chunk_crlf(&h1c->obuf);
1517 }
1518
Willy Tarreau3815b222018-12-11 19:50:43 +01001519 total += count;
1520 goto out;
1521 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001522 tmp->area = h1c->obuf.area + h1c->obuf.head;
1523 }
1524
Christopher Faulet9768c262018-10-22 09:34:31 +02001525 tmp->size = b_room(&h1c->obuf);
1526
Christopher Fauletb2e84162018-12-06 11:39:49 +01001527 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001528 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001529 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001530 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001531 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001532 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001533
Christopher Fauletb2e84162018-12-06 11:39:49 +01001534 vlen = sz;
1535 if (vlen > count) {
1536 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1537 goto copy;
1538 vlen = count;
1539 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001540
Christopher Faulet94b2c762019-05-24 15:28:57 +02001541 if (type == HTX_BLK_UNUSED)
1542 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001543
Christopher Faulet94b2c762019-05-24 15:28:57 +02001544 switch (h1m->state) {
1545 case H1_MSG_RQBEFORE:
1546 if (type != HTX_BLK_REQ_SL)
1547 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001548 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001549 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001550 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001551 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001552 goto copy;
1553 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001554 if (sl->flags & HTX_SL_F_BODYLESS)
1555 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001556 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001557 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001558
Christopher Faulet94b2c762019-05-24 15:28:57 +02001559 case H1_MSG_RPBEFORE:
1560 if (type != HTX_BLK_RES_SL)
1561 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001562 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001563 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001564 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001565 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001566 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001567 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001568 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001569 if (sl->info.res.status < 200 &&
1570 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001571 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001572 h1m->state = H1_MSG_HDR_FIRST;
1573 break;
1574
Christopher Faulet94b2c762019-05-24 15:28:57 +02001575 case H1_MSG_HDR_FIRST:
1576 case H1_MSG_HDR_NAME:
1577 case H1_MSG_HDR_L2_LWS:
1578 if (type == HTX_BLK_EOH)
1579 goto last_lf;
1580 if (type != HTX_BLK_HDR)
1581 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001582 h1m->state = H1_MSG_HDR_NAME;
1583 n = htx_get_blk_name(chn_htx, blk);
1584 v = htx_get_blk_value(chn_htx, blk);
1585
1586 if (isteqi(n, ist("transfer-encoding")))
1587 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001588 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001589 /* Only skip C-L header with invalid value. */
1590 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001591 goto skip_hdr;
1592 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001593 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001594 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001595 if (!v.len)
1596 goto skip_hdr;
1597 }
1598
Christopher Fauletc59ff232018-12-03 13:58:44 +01001599 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001600 goto copy;
1601 skip_hdr:
1602 h1m->state = H1_MSG_HDR_L2_LWS;
1603 break;
1604
Christopher Faulet94b2c762019-05-24 15:28:57 +02001605 case H1_MSG_LAST_LF:
1606 if (type != HTX_BLK_EOH)
1607 goto error;
1608 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001609 h1m->state = H1_MSG_LAST_LF;
1610 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001611 /* There is no "Connection:" header and
1612 * it the conn_mode must be
1613 * processed. So do it */
1614 n = ist("Connection");
1615 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001616 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001617 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001618 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001619 goto copy;
1620 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001621 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001622 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001623
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001624 if ((h1s->meth != HTTP_METH_CONNECT &&
1625 (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 +01001626 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1627 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1628 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1629 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1630 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001631 /* chunking needed but header not seen */
1632 if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28))
1633 goto copy;
1634 h1m->flags |= H1_MF_CHNK;
1635 }
1636
Christopher Faulet9768c262018-10-22 09:34:31 +02001637 if (!chunk_memcat(tmp, "\r\n", 2))
1638 goto copy;
1639
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001640 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1641 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1642 h1_set_req_tunnel_mode(h1s);
1643 }
1644 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1645 /* a successfull reply to a CONNECT or a protocol switching is sent
1646 * to the client . Switch the response to tunnel mode. */
1647 h1_set_res_tunnel_mode(h1s);
1648 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001649 else if ((h1m->flags & H1_MF_RESP) &&
1650 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1651 h1m_init_res(&h1s->res);
1652 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001653 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001654 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001655 else
1656 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001657 break;
1658
Christopher Faulet94b2c762019-05-24 15:28:57 +02001659 case H1_MSG_DATA:
1660 if (type == HTX_BLK_EOD) {
1661 if (!chunk_memcat(tmp, "0\r\n", 3))
1662 goto copy;
1663 h1s->flags |= H1S_F_HAVE_O_EOD;
1664 h1m->state = H1_MSG_TRAILERS;
1665 break;
1666 }
1667 if (type == HTX_BLK_TLR)
1668 goto trailers;
1669 else if (type == HTX_BLK_EOM)
1670 goto done;
1671 else if (type != HTX_BLK_DATA)
1672 goto error;
Christopher Faulet9768c262018-10-22 09:34:31 +02001673 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001674 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001675 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001676 goto copy;
1677 break;
1678
Christopher Faulet94b2c762019-05-24 15:28:57 +02001679 case H1_MSG_TRAILERS:
1680 if (type == HTX_BLK_EOM)
1681 goto done;
1682 else if (type != HTX_BLK_TLR)
1683 goto error;
1684 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001685 h1m->state = H1_MSG_TRAILERS;
Willy Tarreau34d23482019-01-03 17:46:56 +01001686 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001687 if (!chunk_memcat(tmp, "0\r\n", 3))
1688 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001689 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001690 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001691 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001692 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001693 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001694 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001695 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001696 break;
1697
Christopher Faulet94b2c762019-05-24 15:28:57 +02001698 case H1_MSG_DONE:
1699 if (type != HTX_BLK_EOM)
1700 goto error;
1701 done:
1702 h1m->state = H1_MSG_DONE;
Christopher Faulet32188212018-11-20 18:21:43 +01001703 if ((h1m->flags & H1_MF_CHNK)) {
Willy Tarreau34d23482019-01-03 17:46:56 +01001704 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001705 if (!chunk_memcat(tmp, "0\r\n", 3))
1706 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001707 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001708 }
Willy Tarreau34d23482019-01-03 17:46:56 +01001709 if (!(h1s->flags & H1S_F_HAVE_O_TLR)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001710 if (!chunk_memcat(tmp, "\r\n", 2))
1711 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001712 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet32188212018-11-20 18:21:43 +01001713 }
1714 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001715 break;
1716
Christopher Faulet9768c262018-10-22 09:34:31 +02001717 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001718 error:
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001719 h1s->flags |= errflag;
Christopher Faulet9768c262018-10-22 09:34:31 +02001720 break;
1721 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001722
1723 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001724 total += vlen;
1725 count -= vlen;
1726 if (sz == vlen)
1727 blk = htx_remove_blk(chn_htx, blk);
1728 else {
1729 htx_cut_data_blk(chn_htx, blk, vlen);
1730 break;
1731 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001732 }
1733
Christopher Faulet9768c262018-10-22 09:34:31 +02001734 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001735 /* when the output buffer is empty, tmp shares the same area so that we
1736 * only have to update pointers and lengths.
1737 */
Willy Tarreaub57af612019-01-23 20:43:53 +01001738 if (tmp->area == h1c->obuf.area + h1c->obuf.head)
Willy Tarreauc5efa332018-12-05 11:19:27 +01001739 h1c->obuf.data = tmp->data;
1740 else
1741 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001742
Willy Tarreau3815b222018-12-11 19:50:43 +01001743 htx_to_buf(chn_htx, buf);
1744 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001745 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001746 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001747 end:
1748 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001749}
1750
Christopher Faulet51dbc942018-09-13 09:05:15 +02001751/*********************************************************/
1752/* functions below are I/O callbacks from the connection */
1753/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001754static void h1_wake_stream_for_recv(struct h1s *h1s)
1755{
1756 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001757 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001758 tasklet_wakeup(h1s->recv_wait->task);
1759 h1s->recv_wait = NULL;
1760 }
1761}
1762static void h1_wake_stream_for_send(struct h1s *h1s)
1763{
1764 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001765 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001766 tasklet_wakeup(h1s->send_wait->task);
1767 h1s->send_wait = NULL;
1768 }
1769}
1770
Christopher Faulet51dbc942018-09-13 09:05:15 +02001771/*
1772 * Attempt to read data, and subscribe if none available
1773 */
1774static int h1_recv(struct h1c *h1c)
1775{
1776 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001777 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001778 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001779 int rcvd = 0;
1780
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001781 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001782 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001783
Willy Tarreaud58f27f2019-06-03 10:12:22 +02001784 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1785 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1786 return 0;
1787 }
1788
Olivier Houchard75159a92018-12-03 18:46:09 +01001789 if (!h1_recv_allowed(h1c)) {
1790 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001791 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001792 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001793
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001794 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1795 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001796 goto end;
1797 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001798
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001799 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001800 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02001801 h1_wake_stream_for_recv(h1s);
1802 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001803 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001804 }
1805
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001806 /*
1807 * If we only have a small amount of data, realign it,
1808 * it's probably cheaper than doing 2 recv() calls.
1809 */
1810 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1811 b_slow_realign(&h1c->ibuf, trash.area, 0);
1812
Willy Tarreau45f2b892018-12-05 07:59:27 +01001813 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001814 if (max) {
1815 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001816
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001817 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001818 if (!b_data(&h1c->ibuf)) {
1819 /* try to pre-align the buffer like the rxbufs will be
1820 * to optimize memory copies.
1821 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001822 h1c->ibuf.head = sizeof(struct htx);
1823 }
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001824 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001825 }
Christopher Faulet47365272018-10-31 17:40:50 +01001826 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001827 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001828 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001829 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001830 if (h1s->csinfo.t_idle == -1)
1831 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1832 }
Christopher Faulet47365272018-10-31 17:40:50 +01001833 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001834
Christopher Fauletcf56b992018-12-11 16:12:31 +01001835 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001836 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001837 goto end;
1838 }
1839
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001840 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001841
Christopher Faulet9768c262018-10-22 09:34:31 +02001842 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001843 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1844 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001845
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001846 if (conn_xprt_read0_pending(conn) && h1s) {
1847 h1s->flags |= H1S_F_REOS;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001848 rcvd = 1;
1849 }
1850
Christopher Faulet51dbc942018-09-13 09:05:15 +02001851 if (!b_data(&h1c->ibuf))
1852 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001853 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001854 h1c->flags |= H1C_F_IN_FULL;
1855 return rcvd;
1856}
1857
1858
1859/*
1860 * Try to send data if possible
1861 */
1862static int h1_send(struct h1c *h1c)
1863{
1864 struct connection *conn = h1c->conn;
1865 unsigned int flags = 0;
1866 size_t ret;
1867 int sent = 0;
1868
1869 if (conn->flags & CO_FL_ERROR)
1870 return 0;
1871
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001872 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001873 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001874 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001875 return 0;
1876 }
1877
Christopher Faulet51dbc942018-09-13 09:05:15 +02001878 if (!b_data(&h1c->obuf))
1879 goto end;
1880
1881 if (h1c->flags & H1C_F_OUT_FULL)
1882 flags |= CO_SFL_MSG_MORE;
1883
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001884 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001885 if (ret > 0) {
1886 h1c->flags &= ~H1C_F_OUT_FULL;
1887 b_del(&h1c->obuf, ret);
1888 sent = 1;
1889 }
1890
Christopher Faulet145aa472018-12-06 10:56:20 +01001891 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1892 /* error or output closed, nothing to send, clear the buffer to release it */
1893 b_reset(&h1c->obuf);
1894 }
1895
Christopher Faulet51dbc942018-09-13 09:05:15 +02001896 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001897 if (!(h1c->flags & H1C_F_OUT_FULL))
1898 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001899
Christopher Faulet51dbc942018-09-13 09:05:15 +02001900 /* We're done, no more to send */
1901 if (!b_data(&h1c->obuf)) {
1902 h1_release_buf(h1c, &h1c->obuf);
1903 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001904 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001905 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001906 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01001907 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001908
1909 return sent;
1910}
1911
Christopher Faulet51dbc942018-09-13 09:05:15 +02001912
1913/* callback called on any event by the connection handler.
1914 * It applies changes and returns zero, or < 0 if it wants immediate
1915 * destruction of the connection.
1916 */
1917static int h1_process(struct h1c * h1c)
1918{
1919 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001920 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001921
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001922 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001923 return -1;
1924
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001925 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001926 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1927 goto end;
1928 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001929 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001930 }
1931
Christopher Fauletfeb11742018-11-29 15:12:34 +01001932 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001933 if (h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001934 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001935 conn_xprt_read0_pending(conn))
1936 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001937 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001938 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001939 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001940 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001941 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001942 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001943 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001944 }
1945
Christopher Fauletfeb11742018-11-29 15:12:34 +01001946 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1947 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1948
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001949 if (conn_xprt_read0_pending(conn))
1950 h1s->flags |= H1S_F_REOS;
1951
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02001952 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001953 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001954 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001955 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02001956 h1s->cs->flags |= CS_FL_ERROR;
Olivier Houchard75159a92018-12-03 18:46:09 +01001957 h1s->cs->data_cb->wake(h1s->cs);
1958 }
Christopher Faulet47365272018-10-31 17:40:50 +01001959 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001960 if (h1c->task) {
1961 h1c->task->expire = TICK_ETERNITY;
1962 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001963 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 +01001964 ? h1c->shut_timeout
1965 : h1c->timeout));
1966 task_queue(h1c->task);
1967 }
1968 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001969 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001970
1971 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02001972 h1_release(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01001973 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001974}
1975
1976static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1977{
1978 struct h1c *h1c = ctx;
1979 int ret = 0;
1980
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001981 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001982 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001983 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001984 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001985 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986 h1_process(h1c);
1987 return NULL;
1988}
1989
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001990static void h1_reset(struct connection *conn)
1991{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001992 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001993
1994 /* Reset the flags, and let the mux know we're waiting for a connection */
1995 h1c->flags = H1C_F_CS_WAIT_CONN;
1996}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001997
1998static int h1_wake(struct connection *conn)
1999{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002000 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002001 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002002
Christopher Faulet539e0292018-11-19 10:40:09 +01002003 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002004 ret = h1_process(h1c);
2005 if (ret == 0) {
2006 struct h1s *h1s = h1c->h1s;
2007
2008 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
2009 ret = h1s->cs->data_cb->wake(h1s->cs);
2010 }
2011 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002012}
2013
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002014/* Connection timeout management. The principle is that if there's no receipt
2015 * nor sending for a certain amount of time, the connection is closed.
2016 */
2017static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2018{
2019 struct h1c *h1c = context;
2020 int expired = tick_is_expired(t->expire, now_ms);
2021
2022 if (!expired && h1c)
2023 return t;
2024
Olivier Houchard3f795f72019-04-17 22:51:06 +02002025 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002026
2027 if (!h1c) {
2028 /* resources were already deleted */
2029 return NULL;
2030 }
2031
2032 h1c->task = NULL;
2033 /* If a stream is still attached to the mux, just set an error and wait
2034 * for the stream's timeout. Otherwise, release the mux. This is only ok
2035 * because same timeouts are used.
2036 */
2037 if (h1c->h1s && h1c->h1s->cs)
2038 h1c->flags |= H1C_F_CS_ERROR;
2039 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002040 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002041 return NULL;
2042}
2043
Christopher Faulet51dbc942018-09-13 09:05:15 +02002044/*******************************************/
2045/* functions below are used by the streams */
2046/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002047
Christopher Faulet51dbc942018-09-13 09:05:15 +02002048/*
2049 * Attach a new stream to a connection
2050 * (Used for outgoing connections)
2051 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002052static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002053{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002054 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002055 struct conn_stream *cs = NULL;
2056 struct h1s *h1s;
2057
2058 if (h1c->flags & H1C_F_CS_ERROR)
2059 goto end;
2060
2061 cs = cs_new(h1c->conn);
2062 if (!cs)
2063 goto end;
2064
Olivier Houchardf502aca2018-12-14 19:42:40 +01002065 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002066 if (h1s == NULL)
2067 goto end;
2068
2069 return cs;
2070 end:
2071 cs_free(cs);
2072 return NULL;
2073}
2074
2075/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2076 * this mux, it's easy as we can only store a single conn_stream.
2077 */
2078static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2079{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002080 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002081 struct h1s *h1s = h1c->h1s;
2082
2083 if (h1s)
2084 return h1s->cs;
2085
2086 return NULL;
2087}
2088
Christopher Faulet73c12072019-04-08 11:23:22 +02002089static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002090{
Christopher Faulet73c12072019-04-08 11:23:22 +02002091 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002092
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002093 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002094 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002095}
2096
2097/*
2098 * Detach the stream from the connection and possibly release the connection.
2099 */
2100static void h1_detach(struct conn_stream *cs)
2101{
2102 struct h1s *h1s = cs->ctx;
2103 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002104 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002105 int has_keepalive;
2106 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002107
2108 cs->ctx = NULL;
2109 if (!h1s)
2110 return;
2111
Olivier Houchardf502aca2018-12-14 19:42:40 +01002112 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002113 h1c = h1s->h1c;
2114 h1s->cs = NULL;
2115
Olivier Houchard8a786902018-12-15 16:05:40 +01002116 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2117 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2118 h1s_destroy(h1s);
2119
2120 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002121 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002122 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002123 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002124 h1c->conn->flags |= CO_FL_PRIVATE;
2125
Olivier Houchard44d59142018-12-13 18:46:22 +01002126 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002127 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002128 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2129 h1c->conn->owner = NULL;
2130 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2131 /* The server doesn't want it, let's kill the connection right away */
2132 h1c->conn->mux->destroy(h1c->conn);
2133 else
2134 tasklet_wakeup(h1c->wait_event.task);
2135 return;
2136
2137 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002138 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002139 if (h1c->conn->owner == sess) {
2140 int ret = session_check_idle_conn(sess, h1c->conn);
2141 if (ret == -1)
2142 /* The connection got destroyed, let's leave */
2143 return;
2144 else if (ret == 1) {
2145 /* The connection was added to the server list,
2146 * wake the task so we can subscribe to events
2147 */
2148 tasklet_wakeup(h1c->wait_event.task);
2149 return;
2150 }
2151 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002152 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002153 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002154 struct server *srv = objt_server(h1c->conn->target);
2155
2156 if (srv) {
2157 if (h1c->conn->flags & CO_FL_PRIVATE)
2158 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002159 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002160 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2161 else
2162 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2163 }
2164 }
2165 }
2166
Christopher Faulet51dbc942018-09-13 09:05:15 +02002167 /* We don't want to close right now unless the connection is in error */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002168 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01002169 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet73c12072019-04-08 11:23:22 +02002170 h1_release(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002171 else {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002172 tasklet_wakeup(h1c->wait_event.task);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002173 if (h1c->task) {
2174 h1c->task->expire = TICK_ETERNITY;
2175 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002176 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 +01002177 ? h1c->shut_timeout
2178 : h1c->timeout));
2179 task_queue(h1c->task);
2180 }
2181 }
2182 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002183}
2184
2185
2186static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2187{
2188 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002189 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002190
2191 if (!h1s)
2192 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002193 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002194
Christopher Faulet7f366362019-04-08 10:51:20 +02002195 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2196 goto do_shutr;
2197
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002198 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002199 return;
2200
Christopher Faulet7f366362019-04-08 10:51:20 +02002201 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002202 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2203 if (cs->flags & CS_FL_SHR)
2204 return;
2205 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002206 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
2207 (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002208 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 +02002209 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002210}
2211
2212static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2213{
2214 struct h1s *h1s = cs->ctx;
2215 struct h1c *h1c;
2216
2217 if (!h1s)
2218 return;
2219 h1c = h1s->h1c;
2220
Christopher Faulet7f366362019-04-08 10:51:20 +02002221 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2222 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002223
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002224 if ((h1c->flags & H1C_F_UPG_H2C) ||
2225 ((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 +02002226 return;
2227
Christopher Faulet7f366362019-04-08 10:51:20 +02002228 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002229 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2230 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2231 return;
2232
Christopher Faulet666a0c42019-01-08 11:12:04 +01002233 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002234}
2235
Christopher Faulet666a0c42019-01-08 11:12:04 +01002236static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002237{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002238 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002239
Christopher Faulet666a0c42019-01-08 11:12:04 +01002240 conn_xprt_shutw(conn);
2241 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2242 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2243 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002244}
2245
2246/* Called from the upper layer, to unsubscribe to events */
2247static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2248{
2249 struct wait_event *sw;
2250 struct h1s *h1s = cs->ctx;
2251
2252 if (!h1s)
2253 return 0;
2254
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002255 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002256 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002257 BUG_ON(h1s->recv_wait != sw);
2258 sw->events &= ~SUB_RETRY_RECV;
2259 h1s->recv_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002260 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002261 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002262 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002263 BUG_ON(h1s->send_wait != sw);
2264 sw->events &= ~SUB_RETRY_SEND;
2265 h1s->send_wait = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002266 }
2267 return 0;
2268}
2269
2270/* Called from the upper layer, to subscribe to events, such as being able to send */
2271static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2272{
2273 struct wait_event *sw;
2274 struct h1s *h1s = cs->ctx;
2275
2276 if (!h1s)
2277 return -1;
2278
2279 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002280 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002281 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002282 BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
2283 sw->events |= SUB_RETRY_RECV;
2284 h1s->recv_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002285 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002286 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002287 sw = param;
Olivier Houchard00b8f7c2019-05-14 18:02:23 +02002288 BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
2289 sw->events |= SUB_RETRY_SEND;
2290 h1s->send_wait = sw;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002291 return 0;
2292 default:
2293 break;
2294 }
2295 return -1;
2296}
2297
2298/* Called from the upper layer, to receive data */
2299static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2300{
2301 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002302 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002303 size_t ret = 0;
2304
Christopher Faulet539e0292018-11-19 10:40:09 +01002305 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002306 ret = h1_process_input(h1c, buf, count);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002307
Christopher Faulete18777b2019-04-16 16:46:36 +02002308 if (flags & CO_RFL_BUF_FLUSH) {
2309 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2310
2311 if (h1m->state != H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len))
2312 h1s->flags |= H1S_F_BUF_FLUSH;
2313 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002314 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2315 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002316 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002317 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002318 }
2319 return ret;
2320}
2321
2322
2323/* Called from the upper layer, to send data */
2324static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2325{
2326 struct h1s *h1s = cs->ctx;
2327 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002328 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002329
2330 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002331 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002332
2333 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002334 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2335 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002336
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002337 while (total != count) {
2338 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002339
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002340 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2341 ret = h1_process_output(h1c, buf, count);
2342 if (!ret)
2343 break;
2344 total += ret;
2345 if (!h1_send(h1c))
2346 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002347 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002348
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002349 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002350}
2351
Willy Tarreaue5733232019-05-22 19:24:06 +02002352#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002353/* Send and get, using splicing */
2354static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2355{
2356 struct h1s *h1s = cs->ctx;
2357 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2358 int ret = 0;
2359
Christopher Faulet1146f972019-05-29 14:35:24 +02002360 if (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002361 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet1146f972019-05-29 14:35:24 +02002362 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV))
2363 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulete18777b2019-04-16 16:46:36 +02002364 goto end;
2365 }
2366
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002367 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002368 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002369 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002370 }
2371
2372 h1s->flags &= ~H1S_F_BUF_FLUSH;
2373 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002374 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2375 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002376 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002377 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002378 h1m->curr_len -= ret;
Christopher Faulete18777b2019-04-16 16:46:36 +02002379 if (!h1m->curr_len)
2380 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
2381 }
2382
Christopher Faulet1be55f92018-10-02 15:59:23 +02002383 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002384 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002385 h1s->flags |= H1S_F_REOS;
Christopher Faulet038ad812019-04-17 11:03:22 +02002386 if (!pipe->data)
2387 cs->flags |= CS_FL_EOS;
2388 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002389 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002390}
2391
2392static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2393{
2394 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002395 int ret = 0;
2396
2397 if (b_data(&h1s->h1c->obuf))
2398 goto end;
2399
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002400 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002401 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002402 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002403 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002404 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002405 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002406 return ret;
2407}
2408#endif
2409
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002410/* for debugging with CLI's "show fd" command */
2411static void h1_show_fd(struct buffer *msg, struct connection *conn)
2412{
2413 struct h1c *h1c = conn->ctx;
2414 struct h1s *h1s = h1c->h1s;
2415
Christopher Fauletf376a312019-01-04 15:16:06 +01002416 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2417 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002418 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2419 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2420 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2421 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2422
2423 if (h1s) {
2424 char *method;
2425
2426 if (h1s->meth < HTTP_METH_OTHER)
2427 method = http_known_methods[h1s->meth].ptr;
2428 else
2429 method = "UNKNOWN";
2430 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2431 " .meth=%s status=%d",
2432 h1s, h1s->flags,
2433 h1m_state_str(h1s->req.state),
2434 h1m_state_str(h1s->res.state), method, h1s->status);
2435 if (h1s->cs)
2436 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2437 h1s->cs->flags, h1s->cs->data);
2438 }
2439}
2440
Christopher Faulet51dbc942018-09-13 09:05:15 +02002441/****************************************/
2442/* MUX initialization and instanciation */
2443/****************************************/
2444
2445/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002446static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002447 .init = h1_init,
2448 .wake = h1_wake,
2449 .attach = h1_attach,
2450 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002451 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002452 .detach = h1_detach,
2453 .destroy = h1_destroy,
2454 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002455 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002456 .rcv_buf = h1_rcv_buf,
2457 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02002458#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002459 .rcv_pipe = h1_rcv_pipe,
2460 .snd_pipe = h1_snd_pipe,
2461#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002462 .subscribe = h1_subscribe,
2463 .unsubscribe = h1_unsubscribe,
2464 .shutr = h1_shutr,
2465 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002466 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002467 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002468 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02002469 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02002470};
2471
2472
2473/* this mux registers default HTX proto */
2474static struct mux_proto_list mux_proto_htx =
2475{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2476
Willy Tarreau0108d902018-11-25 19:14:37 +01002477INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2478
Christopher Faulet51dbc942018-09-13 09:05:15 +02002479/*
2480 * Local variables:
2481 * c-indent-level: 8
2482 * c-basic-offset: 8
2483 * End:
2484 */