blob: 4dcbcafcf9411068b5daf4859424667d9dd2df61 [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 Fauletb992af02019-03-28 15:42:24 +0100269 h1s->flags = H1S_F_WANT_KAL;
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 Fauletb992af02019-03-28 15:42:24 +0100275 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200276
Christopher Faulet129817b2018-09-20 16:14:40 +0200277 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100278 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
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 Fauletf2824e62018-10-01 12:12:37 +0200344 pool_free(pool_head_h1s, h1s);
345 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200346}
347
Christopher Fauletfeb11742018-11-29 15:12:34 +0100348static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
349{
350 struct h1s *h1s = cs->ctx;
351
352 if (h1s && !conn_is_back(cs->conn))
353 return &h1s->csinfo;
354 return NULL;
355}
356
Christopher Faulet51dbc942018-09-13 09:05:15 +0200357/*
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100358 * Initialize the mux once it's attached. It is expected that conn->ctx
Christopher Faulet51dbc942018-09-13 09:05:15 +0200359 * points to the existing conn_stream (for outgoing connections) or NULL (for
360 * incoming ones). Returns < 0 on error.
361 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100362static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200363{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200364 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100365 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200366
367 h1c = pool_alloc(pool_head_h1c);
368 if (!h1c)
369 goto fail_h1c;
370 h1c->conn = conn;
371 h1c->px = proxy;
372
373 h1c->flags = H1C_F_NONE;
374 h1c->ibuf = BUF_NULL;
375 h1c->obuf = BUF_NULL;
376 h1c->h1s = NULL;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100377 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200378
Christopher Faulet51dbc942018-09-13 09:05:15 +0200379 LIST_INIT(&h1c->buf_wait.list);
380 h1c->wait_event.task = tasklet_new();
381 if (!h1c->wait_event.task)
382 goto fail;
383 h1c->wait_event.task->process = h1_io_cb;
384 h1c->wait_event.task->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100385 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200386
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100387 if (conn->ctx) {
388 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
389 if (tick_isset(proxy->timeout.serverfin))
390 h1c->shut_timeout = proxy->timeout.serverfin;
391 } else {
392 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
393 if (tick_isset(proxy->timeout.clientfin))
394 h1c->shut_timeout = proxy->timeout.clientfin;
395 }
396 if (tick_isset(h1c->timeout)) {
397 t = task_new(tid_bit);
398 if (!t)
399 goto fail;
400
401 h1c->task = t;
402 t->process = h1_timeout_task;
403 t->context = h1c;
404 t->expire = tick_add(now_ms, h1c->timeout);
405 }
406
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200407 if (!(conn->flags & CO_FL_CONNECTED))
408 h1c->flags |= H1C_F_CS_WAIT_CONN;
409
Christopher Fauletf2824e62018-10-01 12:12:37 +0200410 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100411 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200412 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200413
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100414 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200415
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100416
417 if (t)
418 task_queue(t);
419
Christopher Faulet51dbc942018-09-13 09:05:15 +0200420 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard9b960a82019-01-04 16:51:40 +0100421 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200422
423 /* mux->wake will be called soon to complete the operation */
424 return 0;
425
426 fail:
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100427 if (t)
428 task_free(t);
Christopher Faulet47365272018-10-31 17:40:50 +0100429 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200430 tasklet_free(h1c->wait_event.task);
431 pool_free(pool_head_h1c, h1c);
432 fail_h1c:
433 return -1;
434}
435
436
437/* release function for a connection. This one should be called to free all
438 * resources allocated to the mux.
439 */
440static void h1_release(struct connection *conn)
441{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100442 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200443
Christopher Faulet51dbc942018-09-13 09:05:15 +0200444 if (h1c) {
445 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
446 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
447 LIST_DEL(&h1c->buf_wait.list);
448 LIST_INIT(&h1c->buf_wait.list);
449 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
450 }
451
452 h1_release_buf(h1c, &h1c->ibuf);
453 h1_release_buf(h1c, &h1c->obuf);
454
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100455 if (h1c->task) {
456 h1c->task->context = NULL;
457 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
458 h1c->task = NULL;
459 }
460
Christopher Faulet51dbc942018-09-13 09:05:15 +0200461 if (h1c->wait_event.task)
462 tasklet_free(h1c->wait_event.task);
463
Christopher Fauletf2824e62018-10-01 12:12:37 +0200464 h1s_destroy(h1c->h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100465 if (h1c->wait_event.events != 0)
466 conn->xprt->unsubscribe(conn, h1c->wait_event.events,
Christopher Faulet51dbc942018-09-13 09:05:15 +0200467 &h1c->wait_event);
468 pool_free(pool_head_h1c, h1c);
469 }
470
471 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100472 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200473
474 conn_stop_tracking(conn);
475 conn_full_close(conn);
476 if (conn->destroy_cb)
477 conn->destroy_cb(conn);
478 conn_free(conn);
479}
480
481/******************************************************/
482/* functions below are for the H1 protocol processing */
483/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200484/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
485 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200486 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100487static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200488{
Christopher Faulet570d1612018-11-26 11:13:57 +0100489 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200490
Christopher Faulet570d1612018-11-26 11:13:57 +0100491 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200492 (*(p + 5) > '1' ||
493 (*(p + 5) == '1' && *(p + 7) >= '1')))
494 h1m->flags |= H1_MF_VER_11;
495}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200496
Christopher Faulet9768c262018-10-22 09:34:31 +0200497/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
498 * greater or equal to 1.1
499 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100500static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200501{
Christopher Faulet570d1612018-11-26 11:13:57 +0100502 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200503
Christopher Faulet570d1612018-11-26 11:13:57 +0100504 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200505 (*(p + 5) > '1' ||
506 (*(p + 5) == '1' && *(p + 7) >= '1')))
507 h1m->flags |= H1_MF_VER_11;
508}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200509
Christopher Faulet9768c262018-10-22 09:34:31 +0200510/*
511 * Check the validity of the request version. If the version is valid, it
512 * returns 1. Otherwise, it returns 0.
513 */
514static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
515{
516 struct h1c *h1c = h1s->h1c;
517
518 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
519 * exactly one digit "." one digit. This check may be disabled using
520 * option accept-invalid-http-request.
521 */
522 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
523 if (sl.rq.v.len != 8)
524 return 0;
525
526 if (*(sl.rq.v.ptr + 4) != '/' ||
527 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
528 *(sl.rq.v.ptr + 6) != '.' ||
529 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
530 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200531 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200532 else if (!sl.rq.v.len) {
533 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200534
Christopher Faulet9768c262018-10-22 09:34:31 +0200535 /* RFC 1945 allows only GET for HTTP/0.9 requests */
536 if (sl.rq.meth != HTTP_METH_GET)
537 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200538
Christopher Faulet9768c262018-10-22 09:34:31 +0200539 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
540 if (!sl.rq.u.len)
541 return 0;
542
543 /* Add HTTP version */
544 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100545 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200546 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100547
548 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100549 ((*(sl.rq.v.ptr + 5) > '1') ||
550 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100551 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200552 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200553}
554
Christopher Faulet9768c262018-10-22 09:34:31 +0200555/*
556 * Check the validity of the response version. If the version is valid, it
557 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200558 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200559static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200560{
Christopher Faulet9768c262018-10-22 09:34:31 +0200561 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200562
Christopher Faulet9768c262018-10-22 09:34:31 +0200563 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
564 * exactly one digit "." one digit. This check may be disabled using
565 * option accept-invalid-http-request.
566 */
567 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
568 if (sl.st.v.len != 8)
569 return 0;
570
571 if (*(sl.st.v.ptr + 4) != '/' ||
572 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
573 *(sl.st.v.ptr + 6) != '.' ||
574 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
575 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200576 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100577
578 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100579 ((*(sl.st.v.ptr + 5) > '1') ||
580 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100581 h1m->flags |= H1_MF_VER_11;
582
Christopher Faulet9768c262018-10-22 09:34:31 +0200583 return 1;
584}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200585
586/* Deduce the connection mode of the client connection, depending on the
587 * configuration and the H1 message flags. This function is called twice, the
588 * first time when the request is parsed and the second time when the response
589 * is parsed.
590 */
591static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
592{
593 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200594
595 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100596 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200597 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100598 h1s->status == 101) {
599 /* Either we've established an explicit tunnel, or we're
600 * switching the protocol. In both cases, we're very unlikely to
601 * understand the next protocols. We have to switch to tunnel
602 * mode, so that we transfer the request and responses then let
603 * this protocol pass unmodified. When we later implement
604 * specific parsers for such protocols, we'll want to check the
605 * Upgrade header which contains information about that protocol
606 * for responses with status 101 (eg: see RFC2817 about TLS).
607 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200608 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100609 }
610 else if (h1s->flags & H1S_F_WANT_KAL) {
611 /* By default the client is in KAL mode. CLOSE mode mean
612 * it is imposed by the client itself. So only change
613 * KAL mode here. */
614 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
615 /* no length known or explicit close => close */
616 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
617 }
618 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
619 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
620 /* no explict keep-alive and option httpclose => close */
621 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
622 }
623 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200624 }
625 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100626 /* Input direction: first pass */
627 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
628 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200629 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100630 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200631 }
632
633 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
634 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
635 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
636}
637
638/* Deduce the connection mode of the client connection, depending on the
639 * configuration and the H1 message flags. This function is called twice, the
640 * first time when the request is parsed and the second time when the response
641 * is parsed.
642 */
643static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
644{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100645 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100646 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100647 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200648
Christopher Fauletf2824e62018-10-01 12:12:37 +0200649 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100650 /* Input direction: second pass */
651
Christopher Fauletf2824e62018-10-01 12:12:37 +0200652 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100653 h1s->status == 101) {
654 /* Either we've established an explicit tunnel, or we're
655 * switching the protocol. In both cases, we're very unlikely to
656 * understand the next protocols. We have to switch to tunnel
657 * mode, so that we transfer the request and responses then let
658 * this protocol pass unmodified. When we later implement
659 * specific parsers for such protocols, we'll want to check the
660 * Upgrade header which contains information about that protocol
661 * for responses with status 101 (eg: see RFC2817 about TLS).
662 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200663 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100664 }
665 else if (h1s->flags & H1S_F_WANT_KAL) {
666 /* By default the server is in KAL mode. CLOSE mode mean
667 * it is imposed by haproxy itself. So only change KAL
668 * mode here. */
669 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
670 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
671 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
672 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
673 }
674 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200675 }
Christopher Faulet70033782018-12-05 13:50:11 +0100676 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100677 /* Output direction: first pass */
678 if (h1m->flags & H1_MF_CONN_CLO) {
679 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100680 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100681 }
682 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
683 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
684 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
685 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
686 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
687 /* no explicit keep-alive option httpclose/server-close => close */
688 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
689 }
Christopher Faulet70033782018-12-05 13:50:11 +0100690 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200691
692 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
693 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
694 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200695}
696
Christopher Fauletb992af02019-03-28 15:42:24 +0100697static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200698{
699 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200700
701 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
702 * token is found
703 */
704 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200705 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200706
707 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100708 if (!(h1m->flags & H1_MF_VER_11))
709 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200710 }
711 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100712 if (h1m->flags & H1_MF_VER_11)
713 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200714 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200715}
716
Christopher Fauletb992af02019-03-28 15:42:24 +0100717static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200718{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200719 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
720 * token is found
721 */
722 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200723 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200724
725 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100726 if (!(h1m->flags & H1_MF_VER_11) ||
727 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
728 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200729 }
730 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100731 if (h1m->flags & H1_MF_VER_11)
732 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200733 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200734}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200735
Christopher Fauletb992af02019-03-28 15:42:24 +0100736static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200737{
Christopher Fauletb992af02019-03-28 15:42:24 +0100738 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200739 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100740 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200741 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200742}
743
Christopher Fauletb992af02019-03-28 15:42:24 +0100744static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
745{
746 if (!conn_is_back(h1s->h1c->conn))
747 h1_set_cli_conn_mode(h1s, h1m);
748 else
749 h1_set_srv_conn_mode(h1s, h1m);
750
751 if (!(h1m->flags & H1_MF_RESP))
752 h1_update_req_conn_value(h1s, h1m, conn_val);
753 else
754 h1_update_res_conn_value(h1s, h1m, conn_val);
755}
Christopher Faulete44769b2018-11-29 23:01:45 +0100756
757/* Append the description of what is present in error snapshot <es> into <out>.
758 * The description must be small enough to always fit in a buffer. The output
759 * buffer may be the trash so the trash must not be used inside this function.
760 */
761static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
762{
763 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100764 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
765 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
766 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
767 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
768 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
769 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100770}
771/*
772 * Capture a bad request or response and archive it in the proxy's structure.
773 * By default it tries to report the error position as h1m->err_pos. However if
774 * this one is not set, it will then report h1m->next, which is the last known
775 * parsing point. The function is able to deal with wrapping buffers. It always
776 * displays buffers as a contiguous area starting at buf->p. The direction is
777 * determined thanks to the h1m's flags.
778 */
779static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
780 struct h1m *h1m, struct buffer *buf)
781{
782 struct session *sess = h1c->conn->owner;
783 struct proxy *proxy = h1c->px;
784 struct proxy *other_end = sess->fe;
785 union error_snapshot_ctx ctx;
786
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100787 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100788 other_end = si_strm(h1s->cs->data)->be;
789
790 /* http-specific part now */
791 ctx.h1.state = h1m->state;
792 ctx.h1.c_flags = h1c->flags;
793 ctx.h1.s_flags = h1s->flags;
794 ctx.h1.m_flags = h1m->flags;
795 ctx.h1.m_clen = h1m->curr_len;
796 ctx.h1.m_blen = h1m->body_len;
797
798 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
799 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100800 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
801 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100802}
803
Christopher Fauletadb22202018-12-12 10:32:09 +0100804/* Emit the chunksize followed by a CRLF in front of data of the buffer
805 * <buf>. It goes backwards and starts with the byte before the buffer's
806 * head. The caller is responsible for ensuring there is enough room left before
807 * the buffer's head for the string.
808 */
809static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
810{
811 char *beg, *end;
812
813 beg = end = b_head(buf);
814 *--beg = '\n';
815 *--beg = '\r';
816 do {
817 *--beg = hextab[chksz & 0xF];
818 } while (chksz >>= 4);
819 buf->head -= (end - beg);
820 b_add(buf, end - beg);
821}
822
823/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
824 * ensuring there is enough room left in the buffer for the string. */
825static void h1_emit_chunk_crlf(struct buffer *buf)
826{
827 *(b_peek(buf, b_data(buf))) = '\r';
828 *(b_peek(buf, b_data(buf) + 1)) = '\n';
829 b_add(buf, 2);
830}
831
Christopher Faulet129817b2018-09-20 16:14:40 +0200832/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100833 * Switch the request to tunnel mode. This function must only be called for
834 * CONNECT requests. On the client side, the mux is mark as busy on input,
835 * waiting the response.
836 */
837static void h1_set_req_tunnel_mode(struct h1s *h1s)
838{
839 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
840 h1s->req.state = H1_MSG_TUNNEL;
841 if (!conn_is_back(h1s->h1c->conn))
842 h1s->h1c->flags |= H1C_F_IN_BUSY;
843}
844
845/*
846 * Switch the response to tunnel mode. This function must only be called on
847 * successfull replies to CONNECT requests or on protocol switching. On the
848 * server side, if the request is not finished, the mux is mark as busy on
849 * input. Otherwise the request is also switch to tunnel mode.
850 */
851static void h1_set_res_tunnel_mode(struct h1s *h1s)
852{
853 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
854 h1s->res.state = H1_MSG_TUNNEL;
855 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
856 h1s->h1c->flags |= H1C_F_IN_BUSY;
857 else {
858 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
859 h1s->req.state = H1_MSG_TUNNEL;
860 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
861 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
862 tasklet_wakeup(h1s->h1c->wait_event.task);
863 }
864 }
865}
866
867/*
Christopher Fauletcdc90e92019-03-28 13:28:46 +0100868 * Handle 100-Continue responses or any other informational 1xx responses which
869 * is non-final. In such case, this function reset the response parser. It is
870 * the caller responsibility to call this function when appropriate.
871 */
872static void h1_handle_1xx_response(struct h1s *h1s, struct h1m *h1m)
873{
874 if ((h1m->flags & H1_MF_RESP) && h1m->state == H1_MSG_DONE &&
875 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
876 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100877 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletcdc90e92019-03-28 13:28:46 +0100878 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
879 }
880}
881
882/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200883 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200884 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
885 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800886 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200887 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200888static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200889 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200890{
891 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100892 union h1_sl h1sl;
893 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200894 int ret = 0;
895
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100896 if (!max)
897 goto end;
898
Christopher Faulet129817b2018-09-20 16:14:40 +0200899 /* Realing input buffer if necessary */
900 if (b_head(buf) + b_data(buf) > b_wrap(buf))
901 b_slow_realign(buf, trash.area, 0);
902
Christopher Fauletf2824e62018-10-01 12:12:37 +0200903 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100904 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200905 if (ret <= 0) {
906 /* Incomplete or invalid message. If the buffer is full, it's an
907 * error because headers are too large to be handled by the
908 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200909 if (ret < 0 || (!ret && b_full(buf)))
910 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200911 goto end;
912 }
913
914 /* messages headers fully parsed, do some checks to prepare the body
915 * parsing.
916 */
917
918 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200919 if (ret > (b_size(buf) - global.tune.maxrewrite))
920 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200921
Christopher Faulet9768c262018-10-22 09:34:31 +0200922 /* Save the request's method or the response's status, check if the body
923 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200924 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100925 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200926
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100927 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +0200928 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100929
930 if (h1s->meth == HTTP_METH_CONNECT) {
931 /* Switch CONNECT requests to tunnel mode */
932 h1_set_req_tunnel_mode(h1s);
933 }
934 else if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len) {
935 /* Switch requests with no body to done. */
Christopher Faulet129817b2018-09-20 16:14:40 +0200936 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100937 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200938
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100939 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
940 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200941 h1m->err_state = h1m->state;
942 goto vsn_error;
943 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200944 }
945 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100946 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200947
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100948 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
949 h1s->status == 101) {
950 /* Switch successfull replies to CONNECT requests and
951 * protocol switching to tunnel mode. */
952 h1_set_res_tunnel_mode(h1s);
953 }
954 else if ((h1s->meth == HTTP_METH_HEAD) ||
955 (h1s->status >= 100 && h1s->status < 200) ||
956 (h1s->status == 204) || (h1s->status == 304)) {
957 /* Switch responses without body to done. */
Christopher Faulet129817b2018-09-20 16:14:40 +0200958 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
959 h1m->flags |= H1_MF_XFER_LEN;
960 h1m->curr_len = h1m->body_len = 0;
961 h1m->state = H1_MSG_DONE;
962 }
963 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100964 /* Responses with a known body length. Switch requests
965 * with no body to done. */
Christopher Faulet129817b2018-09-20 16:14:40 +0200966 h1m->flags |= H1_MF_XFER_LEN;
967 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
968 h1m->state = H1_MSG_DONE;
969 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100970 else {
971 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +0200972 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100973 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200974
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100975 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
976 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200977 h1m->err_state = h1m->state;
978 goto vsn_error;
979 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200980 }
981
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100982 /* Set HTX start-line flags */
983 if (h1m->flags & H1_MF_VER_11)
984 flags |= HTX_SL_F_VER_11;
985 if (h1m->flags & H1_MF_XFER_ENC)
986 flags |= HTX_SL_F_XFER_ENC;
987 if (h1m->flags & H1_MF_XFER_LEN) {
988 flags |= HTX_SL_F_XFER_LEN;
989 if (h1m->flags & H1_MF_CHNK)
990 flags |= HTX_SL_F_CHNK;
991 else if (h1m->flags & H1_MF_CLEN)
992 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100993 if (h1m->state == H1_MSG_DONE)
994 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100995 }
996
Christopher Faulet9768c262018-10-22 09:34:31 +0200997 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100998 struct htx_sl *sl;
999
1000 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1001 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001002 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001003 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001004 }
1005 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001006 struct htx_sl *sl;
1007
1008 flags |= HTX_SL_F_IS_RESP;
1009 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1010 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001011 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001012 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001013 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001014
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001015 if (h1m->state == H1_MSG_DONE) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001016 if (!htx_add_endof(htx, HTX_BLK_EOM))
1017 goto error;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001018 h1s->cs->flags |= CS_FL_EOI;
1019 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001020
Christopher Fauletb992af02019-03-28 15:42:24 +01001021 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001022
1023 /* If body length cannot be determined, set htx->extra to
1024 * ULLONG_MAX. This value is impossible in other cases.
1025 */
1026 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
1027
1028 /* Recheck there is enough space to do headers rewritting */
1029 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
1030 goto error;
1031
1032 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001033 end:
1034 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001035
1036 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001037 h1m->err_state = h1m->state;
1038 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001039 vsn_error:
1040 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +01001041 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001042 ret = 0;
1043 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001044}
1045
1046/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001047 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1048 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001049 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001050 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001051 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001052static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001053 struct buffer *buf, size_t *ofs, size_t max,
Christopher Fauletafe57842019-01-21 11:55:02 +01001054 struct buffer *htxbuf, size_t reserve)
Christopher Faulet129817b2018-09-20 16:14:40 +02001055{
Christopher Fauletafe57842019-01-21 11:55:02 +01001056 uint32_t data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001057 size_t total = 0;
1058 int ret = 0;
1059
Christopher Fauletafe57842019-01-21 11:55:02 +01001060 data_space = htx_free_data_space(htx);
1061 if (data_space <= reserve)
1062 goto end;
1063 data_space -= reserve;
1064
Christopher Faulet129817b2018-09-20 16:14:40 +02001065 if (h1m->flags & H1_MF_XFER_LEN) {
1066 if (h1m->flags & H1_MF_CLEN) {
1067 /* content-length: read only h2m->body_len */
1068 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001069 if (ret > data_space)
1070 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001071 if ((uint64_t)ret > h1m->curr_len)
1072 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001073 if (ret > b_contig_data(buf, *ofs))
1074 ret = b_contig_data(buf, *ofs);
1075 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001076 /* very often with large files we'll face the following
1077 * situation :
1078 * - htx is empty and points to <htxbuf>
1079 * - ret == buf->data
1080 * - buf->head == sizeof(struct htx)
1081 * => we can swap the buffers and place an htx header into
1082 * the target buffer instead
1083 */
1084 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1085 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1086 void *raw_area = buf->area;
1087 void *htx_area = htxbuf->area;
1088 struct htx_blk *blk;
1089
1090 buf->area = htx_area;
1091 htxbuf->area = raw_area;
1092 htx = (struct htx *)htxbuf->area;
1093 htx->size = htxbuf->size - sizeof(*htx);
1094 htx_reset(htx);
1095 b_set_data(htxbuf, b_size(htxbuf));
1096
1097 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1098 blk->info += ret;
1099 /* nothing else to do, the old buffer now contains an
1100 * empty pre-initialized HTX header
1101 */
1102 }
1103 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001104 goto end;
1105 h1m->curr_len -= ret;
1106 *ofs += ret;
1107 total += ret;
1108 }
1109
1110 if (!h1m->curr_len) {
1111 if (!htx_add_endof(htx, HTX_BLK_EOM))
1112 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001113 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001114 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet9768c262018-10-22 09:34:31 +02001115 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001116 }
1117 else if (h1m->flags & H1_MF_CHNK) {
1118 new_chunk:
1119 /* te:chunked : parse chunks */
1120 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001121 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001122 if (ret <= 0)
1123 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001124 h1m->state = H1_MSG_CHUNK_SIZE;
1125
Christopher Faulet129817b2018-09-20 16:14:40 +02001126 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001127 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001128 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001129 }
1130
1131 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1132 unsigned int chksz;
1133
Christopher Fauletf2824e62018-10-01 12:12:37 +02001134 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001135 if (ret <= 0)
1136 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001137 if (!chksz) {
1138 if (!htx_add_endof(htx, HTX_BLK_EOD))
1139 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001140 h1s->flags |= H1S_F_HAVE_I_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001141 h1m->state = H1_MSG_TRAILERS;
1142 }
1143 else
1144 h1m->state = H1_MSG_DATA;
1145
Christopher Faulet129817b2018-09-20 16:14:40 +02001146 h1m->curr_len = chksz;
1147 h1m->body_len += chksz;
1148 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001149 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001150 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001151 }
1152
1153 if (h1m->state == H1_MSG_DATA) {
1154 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001155 if (ret > data_space)
1156 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001157 if ((uint64_t)ret > h1m->curr_len)
1158 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001159 if (ret > b_contig_data(buf, *ofs))
1160 ret = b_contig_data(buf, *ofs);
1161 if (ret) {
1162 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1163 goto end;
1164 h1m->curr_len -= ret;
1165 max -= ret;
1166 *ofs += ret;
1167 total += ret;
1168 }
1169 if (!h1m->curr_len) {
1170 h1m->state = H1_MSG_CHUNK_CRLF;
Christopher Fauletafe57842019-01-21 11:55:02 +01001171 data_space = htx_free_data_space(htx);
1172 if (data_space <= reserve)
1173 goto end;
1174 data_space -= reserve;
Christopher Faulet9768c262018-10-22 09:34:31 +02001175 goto new_chunk;
1176 }
1177 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001178 }
1179
1180 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001181 /* Trailers were alread parsed, only the EOM
1182 * need to be added */
Willy Tarreau34d23482019-01-03 17:46:56 +01001183 if (h1s->flags & H1S_F_HAVE_I_TLR)
Christopher Faulet17276482018-11-22 11:44:35 +01001184 goto skip_tlr_parsing;
1185
Christopher Fauletf2824e62018-10-01 12:12:37 +02001186 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001187 if (ret > data_space)
1188 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001189 if (ret <= 0)
1190 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001191
1192 /* Realing input buffer if tailers wrap. For now
1193 * this is a workaroung. Because trailers are
1194 * not split on CRLF, like headers, there is no
1195 * way to know where to split it when trailers
1196 * wrap. This is a limitation of
1197 * h1_measure_trailers.
1198 */
1199 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1200 b_slow_realign(buf, trash.area, 0);
1201
1202 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1203 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001204 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001205 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001206 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001207 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001208
Christopher Faulet17276482018-11-22 11:44:35 +01001209 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001210 if (!htx_add_endof(htx, HTX_BLK_EOM))
1211 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001212 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001213 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001214 }
1215 }
1216 else {
1217 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1218 * is no body. Switch the message in DONE state
1219 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001220 if (!htx_add_endof(htx, HTX_BLK_EOM))
1221 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001222 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001223 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001224 }
1225 }
1226 else {
1227 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001228 ret = max;
1229 if (ret > data_space)
1230 ret = data_space;
1231 if (ret > b_contig_data(buf, *ofs))
1232 ret = b_contig_data(buf, *ofs);
1233 if (ret) {
1234 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1235 goto end;
1236
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001237 *ofs += ret;
1238 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001239 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001240 }
1241
1242 end:
1243 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001244 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001245 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001246 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001247 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001248 return 0;
1249 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001250 /* update htx->extra, only when the body length is known */
1251 if (h1m->flags & H1_MF_XFER_LEN)
1252 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001253 return total;
1254}
1255
1256/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001257 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001258 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1259 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001260 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001261static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001262{
Christopher Faulet539e0292018-11-19 10:40:09 +01001263 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001264 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001265 struct htx *htx;
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001266 size_t data = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001267 size_t total = 0;
1268 size_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001269 size_t count, rsv;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001270 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001271
Christopher Faulet539e0292018-11-19 10:40:09 +01001272 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001273
Christopher Fauletf2824e62018-10-01 12:12:37 +02001274 if (!conn_is_back(h1c->conn)) {
1275 h1m = &h1s->req;
1276 errflag = H1S_F_REQ_ERROR;
1277 }
1278 else {
1279 h1m = &h1s->res;
1280 errflag = H1S_F_RES_ERROR;
1281 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001282
Christopher Faulet74027762019-02-26 14:45:05 +01001283 data = htx->data;
1284 count = b_data(&h1c->ibuf);
1285 if (!count)
1286 goto end;
1287 rsv = ((flags & CO_RFL_KEEP_RSV) ? global.tune.maxrewrite : 0);
1288
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001289 if (htx_is_empty(htx))
1290 h1_handle_1xx_response(h1s, h1m);
1291
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001292 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001293 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001294 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 if (!ret)
1296 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001297 }
1298 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Fauletafe57842019-01-21 11:55:02 +01001299 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001300 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001301 if (!ret)
1302 break;
1303 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001304 else if (h1m->state == H1_MSG_DONE) {
1305 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001306 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001307 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001308 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Fauletafe57842019-01-21 11:55:02 +01001309 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001310 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001311 if (!ret)
1312 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001313 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001314 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001315 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001316 break;
1317 }
1318
Christopher Faulet539e0292018-11-19 10:40:09 +01001319 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001320 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001321
Christopher Faulet47365272018-10-31 17:40:50 +01001322 if (h1s->flags & errflag)
1323 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001324
Christopher Faulet539e0292018-11-19 10:40:09 +01001325 b_del(&h1c->ibuf, total);
1326
1327 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001328 htx_to_buf(htx, buf);
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001329 data = (htx->data - data);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001330 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001331 h1c->flags &= ~H1C_F_IN_FULL;
1332 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001333 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001334
Christopher Fauletcf56b992018-12-11 16:12:31 +01001335 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1336
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001337 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001338 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletcf56b992018-12-11 16:12:31 +01001339 else if (!htx_is_empty(htx))
1340 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001341
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001342 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1343 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001344 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001345 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001346 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001347
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001348 return data;
Christopher Faulet47365272018-10-31 17:40:50 +01001349
1350 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001351 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001352 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001353 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001354 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001355 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001356}
1357
Christopher Faulet129817b2018-09-20 16:14:40 +02001358/*
1359 * Process outgoing data. It parses data and transfer them from the channel buffer into
1360 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1361 * 0 if it couldn't proceed.
1362 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001363static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1364{
Christopher Faulet129817b2018-09-20 16:14:40 +02001365 struct h1s *h1s = h1c->h1s;
1366 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001367 struct htx *chn_htx;
1368 struct htx_blk *blk;
1369 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001370 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001371 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001372 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001373
Christopher Faulet47365272018-10-31 17:40:50 +01001374 if (!count)
1375 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001376
Christopher Faulet9768c262018-10-22 09:34:31 +02001377 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001378 if (htx_is_empty(chn_htx))
1379 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001380
Christopher Faulet51dbc942018-09-13 09:05:15 +02001381 if (!h1_get_buf(h1c, &h1c->obuf)) {
1382 h1c->flags |= H1C_F_OUT_ALLOC;
1383 goto end;
1384 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001385
Christopher Fauletf2824e62018-10-01 12:12:37 +02001386 if (!conn_is_back(h1c->conn)) {
1387 h1m = &h1s->res;
1388 errflag = H1S_F_RES_ERROR;
1389 }
1390 else {
1391 h1m = &h1s->req;
1392 errflag = H1S_F_REQ_ERROR;
1393 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001394
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001395 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001396 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001397
1398 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001399
Willy Tarreau3815b222018-12-11 19:50:43 +01001400 /* Perform some optimizations to reduce the number of buffer copies.
1401 * First, if the mux's buffer is empty and the htx area contains
1402 * exactly one data block of the same size as the requested count,
1403 * then it's possible to simply swap the caller's buffer with the
1404 * mux's output buffer and adjust offsets and length to match the
1405 * entire DATA HTX block in the middle. In this case we perform a
1406 * true zero-copy operation from end-to-end. This is the situation
1407 * that happens all the time with large files. Second, if this is not
1408 * possible, but the mux's output buffer is empty, we still have an
1409 * opportunity to avoid the copy to the intermediary buffer, by making
1410 * the intermediary buffer's area point to the output buffer's area.
1411 * In this case we want to skip the HTX header to make sure that copies
1412 * remain aligned and that this operation remains possible all the
1413 * time. This goes for headers, data blocks and any data extracted from
1414 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001415 */
1416 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001417 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001418
1419 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001420 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001421 htx_get_blk_value(chn_htx, blk).len == count) {
1422 void *old_area = h1c->obuf.area;
1423
1424 h1c->obuf.area = buf->area;
1425 h1c->obuf.data = count;
1426
1427 buf->area = old_area;
1428 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001429
1430 /* The message is chunked. We need to emit the chunk
1431 * size. We have at least the size of the struct htx to
1432 * write the chunk envelope. It should be enough.
1433 */
1434 if (h1m->flags & H1_MF_CHNK) {
1435 h1_emit_chunk_size(&h1c->obuf, count);
1436 h1_emit_chunk_crlf(&h1c->obuf);
1437 }
1438
Willy Tarreau3815b222018-12-11 19:50:43 +01001439 total += count;
1440 goto out;
1441 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001442 tmp->area = h1c->obuf.area + h1c->obuf.head;
1443 }
1444
Christopher Faulet9768c262018-10-22 09:34:31 +02001445 tmp->size = b_room(&h1c->obuf);
1446
Christopher Fauletb2e84162018-12-06 11:39:49 +01001447 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001448 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001449 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001450 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001451 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001452 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001453
Christopher Fauletb2e84162018-12-06 11:39:49 +01001454 vlen = sz;
1455 if (vlen > count) {
1456 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1457 goto copy;
1458 vlen = count;
1459 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001460
Christopher Fauletb2e84162018-12-06 11:39:49 +01001461 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001462 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001463 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001464
1465 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001466 h1m_init_req(h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001467 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +02001468 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001469 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001470 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001471 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001472 goto copy;
1473 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001474 if (sl->flags & HTX_SL_F_BODYLESS)
1475 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001476 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001477 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001478
Christopher Faulet9768c262018-10-22 09:34:31 +02001479 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001480 h1m_init_res(h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001481 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +02001482 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001483 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001484 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001485 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001486 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001487 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001488 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001489 if (sl->info.res.status < 200 &&
1490 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1491 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001492 h1m->state = H1_MSG_HDR_FIRST;
1493 break;
1494
1495 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001496 h1m->state = H1_MSG_HDR_NAME;
1497 n = htx_get_blk_name(chn_htx, blk);
1498 v = htx_get_blk_value(chn_htx, blk);
1499
1500 if (isteqi(n, ist("transfer-encoding")))
1501 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001502 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001503 /* Only skip C-L header with invalid value. */
1504 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001505 goto skip_hdr;
1506 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001507 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001508 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001509 if (!v.len)
1510 goto skip_hdr;
1511 }
1512
Christopher Fauletc59ff232018-12-03 13:58:44 +01001513 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001514 goto copy;
1515 skip_hdr:
1516 h1m->state = H1_MSG_HDR_L2_LWS;
1517 break;
1518
1519 case HTX_BLK_PHDR:
1520 /* not implemented yet */
1521 h1m->flags |= errflag;
1522 break;
1523
1524 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001525 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001526 /* There is no "Connection:" header and
1527 * it the conn_mode must be
1528 * processed. So do it */
1529 n = ist("Connection");
1530 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001531 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001532 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001533 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001534 goto copy;
1535 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001536 process_conn_mode = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001537 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001538
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001539 if ((h1s->meth != HTTP_METH_CONNECT &&
1540 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001541 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1542 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1543 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1544 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1545 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001546 /* chunking needed but header not seen */
1547 if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28))
1548 goto copy;
1549 h1m->flags |= H1_MF_CHNK;
1550 }
1551
Christopher Faulet9768c262018-10-22 09:34:31 +02001552 h1m->state = H1_MSG_LAST_LF;
1553 if (!chunk_memcat(tmp, "\r\n", 2))
1554 goto copy;
1555
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001556 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1557 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1558 h1_set_req_tunnel_mode(h1s);
1559 }
1560 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1561 /* a successfull reply to a CONNECT or a protocol switching is sent
1562 * to the client . Switch the response to tunnel mode. */
1563 h1_set_res_tunnel_mode(h1s);
1564 }
1565 else
1566 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001567 break;
1568
1569 case HTX_BLK_DATA:
1570 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001571 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001572 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001573 goto copy;
1574 break;
1575
1576 case HTX_BLK_EOD:
1577 if (!chunk_memcat(tmp, "0\r\n", 3))
1578 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001579 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001580 h1m->state = H1_MSG_TRAILERS;
1581 break;
1582
1583 case HTX_BLK_TLR:
Willy Tarreau34d23482019-01-03 17:46:56 +01001584 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001585 if (!chunk_memcat(tmp, "0\r\n", 3))
1586 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001587 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001588 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001589 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001590 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001591 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001592 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001593 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001594 break;
1595
1596 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001597 if ((h1m->flags & H1_MF_CHNK)) {
Willy Tarreau34d23482019-01-03 17:46:56 +01001598 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001599 if (!chunk_memcat(tmp, "0\r\n", 3))
1600 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001601 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001602 }
Willy Tarreau34d23482019-01-03 17:46:56 +01001603 if (!(h1s->flags & H1S_F_HAVE_O_TLR)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001604 if (!chunk_memcat(tmp, "\r\n", 2))
1605 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001606 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet32188212018-11-20 18:21:43 +01001607 }
1608 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001609 h1m->state = H1_MSG_DONE;
1610 break;
1611
1612 case HTX_BLK_OOB:
1613 v = htx_get_blk_value(chn_htx, blk);
1614 if (!chunk_memcat(tmp, v.ptr, v.len))
1615 goto copy;
1616 break;
1617
1618 default:
1619 h1m->flags |= errflag;
1620 break;
1621 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001622 total += vlen;
1623 count -= vlen;
1624 if (sz == vlen)
1625 blk = htx_remove_blk(chn_htx, blk);
1626 else {
1627 htx_cut_data_blk(chn_htx, blk, vlen);
1628 break;
1629 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001630 }
1631
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001632 if (htx_is_empty(chn_htx))
1633 h1_handle_1xx_response(h1s, h1m);
1634
Christopher Faulet9768c262018-10-22 09:34:31 +02001635 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001636 /* when the output buffer is empty, tmp shares the same area so that we
1637 * only have to update pointers and lengths.
1638 */
Willy Tarreaub57af612019-01-23 20:43:53 +01001639 if (tmp->area == h1c->obuf.area + h1c->obuf.head)
Willy Tarreauc5efa332018-12-05 11:19:27 +01001640 h1c->obuf.data = tmp->data;
1641 else
1642 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001643
Willy Tarreau3815b222018-12-11 19:50:43 +01001644 htx_to_buf(chn_htx, buf);
1645 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001646 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001647 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001648 end:
1649 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001650}
1651
Christopher Faulet51dbc942018-09-13 09:05:15 +02001652/*********************************************************/
1653/* functions below are I/O callbacks from the connection */
1654/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001655static void h1_wake_stream_for_recv(struct h1s *h1s)
1656{
1657 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001658 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001659 tasklet_wakeup(h1s->recv_wait->task);
1660 h1s->recv_wait = NULL;
1661 }
1662}
1663static void h1_wake_stream_for_send(struct h1s *h1s)
1664{
1665 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001666 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001667 tasklet_wakeup(h1s->send_wait->task);
1668 h1s->send_wait = NULL;
1669 }
1670}
1671
Christopher Faulet51dbc942018-09-13 09:05:15 +02001672/*
1673 * Attempt to read data, and subscribe if none available
1674 */
1675static int h1_recv(struct h1c *h1c)
1676{
1677 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001678 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001679 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001680 int rcvd = 0;
1681
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001682 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001683 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001684
Olivier Houchard75159a92018-12-03 18:46:09 +01001685 if (!h1_recv_allowed(h1c)) {
1686 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001688 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001689
Christopher Fauletfeb11742018-11-29 15:12:34 +01001690 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001691 rcvd = 1;
1692 goto end;
1693 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001694
Christopher Faulet51dbc942018-09-13 09:05:15 +02001695 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1696 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001697 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001698 }
1699
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001700 /*
1701 * If we only have a small amount of data, realign it,
1702 * it's probably cheaper than doing 2 recv() calls.
1703 */
1704 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1705 b_slow_realign(&h1c->ibuf, trash.area, 0);
1706
Willy Tarreau45f2b892018-12-05 07:59:27 +01001707 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001708 if (max) {
1709 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001710
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001711 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001712 if (!b_data(&h1c->ibuf)) {
1713 /* try to pre-align the buffer like the rxbufs will be
1714 * to optimize memory copies.
1715 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001716 h1c->ibuf.head = sizeof(struct htx);
1717 }
Willy Tarreauc0960d12018-12-14 10:59:15 +01001718 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001719 }
Christopher Faulet47365272018-10-31 17:40:50 +01001720 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001721 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001722 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001723 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001724 if (h1s->csinfo.t_idle == -1)
1725 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1726 }
Christopher Faulet47365272018-10-31 17:40:50 +01001727 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001728
Christopher Fauletcf56b992018-12-11 16:12:31 +01001729 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001730 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001731 goto end;
1732 }
1733
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001734 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001735
Christopher Faulet9768c262018-10-22 09:34:31 +02001736 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001737 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1738 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001739
Christopher Faulete6b39942018-12-07 09:42:49 +01001740 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Christopher Faulet87a8f352019-03-22 14:51:36 +01001741 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001742 if (!b_data(&h1c->ibuf))
1743 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001744 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001745 h1c->flags |= H1C_F_IN_FULL;
1746 return rcvd;
1747}
1748
1749
1750/*
1751 * Try to send data if possible
1752 */
1753static int h1_send(struct h1c *h1c)
1754{
1755 struct connection *conn = h1c->conn;
1756 unsigned int flags = 0;
1757 size_t ret;
1758 int sent = 0;
1759
1760 if (conn->flags & CO_FL_ERROR)
1761 return 0;
1762
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001763 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001764 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1765 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001766 return 0;
1767 }
1768
Christopher Faulet51dbc942018-09-13 09:05:15 +02001769 if (!b_data(&h1c->obuf))
1770 goto end;
1771
1772 if (h1c->flags & H1C_F_OUT_FULL)
1773 flags |= CO_SFL_MSG_MORE;
1774
1775 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1776 if (ret > 0) {
1777 h1c->flags &= ~H1C_F_OUT_FULL;
1778 b_del(&h1c->obuf, ret);
1779 sent = 1;
1780 }
1781
Christopher Faulet145aa472018-12-06 10:56:20 +01001782 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1783 /* error or output closed, nothing to send, clear the buffer to release it */
1784 b_reset(&h1c->obuf);
1785 }
1786
Christopher Faulet51dbc942018-09-13 09:05:15 +02001787 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001788 if (!(h1c->flags & H1C_F_OUT_FULL))
1789 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001790
Christopher Faulet51dbc942018-09-13 09:05:15 +02001791 /* We're done, no more to send */
1792 if (!b_data(&h1c->obuf)) {
1793 h1_release_buf(h1c, &h1c->obuf);
1794 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001795 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001796 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001797 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1798 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001799
1800 return sent;
1801}
1802
Christopher Faulet51dbc942018-09-13 09:05:15 +02001803
1804/* callback called on any event by the connection handler.
1805 * It applies changes and returns zero, or < 0 if it wants immediate
1806 * destruction of the connection.
1807 */
1808static int h1_process(struct h1c * h1c)
1809{
1810 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001811 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001812
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001813 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001814 return -1;
1815
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001816 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001817 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1818 goto end;
1819 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001820 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001821 }
1822
Christopher Fauletfeb11742018-11-29 15:12:34 +01001823 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001824 if (h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001825 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001826 conn_xprt_read0_pending(conn))
1827 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001828 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001829 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001830 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001831 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001832 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001833 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001834 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001835 }
1836
Christopher Fauletfeb11742018-11-29 15:12:34 +01001837 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1838 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1839
Olivier Houchard75159a92018-12-03 18:46:09 +01001840 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1841 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001842 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001843 int flags = 0;
1844
1845 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1846 flags |= CS_FL_ERROR;
1847 if (conn_xprt_read0_pending(conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001848 flags |= CS_FL_REOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001849 h1s->cs->flags |= flags;
1850 h1s->cs->data_cb->wake(h1s->cs);
1851 }
Christopher Faulet47365272018-10-31 17:40:50 +01001852 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001853 if (h1c->task) {
1854 h1c->task->expire = TICK_ETERNITY;
1855 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001856 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 +01001857 ? h1c->shut_timeout
1858 : h1c->timeout));
1859 task_queue(h1c->task);
1860 }
1861 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001862 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001863
1864 release:
1865 h1_release(conn);
1866 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001867}
1868
1869static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1870{
1871 struct h1c *h1c = ctx;
1872 int ret = 0;
1873
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001874 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001875 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001876 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001877 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001878 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001879 h1_process(h1c);
1880 return NULL;
1881}
1882
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001883static void h1_reset(struct connection *conn)
1884{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001885 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001886
1887 /* Reset the flags, and let the mux know we're waiting for a connection */
1888 h1c->flags = H1C_F_CS_WAIT_CONN;
1889}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001890
1891static int h1_wake(struct connection *conn)
1892{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001893 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001894 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001895
Christopher Faulet539e0292018-11-19 10:40:09 +01001896 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001897 ret = h1_process(h1c);
1898 if (ret == 0) {
1899 struct h1s *h1s = h1c->h1s;
1900
1901 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1902 ret = h1s->cs->data_cb->wake(h1s->cs);
1903 }
1904 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001905}
1906
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001907/* Connection timeout management. The principle is that if there's no receipt
1908 * nor sending for a certain amount of time, the connection is closed.
1909 */
1910static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
1911{
1912 struct h1c *h1c = context;
1913 int expired = tick_is_expired(t->expire, now_ms);
1914
1915 if (!expired && h1c)
1916 return t;
1917
1918 task_delete(t);
1919 task_free(t);
1920
1921 if (!h1c) {
1922 /* resources were already deleted */
1923 return NULL;
1924 }
1925
1926 h1c->task = NULL;
1927 /* If a stream is still attached to the mux, just set an error and wait
1928 * for the stream's timeout. Otherwise, release the mux. This is only ok
1929 * because same timeouts are used.
1930 */
1931 if (h1c->h1s && h1c->h1s->cs)
1932 h1c->flags |= H1C_F_CS_ERROR;
1933 else
1934 h1_release(h1c->conn);
1935 return NULL;
1936}
1937
Christopher Faulet51dbc942018-09-13 09:05:15 +02001938/*******************************************/
1939/* functions below are used by the streams */
1940/*******************************************/
1941/*
1942 * Attach a new stream to a connection
1943 * (Used for outgoing connections)
1944 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001945static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001946{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001947 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001948 struct conn_stream *cs = NULL;
1949 struct h1s *h1s;
1950
1951 if (h1c->flags & H1C_F_CS_ERROR)
1952 goto end;
1953
1954 cs = cs_new(h1c->conn);
1955 if (!cs)
1956 goto end;
1957
Olivier Houchardf502aca2018-12-14 19:42:40 +01001958 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001959 if (h1s == NULL)
1960 goto end;
1961
1962 return cs;
1963 end:
1964 cs_free(cs);
1965 return NULL;
1966}
1967
1968/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1969 * this mux, it's easy as we can only store a single conn_stream.
1970 */
1971static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1972{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001973 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001974 struct h1s *h1s = h1c->h1s;
1975
1976 if (h1s)
1977 return h1s->cs;
1978
1979 return NULL;
1980}
1981
1982static void h1_destroy(struct connection *conn)
1983{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001984 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001985
1986 if (!h1c->h1s)
1987 h1_release(conn);
1988}
1989
1990/*
1991 * Detach the stream from the connection and possibly release the connection.
1992 */
1993static void h1_detach(struct conn_stream *cs)
1994{
1995 struct h1s *h1s = cs->ctx;
1996 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001997 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01001998 int has_keepalive;
1999 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002000
2001 cs->ctx = NULL;
2002 if (!h1s)
2003 return;
2004
Olivier Houchardf502aca2018-12-14 19:42:40 +01002005 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002006 h1c = h1s->h1c;
2007 h1s->cs = NULL;
2008
Olivier Houchard8a786902018-12-15 16:05:40 +01002009 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2010 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2011 h1s_destroy(h1s);
2012
2013 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002014 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002015 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002016 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002017 h1c->conn->flags |= CO_FL_PRIVATE;
2018
Olivier Houchard44d59142018-12-13 18:46:22 +01002019 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002020 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002021 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2022 h1c->conn->owner = NULL;
2023 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2024 /* The server doesn't want it, let's kill the connection right away */
2025 h1c->conn->mux->destroy(h1c->conn);
2026 else
2027 tasklet_wakeup(h1c->wait_event.task);
2028 return;
2029
2030 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002031 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002032 if (h1c->conn->owner == sess) {
2033 int ret = session_check_idle_conn(sess, h1c->conn);
2034 if (ret == -1)
2035 /* The connection got destroyed, let's leave */
2036 return;
2037 else if (ret == 1) {
2038 /* The connection was added to the server list,
2039 * wake the task so we can subscribe to events
2040 */
2041 tasklet_wakeup(h1c->wait_event.task);
2042 return;
2043 }
2044 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002045 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002046 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002047 struct server *srv = objt_server(h1c->conn->target);
2048
2049 if (srv) {
2050 if (h1c->conn->flags & CO_FL_PRIVATE)
2051 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002052 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002053 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2054 else
2055 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2056 }
2057 }
2058 }
2059
Christopher Faulet51dbc942018-09-13 09:05:15 +02002060 /* We don't want to close right now unless the connection is in error */
Christopher Faulet666a0c42019-01-08 11:12:04 +01002061 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01002062 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002063 h1_release(h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002064 else {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002065 tasklet_wakeup(h1c->wait_event.task);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002066 if (h1c->task) {
2067 h1c->task->expire = TICK_ETERNITY;
2068 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002069 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 +01002070 ? h1c->shut_timeout
2071 : h1c->timeout));
2072 task_queue(h1c->task);
2073 }
2074 }
2075 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002076}
2077
2078
2079static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2080{
2081 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002082 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002083
2084 if (!h1s)
2085 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002086 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002087
Christopher Faulet7f366362019-04-08 10:51:20 +02002088 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2089 goto do_shutr;
2090
2091 if (h1s->flags & H1S_F_WANT_KAL)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002092 return;
2093
Christopher Faulet7f366362019-04-08 10:51:20 +02002094 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002095 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2096 if (cs->flags & CS_FL_SHR)
2097 return;
2098 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
2099 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002100 if ((cs->conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet7f366362019-04-08 10:51:20 +02002101 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002102}
2103
2104static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2105{
2106 struct h1s *h1s = cs->ctx;
2107 struct h1c *h1c;
2108
2109 if (!h1s)
2110 return;
2111 h1c = h1s->h1c;
2112
Christopher Faulet7f366362019-04-08 10:51:20 +02002113 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2114 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002115
Christopher Faulet7f366362019-04-08 10:51:20 +02002116 if ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002117 return;
2118
Christopher Faulet7f366362019-04-08 10:51:20 +02002119 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002120 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2121 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2122 return;
2123
Christopher Faulet666a0c42019-01-08 11:12:04 +01002124 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002125}
2126
Christopher Faulet666a0c42019-01-08 11:12:04 +01002127static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002128{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002129 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002130
Christopher Faulet666a0c42019-01-08 11:12:04 +01002131 conn_xprt_shutw(conn);
2132 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2133 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2134 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002135}
2136
2137/* Called from the upper layer, to unsubscribe to events */
2138static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2139{
2140 struct wait_event *sw;
2141 struct h1s *h1s = cs->ctx;
2142
2143 if (!h1s)
2144 return 0;
2145
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002146 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002147 sw = param;
2148 if (h1s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002149 sw->events &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002150 h1s->recv_wait = NULL;
2151 }
2152 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002153 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002154 sw = param;
2155 if (h1s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002156 sw->events &= ~SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002157 h1s->send_wait = NULL;
2158 }
2159 }
2160 return 0;
2161}
2162
2163/* Called from the upper layer, to subscribe to events, such as being able to send */
2164static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2165{
2166 struct wait_event *sw;
2167 struct h1s *h1s = cs->ctx;
2168
2169 if (!h1s)
2170 return -1;
2171
2172 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002173 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002174 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002175 if (!(sw->events & SUB_RETRY_RECV)) {
2176 sw->events |= SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002177 sw->handle = h1s;
2178 h1s->recv_wait = sw;
2179 }
2180 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002181 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002182 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002183 if (!(sw->events & SUB_RETRY_SEND)) {
2184 sw->events |= SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002185 sw->handle = h1s;
2186 h1s->send_wait = sw;
2187 }
2188 return 0;
2189 default:
2190 break;
2191 }
2192 return -1;
2193}
2194
2195/* Called from the upper layer, to receive data */
2196static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2197{
2198 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002199 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002200 size_t ret = 0;
2201
Christopher Faulet539e0292018-11-19 10:40:09 +01002202 if (!(h1c->flags & H1C_F_IN_ALLOC))
2203 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002204
2205 if (flags & CO_RFL_BUF_FLUSH)
2206 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002207 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2208 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002209 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002210 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002211 }
2212 return ret;
2213}
2214
2215
2216/* Called from the upper layer, to send data */
2217static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2218{
2219 struct h1s *h1s = cs->ctx;
2220 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002221 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002222
2223 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002224 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002225
2226 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002227 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2228 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002229
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002230 while (total != count) {
2231 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002232
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002233 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2234 ret = h1_process_output(h1c, buf, count);
2235 if (!ret)
2236 break;
2237 total += ret;
2238 if (!h1_send(h1c))
2239 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002240 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002241
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002242 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002243}
2244
Christopher Faulet1be55f92018-10-02 15:59:23 +02002245#if defined(CONFIG_HAP_LINUX_SPLICE)
2246/* Send and get, using splicing */
2247static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2248{
2249 struct h1s *h1s = cs->ctx;
2250 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2251 int ret = 0;
2252
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002253 if (b_data(&h1s->h1c->ibuf)) {
2254 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002255 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002256 }
2257
2258 h1s->flags &= ~H1S_F_BUF_FLUSH;
2259 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002260 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2261 count = h1m->curr_len;
2262 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2263 if (h1m->state == H1_MSG_DATA && ret > 0)
2264 h1m->curr_len -= ret;
2265 end:
2266 return ret;
2267
2268}
2269
2270static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2271{
2272 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002273 int ret = 0;
2274
2275 if (b_data(&h1s->h1c->obuf))
2276 goto end;
2277
2278 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002279 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002280 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002281 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
2282 cs->conn->xprt->subscribe(cs->conn, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002283 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002284 return ret;
2285}
2286#endif
2287
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002288/* for debugging with CLI's "show fd" command */
2289static void h1_show_fd(struct buffer *msg, struct connection *conn)
2290{
2291 struct h1c *h1c = conn->ctx;
2292 struct h1s *h1s = h1c->h1s;
2293
Christopher Fauletf376a312019-01-04 15:16:06 +01002294 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2295 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002296 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2297 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2298 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2299 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2300
2301 if (h1s) {
2302 char *method;
2303
2304 if (h1s->meth < HTTP_METH_OTHER)
2305 method = http_known_methods[h1s->meth].ptr;
2306 else
2307 method = "UNKNOWN";
2308 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2309 " .meth=%s status=%d",
2310 h1s, h1s->flags,
2311 h1m_state_str(h1s->req.state),
2312 h1m_state_str(h1s->res.state), method, h1s->status);
2313 if (h1s->cs)
2314 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2315 h1s->cs->flags, h1s->cs->data);
2316 }
2317}
2318
Christopher Faulet51dbc942018-09-13 09:05:15 +02002319/****************************************/
2320/* MUX initialization and instanciation */
2321/****************************************/
2322
2323/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002324static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002325 .init = h1_init,
2326 .wake = h1_wake,
2327 .attach = h1_attach,
2328 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002329 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002330 .detach = h1_detach,
2331 .destroy = h1_destroy,
2332 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002333 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002334 .rcv_buf = h1_rcv_buf,
2335 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002336#if defined(CONFIG_HAP_LINUX_SPLICE)
2337 .rcv_pipe = h1_rcv_pipe,
2338 .snd_pipe = h1_snd_pipe,
2339#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002340 .subscribe = h1_subscribe,
2341 .unsubscribe = h1_unsubscribe,
2342 .shutr = h1_shutr,
2343 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002344 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002345 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002346 .flags = MX_FL_HTX,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002347 .name = "h1",
2348};
2349
2350
2351/* this mux registers default HTX proto */
2352static struct mux_proto_list mux_proto_htx =
2353{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2354
Willy Tarreau0108d902018-11-25 19:14:37 +01002355INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2356
Christopher Faulet51dbc942018-09-13 09:05:15 +02002357/*
2358 * Local variables:
2359 * c-indent-level: 8
2360 * c-basic-offset: 8
2361 * End:
2362 */