blob: a40ef3f4ae1603c28d0b9936733f7c4bf8a3e5d7 [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;
Christopher Faulete9b70722019-04-08 10:46:02 +0200290
291 /* For frontend connections we should always have a session */
292 if (!sess)
293 sess = h1c->conn->owner;
294
295 h1s->csinfo.create_date = sess->accept_date;
296 h1s->csinfo.tv_create = sess->tv_accept;
297 h1s->csinfo.t_handshake = sess->t_handshake;
298 h1s->csinfo.t_idle = -1;
Christopher Faulet129817b2018-09-20 16:14:40 +0200299 }
300 else {
301 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
302 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200303
Christopher Fauletfeb11742018-11-29 15:12:34 +0100304 h1s->csinfo.create_date = date;
305 h1s->csinfo.tv_create = now;
306 h1s->csinfo.t_handshake = 0;
307 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200308 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100309
Christopher Faulete9b70722019-04-08 10:46:02 +0200310 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
311 * create a new one.
312 */
313 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200314 cs->ctx = h1s;
315 h1s->cs = cs;
316 }
Christopher Faulet47365272018-10-31 17:40:50 +0100317 else {
318 cs = h1s_new_cs(h1s);
319 if (!cs)
320 goto fail;
321 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200322 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100323
324 fail:
325 pool_free(pool_head_h1s, h1s);
326 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200327}
328
329static void h1s_destroy(struct h1s *h1s)
330{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200331 if (h1s) {
332 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200333
Christopher Fauletf2824e62018-10-01 12:12:37 +0200334 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200335
Christopher Fauletf2824e62018-10-01 12:12:37 +0200336 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100337 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200338 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100339 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200340
Christopher Fauletcb55f482018-12-10 11:56:47 +0100341 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100342 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
343 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
344 h1c->flags |= H1C_F_CS_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200345 pool_free(pool_head_h1s, h1s);
346 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200347}
348
Christopher Fauletfeb11742018-11-29 15:12:34 +0100349static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
350{
351 struct h1s *h1s = cs->ctx;
352
353 if (h1s && !conn_is_back(cs->conn))
354 return &h1s->csinfo;
355 return NULL;
356}
357
Christopher Faulet51dbc942018-09-13 09:05:15 +0200358/*
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100359 * Initialize the mux once it's attached. It is expected that conn->ctx
Christopher Faulet51dbc942018-09-13 09:05:15 +0200360 * points to the existing conn_stream (for outgoing connections) or NULL (for
361 * incoming ones). Returns < 0 on error.
362 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100363static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200364{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200365 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100366 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200367
368 h1c = pool_alloc(pool_head_h1c);
369 if (!h1c)
370 goto fail_h1c;
371 h1c->conn = conn;
372 h1c->px = proxy;
373
374 h1c->flags = H1C_F_NONE;
375 h1c->ibuf = BUF_NULL;
376 h1c->obuf = BUF_NULL;
377 h1c->h1s = NULL;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100378 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200379
Christopher Faulet51dbc942018-09-13 09:05:15 +0200380 LIST_INIT(&h1c->buf_wait.list);
381 h1c->wait_event.task = tasklet_new();
382 if (!h1c->wait_event.task)
383 goto fail;
384 h1c->wait_event.task->process = h1_io_cb;
385 h1c->wait_event.task->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100386 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200387
Christopher Faulete9b70722019-04-08 10:46:02 +0200388 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100389 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
390 if (tick_isset(proxy->timeout.serverfin))
391 h1c->shut_timeout = proxy->timeout.serverfin;
392 } else {
393 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
394 if (tick_isset(proxy->timeout.clientfin))
395 h1c->shut_timeout = proxy->timeout.clientfin;
396 }
397 if (tick_isset(h1c->timeout)) {
398 t = task_new(tid_bit);
399 if (!t)
400 goto fail;
401
402 h1c->task = t;
403 t->process = h1_timeout_task;
404 t->context = h1c;
405 t->expire = tick_add(now_ms, h1c->timeout);
406 }
407
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200408 if (!(conn->flags & CO_FL_CONNECTED))
409 h1c->flags |= H1C_F_CS_WAIT_CONN;
410
Christopher Fauletf2824e62018-10-01 12:12:37 +0200411 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100412 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200413 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200414
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100415 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200416
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100417
418 if (t)
419 task_queue(t);
420
Christopher Faulet51dbc942018-09-13 09:05:15 +0200421 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard9b960a82019-01-04 16:51:40 +0100422 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200423
424 /* mux->wake will be called soon to complete the operation */
425 return 0;
426
427 fail:
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100428 if (t)
429 task_free(t);
Christopher Faulet47365272018-10-31 17:40:50 +0100430 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200431 tasklet_free(h1c->wait_event.task);
432 pool_free(pool_head_h1c, h1c);
433 fail_h1c:
434 return -1;
435}
436
437
438/* release function for a connection. This one should be called to free all
439 * resources allocated to the mux.
440 */
441static void h1_release(struct connection *conn)
442{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100443 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200444
Christopher Faulet51dbc942018-09-13 09:05:15 +0200445 if (h1c) {
446 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
447 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
448 LIST_DEL(&h1c->buf_wait.list);
449 LIST_INIT(&h1c->buf_wait.list);
450 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
451 }
452
453 h1_release_buf(h1c, &h1c->ibuf);
454 h1_release_buf(h1c, &h1c->obuf);
455
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100456 if (h1c->task) {
457 h1c->task->context = NULL;
458 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
459 h1c->task = NULL;
460 }
461
Christopher Faulet51dbc942018-09-13 09:05:15 +0200462 if (h1c->wait_event.task)
463 tasklet_free(h1c->wait_event.task);
464
Christopher Fauletf2824e62018-10-01 12:12:37 +0200465 h1s_destroy(h1c->h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100466 if (h1c->wait_event.events != 0)
467 conn->xprt->unsubscribe(conn, h1c->wait_event.events,
Christopher Faulet51dbc942018-09-13 09:05:15 +0200468 &h1c->wait_event);
469 pool_free(pool_head_h1c, h1c);
470 }
471
472 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100473 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200474
475 conn_stop_tracking(conn);
476 conn_full_close(conn);
477 if (conn->destroy_cb)
478 conn->destroy_cb(conn);
479 conn_free(conn);
480}
481
482/******************************************************/
483/* functions below are for the H1 protocol processing */
484/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200485/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
486 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200487 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100488static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200489{
Christopher Faulet570d1612018-11-26 11:13:57 +0100490 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200491
Christopher Faulet570d1612018-11-26 11:13:57 +0100492 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200493 (*(p + 5) > '1' ||
494 (*(p + 5) == '1' && *(p + 7) >= '1')))
495 h1m->flags |= H1_MF_VER_11;
496}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200497
Christopher Faulet9768c262018-10-22 09:34:31 +0200498/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
499 * greater or equal to 1.1
500 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100501static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200502{
Christopher Faulet570d1612018-11-26 11:13:57 +0100503 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200504
Christopher Faulet570d1612018-11-26 11:13:57 +0100505 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200506 (*(p + 5) > '1' ||
507 (*(p + 5) == '1' && *(p + 7) >= '1')))
508 h1m->flags |= H1_MF_VER_11;
509}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200510
Christopher Faulet9768c262018-10-22 09:34:31 +0200511/*
512 * Check the validity of the request version. If the version is valid, it
513 * returns 1. Otherwise, it returns 0.
514 */
515static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
516{
517 struct h1c *h1c = h1s->h1c;
518
519 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
520 * exactly one digit "." one digit. This check may be disabled using
521 * option accept-invalid-http-request.
522 */
523 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
524 if (sl.rq.v.len != 8)
525 return 0;
526
527 if (*(sl.rq.v.ptr + 4) != '/' ||
528 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
529 *(sl.rq.v.ptr + 6) != '.' ||
530 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
531 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200532 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200533 else if (!sl.rq.v.len) {
534 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200535
Christopher Faulet9768c262018-10-22 09:34:31 +0200536 /* RFC 1945 allows only GET for HTTP/0.9 requests */
537 if (sl.rq.meth != HTTP_METH_GET)
538 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200539
Christopher Faulet9768c262018-10-22 09:34:31 +0200540 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
541 if (!sl.rq.u.len)
542 return 0;
543
544 /* Add HTTP version */
545 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100546 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200547 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100548
549 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100550 ((*(sl.rq.v.ptr + 5) > '1') ||
551 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100552 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200553 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200554}
555
Christopher Faulet9768c262018-10-22 09:34:31 +0200556/*
557 * Check the validity of the response version. If the version is valid, it
558 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200559 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200560static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200561{
Christopher Faulet9768c262018-10-22 09:34:31 +0200562 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200563
Christopher Faulet9768c262018-10-22 09:34:31 +0200564 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
565 * exactly one digit "." one digit. This check may be disabled using
566 * option accept-invalid-http-request.
567 */
568 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
569 if (sl.st.v.len != 8)
570 return 0;
571
572 if (*(sl.st.v.ptr + 4) != '/' ||
573 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
574 *(sl.st.v.ptr + 6) != '.' ||
575 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
576 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200577 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100578
579 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100580 ((*(sl.st.v.ptr + 5) > '1') ||
581 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100582 h1m->flags |= H1_MF_VER_11;
583
Christopher Faulet9768c262018-10-22 09:34:31 +0200584 return 1;
585}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200586
587/* Deduce the connection mode of the client connection, depending on the
588 * configuration and the H1 message flags. This function is called twice, the
589 * first time when the request is parsed and the second time when the response
590 * is parsed.
591 */
592static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
593{
594 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200595
596 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100597 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200598 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100599 h1s->status == 101) {
600 /* Either we've established an explicit tunnel, or we're
601 * switching the protocol. In both cases, we're very unlikely to
602 * understand the next protocols. We have to switch to tunnel
603 * mode, so that we transfer the request and responses then let
604 * this protocol pass unmodified. When we later implement
605 * specific parsers for such protocols, we'll want to check the
606 * Upgrade header which contains information about that protocol
607 * for responses with status 101 (eg: see RFC2817 about TLS).
608 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200609 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100610 }
611 else if (h1s->flags & H1S_F_WANT_KAL) {
612 /* By default the client is in KAL mode. CLOSE mode mean
613 * it is imposed by the client itself. So only change
614 * KAL mode here. */
615 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
616 /* no length known or explicit close => close */
617 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
618 }
619 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
620 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
621 /* no explict keep-alive and option httpclose => close */
622 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
623 }
624 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200625 }
626 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100627 /* Input direction: first pass */
628 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
629 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200630 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100631 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200632 }
633
634 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
635 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
636 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
637}
638
639/* Deduce the connection mode of the client connection, depending on the
640 * configuration and the H1 message flags. This function is called twice, the
641 * first time when the request is parsed and the second time when the response
642 * is parsed.
643 */
644static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
645{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100646 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100647 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100648 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200649
Christopher Fauletf2824e62018-10-01 12:12:37 +0200650 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100651 /* Input direction: second pass */
652
Christopher Fauletf2824e62018-10-01 12:12:37 +0200653 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100654 h1s->status == 101) {
655 /* Either we've established an explicit tunnel, or we're
656 * switching the protocol. In both cases, we're very unlikely to
657 * understand the next protocols. We have to switch to tunnel
658 * mode, so that we transfer the request and responses then let
659 * this protocol pass unmodified. When we later implement
660 * specific parsers for such protocols, we'll want to check the
661 * Upgrade header which contains information about that protocol
662 * for responses with status 101 (eg: see RFC2817 about TLS).
663 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200664 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletb992af02019-03-28 15:42:24 +0100665 }
666 else if (h1s->flags & H1S_F_WANT_KAL) {
667 /* By default the server is in KAL mode. CLOSE mode mean
668 * it is imposed by haproxy itself. So only change KAL
669 * mode here. */
670 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
671 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
672 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
673 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
674 }
675 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200676 }
Christopher Faulet70033782018-12-05 13:50:11 +0100677 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100678 /* Output direction: first pass */
679 if (h1m->flags & H1_MF_CONN_CLO) {
680 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100681 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletb992af02019-03-28 15:42:24 +0100682 }
683 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
684 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
685 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
686 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
687 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
688 /* no explicit keep-alive option httpclose/server-close => close */
689 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
690 }
Christopher Faulet70033782018-12-05 13:50:11 +0100691 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200692
693 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
694 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
695 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200696}
697
Christopher Fauletb992af02019-03-28 15:42:24 +0100698static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200699{
700 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200701
702 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
703 * token is found
704 */
705 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200706 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200707
708 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100709 if (!(h1m->flags & H1_MF_VER_11))
710 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200711 }
712 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Fauletb992af02019-03-28 15:42:24 +0100713 if (h1m->flags & H1_MF_VER_11)
714 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200715 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200716}
717
Christopher Fauletb992af02019-03-28 15:42:24 +0100718static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200719{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200720 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
721 * token is found
722 */
723 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200724 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200725
726 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100727 if (!(h1m->flags & H1_MF_VER_11) ||
728 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11))
729 *conn_val = ist("keep-alive");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200730 }
731 else { /* H1S_F_WANT_CLO */
Christopher Fauletb992af02019-03-28 15:42:24 +0100732 if (h1m->flags & H1_MF_VER_11)
733 *conn_val = ist("close");
Christopher Fauletf2824e62018-10-01 12:12:37 +0200734 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200735}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200736
Christopher Fauletb992af02019-03-28 15:42:24 +0100737static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +0200738{
Christopher Fauletb992af02019-03-28 15:42:24 +0100739 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +0200740 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +0100741 else
Christopher Faulet9768c262018-10-22 09:34:31 +0200742 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200743}
744
Christopher Fauletb992af02019-03-28 15:42:24 +0100745static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
746{
747 if (!conn_is_back(h1s->h1c->conn))
748 h1_set_cli_conn_mode(h1s, h1m);
749 else
750 h1_set_srv_conn_mode(h1s, h1m);
751
752 if (!(h1m->flags & H1_MF_RESP))
753 h1_update_req_conn_value(h1s, h1m, conn_val);
754 else
755 h1_update_res_conn_value(h1s, h1m, conn_val);
756}
Christopher Faulete44769b2018-11-29 23:01:45 +0100757
758/* Append the description of what is present in error snapshot <es> into <out>.
759 * The description must be small enough to always fit in a buffer. The output
760 * buffer may be the trash so the trash must not be used inside this function.
761 */
762static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
763{
764 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100765 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
766 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
767 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
768 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
769 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
770 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100771}
772/*
773 * Capture a bad request or response and archive it in the proxy's structure.
774 * By default it tries to report the error position as h1m->err_pos. However if
775 * this one is not set, it will then report h1m->next, which is the last known
776 * parsing point. The function is able to deal with wrapping buffers. It always
777 * displays buffers as a contiguous area starting at buf->p. The direction is
778 * determined thanks to the h1m's flags.
779 */
780static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
781 struct h1m *h1m, struct buffer *buf)
782{
783 struct session *sess = h1c->conn->owner;
784 struct proxy *proxy = h1c->px;
785 struct proxy *other_end = sess->fe;
786 union error_snapshot_ctx ctx;
787
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100788 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100789 other_end = si_strm(h1s->cs->data)->be;
790
791 /* http-specific part now */
792 ctx.h1.state = h1m->state;
793 ctx.h1.c_flags = h1c->flags;
794 ctx.h1.s_flags = h1s->flags;
795 ctx.h1.m_flags = h1m->flags;
796 ctx.h1.m_clen = h1m->curr_len;
797 ctx.h1.m_blen = h1m->body_len;
798
799 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
800 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100801 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
802 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100803}
804
Christopher Fauletadb22202018-12-12 10:32:09 +0100805/* Emit the chunksize followed by a CRLF in front of data of the buffer
806 * <buf>. It goes backwards and starts with the byte before the buffer's
807 * head. The caller is responsible for ensuring there is enough room left before
808 * the buffer's head for the string.
809 */
810static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
811{
812 char *beg, *end;
813
814 beg = end = b_head(buf);
815 *--beg = '\n';
816 *--beg = '\r';
817 do {
818 *--beg = hextab[chksz & 0xF];
819 } while (chksz >>= 4);
820 buf->head -= (end - beg);
821 b_add(buf, end - beg);
822}
823
824/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
825 * ensuring there is enough room left in the buffer for the string. */
826static void h1_emit_chunk_crlf(struct buffer *buf)
827{
828 *(b_peek(buf, b_data(buf))) = '\r';
829 *(b_peek(buf, b_data(buf) + 1)) = '\n';
830 b_add(buf, 2);
831}
832
Christopher Faulet129817b2018-09-20 16:14:40 +0200833/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100834 * Switch the request to tunnel mode. This function must only be called for
835 * CONNECT requests. On the client side, the mux is mark as busy on input,
836 * waiting the response.
837 */
838static void h1_set_req_tunnel_mode(struct h1s *h1s)
839{
840 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
841 h1s->req.state = H1_MSG_TUNNEL;
842 if (!conn_is_back(h1s->h1c->conn))
843 h1s->h1c->flags |= H1C_F_IN_BUSY;
844}
845
846/*
847 * Switch the response to tunnel mode. This function must only be called on
848 * successfull replies to CONNECT requests or on protocol switching. On the
849 * server side, if the request is not finished, the mux is mark as busy on
850 * input. Otherwise the request is also switch to tunnel mode.
851 */
852static void h1_set_res_tunnel_mode(struct h1s *h1s)
853{
854 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
855 h1s->res.state = H1_MSG_TUNNEL;
856 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
857 h1s->h1c->flags |= H1C_F_IN_BUSY;
858 else {
859 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
860 h1s->req.state = H1_MSG_TUNNEL;
861 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
862 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
863 tasklet_wakeup(h1s->h1c->wait_event.task);
864 }
865 }
866}
867
868/*
Christopher Fauletcdc90e92019-03-28 13:28:46 +0100869 * Handle 100-Continue responses or any other informational 1xx responses which
870 * is non-final. In such case, this function reset the response parser. It is
871 * the caller responsibility to call this function when appropriate.
872 */
873static void h1_handle_1xx_response(struct h1s *h1s, struct h1m *h1m)
874{
875 if ((h1m->flags & H1_MF_RESP) && h1m->state == H1_MSG_DONE &&
876 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
877 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100878 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletcdc90e92019-03-28 13:28:46 +0100879 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
880 }
881}
882
883/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200884 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200885 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
886 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800887 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200888 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200889static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200890 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200891{
892 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100893 union h1_sl h1sl;
894 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200895 int ret = 0;
896
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100897 if (!max)
898 goto end;
899
Christopher Faulet129817b2018-09-20 16:14:40 +0200900 /* Realing input buffer if necessary */
901 if (b_head(buf) + b_data(buf) > b_wrap(buf))
902 b_slow_realign(buf, trash.area, 0);
903
Christopher Fauletf2824e62018-10-01 12:12:37 +0200904 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100905 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200906 if (ret <= 0) {
907 /* Incomplete or invalid message. If the buffer is full, it's an
908 * error because headers are too large to be handled by the
909 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200910 if (ret < 0 || (!ret && b_full(buf)))
911 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200912 goto end;
913 }
914
915 /* messages headers fully parsed, do some checks to prepare the body
916 * parsing.
917 */
918
919 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200920 if (ret > (b_size(buf) - global.tune.maxrewrite))
921 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200922
Christopher Faulet9768c262018-10-22 09:34:31 +0200923 /* Save the request's method or the response's status, check if the body
924 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200925 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100926 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200927
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100928 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +0200929 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100930
931 if (h1s->meth == HTTP_METH_CONNECT) {
932 /* Switch CONNECT requests to tunnel mode */
933 h1_set_req_tunnel_mode(h1s);
934 }
935 else if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len) {
936 /* Switch requests with no body to done. */
Christopher Faulet129817b2018-09-20 16:14:40 +0200937 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100938 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200939
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100940 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
941 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200942 h1m->err_state = h1m->state;
943 goto vsn_error;
944 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200945 }
946 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100947 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200948
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100949 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
950 h1s->status == 101) {
951 /* Switch successfull replies to CONNECT requests and
952 * protocol switching to tunnel mode. */
953 h1_set_res_tunnel_mode(h1s);
954 }
955 else if ((h1s->meth == HTTP_METH_HEAD) ||
956 (h1s->status >= 100 && h1s->status < 200) ||
957 (h1s->status == 204) || (h1s->status == 304)) {
958 /* Switch responses without body to done. */
Christopher Faulet129817b2018-09-20 16:14:40 +0200959 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
960 h1m->flags |= H1_MF_XFER_LEN;
961 h1m->curr_len = h1m->body_len = 0;
962 h1m->state = H1_MSG_DONE;
963 }
964 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100965 /* Responses with a known body length. Switch requests
966 * with no body to done. */
Christopher Faulet129817b2018-09-20 16:14:40 +0200967 h1m->flags |= H1_MF_XFER_LEN;
968 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
969 h1m->state = H1_MSG_DONE;
970 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100971 else {
972 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +0200973 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100974 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200975
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100976 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
977 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200978 h1m->err_state = h1m->state;
979 goto vsn_error;
980 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200981 }
982
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100983 /* Set HTX start-line flags */
984 if (h1m->flags & H1_MF_VER_11)
985 flags |= HTX_SL_F_VER_11;
986 if (h1m->flags & H1_MF_XFER_ENC)
987 flags |= HTX_SL_F_XFER_ENC;
988 if (h1m->flags & H1_MF_XFER_LEN) {
989 flags |= HTX_SL_F_XFER_LEN;
990 if (h1m->flags & H1_MF_CHNK)
991 flags |= HTX_SL_F_CHNK;
992 else if (h1m->flags & H1_MF_CLEN)
993 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100994 if (h1m->state == H1_MSG_DONE)
995 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100996 }
997
Christopher Faulet9768c262018-10-22 09:34:31 +0200998 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100999 struct htx_sl *sl;
1000
1001 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1002 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001003 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001004 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001005 }
1006 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001007 struct htx_sl *sl;
1008
1009 flags |= HTX_SL_F_IS_RESP;
1010 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1011 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001012 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001013 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001014 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001015
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001016 if (h1m->state == H1_MSG_DONE) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001017 if (!htx_add_endof(htx, HTX_BLK_EOM))
1018 goto error;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001019 h1s->cs->flags |= CS_FL_EOI;
1020 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001021
Christopher Fauletb992af02019-03-28 15:42:24 +01001022 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001023
1024 /* If body length cannot be determined, set htx->extra to
1025 * ULLONG_MAX. This value is impossible in other cases.
1026 */
1027 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
1028
1029 /* Recheck there is enough space to do headers rewritting */
1030 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
1031 goto error;
1032
1033 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001034 end:
1035 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001036
1037 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001038 h1m->err_state = h1m->state;
1039 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001040 vsn_error:
1041 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +01001042 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001043 ret = 0;
1044 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001045}
1046
1047/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001048 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1049 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001050 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001051 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001052 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001053static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001054 struct buffer *buf, size_t *ofs, size_t max,
Christopher Fauletafe57842019-01-21 11:55:02 +01001055 struct buffer *htxbuf, size_t reserve)
Christopher Faulet129817b2018-09-20 16:14:40 +02001056{
Christopher Fauletafe57842019-01-21 11:55:02 +01001057 uint32_t data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001058 size_t total = 0;
1059 int ret = 0;
1060
Christopher Fauletafe57842019-01-21 11:55:02 +01001061 data_space = htx_free_data_space(htx);
1062 if (data_space <= reserve)
1063 goto end;
1064 data_space -= reserve;
1065
Christopher Faulet129817b2018-09-20 16:14:40 +02001066 if (h1m->flags & H1_MF_XFER_LEN) {
1067 if (h1m->flags & H1_MF_CLEN) {
1068 /* content-length: read only h2m->body_len */
1069 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001070 if (ret > data_space)
1071 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001072 if ((uint64_t)ret > h1m->curr_len)
1073 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001074 if (ret > b_contig_data(buf, *ofs))
1075 ret = b_contig_data(buf, *ofs);
1076 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001077 /* very often with large files we'll face the following
1078 * situation :
1079 * - htx is empty and points to <htxbuf>
1080 * - ret == buf->data
1081 * - buf->head == sizeof(struct htx)
1082 * => we can swap the buffers and place an htx header into
1083 * the target buffer instead
1084 */
1085 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1086 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1087 void *raw_area = buf->area;
1088 void *htx_area = htxbuf->area;
1089 struct htx_blk *blk;
1090
1091 buf->area = htx_area;
1092 htxbuf->area = raw_area;
1093 htx = (struct htx *)htxbuf->area;
1094 htx->size = htxbuf->size - sizeof(*htx);
1095 htx_reset(htx);
1096 b_set_data(htxbuf, b_size(htxbuf));
1097
1098 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1099 blk->info += ret;
1100 /* nothing else to do, the old buffer now contains an
1101 * empty pre-initialized HTX header
1102 */
1103 }
1104 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001105 goto end;
1106 h1m->curr_len -= ret;
1107 *ofs += ret;
1108 total += ret;
1109 }
1110
1111 if (!h1m->curr_len) {
1112 if (!htx_add_endof(htx, HTX_BLK_EOM))
1113 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001114 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001115 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet9768c262018-10-22 09:34:31 +02001116 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001117 }
1118 else if (h1m->flags & H1_MF_CHNK) {
1119 new_chunk:
1120 /* te:chunked : parse chunks */
1121 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001122 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001123 if (ret <= 0)
1124 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001125 h1m->state = H1_MSG_CHUNK_SIZE;
1126
Christopher Faulet129817b2018-09-20 16:14:40 +02001127 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001128 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001129 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001130 }
1131
1132 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1133 unsigned int chksz;
1134
Christopher Fauletf2824e62018-10-01 12:12:37 +02001135 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001136 if (ret <= 0)
1137 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001138 if (!chksz) {
1139 if (!htx_add_endof(htx, HTX_BLK_EOD))
1140 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001141 h1s->flags |= H1S_F_HAVE_I_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001142 h1m->state = H1_MSG_TRAILERS;
1143 }
1144 else
1145 h1m->state = H1_MSG_DATA;
1146
Christopher Faulet129817b2018-09-20 16:14:40 +02001147 h1m->curr_len = chksz;
1148 h1m->body_len += chksz;
1149 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001150 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001151 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001152 }
1153
1154 if (h1m->state == H1_MSG_DATA) {
1155 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001156 if (ret > data_space)
1157 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001158 if ((uint64_t)ret > h1m->curr_len)
1159 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001160 if (ret > b_contig_data(buf, *ofs))
1161 ret = b_contig_data(buf, *ofs);
1162 if (ret) {
1163 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1164 goto end;
1165 h1m->curr_len -= ret;
1166 max -= ret;
1167 *ofs += ret;
1168 total += ret;
1169 }
1170 if (!h1m->curr_len) {
1171 h1m->state = H1_MSG_CHUNK_CRLF;
Christopher Fauletafe57842019-01-21 11:55:02 +01001172 data_space = htx_free_data_space(htx);
1173 if (data_space <= reserve)
1174 goto end;
1175 data_space -= reserve;
Christopher Faulet9768c262018-10-22 09:34:31 +02001176 goto new_chunk;
1177 }
1178 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001179 }
1180
1181 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001182 /* Trailers were alread parsed, only the EOM
1183 * need to be added */
Willy Tarreau34d23482019-01-03 17:46:56 +01001184 if (h1s->flags & H1S_F_HAVE_I_TLR)
Christopher Faulet17276482018-11-22 11:44:35 +01001185 goto skip_tlr_parsing;
1186
Christopher Fauletf2824e62018-10-01 12:12:37 +02001187 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001188 if (ret > data_space)
1189 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001190 if (ret <= 0)
1191 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001192
1193 /* Realing input buffer if tailers wrap. For now
1194 * this is a workaroung. Because trailers are
1195 * not split on CRLF, like headers, there is no
1196 * way to know where to split it when trailers
1197 * wrap. This is a limitation of
1198 * h1_measure_trailers.
1199 */
1200 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1201 b_slow_realign(buf, trash.area, 0);
1202
1203 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1204 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001205 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001206 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001207 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001208 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001209
Christopher Faulet17276482018-11-22 11:44:35 +01001210 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001211 if (!htx_add_endof(htx, HTX_BLK_EOM))
1212 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001213 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001214 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001215 }
1216 }
1217 else {
1218 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1219 * is no body. Switch the message in DONE state
1220 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001221 if (!htx_add_endof(htx, HTX_BLK_EOM))
1222 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001223 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001224 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001225 }
1226 }
1227 else {
1228 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001229 ret = max;
1230 if (ret > data_space)
1231 ret = data_space;
1232 if (ret > b_contig_data(buf, *ofs))
1233 ret = b_contig_data(buf, *ofs);
1234 if (ret) {
1235 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1236 goto end;
1237
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001238 *ofs += ret;
1239 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001241 }
1242
1243 end:
1244 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001245 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001246 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001247 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001248 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001249 return 0;
1250 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001251 /* update htx->extra, only when the body length is known */
1252 if (h1m->flags & H1_MF_XFER_LEN)
1253 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001254 return total;
1255}
1256
1257/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001258 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001259 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1260 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001261 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001262static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001263{
Christopher Faulet539e0292018-11-19 10:40:09 +01001264 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001265 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001266 struct htx *htx;
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001267 size_t data = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001268 size_t total = 0;
1269 size_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001270 size_t count, rsv;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001271 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001272
Christopher Faulet539e0292018-11-19 10:40:09 +01001273 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001274
Christopher Fauletf2824e62018-10-01 12:12:37 +02001275 if (!conn_is_back(h1c->conn)) {
1276 h1m = &h1s->req;
1277 errflag = H1S_F_REQ_ERROR;
1278 }
1279 else {
1280 h1m = &h1s->res;
1281 errflag = H1S_F_RES_ERROR;
1282 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001283
Christopher Faulet74027762019-02-26 14:45:05 +01001284 data = htx->data;
1285 count = b_data(&h1c->ibuf);
1286 if (!count)
1287 goto end;
1288 rsv = ((flags & CO_RFL_KEEP_RSV) ? global.tune.maxrewrite : 0);
1289
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001290 if (htx_is_empty(htx))
1291 h1_handle_1xx_response(h1s, h1m);
1292
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001293 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001294 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001295 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001296 if (!ret)
1297 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001298 }
1299 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Fauletafe57842019-01-21 11:55:02 +01001300 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001301 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001302 if (!ret)
1303 break;
1304 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001305 else if (h1m->state == H1_MSG_DONE) {
1306 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001307 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001308 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001309 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Fauletafe57842019-01-21 11:55:02 +01001310 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001311 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001312 if (!ret)
1313 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001314 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001315 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001316 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001317 break;
1318 }
1319
Christopher Faulet539e0292018-11-19 10:40:09 +01001320 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001321 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001322
Christopher Faulet47365272018-10-31 17:40:50 +01001323 if (h1s->flags & errflag)
1324 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001325
Christopher Faulet539e0292018-11-19 10:40:09 +01001326 b_del(&h1c->ibuf, total);
1327
1328 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001329 htx_to_buf(htx, buf);
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001330 data = (htx->data - data);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001331 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001332 h1c->flags &= ~H1C_F_IN_FULL;
1333 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001334 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001335
Christopher Fauletcf56b992018-12-11 16:12:31 +01001336 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1337
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001338 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001339 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletcf56b992018-12-11 16:12:31 +01001340 else if (!htx_is_empty(htx))
1341 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001342
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001343 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1344 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001345 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001346 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001347 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001348
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001349 return data;
Christopher Faulet47365272018-10-31 17:40:50 +01001350
1351 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001352 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001353 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001354 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001355 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001356 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001357}
1358
Christopher Faulet129817b2018-09-20 16:14:40 +02001359/*
1360 * Process outgoing data. It parses data and transfer them from the channel buffer into
1361 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1362 * 0 if it couldn't proceed.
1363 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001364static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1365{
Christopher Faulet129817b2018-09-20 16:14:40 +02001366 struct h1s *h1s = h1c->h1s;
1367 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001368 struct htx *chn_htx;
1369 struct htx_blk *blk;
1370 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001371 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001372 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001373 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001374
Christopher Faulet47365272018-10-31 17:40:50 +01001375 if (!count)
1376 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001377
Christopher Faulet9768c262018-10-22 09:34:31 +02001378 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001379 if (htx_is_empty(chn_htx))
1380 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001381
Christopher Faulet51dbc942018-09-13 09:05:15 +02001382 if (!h1_get_buf(h1c, &h1c->obuf)) {
1383 h1c->flags |= H1C_F_OUT_ALLOC;
1384 goto end;
1385 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001386
Christopher Fauletf2824e62018-10-01 12:12:37 +02001387 if (!conn_is_back(h1c->conn)) {
1388 h1m = &h1s->res;
1389 errflag = H1S_F_RES_ERROR;
1390 }
1391 else {
1392 h1m = &h1s->req;
1393 errflag = H1S_F_REQ_ERROR;
1394 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001395
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001396 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001397 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001398
1399 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001400
Willy Tarreau3815b222018-12-11 19:50:43 +01001401 /* Perform some optimizations to reduce the number of buffer copies.
1402 * First, if the mux's buffer is empty and the htx area contains
1403 * exactly one data block of the same size as the requested count,
1404 * then it's possible to simply swap the caller's buffer with the
1405 * mux's output buffer and adjust offsets and length to match the
1406 * entire DATA HTX block in the middle. In this case we perform a
1407 * true zero-copy operation from end-to-end. This is the situation
1408 * that happens all the time with large files. Second, if this is not
1409 * possible, but the mux's output buffer is empty, we still have an
1410 * opportunity to avoid the copy to the intermediary buffer, by making
1411 * the intermediary buffer's area point to the output buffer's area.
1412 * In this case we want to skip the HTX header to make sure that copies
1413 * remain aligned and that this operation remains possible all the
1414 * time. This goes for headers, data blocks and any data extracted from
1415 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001416 */
1417 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001418 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001419
1420 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001421 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001422 htx_get_blk_value(chn_htx, blk).len == count) {
1423 void *old_area = h1c->obuf.area;
1424
1425 h1c->obuf.area = buf->area;
1426 h1c->obuf.data = count;
1427
1428 buf->area = old_area;
1429 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001430
1431 /* The message is chunked. We need to emit the chunk
1432 * size. We have at least the size of the struct htx to
1433 * write the chunk envelope. It should be enough.
1434 */
1435 if (h1m->flags & H1_MF_CHNK) {
1436 h1_emit_chunk_size(&h1c->obuf, count);
1437 h1_emit_chunk_crlf(&h1c->obuf);
1438 }
1439
Willy Tarreau3815b222018-12-11 19:50:43 +01001440 total += count;
1441 goto out;
1442 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001443 tmp->area = h1c->obuf.area + h1c->obuf.head;
1444 }
1445
Christopher Faulet9768c262018-10-22 09:34:31 +02001446 tmp->size = b_room(&h1c->obuf);
1447
Christopher Fauletb2e84162018-12-06 11:39:49 +01001448 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001449 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001450 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001451 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001452 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001453 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001454
Christopher Fauletb2e84162018-12-06 11:39:49 +01001455 vlen = sz;
1456 if (vlen > count) {
1457 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1458 goto copy;
1459 vlen = count;
1460 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001461
Christopher Fauletb2e84162018-12-06 11:39:49 +01001462 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001463 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001464 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001465
1466 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001467 h1m_init_req(h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001468 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +02001469 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001470 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001471 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001472 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001473 goto copy;
1474 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001475 if (sl->flags & HTX_SL_F_BODYLESS)
1476 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001477 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001478 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001479
Christopher Faulet9768c262018-10-22 09:34:31 +02001480 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001481 h1m_init_res(h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001482 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +02001483 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001484 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001485 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001486 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001487 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001488 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001489 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001490 if (sl->info.res.status < 200 &&
1491 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1492 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001493 h1m->state = H1_MSG_HDR_FIRST;
1494 break;
1495
1496 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001497 h1m->state = H1_MSG_HDR_NAME;
1498 n = htx_get_blk_name(chn_htx, blk);
1499 v = htx_get_blk_value(chn_htx, blk);
1500
1501 if (isteqi(n, ist("transfer-encoding")))
1502 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001503 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001504 /* Only skip C-L header with invalid value. */
1505 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001506 goto skip_hdr;
1507 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001508 else if (isteqi(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001509 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001510 if (!v.len)
1511 goto skip_hdr;
1512 }
1513
Christopher Fauletc59ff232018-12-03 13:58:44 +01001514 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001515 goto copy;
1516 skip_hdr:
1517 h1m->state = H1_MSG_HDR_L2_LWS;
1518 break;
1519
1520 case HTX_BLK_PHDR:
1521 /* not implemented yet */
1522 h1m->flags |= errflag;
1523 break;
1524
1525 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001526 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001527 /* There is no "Connection:" header and
1528 * it the conn_mode must be
1529 * processed. So do it */
1530 n = ist("Connection");
1531 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001532 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001533 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001534 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001535 goto copy;
1536 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001537 process_conn_mode = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001538 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001539
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001540 if ((h1s->meth != HTTP_METH_CONNECT &&
1541 (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 +01001542 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1543 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1544 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1545 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1546 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001547 /* chunking needed but header not seen */
1548 if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28))
1549 goto copy;
1550 h1m->flags |= H1_MF_CHNK;
1551 }
1552
Christopher Faulet9768c262018-10-22 09:34:31 +02001553 h1m->state = H1_MSG_LAST_LF;
1554 if (!chunk_memcat(tmp, "\r\n", 2))
1555 goto copy;
1556
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001557 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1558 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1559 h1_set_req_tunnel_mode(h1s);
1560 }
1561 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1562 /* a successfull reply to a CONNECT or a protocol switching is sent
1563 * to the client . Switch the response to tunnel mode. */
1564 h1_set_res_tunnel_mode(h1s);
1565 }
1566 else
1567 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001568 break;
1569
1570 case HTX_BLK_DATA:
1571 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001572 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001573 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001574 goto copy;
1575 break;
1576
1577 case HTX_BLK_EOD:
1578 if (!chunk_memcat(tmp, "0\r\n", 3))
1579 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001580 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001581 h1m->state = H1_MSG_TRAILERS;
1582 break;
1583
1584 case HTX_BLK_TLR:
Willy Tarreau34d23482019-01-03 17:46:56 +01001585 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001586 if (!chunk_memcat(tmp, "0\r\n", 3))
1587 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001588 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001589 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001590 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001591 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001592 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001593 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001594 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001595 break;
1596
1597 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001598 if ((h1m->flags & H1_MF_CHNK)) {
Willy Tarreau34d23482019-01-03 17:46:56 +01001599 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001600 if (!chunk_memcat(tmp, "0\r\n", 3))
1601 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001602 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001603 }
Willy Tarreau34d23482019-01-03 17:46:56 +01001604 if (!(h1s->flags & H1S_F_HAVE_O_TLR)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001605 if (!chunk_memcat(tmp, "\r\n", 2))
1606 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001607 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet32188212018-11-20 18:21:43 +01001608 }
1609 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001610 h1m->state = H1_MSG_DONE;
1611 break;
1612
1613 case HTX_BLK_OOB:
1614 v = htx_get_blk_value(chn_htx, blk);
1615 if (!chunk_memcat(tmp, v.ptr, v.len))
1616 goto copy;
1617 break;
1618
1619 default:
1620 h1m->flags |= errflag;
1621 break;
1622 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001623 total += vlen;
1624 count -= vlen;
1625 if (sz == vlen)
1626 blk = htx_remove_blk(chn_htx, blk);
1627 else {
1628 htx_cut_data_blk(chn_htx, blk, vlen);
1629 break;
1630 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001631 }
1632
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001633 if (htx_is_empty(chn_htx))
1634 h1_handle_1xx_response(h1s, h1m);
1635
Christopher Faulet9768c262018-10-22 09:34:31 +02001636 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001637 /* when the output buffer is empty, tmp shares the same area so that we
1638 * only have to update pointers and lengths.
1639 */
Willy Tarreaub57af612019-01-23 20:43:53 +01001640 if (tmp->area == h1c->obuf.area + h1c->obuf.head)
Willy Tarreauc5efa332018-12-05 11:19:27 +01001641 h1c->obuf.data = tmp->data;
1642 else
1643 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001644
Willy Tarreau3815b222018-12-11 19:50:43 +01001645 htx_to_buf(chn_htx, buf);
1646 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001647 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001648 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 end:
1650 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001651}
1652
Christopher Faulet51dbc942018-09-13 09:05:15 +02001653/*********************************************************/
1654/* functions below are I/O callbacks from the connection */
1655/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001656static void h1_wake_stream_for_recv(struct h1s *h1s)
1657{
1658 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001659 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001660 tasklet_wakeup(h1s->recv_wait->task);
1661 h1s->recv_wait = NULL;
1662 }
1663}
1664static void h1_wake_stream_for_send(struct h1s *h1s)
1665{
1666 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001667 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001668 tasklet_wakeup(h1s->send_wait->task);
1669 h1s->send_wait = NULL;
1670 }
1671}
1672
Christopher Faulet51dbc942018-09-13 09:05:15 +02001673/*
1674 * Attempt to read data, and subscribe if none available
1675 */
1676static int h1_recv(struct h1c *h1c)
1677{
1678 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001679 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001680 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001681 int rcvd = 0;
1682
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001683 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001684 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001685
Olivier Houchard75159a92018-12-03 18:46:09 +01001686 if (!h1_recv_allowed(h1c)) {
1687 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001688 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001689 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001690
Christopher Fauletfeb11742018-11-29 15:12:34 +01001691 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001692 rcvd = 1;
1693 goto end;
1694 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001695
Christopher Faulet51dbc942018-09-13 09:05:15 +02001696 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1697 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001698 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001699 }
1700
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001701 /*
1702 * If we only have a small amount of data, realign it,
1703 * it's probably cheaper than doing 2 recv() calls.
1704 */
1705 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1706 b_slow_realign(&h1c->ibuf, trash.area, 0);
1707
Willy Tarreau45f2b892018-12-05 07:59:27 +01001708 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001709 if (max) {
1710 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001711
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001712 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001713 if (!b_data(&h1c->ibuf)) {
1714 /* try to pre-align the buffer like the rxbufs will be
1715 * to optimize memory copies.
1716 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001717 h1c->ibuf.head = sizeof(struct htx);
1718 }
Willy Tarreauc0960d12018-12-14 10:59:15 +01001719 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001720 }
Christopher Faulet47365272018-10-31 17:40:50 +01001721 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001722 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001723 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001724 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001725 if (h1s->csinfo.t_idle == -1)
1726 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1727 }
Christopher Faulet47365272018-10-31 17:40:50 +01001728 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001729
Christopher Fauletcf56b992018-12-11 16:12:31 +01001730 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001731 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001732 goto end;
1733 }
1734
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001735 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001736
Christopher Faulet9768c262018-10-22 09:34:31 +02001737 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001738 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1739 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001740
Christopher Faulete6b39942018-12-07 09:42:49 +01001741 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Christopher Faulet87a8f352019-03-22 14:51:36 +01001742 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001743 if (!b_data(&h1c->ibuf))
1744 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001745 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001746 h1c->flags |= H1C_F_IN_FULL;
1747 return rcvd;
1748}
1749
1750
1751/*
1752 * Try to send data if possible
1753 */
1754static int h1_send(struct h1c *h1c)
1755{
1756 struct connection *conn = h1c->conn;
1757 unsigned int flags = 0;
1758 size_t ret;
1759 int sent = 0;
1760
1761 if (conn->flags & CO_FL_ERROR)
1762 return 0;
1763
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001764 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001765 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1766 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001767 return 0;
1768 }
1769
Christopher Faulet51dbc942018-09-13 09:05:15 +02001770 if (!b_data(&h1c->obuf))
1771 goto end;
1772
1773 if (h1c->flags & H1C_F_OUT_FULL)
1774 flags |= CO_SFL_MSG_MORE;
1775
1776 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1777 if (ret > 0) {
1778 h1c->flags &= ~H1C_F_OUT_FULL;
1779 b_del(&h1c->obuf, ret);
1780 sent = 1;
1781 }
1782
Christopher Faulet145aa472018-12-06 10:56:20 +01001783 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1784 /* error or output closed, nothing to send, clear the buffer to release it */
1785 b_reset(&h1c->obuf);
1786 }
1787
Christopher Faulet51dbc942018-09-13 09:05:15 +02001788 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001789 if (!(h1c->flags & H1C_F_OUT_FULL))
1790 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001791
Christopher Faulet51dbc942018-09-13 09:05:15 +02001792 /* We're done, no more to send */
1793 if (!b_data(&h1c->obuf)) {
1794 h1_release_buf(h1c, &h1c->obuf);
1795 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001796 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001797 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001798 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1799 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001800
1801 return sent;
1802}
1803
Christopher Faulet51dbc942018-09-13 09:05:15 +02001804
1805/* callback called on any event by the connection handler.
1806 * It applies changes and returns zero, or < 0 if it wants immediate
1807 * destruction of the connection.
1808 */
1809static int h1_process(struct h1c * h1c)
1810{
1811 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001812 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001813
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001814 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001815 return -1;
1816
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001817 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001818 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1819 goto end;
1820 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001821 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001822 }
1823
Christopher Fauletfeb11742018-11-29 15:12:34 +01001824 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001825 if (h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001826 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001827 conn_xprt_read0_pending(conn))
1828 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001829 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001830 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001831 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001832 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001833 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001834 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001835 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001836 }
1837
Christopher Fauletfeb11742018-11-29 15:12:34 +01001838 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1839 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1840
Olivier Houchard75159a92018-12-03 18:46:09 +01001841 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1842 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001843 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001844 int flags = 0;
1845
1846 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1847 flags |= CS_FL_ERROR;
1848 if (conn_xprt_read0_pending(conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001849 flags |= CS_FL_REOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001850 h1s->cs->flags |= flags;
1851 h1s->cs->data_cb->wake(h1s->cs);
1852 }
Christopher Faulet47365272018-10-31 17:40:50 +01001853 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001854 if (h1c->task) {
1855 h1c->task->expire = TICK_ETERNITY;
1856 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001857 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 +01001858 ? h1c->shut_timeout
1859 : h1c->timeout));
1860 task_queue(h1c->task);
1861 }
1862 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001863 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001864
1865 release:
1866 h1_release(conn);
1867 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001868}
1869
1870static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1871{
1872 struct h1c *h1c = ctx;
1873 int ret = 0;
1874
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001875 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001876 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001877 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001878 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001879 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001880 h1_process(h1c);
1881 return NULL;
1882}
1883
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001884static void h1_reset(struct connection *conn)
1885{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001886 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001887
1888 /* Reset the flags, and let the mux know we're waiting for a connection */
1889 h1c->flags = H1C_F_CS_WAIT_CONN;
1890}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001891
1892static int h1_wake(struct connection *conn)
1893{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001894 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001895 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001896
Christopher Faulet539e0292018-11-19 10:40:09 +01001897 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001898 ret = h1_process(h1c);
1899 if (ret == 0) {
1900 struct h1s *h1s = h1c->h1s;
1901
1902 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1903 ret = h1s->cs->data_cb->wake(h1s->cs);
1904 }
1905 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001906}
1907
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001908/* Connection timeout management. The principle is that if there's no receipt
1909 * nor sending for a certain amount of time, the connection is closed.
1910 */
1911static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
1912{
1913 struct h1c *h1c = context;
1914 int expired = tick_is_expired(t->expire, now_ms);
1915
1916 if (!expired && h1c)
1917 return t;
1918
1919 task_delete(t);
1920 task_free(t);
1921
1922 if (!h1c) {
1923 /* resources were already deleted */
1924 return NULL;
1925 }
1926
1927 h1c->task = NULL;
1928 /* If a stream is still attached to the mux, just set an error and wait
1929 * for the stream's timeout. Otherwise, release the mux. This is only ok
1930 * because same timeouts are used.
1931 */
1932 if (h1c->h1s && h1c->h1s->cs)
1933 h1c->flags |= H1C_F_CS_ERROR;
1934 else
1935 h1_release(h1c->conn);
1936 return NULL;
1937}
1938
Christopher Faulet51dbc942018-09-13 09:05:15 +02001939/*******************************************/
1940/* functions below are used by the streams */
1941/*******************************************/
1942/*
1943 * Attach a new stream to a connection
1944 * (Used for outgoing connections)
1945 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001946static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001947{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001948 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001949 struct conn_stream *cs = NULL;
1950 struct h1s *h1s;
1951
1952 if (h1c->flags & H1C_F_CS_ERROR)
1953 goto end;
1954
1955 cs = cs_new(h1c->conn);
1956 if (!cs)
1957 goto end;
1958
Olivier Houchardf502aca2018-12-14 19:42:40 +01001959 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001960 if (h1s == NULL)
1961 goto end;
1962
1963 return cs;
1964 end:
1965 cs_free(cs);
1966 return NULL;
1967}
1968
1969/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1970 * this mux, it's easy as we can only store a single conn_stream.
1971 */
1972static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1973{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001974 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001975 struct h1s *h1s = h1c->h1s;
1976
1977 if (h1s)
1978 return h1s->cs;
1979
1980 return NULL;
1981}
1982
1983static void h1_destroy(struct connection *conn)
1984{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001985 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986
1987 if (!h1c->h1s)
1988 h1_release(conn);
1989}
1990
1991/*
1992 * Detach the stream from the connection and possibly release the connection.
1993 */
1994static void h1_detach(struct conn_stream *cs)
1995{
1996 struct h1s *h1s = cs->ctx;
1997 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001998 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01001999 int has_keepalive;
2000 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002001
2002 cs->ctx = NULL;
2003 if (!h1s)
2004 return;
2005
Olivier Houchardf502aca2018-12-14 19:42:40 +01002006 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002007 h1c = h1s->h1c;
2008 h1s->cs = NULL;
2009
Olivier Houchard8a786902018-12-15 16:05:40 +01002010 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2011 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2012 h1s_destroy(h1s);
2013
2014 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002015 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002016 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002017 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002018 h1c->conn->flags |= CO_FL_PRIVATE;
2019
Olivier Houchard44d59142018-12-13 18:46:22 +01002020 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002021 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002022 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2023 h1c->conn->owner = NULL;
2024 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2025 /* The server doesn't want it, let's kill the connection right away */
2026 h1c->conn->mux->destroy(h1c->conn);
2027 else
2028 tasklet_wakeup(h1c->wait_event.task);
2029 return;
2030
2031 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002032 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002033 if (h1c->conn->owner == sess) {
2034 int ret = session_check_idle_conn(sess, h1c->conn);
2035 if (ret == -1)
2036 /* The connection got destroyed, let's leave */
2037 return;
2038 else if (ret == 1) {
2039 /* The connection was added to the server list,
2040 * wake the task so we can subscribe to events
2041 */
2042 tasklet_wakeup(h1c->wait_event.task);
2043 return;
2044 }
2045 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002046 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002047 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002048 struct server *srv = objt_server(h1c->conn->target);
2049
2050 if (srv) {
2051 if (h1c->conn->flags & CO_FL_PRIVATE)
2052 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002053 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002054 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2055 else
2056 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2057 }
2058 }
2059 }
2060
Christopher Faulet51dbc942018-09-13 09:05:15 +02002061 /* We don't want to close right now unless the connection is in error */
Christopher Faulet666a0c42019-01-08 11:12:04 +01002062 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01002063 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002064 h1_release(h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002065 else {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002066 tasklet_wakeup(h1c->wait_event.task);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002067 if (h1c->task) {
2068 h1c->task->expire = TICK_ETERNITY;
2069 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002070 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 +01002071 ? h1c->shut_timeout
2072 : h1c->timeout));
2073 task_queue(h1c->task);
2074 }
2075 }
2076 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002077}
2078
2079
2080static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2081{
2082 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002083 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002084
2085 if (!h1s)
2086 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002087 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002088
Christopher Faulet7f366362019-04-08 10:51:20 +02002089 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2090 goto do_shutr;
2091
2092 if (h1s->flags & H1S_F_WANT_KAL)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002093 return;
2094
Christopher Faulet7f366362019-04-08 10:51:20 +02002095 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002096 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2097 if (cs->flags & CS_FL_SHR)
2098 return;
2099 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
2100 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002101 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 +02002102 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002103}
2104
2105static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2106{
2107 struct h1s *h1s = cs->ctx;
2108 struct h1c *h1c;
2109
2110 if (!h1s)
2111 return;
2112 h1c = h1s->h1c;
2113
Christopher Faulet7f366362019-04-08 10:51:20 +02002114 if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
2115 goto do_shutw;
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002116
Christopher Faulet7f366362019-04-08 10:51:20 +02002117 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 +02002118 return;
2119
Christopher Faulet7f366362019-04-08 10:51:20 +02002120 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002121 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2122 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2123 return;
2124
Christopher Faulet666a0c42019-01-08 11:12:04 +01002125 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002126}
2127
Christopher Faulet666a0c42019-01-08 11:12:04 +01002128static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002129{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002130 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002131
Christopher Faulet666a0c42019-01-08 11:12:04 +01002132 conn_xprt_shutw(conn);
2133 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2134 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2135 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002136}
2137
2138/* Called from the upper layer, to unsubscribe to events */
2139static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2140{
2141 struct wait_event *sw;
2142 struct h1s *h1s = cs->ctx;
2143
2144 if (!h1s)
2145 return 0;
2146
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002147 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002148 sw = param;
2149 if (h1s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002150 sw->events &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002151 h1s->recv_wait = NULL;
2152 }
2153 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002154 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002155 sw = param;
2156 if (h1s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002157 sw->events &= ~SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002158 h1s->send_wait = NULL;
2159 }
2160 }
2161 return 0;
2162}
2163
2164/* Called from the upper layer, to subscribe to events, such as being able to send */
2165static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2166{
2167 struct wait_event *sw;
2168 struct h1s *h1s = cs->ctx;
2169
2170 if (!h1s)
2171 return -1;
2172
2173 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002174 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002175 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002176 if (!(sw->events & SUB_RETRY_RECV)) {
2177 sw->events |= SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002178 sw->handle = h1s;
2179 h1s->recv_wait = sw;
2180 }
2181 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002182 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002183 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002184 if (!(sw->events & SUB_RETRY_SEND)) {
2185 sw->events |= SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002186 sw->handle = h1s;
2187 h1s->send_wait = sw;
2188 }
2189 return 0;
2190 default:
2191 break;
2192 }
2193 return -1;
2194}
2195
2196/* Called from the upper layer, to receive data */
2197static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2198{
2199 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002200 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002201 size_t ret = 0;
2202
Christopher Faulet539e0292018-11-19 10:40:09 +01002203 if (!(h1c->flags & H1C_F_IN_ALLOC))
2204 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002205
2206 if (flags & CO_RFL_BUF_FLUSH)
2207 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002208 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2209 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002210 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002211 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002212 }
2213 return ret;
2214}
2215
2216
2217/* Called from the upper layer, to send data */
2218static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2219{
2220 struct h1s *h1s = cs->ctx;
2221 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002222 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002223
2224 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002225 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002226
2227 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002228 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2229 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002230
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002231 while (total != count) {
2232 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002233
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002234 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2235 ret = h1_process_output(h1c, buf, count);
2236 if (!ret)
2237 break;
2238 total += ret;
2239 if (!h1_send(h1c))
2240 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002241 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002242
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002243 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002244}
2245
Christopher Faulet1be55f92018-10-02 15:59:23 +02002246#if defined(CONFIG_HAP_LINUX_SPLICE)
2247/* Send and get, using splicing */
2248static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2249{
2250 struct h1s *h1s = cs->ctx;
2251 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2252 int ret = 0;
2253
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002254 if (b_data(&h1s->h1c->ibuf)) {
2255 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002256 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002257 }
2258
2259 h1s->flags &= ~H1S_F_BUF_FLUSH;
2260 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002261 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2262 count = h1m->curr_len;
2263 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2264 if (h1m->state == H1_MSG_DATA && ret > 0)
2265 h1m->curr_len -= ret;
2266 end:
2267 return ret;
2268
2269}
2270
2271static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2272{
2273 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002274 int ret = 0;
2275
2276 if (b_data(&h1s->h1c->obuf))
2277 goto end;
2278
2279 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002280 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002281 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002282 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
2283 cs->conn->xprt->subscribe(cs->conn, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002284 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002285 return ret;
2286}
2287#endif
2288
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002289/* for debugging with CLI's "show fd" command */
2290static void h1_show_fd(struct buffer *msg, struct connection *conn)
2291{
2292 struct h1c *h1c = conn->ctx;
2293 struct h1s *h1s = h1c->h1s;
2294
Christopher Fauletf376a312019-01-04 15:16:06 +01002295 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2296 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002297 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2298 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2299 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2300 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2301
2302 if (h1s) {
2303 char *method;
2304
2305 if (h1s->meth < HTTP_METH_OTHER)
2306 method = http_known_methods[h1s->meth].ptr;
2307 else
2308 method = "UNKNOWN";
2309 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2310 " .meth=%s status=%d",
2311 h1s, h1s->flags,
2312 h1m_state_str(h1s->req.state),
2313 h1m_state_str(h1s->res.state), method, h1s->status);
2314 if (h1s->cs)
2315 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2316 h1s->cs->flags, h1s->cs->data);
2317 }
2318}
2319
Christopher Faulet51dbc942018-09-13 09:05:15 +02002320/****************************************/
2321/* MUX initialization and instanciation */
2322/****************************************/
2323
2324/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002325static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002326 .init = h1_init,
2327 .wake = h1_wake,
2328 .attach = h1_attach,
2329 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002330 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002331 .detach = h1_detach,
2332 .destroy = h1_destroy,
2333 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002334 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002335 .rcv_buf = h1_rcv_buf,
2336 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002337#if defined(CONFIG_HAP_LINUX_SPLICE)
2338 .rcv_pipe = h1_rcv_pipe,
2339 .snd_pipe = h1_snd_pipe,
2340#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002341 .subscribe = h1_subscribe,
2342 .unsubscribe = h1_unsubscribe,
2343 .shutr = h1_shutr,
2344 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002345 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002346 .reset = h1_reset,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02002347 .flags = MX_FL_HTX,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002348 .name = "h1",
2349};
2350
2351
2352/* this mux registers default HTX proto */
2353static struct mux_proto_list mux_proto_htx =
2354{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2355
Willy Tarreau0108d902018-11-25 19:14:37 +01002356INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2357
Christopher Faulet51dbc942018-09-13 09:05:15 +02002358/*
2359 * Local variables:
2360 * c-indent-level: 8
2361 * c-basic-offset: 8
2362 * End:
2363 */