blob: 59784051ea9fa766e2175f0a36be565ebb02d33e [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>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010015#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010016#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020017
Christopher Faulet1be55f92018-10-02 15:59:23 +020018#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020019#include <types/proxy.h>
20#include <types/session.h>
21
Christopher Faulet51dbc942018-09-13 09:05:15 +020022#include <proto/connection.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020023#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020024#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020026#include <proto/stream.h>
27#include <proto/stream_interface.h>
28
29/*
30 * H1 Connection flags (32 bits)
31 */
32#define H1C_F_NONE 0x00000000
33
34/* Flags indicating why writing output data are blocked */
35#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
36#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
37/* 0x00000004 - 0x00000008 unused */
38
39/* Flags indicating why reading input data are blocked. */
40#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
41#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010042#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010043/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020044
45#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
46#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet666a0c42019-01-08 11:12:04 +010047#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020048#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020049
Christopher Fauletf2824e62018-10-01 12:12:37 +020050#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet129817b2018-09-20 16:14:40 +020051
Christopher Faulet51dbc942018-09-13 09:05:15 +020052/*
53 * H1 Stream flags (32 bits)
54 */
Christopher Faulet129817b2018-09-20 16:14:40 +020055#define H1S_F_NONE 0x00000000
56#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020057#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
58#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Christopher Faulet539e0292018-11-19 10:40:09 +010059/* 0x00000008 unused */
Christopher Fauletf2824e62018-10-01 12:12:37 +020060#define H1S_F_WANT_KAL 0x00000010
61#define H1S_F_WANT_TUN 0x00000020
62#define H1S_F_WANT_CLO 0x00000040
63#define H1S_F_WANT_MSK 0x00000070
64#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010065#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010066#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Willy Tarreau34d23482019-01-03 17:46:56 +010067#define H1S_F_HAVE_I_EOD 0x00000400 /* Set during input process to know the last empty chunk was processed */
68#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
69#define H1S_F_HAVE_O_EOD 0x00001000 /* Set during output process to know the last empty chunk was processed */
70#define H1S_F_HAVE_O_TLR 0x00002000 /* Set during output process to know the trailers were processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020071
72/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020073struct h1c {
74 struct connection *conn;
75 struct proxy *px;
76 uint32_t flags; /* Connection flags: H1C_F_* */
77
78 struct buffer ibuf; /* Input buffer to store data before parsing */
79 struct buffer obuf; /* Output buffer to store data after reformatting */
80
81 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
82 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
83
84 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010085 struct task *task; /* timeout management task */
86 int timeout; /* idle timeout duration in ticks */
87 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020088};
89
90/* H1 stream descriptor */
91struct h1s {
92 struct h1c *h1c;
93 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010094 struct cs_info csinfo; /* CS info, only used for client connections */
95 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020096
Christopher Faulet51dbc942018-09-13 09:05:15 +020097 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
98 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020099
Olivier Houchardf502aca2018-12-14 19:42:40 +0100100 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200101 struct h1m req;
102 struct h1m res;
103
104 enum http_meth_t meth; /* HTTP resquest method */
105 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200106};
107
108/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100109DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
110DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200111
Christopher Faulet51dbc942018-09-13 09:05:15 +0200112static int h1_recv(struct h1c *h1c);
113static int h1_send(struct h1c *h1c);
114static int h1_process(struct h1c *h1c);
115static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100116static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100117static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200118
119/*****************************************************/
120/* functions below are for dynamic buffer management */
121/*****************************************************/
122/*
123 * Indicates whether or not the we may call the h1_recv() function to
124 * attempt to receive data into the buffer and/or parse pending data. The
125 * condition is a bit complex due to some API limits for now. The rules are the
126 * following :
127 * - if an error or a shutdown was detected on the connection and the buffer
128 * is empty, we must not attempt to receive
129 * - if the input buffer failed to be allocated, we must not try to receive
130 * and we know there is nothing pending
131 * - if no flag indicates a blocking condition, we may attempt to receive,
132 * regardless of whether the input buffer is full or not, so that only de
133 * receiving part decides whether or not to block. This is needed because
134 * the connection API indeed prevents us from re-enabling receipt that is
135 * already enabled in a polled state, so we must always immediately stop as
136 * soon as the mux can't proceed so as never to hit an end of read with data
137 * pending in the buffers.
138 * - otherwise must may not attempt to receive
139 */
140static inline int h1_recv_allowed(const struct h1c *h1c)
141{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100142 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200143 return 0;
144
Christopher Fauletcf56b992018-12-11 16:12:31 +0100145 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
146 return 0;
147
Christopher Fauletcb55f482018-12-10 11:56:47 +0100148 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200149 return 1;
150
151 return 0;
152}
153
154/*
155 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
156 * flags are used to figure what buffer was requested. It returns 1 if the
157 * allocation succeeds, in which case the connection is woken up, or 0 if it's
158 * impossible to wake up and we prefer to be woken up later.
159 */
160static int h1_buf_available(void *target)
161{
162 struct h1c *h1c = target;
163
164 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
165 h1c->flags &= ~H1C_F_IN_ALLOC;
166 if (h1_recv_allowed(h1c))
167 tasklet_wakeup(h1c->wait_event.task);
168 return 1;
169 }
170
171 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
172 h1c->flags &= ~H1C_F_OUT_ALLOC;
173 tasklet_wakeup(h1c->wait_event.task);
174 return 1;
175 }
176
Christopher Faulet51dbc942018-09-13 09:05:15 +0200177 return 0;
178}
179
180/*
181 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
182 */
183static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
184{
185 struct buffer *buf = NULL;
186
187 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
188 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
189 h1c->buf_wait.target = h1c;
190 h1c->buf_wait.wakeup_cb = h1_buf_available;
191 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
192 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
193 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
194 __conn_xprt_stop_recv(h1c->conn);
195 }
196 return buf;
197}
198
199/*
200 * Release a buffer, if any, and try to wake up entities waiting in the buffer
201 * wait queue.
202 */
203static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
204{
205 if (bptr->size) {
206 b_free(bptr);
207 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
208 }
209}
210
Willy Tarreau00f18a32019-01-26 12:19:01 +0100211/* returns the number of streams in use on a connection to figure if it's
212 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
213 * as the caller will want to know if it was the last one after a detach().
214 */
215static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200216{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100217 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200218
Willy Tarreau00f18a32019-01-26 12:19:01 +0100219 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200220}
221
Willy Tarreau00f18a32019-01-26 12:19:01 +0100222/* returns the number of streams still available on a connection */
223static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100224{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100225 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100226}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200227
Willy Tarreau00f18a32019-01-26 12:19:01 +0100228
Christopher Faulet51dbc942018-09-13 09:05:15 +0200229/*****************************************************************/
230/* functions below are dedicated to the mux setup and management */
231/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100232static struct conn_stream *h1s_new_cs(struct h1s *h1s)
233{
234 struct conn_stream *cs;
235
236 cs = cs_new(h1s->h1c->conn);
237 if (!cs)
238 goto err;
239 h1s->cs = cs;
240 cs->ctx = h1s;
241
242 if (h1s->flags & H1S_F_NOT_FIRST)
243 cs->flags |= CS_FL_NOT_FIRST;
244
245 if (stream_create_from_cs(cs) < 0)
246 goto err;
247 return cs;
248
249 err:
250 cs_free(cs);
251 h1s->cs = NULL;
252 return NULL;
253}
254
Olivier Houchardf502aca2018-12-14 19:42:40 +0100255static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200256{
257 struct h1s *h1s;
258
259 h1s = pool_alloc(pool_head_h1s);
260 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100261 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200262
263 h1s->h1c = h1c;
264 h1c->h1s = h1s;
265
Olivier Houchardf502aca2018-12-14 19:42:40 +0100266 h1s->sess = sess;
267
Christopher Faulet51dbc942018-09-13 09:05:15 +0200268 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200269 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200270
271 h1s->recv_wait = NULL;
272 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200273
274 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200275 h1s->req.flags |= H1_MF_NO_PHDR;
276
Christopher Faulet129817b2018-09-20 16:14:40 +0200277 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200278 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200279
280 h1s->status = 0;
281 h1s->meth = HTTP_METH_OTHER;
282
Christopher Faulet47365272018-10-31 17:40:50 +0100283 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
284 h1s->flags |= H1S_F_NOT_FIRST;
285 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
286
Christopher Faulet129817b2018-09-20 16:14:40 +0200287 if (!conn_is_back(h1c->conn)) {
288 if (h1c->px->options2 & PR_O2_REQBUG_OK)
289 h1s->req.err_pos = -1;
290 }
291 else {
292 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
293 h1s->res.err_pos = -1;
294 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200295
Christopher Faulet539e0292018-11-19 10:40:09 +0100296 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
297 * create a new one.
298 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200299 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100300 h1s->csinfo.create_date = date;
301 h1s->csinfo.tv_create = now;
302 h1s->csinfo.t_handshake = 0;
303 h1s->csinfo.t_idle = -1;
304
Christopher Fauletf2824e62018-10-01 12:12:37 +0200305 cs->ctx = h1s;
306 h1s->cs = cs;
307 }
Christopher Faulet47365272018-10-31 17:40:50 +0100308 else {
Olivier Houchardf502aca2018-12-14 19:42:40 +0100309 /* For frontend connections we should always have a session */
310 sess = h1c->conn->owner;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100311
312 h1s->csinfo.create_date = sess->accept_date;
313 h1s->csinfo.tv_create = sess->tv_accept;
314 h1s->csinfo.t_handshake = sess->t_handshake;
315 h1s->csinfo.t_idle = -1;
316
Christopher Faulet47365272018-10-31 17:40:50 +0100317 cs = h1s_new_cs(h1s);
318 if (!cs)
319 goto fail;
320 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200321 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100322
323 fail:
324 pool_free(pool_head_h1s, h1s);
325 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200326}
327
328static void h1s_destroy(struct h1s *h1s)
329{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200330 if (h1s) {
331 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200332
Christopher Fauletf2824e62018-10-01 12:12:37 +0200333 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200334
Christopher Fauletf2824e62018-10-01 12:12:37 +0200335 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100336 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200337 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100338 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200339
Christopher Fauletcb55f482018-12-10 11:56:47 +0100340 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100341 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
342 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
343 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200344
Christopher Fauletf2824e62018-10-01 12:12:37 +0200345 cs_free(h1s->cs);
346 pool_free(pool_head_h1s, h1s);
347 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200348}
349
Christopher Fauletfeb11742018-11-29 15:12:34 +0100350static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
351{
352 struct h1s *h1s = cs->ctx;
353
354 if (h1s && !conn_is_back(cs->conn))
355 return &h1s->csinfo;
356 return NULL;
357}
358
Christopher Faulet51dbc942018-09-13 09:05:15 +0200359/*
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100360 * Initialize the mux once it's attached. It is expected that conn->ctx
Christopher Faulet51dbc942018-09-13 09:05:15 +0200361 * points to the existing conn_stream (for outgoing connections) or NULL (for
362 * incoming ones). Returns < 0 on error.
363 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100364static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200365{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200366 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100367 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200368
369 h1c = pool_alloc(pool_head_h1c);
370 if (!h1c)
371 goto fail_h1c;
372 h1c->conn = conn;
373 h1c->px = proxy;
374
375 h1c->flags = H1C_F_NONE;
376 h1c->ibuf = BUF_NULL;
377 h1c->obuf = BUF_NULL;
378 h1c->h1s = NULL;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100379 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200380
Christopher Faulet51dbc942018-09-13 09:05:15 +0200381 LIST_INIT(&h1c->buf_wait.list);
382 h1c->wait_event.task = tasklet_new();
383 if (!h1c->wait_event.task)
384 goto fail;
385 h1c->wait_event.task->process = h1_io_cb;
386 h1c->wait_event.task->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100387 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200388
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100389 if (conn->ctx) {
390 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
391 if (tick_isset(proxy->timeout.serverfin))
392 h1c->shut_timeout = proxy->timeout.serverfin;
393 } else {
394 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
395 if (tick_isset(proxy->timeout.clientfin))
396 h1c->shut_timeout = proxy->timeout.clientfin;
397 }
398 if (tick_isset(h1c->timeout)) {
399 t = task_new(tid_bit);
400 if (!t)
401 goto fail;
402
403 h1c->task = t;
404 t->process = h1_timeout_task;
405 t->context = h1c;
406 t->expire = tick_add(now_ms, h1c->timeout);
407 }
408
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200409 if (!(conn->flags & CO_FL_CONNECTED))
410 h1c->flags |= H1C_F_CS_WAIT_CONN;
411
Christopher Fauletf2824e62018-10-01 12:12:37 +0200412 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100413 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200414 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200415
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100416 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200417
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100418
419 if (t)
420 task_queue(t);
421
Christopher Faulet51dbc942018-09-13 09:05:15 +0200422 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard9b960a82019-01-04 16:51:40 +0100423 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200424
425 /* mux->wake will be called soon to complete the operation */
426 return 0;
427
428 fail:
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100429 if (t)
430 task_free(t);
Christopher Faulet47365272018-10-31 17:40:50 +0100431 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200432 tasklet_free(h1c->wait_event.task);
433 pool_free(pool_head_h1c, h1c);
434 fail_h1c:
435 return -1;
436}
437
438
439/* release function for a connection. This one should be called to free all
440 * resources allocated to the mux.
441 */
442static void h1_release(struct connection *conn)
443{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100444 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200445
Christopher Faulet51dbc942018-09-13 09:05:15 +0200446 if (h1c) {
447 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
448 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
449 LIST_DEL(&h1c->buf_wait.list);
450 LIST_INIT(&h1c->buf_wait.list);
451 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
452 }
453
454 h1_release_buf(h1c, &h1c->ibuf);
455 h1_release_buf(h1c, &h1c->obuf);
456
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100457 if (h1c->task) {
458 h1c->task->context = NULL;
459 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
460 h1c->task = NULL;
461 }
462
Christopher Faulet51dbc942018-09-13 09:05:15 +0200463 if (h1c->wait_event.task)
464 tasklet_free(h1c->wait_event.task);
465
Christopher Fauletf2824e62018-10-01 12:12:37 +0200466 h1s_destroy(h1c->h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100467 if (h1c->wait_event.events != 0)
468 conn->xprt->unsubscribe(conn, h1c->wait_event.events,
Christopher Faulet51dbc942018-09-13 09:05:15 +0200469 &h1c->wait_event);
470 pool_free(pool_head_h1c, h1c);
471 }
472
473 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100474 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200475
476 conn_stop_tracking(conn);
477 conn_full_close(conn);
478 if (conn->destroy_cb)
479 conn->destroy_cb(conn);
480 conn_free(conn);
481}
482
483/******************************************************/
484/* functions below are for the H1 protocol processing */
485/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200486/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
487 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200488 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100489static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200490{
Christopher Faulet570d1612018-11-26 11:13:57 +0100491 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200492
Christopher Faulet570d1612018-11-26 11:13:57 +0100493 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200494 (*(p + 5) > '1' ||
495 (*(p + 5) == '1' && *(p + 7) >= '1')))
496 h1m->flags |= H1_MF_VER_11;
497}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200498
Christopher Faulet9768c262018-10-22 09:34:31 +0200499/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
500 * greater or equal to 1.1
501 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100502static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200503{
Christopher Faulet570d1612018-11-26 11:13:57 +0100504 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200505
Christopher Faulet570d1612018-11-26 11:13:57 +0100506 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200507 (*(p + 5) > '1' ||
508 (*(p + 5) == '1' && *(p + 7) >= '1')))
509 h1m->flags |= H1_MF_VER_11;
510}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200511
Christopher Faulet9768c262018-10-22 09:34:31 +0200512/*
513 * Check the validity of the request version. If the version is valid, it
514 * returns 1. Otherwise, it returns 0.
515 */
516static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
517{
518 struct h1c *h1c = h1s->h1c;
519
520 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
521 * exactly one digit "." one digit. This check may be disabled using
522 * option accept-invalid-http-request.
523 */
524 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
525 if (sl.rq.v.len != 8)
526 return 0;
527
528 if (*(sl.rq.v.ptr + 4) != '/' ||
529 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
530 *(sl.rq.v.ptr + 6) != '.' ||
531 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
532 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200533 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200534 else if (!sl.rq.v.len) {
535 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200536
Christopher Faulet9768c262018-10-22 09:34:31 +0200537 /* RFC 1945 allows only GET for HTTP/0.9 requests */
538 if (sl.rq.meth != HTTP_METH_GET)
539 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200540
Christopher Faulet9768c262018-10-22 09:34:31 +0200541 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
542 if (!sl.rq.u.len)
543 return 0;
544
545 /* Add HTTP version */
546 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100547 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200548 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100549
550 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100551 ((*(sl.rq.v.ptr + 5) > '1') ||
552 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100553 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200554 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200555}
556
Christopher Faulet9768c262018-10-22 09:34:31 +0200557/*
558 * Check the validity of the response version. If the version is valid, it
559 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200560 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200561static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200562{
Christopher Faulet9768c262018-10-22 09:34:31 +0200563 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200564
Christopher Faulet9768c262018-10-22 09:34:31 +0200565 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
566 * exactly one digit "." one digit. This check may be disabled using
567 * option accept-invalid-http-request.
568 */
569 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
570 if (sl.st.v.len != 8)
571 return 0;
572
573 if (*(sl.st.v.ptr + 4) != '/' ||
574 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
575 *(sl.st.v.ptr + 6) != '.' ||
576 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
577 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200578 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100579
580 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100581 ((*(sl.st.v.ptr + 5) > '1') ||
582 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100583 h1m->flags |= H1_MF_VER_11;
584
Christopher Faulet9768c262018-10-22 09:34:31 +0200585 return 1;
586}
587/* Remove all "Connection:" headers from the HTX message <htx> */
588static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
589{
590 struct ist hdr = {.ptr = "Connection", .len = 10};
591 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200592
Christopher Faulet9768c262018-10-22 09:34:31 +0200593 while (http_find_header(htx, hdr, &ctx, 1))
594 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200595
Christopher Faulet9768c262018-10-22 09:34:31 +0200596 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
597}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200598
Christopher Faulet9768c262018-10-22 09:34:31 +0200599/* Add a "Connection:" header with the value <value> into the HTX message
600 * <htx>.
601 */
602static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
603{
604 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200605
Christopher Faulet9768c262018-10-22 09:34:31 +0200606 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200607}
608
609/* Deduce the connection mode of the client connection, depending on the
610 * configuration and the H1 message flags. This function is called twice, the
611 * first time when the request is parsed and the second time when the response
612 * is parsed.
613 */
614static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
615{
616 struct proxy *fe = h1s->h1c->px;
617 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
618
619 /* Tunnel mode can only by set on the frontend */
620 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
621 flag = H1S_F_WANT_TUN;
622 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
623 flag = H1S_F_WANT_CLO;
624
625 /* flags order: CLO > SCL > TUN > KAL */
626 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
627 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
628
629 if (h1m->flags & H1_MF_RESP) {
630 /* Either we've established an explicit tunnel, or we're
631 * switching the protocol. In both cases, we're very unlikely to
632 * understand the next protocols. We have to switch to tunnel
633 * mode, so that we transfer the request and responses then let
634 * this protocol pass unmodified. When we later implement
635 * specific parsers for such protocols, we'll want to check the
636 * Upgrade header which contains information about that protocol
637 * for responses with status 101 (eg: see RFC2817 about TLS).
638 */
639 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
640 h1s->status == 101)
641 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletbd44ca62018-11-29 09:55:22 +0100642 else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */
643 (h1m->flags & H1_MF_CONN_CLO && h1s->req.state != H1_MSG_DONE)) /*explicit close and unfinished request */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200644 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
645 }
646 else {
647 if (h1s->flags & H1S_F_WANT_KAL &&
648 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
649 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
650 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
651 }
652
653 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
654 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
655 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
656}
657
658/* Deduce the connection mode of the client connection, depending on the
659 * configuration and the H1 message flags. This function is called twice, the
660 * first time when the request is parsed and the second time when the response
661 * is parsed.
662 */
663static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
664{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100665 struct h1c *h1c = h1s->h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100666 struct session *sess = h1s->sess;
Christopher Fauleta1692f52018-11-22 10:19:50 +0100667 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200668 int flag = H1S_F_WANT_KAL;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100669 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200670
671 /* Tunnel mode can only by set on the frontend */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100672 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200673 flag = H1S_F_WANT_TUN;
674
675 /* For the server connection: server-close == httpclose */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100676 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200677 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Olivier Houchardf502aca2018-12-14 19:42:40 +0100678 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200679 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
680 flag = H1S_F_WANT_CLO;
681
682 /* flags order: CLO > SCL > TUN > KAL */
683 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
684 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
685
686 if (h1m->flags & H1_MF_RESP) {
687 /* Either we've established an explicit tunnel, or we're
688 * switching the protocol. In both cases, we're very unlikely to
689 * understand the next protocols. We have to switch to tunnel
690 * mode, so that we transfer the request and responses then let
691 * this protocol pass unmodified. When we later implement
692 * specific parsers for such protocols, we'll want to check the
693 * Upgrade header which contains information about that protocol
694 * for responses with status 101 (eg: see RFC2817 about TLS).
695 */
696 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
697 h1s->status == 101)
698 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
699 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
700 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
701 else if (h1s->flags & H1S_F_WANT_KAL &&
702 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
703 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
704 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
705 }
Christopher Faulet70033782018-12-05 13:50:11 +0100706 else {
707 if (h1s->flags & H1S_F_WANT_KAL &&
708 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
709 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
710 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
711 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200712
713 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
714 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
715 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200716}
717
Christopher Faulet9768c262018-10-22 09:34:31 +0200718static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
719 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200720{
721 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200722
723 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
724 * token is found
725 */
726 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200727 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200728
729 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200730 if (h1m->flags & H1_MF_CONN_CLO) {
731 if (conn_val)
732 *conn_val = ist("");
733 if (htx)
734 h1_remove_conn_hdrs(h1m, htx);
735 }
736 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
737 if (conn_val)
738 *conn_val = ist("keep-alive");
739 if (htx)
740 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
741 }
Christopher Fauletce851492018-12-07 18:06:59 +0100742 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
743 if (conn_val)
744 *conn_val = ist("");
745 if (htx)
746 h1_remove_conn_hdrs(h1m, htx);
747 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200748 }
749 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200750 if (h1m->flags & H1_MF_CONN_KAL) {
751 if (conn_val)
752 *conn_val = ist("");
753 if (htx)
754 h1_remove_conn_hdrs(h1m, htx);
755 }
756 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
757 if (conn_val)
758 *conn_val = ist("close");
759 if (htx)
760 h1_add_conn_hdr(h1m, htx, ist("close"));
761 }
Christopher Fauletce851492018-12-07 18:06:59 +0100762 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
763 if (conn_val)
764 *conn_val = ist("");
765 if (htx)
766 h1_remove_conn_hdrs(h1m, htx);
767 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200768 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200769}
770
Christopher Faulet9768c262018-10-22 09:34:31 +0200771static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
772 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200773{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200774 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
775 * token is found
776 */
777 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200778 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200779
780 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200781 if (h1m->flags & H1_MF_CONN_CLO) {
782 if (conn_val)
783 *conn_val = ist("");
784 if (htx)
785 h1_remove_conn_hdrs(h1m, htx);
786 }
Willy Tarreau7701cad2019-02-08 15:35:38 +0100787 if (!(h1m->flags & H1_MF_CONN_KAL) &&
788 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200789 if (conn_val)
790 *conn_val = ist("keep-alive");
791 if (htx)
792 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
793 }
Willy Tarreau7701cad2019-02-08 15:35:38 +0100794 else if ((h1m->flags & H1_MF_CONN_KAL) &&
795 ((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
Christopher Fauletce851492018-12-07 18:06:59 +0100796 if (conn_val)
797 *conn_val = ist("");
798 if (htx)
799 h1_remove_conn_hdrs(h1m, htx);
800 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200801 }
802 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200803 if (h1m->flags & H1_MF_CONN_KAL) {
804 if (conn_val)
805 *conn_val = ist("");
806 if (htx)
807 h1_remove_conn_hdrs(h1m, htx);
808 }
809 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
810 if (conn_val)
811 *conn_val = ist("close");
812 if (htx)
813 h1_add_conn_hdr(h1m, htx, ist("close"));
814 }
Christopher Fauletce851492018-12-07 18:06:59 +0100815 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
816 if (conn_val)
817 *conn_val = ist("");
818 if (htx)
819 h1_remove_conn_hdrs(h1m, htx);
820 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200821 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200822}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200823
Christopher Faulet9768c262018-10-22 09:34:31 +0200824/* Set the right connection mode and update "Connection:" header if
825 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
826 * message is updated accordingly. When <conn_val> is not NULL, it is set with
827 * the new header value.
828 */
829static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
830 struct htx *htx, struct ist *conn_val)
831{
832 if (!conn_is_back(h1s->h1c->conn)) {
833 h1_set_cli_conn_mode(h1s, h1m);
834 if (h1m->flags & H1_MF_RESP)
835 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
836 }
837 else {
838 h1_set_srv_conn_mode(h1s, h1m);
839 if (!(h1m->flags & H1_MF_RESP))
840 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
841 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200842}
843
Christopher Faulete44769b2018-11-29 23:01:45 +0100844
845/* Append the description of what is present in error snapshot <es> into <out>.
846 * The description must be small enough to always fit in a buffer. The output
847 * buffer may be the trash so the trash must not be used inside this function.
848 */
849static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
850{
851 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100852 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
853 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
854 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
855 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
856 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
857 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100858}
859/*
860 * Capture a bad request or response and archive it in the proxy's structure.
861 * By default it tries to report the error position as h1m->err_pos. However if
862 * this one is not set, it will then report h1m->next, which is the last known
863 * parsing point. The function is able to deal with wrapping buffers. It always
864 * displays buffers as a contiguous area starting at buf->p. The direction is
865 * determined thanks to the h1m's flags.
866 */
867static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
868 struct h1m *h1m, struct buffer *buf)
869{
870 struct session *sess = h1c->conn->owner;
871 struct proxy *proxy = h1c->px;
872 struct proxy *other_end = sess->fe;
873 union error_snapshot_ctx ctx;
874
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100875 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100876 other_end = si_strm(h1s->cs->data)->be;
877
878 /* http-specific part now */
879 ctx.h1.state = h1m->state;
880 ctx.h1.c_flags = h1c->flags;
881 ctx.h1.s_flags = h1s->flags;
882 ctx.h1.m_flags = h1m->flags;
883 ctx.h1.m_clen = h1m->curr_len;
884 ctx.h1.m_blen = h1m->body_len;
885
886 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
887 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100888 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
889 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100890}
891
Christopher Fauletadb22202018-12-12 10:32:09 +0100892/* Emit the chunksize followed by a CRLF in front of data of the buffer
893 * <buf>. It goes backwards and starts with the byte before the buffer's
894 * head. The caller is responsible for ensuring there is enough room left before
895 * the buffer's head for the string.
896 */
897static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
898{
899 char *beg, *end;
900
901 beg = end = b_head(buf);
902 *--beg = '\n';
903 *--beg = '\r';
904 do {
905 *--beg = hextab[chksz & 0xF];
906 } while (chksz >>= 4);
907 buf->head -= (end - beg);
908 b_add(buf, end - beg);
909}
910
911/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
912 * ensuring there is enough room left in the buffer for the string. */
913static void h1_emit_chunk_crlf(struct buffer *buf)
914{
915 *(b_peek(buf, b_data(buf))) = '\r';
916 *(b_peek(buf, b_data(buf) + 1)) = '\n';
917 b_add(buf, 2);
918}
919
Christopher Faulet129817b2018-09-20 16:14:40 +0200920/*
921 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200922 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
923 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800924 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200925 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200926static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200927 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200928{
929 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100930 union h1_sl h1sl;
931 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200932 int ret = 0;
933
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100934 if (!max)
935 goto end;
936
Christopher Faulet129817b2018-09-20 16:14:40 +0200937 /* Realing input buffer if necessary */
938 if (b_head(buf) + b_data(buf) > b_wrap(buf))
939 b_slow_realign(buf, trash.area, 0);
940
Christopher Fauletf2824e62018-10-01 12:12:37 +0200941 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100942 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200943 if (ret <= 0) {
944 /* Incomplete or invalid message. If the buffer is full, it's an
945 * error because headers are too large to be handled by the
946 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200947 if (ret < 0 || (!ret && b_full(buf)))
948 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200949 goto end;
950 }
951
952 /* messages headers fully parsed, do some checks to prepare the body
953 * parsing.
954 */
955
956 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200957 if (ret > (b_size(buf) - global.tune.maxrewrite))
958 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200959
Christopher Faulet9768c262018-10-22 09:34:31 +0200960 /* Save the request's method or the response's status, check if the body
961 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200962 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100963 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200964
Christopher Faulet129817b2018-09-20 16:14:40 +0200965 /* Request have always a known length */
966 h1m->flags |= H1_MF_XFER_LEN;
967 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
968 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200969
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100970 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
971 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200972 h1m->err_state = h1m->state;
973 goto vsn_error;
974 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200975 }
976 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100977 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200978
979 if ((h1s->meth == HTTP_METH_HEAD) ||
980 (h1s->status >= 100 && h1s->status < 200) ||
981 (h1s->status == 204) || (h1s->status == 304) ||
982 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
983 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
984 h1m->flags |= H1_MF_XFER_LEN;
985 h1m->curr_len = h1m->body_len = 0;
986 h1m->state = H1_MSG_DONE;
987 }
988 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
989 h1m->flags |= H1_MF_XFER_LEN;
990 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
991 h1m->state = H1_MSG_DONE;
992 }
993 else
994 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200995
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100996 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
997 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200998 h1m->err_state = h1m->state;
999 goto vsn_error;
1000 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001001 }
1002
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001003 /* Set HTX start-line flags */
1004 if (h1m->flags & H1_MF_VER_11)
1005 flags |= HTX_SL_F_VER_11;
1006 if (h1m->flags & H1_MF_XFER_ENC)
1007 flags |= HTX_SL_F_XFER_ENC;
1008 if (h1m->flags & H1_MF_XFER_LEN) {
1009 flags |= HTX_SL_F_XFER_LEN;
1010 if (h1m->flags & H1_MF_CHNK)
1011 flags |= HTX_SL_F_CHNK;
1012 else if (h1m->flags & H1_MF_CLEN)
1013 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001014 if (h1m->state == H1_MSG_DONE)
1015 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001016 }
1017
Christopher Faulet9768c262018-10-22 09:34:31 +02001018 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001019 struct htx_sl *sl;
1020
1021 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1022 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001023 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001024 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001025 }
1026 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001027 struct htx_sl *sl;
1028
1029 flags |= HTX_SL_F_IS_RESP;
1030 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1031 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001032 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001033 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001034 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001035
Christopher Faulet9768c262018-10-22 09:34:31 +02001036 if (h1m->state == H1_MSG_DONE)
1037 if (!htx_add_endof(htx, HTX_BLK_EOM))
1038 goto error;
1039
1040 h1_process_conn_mode(h1s, h1m, htx, NULL);
1041
1042 /* If body length cannot be determined, set htx->extra to
1043 * ULLONG_MAX. This value is impossible in other cases.
1044 */
1045 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
1046
1047 /* Recheck there is enough space to do headers rewritting */
1048 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
1049 goto error;
1050
1051 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001052 end:
1053 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001054
1055 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001056 h1m->err_state = h1m->state;
1057 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001058 vsn_error:
1059 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +01001060 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001061 ret = 0;
1062 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001063}
1064
1065/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001066 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1067 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001068 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001069 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001070 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001071static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001072 struct buffer *buf, size_t *ofs, size_t max,
Christopher Fauletafe57842019-01-21 11:55:02 +01001073 struct buffer *htxbuf, size_t reserve)
Christopher Faulet129817b2018-09-20 16:14:40 +02001074{
Christopher Fauletafe57842019-01-21 11:55:02 +01001075 uint32_t data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001076 size_t total = 0;
1077 int ret = 0;
1078
Christopher Fauletafe57842019-01-21 11:55:02 +01001079 data_space = htx_free_data_space(htx);
1080 if (data_space <= reserve)
1081 goto end;
1082 data_space -= reserve;
1083
Christopher Faulet129817b2018-09-20 16:14:40 +02001084 if (h1m->flags & H1_MF_XFER_LEN) {
1085 if (h1m->flags & H1_MF_CLEN) {
1086 /* content-length: read only h2m->body_len */
1087 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001088 if (ret > data_space)
1089 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001090 if ((uint64_t)ret > h1m->curr_len)
1091 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001092 if (ret > b_contig_data(buf, *ofs))
1093 ret = b_contig_data(buf, *ofs);
1094 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001095 /* very often with large files we'll face the following
1096 * situation :
1097 * - htx is empty and points to <htxbuf>
1098 * - ret == buf->data
1099 * - buf->head == sizeof(struct htx)
1100 * => we can swap the buffers and place an htx header into
1101 * the target buffer instead
1102 */
1103 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1104 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1105 void *raw_area = buf->area;
1106 void *htx_area = htxbuf->area;
1107 struct htx_blk *blk;
1108
1109 buf->area = htx_area;
1110 htxbuf->area = raw_area;
1111 htx = (struct htx *)htxbuf->area;
1112 htx->size = htxbuf->size - sizeof(*htx);
1113 htx_reset(htx);
1114 b_set_data(htxbuf, b_size(htxbuf));
1115
1116 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1117 blk->info += ret;
1118 /* nothing else to do, the old buffer now contains an
1119 * empty pre-initialized HTX header
1120 */
1121 }
1122 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001123 goto end;
1124 h1m->curr_len -= ret;
1125 *ofs += ret;
1126 total += ret;
1127 }
1128
1129 if (!h1m->curr_len) {
1130 if (!htx_add_endof(htx, HTX_BLK_EOM))
1131 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001132 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +02001133 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001134 }
1135 else if (h1m->flags & H1_MF_CHNK) {
1136 new_chunk:
1137 /* te:chunked : parse chunks */
1138 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001139 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001140 if (ret <= 0)
1141 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001142 h1m->state = H1_MSG_CHUNK_SIZE;
1143
Christopher Faulet129817b2018-09-20 16:14:40 +02001144 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001145 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001146 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001147 }
1148
1149 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1150 unsigned int chksz;
1151
Christopher Fauletf2824e62018-10-01 12:12:37 +02001152 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001153 if (ret <= 0)
1154 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001155 if (!chksz) {
1156 if (!htx_add_endof(htx, HTX_BLK_EOD))
1157 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001158 h1s->flags |= H1S_F_HAVE_I_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001159 h1m->state = H1_MSG_TRAILERS;
1160 }
1161 else
1162 h1m->state = H1_MSG_DATA;
1163
Christopher Faulet129817b2018-09-20 16:14:40 +02001164 h1m->curr_len = chksz;
1165 h1m->body_len += chksz;
1166 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001167 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001168 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001169 }
1170
1171 if (h1m->state == H1_MSG_DATA) {
1172 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001173 if (ret > data_space)
1174 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001175 if ((uint64_t)ret > h1m->curr_len)
1176 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001177 if (ret > b_contig_data(buf, *ofs))
1178 ret = b_contig_data(buf, *ofs);
1179 if (ret) {
1180 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1181 goto end;
1182 h1m->curr_len -= ret;
1183 max -= ret;
1184 *ofs += ret;
1185 total += ret;
1186 }
1187 if (!h1m->curr_len) {
1188 h1m->state = H1_MSG_CHUNK_CRLF;
Christopher Fauletafe57842019-01-21 11:55:02 +01001189 data_space = htx_free_data_space(htx);
1190 if (data_space <= reserve)
1191 goto end;
1192 data_space -= reserve;
Christopher Faulet9768c262018-10-22 09:34:31 +02001193 goto new_chunk;
1194 }
1195 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001196 }
1197
1198 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001199 /* Trailers were alread parsed, only the EOM
1200 * need to be added */
Willy Tarreau34d23482019-01-03 17:46:56 +01001201 if (h1s->flags & H1S_F_HAVE_I_TLR)
Christopher Faulet17276482018-11-22 11:44:35 +01001202 goto skip_tlr_parsing;
1203
Christopher Fauletf2824e62018-10-01 12:12:37 +02001204 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001205 if (ret > data_space)
1206 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001207 if (ret <= 0)
1208 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001209
1210 /* Realing input buffer if tailers wrap. For now
1211 * this is a workaroung. Because trailers are
1212 * not split on CRLF, like headers, there is no
1213 * way to know where to split it when trailers
1214 * wrap. This is a limitation of
1215 * h1_measure_trailers.
1216 */
1217 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1218 b_slow_realign(buf, trash.area, 0);
1219
1220 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1221 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001222 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001223 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001224 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001225 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001226
Christopher Faulet17276482018-11-22 11:44:35 +01001227 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001228 if (!htx_add_endof(htx, HTX_BLK_EOM))
1229 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001230 h1m->state = H1_MSG_DONE;
1231 }
1232 }
1233 else {
1234 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1235 * is no body. Switch the message in DONE state
1236 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001237 if (!htx_add_endof(htx, HTX_BLK_EOM))
1238 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001239 h1m->state = H1_MSG_DONE;
1240 }
1241 }
1242 else {
1243 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001244 ret = max;
1245 if (ret > data_space)
1246 ret = data_space;
1247 if (ret > b_contig_data(buf, *ofs))
1248 ret = b_contig_data(buf, *ofs);
1249 if (ret) {
1250 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1251 goto end;
1252
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001253 *ofs += ret;
1254 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001255 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001256 }
1257
1258 end:
1259 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001260 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001261 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001262 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001263 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001264 return 0;
1265 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001266 /* update htx->extra, only when the body length is known */
1267 if (h1m->flags & H1_MF_XFER_LEN)
1268 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001269 return total;
1270}
1271
1272/*
1273 * Synchronize the request and the response before reseting them. Except for 1xx
1274 * responses, we wait that the request and the response are in DONE state and
1275 * that all data are forwarded for both. For 1xx responses, only the response is
1276 * reset, waiting the final one. Many 1xx messages can be sent.
1277 */
1278static void h1_sync_messages(struct h1c *h1c)
1279{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001280 struct h1s *h1s = h1c->h1s;
1281
1282 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001283 return;
1284
Christopher Fauletf2824e62018-10-01 12:12:37 +02001285 if (h1s->res.state == H1_MSG_DONE &&
1286 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001287 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001288 /* For 100-Continue response or any other informational 1xx
1289 * response which is non-final, don't reset the request, the
1290 * transaction is not finished. We take care the response was
1291 * transferred before.
1292 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001293 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001294 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001295 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet129817b2018-09-20 16:14:40 +02001296 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001297 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001298 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1299 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001300 h1m_init_req(&h1s->req);
1301 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001302 h1s->req.state = H1_MSG_TUNNEL;
1303 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001304 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001305 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001306 }
1307}
1308
1309/*
1310 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001311 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1312 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001313 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001314static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001315{
Christopher Faulet539e0292018-11-19 10:40:09 +01001316 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001317 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001318 struct htx *htx;
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001319 size_t data = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001320 size_t total = 0;
1321 size_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001322 size_t count, rsv;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001323 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001324
Christopher Faulet539e0292018-11-19 10:40:09 +01001325 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001326
Christopher Fauletf2824e62018-10-01 12:12:37 +02001327 if (!conn_is_back(h1c->conn)) {
1328 h1m = &h1s->req;
1329 errflag = H1S_F_REQ_ERROR;
1330 }
1331 else {
1332 h1m = &h1s->res;
1333 errflag = H1S_F_RES_ERROR;
1334 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001335
Christopher Faulet74027762019-02-26 14:45:05 +01001336 data = htx->data;
1337 count = b_data(&h1c->ibuf);
1338 if (!count)
1339 goto end;
1340 rsv = ((flags & CO_RFL_KEEP_RSV) ? global.tune.maxrewrite : 0);
1341
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001342 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001343 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001344 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001345 if (!ret)
1346 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001347 }
1348 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Fauletafe57842019-01-21 11:55:02 +01001349 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001350 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001351 if (!ret)
1352 break;
1353 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001354 else if (h1m->state == H1_MSG_DONE) {
1355 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001356 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001357 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001358 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Fauletafe57842019-01-21 11:55:02 +01001359 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001360 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001361 if (!ret)
1362 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001363 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001364 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001365 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001366 break;
1367 }
1368
Christopher Faulet539e0292018-11-19 10:40:09 +01001369 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001370 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001371
Christopher Faulet47365272018-10-31 17:40:50 +01001372 if (h1s->flags & errflag)
1373 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001374
Christopher Faulet539e0292018-11-19 10:40:09 +01001375 b_del(&h1c->ibuf, total);
1376
1377 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001378 htx_to_buf(htx, buf);
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001379 data = (htx->data - data);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001380 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001381 h1c->flags &= ~H1C_F_IN_FULL;
1382 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001383 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001384
Christopher Fauletcf56b992018-12-11 16:12:31 +01001385 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1386
1387 if (!b_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001388 h1_release_buf(h1c, &h1c->ibuf);
1389 h1_sync_messages(h1c);
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001390 }
Christopher Fauletcf56b992018-12-11 16:12:31 +01001391 else if (!htx_is_empty(htx))
1392 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001393
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001394 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1395 h1s->cs->flags |= CS_FL_EOS;
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001396 if (h1m->state < H1_MSG_DONE)
1397 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001398 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001399
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001400 return data;
Christopher Faulet47365272018-10-31 17:40:50 +01001401
1402 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001403 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001404 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001405 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001406 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001407 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001408}
1409
Christopher Faulet129817b2018-09-20 16:14:40 +02001410/*
1411 * Process outgoing data. It parses data and transfer them from the channel buffer into
1412 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1413 * 0 if it couldn't proceed.
1414 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001415static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1416{
Christopher Faulet129817b2018-09-20 16:14:40 +02001417 struct h1s *h1s = h1c->h1s;
1418 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001419 struct htx *chn_htx;
1420 struct htx_blk *blk;
1421 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001422 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001423 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001424 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001425
Christopher Faulet47365272018-10-31 17:40:50 +01001426 if (!count)
1427 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001428
Christopher Faulet9768c262018-10-22 09:34:31 +02001429 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001430 if (htx_is_empty(chn_htx))
1431 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001432
Christopher Faulet51dbc942018-09-13 09:05:15 +02001433 if (!h1_get_buf(h1c, &h1c->obuf)) {
1434 h1c->flags |= H1C_F_OUT_ALLOC;
1435 goto end;
1436 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001437
Christopher Fauletf2824e62018-10-01 12:12:37 +02001438 if (!conn_is_back(h1c->conn)) {
1439 h1m = &h1s->res;
1440 errflag = H1S_F_RES_ERROR;
1441 }
1442 else {
1443 h1m = &h1s->req;
1444 errflag = H1S_F_REQ_ERROR;
1445 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001446
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001447 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001448 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001449
1450 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001451
Willy Tarreau3815b222018-12-11 19:50:43 +01001452 /* Perform some optimizations to reduce the number of buffer copies.
1453 * First, if the mux's buffer is empty and the htx area contains
1454 * exactly one data block of the same size as the requested count,
1455 * then it's possible to simply swap the caller's buffer with the
1456 * mux's output buffer and adjust offsets and length to match the
1457 * entire DATA HTX block in the middle. In this case we perform a
1458 * true zero-copy operation from end-to-end. This is the situation
1459 * that happens all the time with large files. Second, if this is not
1460 * possible, but the mux's output buffer is empty, we still have an
1461 * opportunity to avoid the copy to the intermediary buffer, by making
1462 * the intermediary buffer's area point to the output buffer's area.
1463 * In this case we want to skip the HTX header to make sure that copies
1464 * remain aligned and that this operation remains possible all the
1465 * time. This goes for headers, data blocks and any data extracted from
1466 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001467 */
1468 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001469 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001470
1471 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001472 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001473 htx_get_blk_value(chn_htx, blk).len == count) {
1474 void *old_area = h1c->obuf.area;
1475
1476 h1c->obuf.area = buf->area;
1477 h1c->obuf.data = count;
1478
1479 buf->area = old_area;
1480 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001481
1482 /* The message is chunked. We need to emit the chunk
1483 * size. We have at least the size of the struct htx to
1484 * write the chunk envelope. It should be enough.
1485 */
1486 if (h1m->flags & H1_MF_CHNK) {
1487 h1_emit_chunk_size(&h1c->obuf, count);
1488 h1_emit_chunk_crlf(&h1c->obuf);
1489 }
1490
Willy Tarreau3815b222018-12-11 19:50:43 +01001491 total += count;
1492 goto out;
1493 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001494 tmp->area = h1c->obuf.area + h1c->obuf.head;
1495 }
1496
Christopher Faulet9768c262018-10-22 09:34:31 +02001497 tmp->size = b_room(&h1c->obuf);
1498
Christopher Fauletb2e84162018-12-06 11:39:49 +01001499 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001500 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001501 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001502 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001503 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001504 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001505
Christopher Fauletb2e84162018-12-06 11:39:49 +01001506 vlen = sz;
1507 if (vlen > count) {
1508 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1509 goto copy;
1510 vlen = count;
1511 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001512
Christopher Fauletb2e84162018-12-06 11:39:49 +01001513 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001514 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001515 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001516
1517 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001518 h1m_init_req(h1m);
1519 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001520 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001521 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001522 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001523 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001524 goto copy;
1525 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001526 if (sl->flags & HTX_SL_F_BODYLESS)
1527 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001528 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001529 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001530
Christopher Faulet9768c262018-10-22 09:34:31 +02001531 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001532 h1m_init_res(h1m);
1533 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001534 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001535 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001536 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001537 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001538 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001539 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001540 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001541 if (sl->info.res.status < 200 &&
1542 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1543 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001544 h1m->state = H1_MSG_HDR_FIRST;
1545 break;
1546
1547 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001548 h1m->state = H1_MSG_HDR_NAME;
1549 n = htx_get_blk_name(chn_htx, blk);
1550 v = htx_get_blk_value(chn_htx, blk);
1551
1552 if (isteqi(n, ist("transfer-encoding")))
1553 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001554 else if (isteqi(n, ist("content-length"))) {
1555 if (h1_parse_cont_len_header(h1m, &v) <= 0)
1556 goto skip_hdr;
1557 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001558 else if (isteqi(n, ist("connection"))) {
1559 h1_parse_connection_header(h1m, v);
1560 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001561 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001562 if (!v.len)
1563 goto skip_hdr;
1564 }
1565
Christopher Fauletc59ff232018-12-03 13:58:44 +01001566 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001567 goto copy;
1568 skip_hdr:
1569 h1m->state = H1_MSG_HDR_L2_LWS;
1570 break;
1571
1572 case HTX_BLK_PHDR:
1573 /* not implemented yet */
1574 h1m->flags |= errflag;
1575 break;
1576
1577 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001578 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001579 /* There is no "Connection:" header and
1580 * it the conn_mode must be
1581 * processed. So do it */
1582 n = ist("Connection");
1583 v = ist("");
1584 h1_process_conn_mode(h1s, h1m, NULL, &v);
1585 process_conn_mode = 0;
1586 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001587 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001588 goto copy;
1589 }
1590 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001591
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001592 if (((h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1593 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1594 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1595 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1596 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1597 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001598 /* chunking needed but header not seen */
1599 if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28))
1600 goto copy;
1601 h1m->flags |= H1_MF_CHNK;
1602 }
1603
Christopher Faulet9768c262018-10-22 09:34:31 +02001604 h1m->state = H1_MSG_LAST_LF;
1605 if (!chunk_memcat(tmp, "\r\n", 2))
1606 goto copy;
1607
1608 h1m->state = H1_MSG_DATA;
1609 break;
1610
1611 case HTX_BLK_DATA:
1612 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001613 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001614 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001615 goto copy;
1616 break;
1617
1618 case HTX_BLK_EOD:
1619 if (!chunk_memcat(tmp, "0\r\n", 3))
1620 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001621 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001622 h1m->state = H1_MSG_TRAILERS;
1623 break;
1624
1625 case HTX_BLK_TLR:
Willy Tarreau34d23482019-01-03 17:46:56 +01001626 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001627 if (!chunk_memcat(tmp, "0\r\n", 3))
1628 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001629 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001630 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001631 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001632 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001633 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001634 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001635 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001636 break;
1637
1638 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001639 if ((h1m->flags & H1_MF_CHNK)) {
Willy Tarreau34d23482019-01-03 17:46:56 +01001640 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001641 if (!chunk_memcat(tmp, "0\r\n", 3))
1642 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001643 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001644 }
Willy Tarreau34d23482019-01-03 17:46:56 +01001645 if (!(h1s->flags & H1S_F_HAVE_O_TLR)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001646 if (!chunk_memcat(tmp, "\r\n", 2))
1647 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001648 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet32188212018-11-20 18:21:43 +01001649 }
1650 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001651 h1m->state = H1_MSG_DONE;
1652 break;
1653
1654 case HTX_BLK_OOB:
1655 v = htx_get_blk_value(chn_htx, blk);
1656 if (!chunk_memcat(tmp, v.ptr, v.len))
1657 goto copy;
1658 break;
1659
1660 default:
1661 h1m->flags |= errflag;
1662 break;
1663 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001664 total += vlen;
1665 count -= vlen;
1666 if (sz == vlen)
1667 blk = htx_remove_blk(chn_htx, blk);
1668 else {
1669 htx_cut_data_blk(chn_htx, blk, vlen);
1670 break;
1671 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001672 }
1673
Christopher Faulet9768c262018-10-22 09:34:31 +02001674 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001675 /* when the output buffer is empty, tmp shares the same area so that we
1676 * only have to update pointers and lengths.
1677 */
Willy Tarreaub57af612019-01-23 20:43:53 +01001678 if (tmp->area == h1c->obuf.area + h1c->obuf.head)
Willy Tarreauc5efa332018-12-05 11:19:27 +01001679 h1c->obuf.data = tmp->data;
1680 else
1681 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001682
Willy Tarreau3815b222018-12-11 19:50:43 +01001683 htx_to_buf(chn_htx, buf);
1684 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001685 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001686 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 end:
1688 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001689}
1690
Christopher Faulet51dbc942018-09-13 09:05:15 +02001691/*********************************************************/
1692/* functions below are I/O callbacks from the connection */
1693/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001694static void h1_wake_stream_for_recv(struct h1s *h1s)
1695{
1696 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001697 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001698 tasklet_wakeup(h1s->recv_wait->task);
1699 h1s->recv_wait = NULL;
1700 }
1701}
1702static void h1_wake_stream_for_send(struct h1s *h1s)
1703{
1704 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001705 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001706 tasklet_wakeup(h1s->send_wait->task);
1707 h1s->send_wait = NULL;
1708 }
1709}
1710
Christopher Faulet51dbc942018-09-13 09:05:15 +02001711/*
1712 * Attempt to read data, and subscribe if none available
1713 */
1714static int h1_recv(struct h1c *h1c)
1715{
1716 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001717 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001718 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001719 int rcvd = 0;
1720
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001721 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001722 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001723
Olivier Houchard75159a92018-12-03 18:46:09 +01001724 if (!h1_recv_allowed(h1c)) {
1725 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001726 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001727 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001728
Christopher Fauletfeb11742018-11-29 15:12:34 +01001729 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001730 rcvd = 1;
1731 goto end;
1732 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001733
Christopher Faulet51dbc942018-09-13 09:05:15 +02001734 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1735 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001736 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001737 }
1738
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001739 /*
1740 * If we only have a small amount of data, realign it,
1741 * it's probably cheaper than doing 2 recv() calls.
1742 */
1743 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1744 b_slow_realign(&h1c->ibuf, trash.area, 0);
1745
Willy Tarreau45f2b892018-12-05 07:59:27 +01001746 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001747 if (max) {
1748 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001749
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001750 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001751 if (!b_data(&h1c->ibuf)) {
1752 /* try to pre-align the buffer like the rxbufs will be
1753 * to optimize memory copies.
1754 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001755 h1c->ibuf.head = sizeof(struct htx);
1756 }
Willy Tarreauc0960d12018-12-14 10:59:15 +01001757 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001758 }
Christopher Faulet47365272018-10-31 17:40:50 +01001759 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001760 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001761 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001762 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001763 if (h1s->csinfo.t_idle == -1)
1764 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1765 }
Christopher Faulet47365272018-10-31 17:40:50 +01001766 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001767
Christopher Fauletcf56b992018-12-11 16:12:31 +01001768 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001769 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001770 goto end;
1771 }
1772
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001773 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001774
Christopher Faulet9768c262018-10-22 09:34:31 +02001775 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001776 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1777 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001778
Christopher Faulete6b39942018-12-07 09:42:49 +01001779 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Olivier Houchard6a2d3342018-12-06 17:41:26 +01001780 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001781 if (!b_data(&h1c->ibuf))
1782 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001783 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001784 h1c->flags |= H1C_F_IN_FULL;
1785 return rcvd;
1786}
1787
1788
1789/*
1790 * Try to send data if possible
1791 */
1792static int h1_send(struct h1c *h1c)
1793{
1794 struct connection *conn = h1c->conn;
1795 unsigned int flags = 0;
1796 size_t ret;
1797 int sent = 0;
1798
1799 if (conn->flags & CO_FL_ERROR)
1800 return 0;
1801
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001802 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001803 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1804 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001805 return 0;
1806 }
1807
Christopher Faulet51dbc942018-09-13 09:05:15 +02001808 if (!b_data(&h1c->obuf))
1809 goto end;
1810
1811 if (h1c->flags & H1C_F_OUT_FULL)
1812 flags |= CO_SFL_MSG_MORE;
1813
1814 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1815 if (ret > 0) {
1816 h1c->flags &= ~H1C_F_OUT_FULL;
1817 b_del(&h1c->obuf, ret);
1818 sent = 1;
1819 }
1820
Christopher Faulet145aa472018-12-06 10:56:20 +01001821 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1822 /* error or output closed, nothing to send, clear the buffer to release it */
1823 b_reset(&h1c->obuf);
1824 }
1825
Christopher Faulet51dbc942018-09-13 09:05:15 +02001826 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001827 if (!(h1c->flags & H1C_F_OUT_FULL))
1828 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001829
Christopher Faulet51dbc942018-09-13 09:05:15 +02001830 /* We're done, no more to send */
1831 if (!b_data(&h1c->obuf)) {
1832 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001833 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001834 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001835 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001836 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001837 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1838 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001839
1840 return sent;
1841}
1842
Christopher Faulet51dbc942018-09-13 09:05:15 +02001843
1844/* callback called on any event by the connection handler.
1845 * It applies changes and returns zero, or < 0 if it wants immediate
1846 * destruction of the connection.
1847 */
1848static int h1_process(struct h1c * h1c)
1849{
1850 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001851 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001852
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001853 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001854 return -1;
1855
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001856 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001857 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1858 goto end;
1859 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001860 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001861 }
1862
Christopher Fauletfeb11742018-11-29 15:12:34 +01001863 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001864 if (h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001865 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001866 conn_xprt_read0_pending(conn))
1867 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001868 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001869 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001870 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001871 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001872 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001873 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001874 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001875 }
1876
Christopher Fauletfeb11742018-11-29 15:12:34 +01001877 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1878 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1879
Olivier Houchard75159a92018-12-03 18:46:09 +01001880 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1881 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001882 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001883 int flags = 0;
1884
1885 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1886 flags |= CS_FL_ERROR;
1887 if (conn_xprt_read0_pending(conn))
Christopher Faulet5f50f5e2018-12-07 11:39:55 +01001888 flags |= CS_FL_EOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001889 h1s->cs->flags |= flags;
1890 h1s->cs->data_cb->wake(h1s->cs);
1891 }
Christopher Faulet47365272018-10-31 17:40:50 +01001892 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001893 if (h1c->task) {
1894 h1c->task->expire = TICK_ETERNITY;
1895 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001896 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 +01001897 ? h1c->shut_timeout
1898 : h1c->timeout));
1899 task_queue(h1c->task);
1900 }
1901 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001902 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001903
1904 release:
1905 h1_release(conn);
1906 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001907}
1908
1909static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1910{
1911 struct h1c *h1c = ctx;
1912 int ret = 0;
1913
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001914 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001915 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001916 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001917 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001918 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001919 h1_process(h1c);
1920 return NULL;
1921}
1922
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001923static void h1_reset(struct connection *conn)
1924{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001925 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001926
1927 /* Reset the flags, and let the mux know we're waiting for a connection */
1928 h1c->flags = H1C_F_CS_WAIT_CONN;
1929}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001930
1931static int h1_wake(struct connection *conn)
1932{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001933 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001934 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001935
Christopher Faulet539e0292018-11-19 10:40:09 +01001936 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001937 ret = h1_process(h1c);
1938 if (ret == 0) {
1939 struct h1s *h1s = h1c->h1s;
1940
1941 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1942 ret = h1s->cs->data_cb->wake(h1s->cs);
1943 }
1944 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001945}
1946
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001947/* Connection timeout management. The principle is that if there's no receipt
1948 * nor sending for a certain amount of time, the connection is closed.
1949 */
1950static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
1951{
1952 struct h1c *h1c = context;
1953 int expired = tick_is_expired(t->expire, now_ms);
1954
1955 if (!expired && h1c)
1956 return t;
1957
1958 task_delete(t);
1959 task_free(t);
1960
1961 if (!h1c) {
1962 /* resources were already deleted */
1963 return NULL;
1964 }
1965
1966 h1c->task = NULL;
1967 /* If a stream is still attached to the mux, just set an error and wait
1968 * for the stream's timeout. Otherwise, release the mux. This is only ok
1969 * because same timeouts are used.
1970 */
1971 if (h1c->h1s && h1c->h1s->cs)
1972 h1c->flags |= H1C_F_CS_ERROR;
1973 else
1974 h1_release(h1c->conn);
1975 return NULL;
1976}
1977
Christopher Faulet51dbc942018-09-13 09:05:15 +02001978/*******************************************/
1979/* functions below are used by the streams */
1980/*******************************************/
1981/*
1982 * Attach a new stream to a connection
1983 * (Used for outgoing connections)
1984 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001985static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001987 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001988 struct conn_stream *cs = NULL;
1989 struct h1s *h1s;
1990
1991 if (h1c->flags & H1C_F_CS_ERROR)
1992 goto end;
1993
1994 cs = cs_new(h1c->conn);
1995 if (!cs)
1996 goto end;
1997
Olivier Houchardf502aca2018-12-14 19:42:40 +01001998 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001999 if (h1s == NULL)
2000 goto end;
2001
2002 return cs;
2003 end:
2004 cs_free(cs);
2005 return NULL;
2006}
2007
2008/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2009 * this mux, it's easy as we can only store a single conn_stream.
2010 */
2011static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2012{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002013 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002014 struct h1s *h1s = h1c->h1s;
2015
2016 if (h1s)
2017 return h1s->cs;
2018
2019 return NULL;
2020}
2021
2022static void h1_destroy(struct connection *conn)
2023{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002024 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002025
2026 if (!h1c->h1s)
2027 h1_release(conn);
2028}
2029
2030/*
2031 * Detach the stream from the connection and possibly release the connection.
2032 */
2033static void h1_detach(struct conn_stream *cs)
2034{
2035 struct h1s *h1s = cs->ctx;
2036 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002037 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002038 int has_keepalive;
2039 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002040
2041 cs->ctx = NULL;
2042 if (!h1s)
2043 return;
2044
Olivier Houchardf502aca2018-12-14 19:42:40 +01002045 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002046 h1c = h1s->h1c;
2047 h1s->cs = NULL;
2048
Olivier Houchard8a786902018-12-15 16:05:40 +01002049 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2050 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2051 h1s_destroy(h1s);
2052
2053 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002054 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002055 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002056 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002057 h1c->conn->flags |= CO_FL_PRIVATE;
2058
Olivier Houchard44d59142018-12-13 18:46:22 +01002059 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002060 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002061 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2062 h1c->conn->owner = NULL;
2063 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2064 /* The server doesn't want it, let's kill the connection right away */
2065 h1c->conn->mux->destroy(h1c->conn);
2066 else
2067 tasklet_wakeup(h1c->wait_event.task);
2068 return;
2069
2070 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002071 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002072 if (h1c->conn->owner == sess) {
2073 int ret = session_check_idle_conn(sess, h1c->conn);
2074 if (ret == -1)
2075 /* The connection got destroyed, let's leave */
2076 return;
2077 else if (ret == 1) {
2078 /* The connection was added to the server list,
2079 * wake the task so we can subscribe to events
2080 */
2081 tasklet_wakeup(h1c->wait_event.task);
2082 return;
2083 }
2084 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002085 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002086 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002087 struct server *srv = objt_server(h1c->conn->target);
2088
2089 if (srv) {
2090 if (h1c->conn->flags & CO_FL_PRIVATE)
2091 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002092 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002093 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2094 else
2095 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2096 }
2097 }
2098 }
2099
Christopher Faulet51dbc942018-09-13 09:05:15 +02002100 /* We don't want to close right now unless the connection is in error */
Christopher Faulet666a0c42019-01-08 11:12:04 +01002101 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01002102 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002103 h1_release(h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002104 else {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002105 tasklet_wakeup(h1c->wait_event.task);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002106 if (h1c->task) {
2107 h1c->task->expire = TICK_ETERNITY;
2108 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002109 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 +01002110 ? h1c->shut_timeout
2111 : h1c->timeout));
2112 task_queue(h1c->task);
2113 }
2114 }
2115 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002116}
2117
2118
2119static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2120{
2121 struct h1s *h1s = cs->ctx;
2122
2123 if (!h1s)
2124 return;
2125
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002126 if ((h1s->flags & H1S_F_WANT_KAL) &&
2127 !(cs->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002128 return;
2129
Christopher Faulet51dbc942018-09-13 09:05:15 +02002130 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2131 if (cs->flags & CS_FL_SHR)
2132 return;
2133 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
2134 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002135 if ((cs->conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2136 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002137}
2138
2139static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2140{
2141 struct h1s *h1s = cs->ctx;
2142 struct h1c *h1c;
2143
2144 if (!h1s)
2145 return;
2146 h1c = h1s->h1c;
2147
Christopher Fauletf2824e62018-10-01 12:12:37 +02002148 if ((h1s->flags & H1S_F_WANT_KAL) &&
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002149 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) &&
2150
Christopher Fauletf2824e62018-10-01 12:12:37 +02002151 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2152 return;
2153
Christopher Faulet51dbc942018-09-13 09:05:15 +02002154 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2155 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2156 return;
2157
Christopher Faulet666a0c42019-01-08 11:12:04 +01002158 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002159}
2160
Christopher Faulet666a0c42019-01-08 11:12:04 +01002161static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002162{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002163 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002164
Christopher Faulet666a0c42019-01-08 11:12:04 +01002165 conn_xprt_shutw(conn);
2166 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2167 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2168 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002169}
2170
2171/* Called from the upper layer, to unsubscribe to events */
2172static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2173{
2174 struct wait_event *sw;
2175 struct h1s *h1s = cs->ctx;
2176
2177 if (!h1s)
2178 return 0;
2179
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002180 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002181 sw = param;
2182 if (h1s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002183 sw->events &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002184 h1s->recv_wait = NULL;
2185 }
2186 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002187 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002188 sw = param;
2189 if (h1s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002190 sw->events &= ~SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002191 h1s->send_wait = NULL;
2192 }
2193 }
2194 return 0;
2195}
2196
2197/* Called from the upper layer, to subscribe to events, such as being able to send */
2198static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2199{
2200 struct wait_event *sw;
2201 struct h1s *h1s = cs->ctx;
2202
2203 if (!h1s)
2204 return -1;
2205
2206 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002207 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002208 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002209 if (!(sw->events & SUB_RETRY_RECV)) {
2210 sw->events |= SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002211 sw->handle = h1s;
2212 h1s->recv_wait = sw;
2213 }
2214 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002215 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002216 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002217 if (!(sw->events & SUB_RETRY_SEND)) {
2218 sw->events |= SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002219 sw->handle = h1s;
2220 h1s->send_wait = sw;
2221 }
2222 return 0;
2223 default:
2224 break;
2225 }
2226 return -1;
2227}
2228
2229/* Called from the upper layer, to receive data */
2230static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2231{
2232 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002233 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002234 size_t ret = 0;
2235
Christopher Faulet539e0292018-11-19 10:40:09 +01002236 if (!(h1c->flags & H1C_F_IN_ALLOC))
2237 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002238
2239 if (flags & CO_RFL_BUF_FLUSH)
2240 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002241 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2242 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002243 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002244 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002245 }
2246 return ret;
2247}
2248
2249
2250/* Called from the upper layer, to send data */
2251static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2252{
2253 struct h1s *h1s = cs->ctx;
2254 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002255 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002256
2257 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002258 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002259
2260 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002261 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2262 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002263
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002264 while (total != count) {
2265 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002266
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002267 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2268 ret = h1_process_output(h1c, buf, count);
2269 if (!ret)
2270 break;
2271 total += ret;
2272 if (!h1_send(h1c))
2273 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002274 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002275
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002276 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002277}
2278
Christopher Faulet1be55f92018-10-02 15:59:23 +02002279#if defined(CONFIG_HAP_LINUX_SPLICE)
2280/* Send and get, using splicing */
2281static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2282{
2283 struct h1s *h1s = cs->ctx;
2284 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2285 int ret = 0;
2286
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002287 if (b_data(&h1s->h1c->ibuf)) {
2288 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002289 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002290 }
2291
2292 h1s->flags &= ~H1S_F_BUF_FLUSH;
2293 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002294 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2295 count = h1m->curr_len;
2296 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2297 if (h1m->state == H1_MSG_DATA && ret > 0)
2298 h1m->curr_len -= ret;
2299 end:
2300 return ret;
2301
2302}
2303
2304static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2305{
2306 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002307 int ret = 0;
2308
2309 if (b_data(&h1s->h1c->obuf))
2310 goto end;
2311
2312 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002313 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002314 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002315 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
2316 cs->conn->xprt->subscribe(cs->conn, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002317 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002318 return ret;
2319}
2320#endif
2321
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002322/* for debugging with CLI's "show fd" command */
2323static void h1_show_fd(struct buffer *msg, struct connection *conn)
2324{
2325 struct h1c *h1c = conn->ctx;
2326 struct h1s *h1s = h1c->h1s;
2327
Christopher Fauletf376a312019-01-04 15:16:06 +01002328 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2329 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002330 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2331 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2332 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2333 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2334
2335 if (h1s) {
2336 char *method;
2337
2338 if (h1s->meth < HTTP_METH_OTHER)
2339 method = http_known_methods[h1s->meth].ptr;
2340 else
2341 method = "UNKNOWN";
2342 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2343 " .meth=%s status=%d",
2344 h1s, h1s->flags,
2345 h1m_state_str(h1s->req.state),
2346 h1m_state_str(h1s->res.state), method, h1s->status);
2347 if (h1s->cs)
2348 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2349 h1s->cs->flags, h1s->cs->data);
2350 }
2351}
2352
Christopher Faulet51dbc942018-09-13 09:05:15 +02002353/****************************************/
2354/* MUX initialization and instanciation */
2355/****************************************/
2356
2357/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002358static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002359 .init = h1_init,
2360 .wake = h1_wake,
2361 .attach = h1_attach,
2362 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002363 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002364 .detach = h1_detach,
2365 .destroy = h1_destroy,
2366 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002367 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002368 .rcv_buf = h1_rcv_buf,
2369 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002370#if defined(CONFIG_HAP_LINUX_SPLICE)
2371 .rcv_pipe = h1_rcv_pipe,
2372 .snd_pipe = h1_snd_pipe,
2373#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002374 .subscribe = h1_subscribe,
2375 .unsubscribe = h1_unsubscribe,
2376 .shutr = h1_shutr,
2377 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002378 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002379 .reset = h1_reset,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002380 .flags = MX_FL_NONE,
2381 .name = "h1",
2382};
2383
2384
2385/* this mux registers default HTX proto */
2386static struct mux_proto_list mux_proto_htx =
2387{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2388
Willy Tarreau0108d902018-11-25 19:14:37 +01002389INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2390
Christopher Faulet51dbc942018-09-13 09:05:15 +02002391/*
2392 * Local variables:
2393 * c-indent-level: 8
2394 * c-basic-offset: 8
2395 * End:
2396 */