blob: 32004c68c4f604bcf3ef5913bd5b226db8c4f2de [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 */
47#define H1C_F_CS_SHUTW 0x00004000 /* connection is already shut down */
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 */
Christopher Faulet32188212018-11-20 18:21:43 +010067#define H1S_F_HAVE_EOD 0x00000400 /* Set during input/output process to know the last empty chunk was processed */
68#define H1S_F_HAVE_TLR 0x00000800 /* Set during input/output process to know the trailers were processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020069
70/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020071struct h1c {
72 struct connection *conn;
73 struct proxy *px;
74 uint32_t flags; /* Connection flags: H1C_F_* */
75
76 struct buffer ibuf; /* Input buffer to store data before parsing */
77 struct buffer obuf; /* Output buffer to store data after reformatting */
78
79 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
80 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
81
82 struct h1s *h1s; /* H1 stream descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020083};
84
85/* H1 stream descriptor */
86struct h1s {
87 struct h1c *h1c;
88 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010089 struct cs_info csinfo; /* CS info, only used for client connections */
90 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020091
Christopher Faulet51dbc942018-09-13 09:05:15 +020092 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
93 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020094
95 struct h1m req;
96 struct h1m res;
97
98 enum http_meth_t meth; /* HTTP resquest method */
99 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200100};
101
102/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100103DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
104DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200105
Christopher Faulet51dbc942018-09-13 09:05:15 +0200106static int h1_recv(struct h1c *h1c);
107static int h1_send(struct h1c *h1c);
108static int h1_process(struct h1c *h1c);
109static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
110static void h1_shutw_conn(struct connection *conn);
111
112/*****************************************************/
113/* functions below are for dynamic buffer management */
114/*****************************************************/
115/*
116 * Indicates whether or not the we may call the h1_recv() function to
117 * attempt to receive data into the buffer and/or parse pending data. The
118 * condition is a bit complex due to some API limits for now. The rules are the
119 * following :
120 * - if an error or a shutdown was detected on the connection and the buffer
121 * is empty, we must not attempt to receive
122 * - if the input buffer failed to be allocated, we must not try to receive
123 * and we know there is nothing pending
124 * - if no flag indicates a blocking condition, we may attempt to receive,
125 * regardless of whether the input buffer is full or not, so that only de
126 * receiving part decides whether or not to block. This is needed because
127 * the connection API indeed prevents us from re-enabling receipt that is
128 * already enabled in a polled state, so we must always immediately stop as
129 * soon as the mux can't proceed so as never to hit an end of read with data
130 * pending in the buffers.
131 * - otherwise must may not attempt to receive
132 */
133static inline int h1_recv_allowed(const struct h1c *h1c)
134{
Christopher Fauletcf56b992018-12-11 16:12:31 +0100135 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200136 return 0;
137
Christopher Fauletcf56b992018-12-11 16:12:31 +0100138 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
139 return 0;
140
Christopher Fauletcb55f482018-12-10 11:56:47 +0100141 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200142 return 1;
143
144 return 0;
145}
146
147/*
148 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
149 * flags are used to figure what buffer was requested. It returns 1 if the
150 * allocation succeeds, in which case the connection is woken up, or 0 if it's
151 * impossible to wake up and we prefer to be woken up later.
152 */
153static int h1_buf_available(void *target)
154{
155 struct h1c *h1c = target;
156
157 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
158 h1c->flags &= ~H1C_F_IN_ALLOC;
159 if (h1_recv_allowed(h1c))
160 tasklet_wakeup(h1c->wait_event.task);
161 return 1;
162 }
163
164 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
165 h1c->flags &= ~H1C_F_OUT_ALLOC;
166 tasklet_wakeup(h1c->wait_event.task);
167 return 1;
168 }
169
Christopher Faulet51dbc942018-09-13 09:05:15 +0200170 return 0;
171}
172
173/*
174 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
175 */
176static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
177{
178 struct buffer *buf = NULL;
179
180 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
181 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
182 h1c->buf_wait.target = h1c;
183 h1c->buf_wait.wakeup_cb = h1_buf_available;
184 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
185 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
186 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
187 __conn_xprt_stop_recv(h1c->conn);
188 }
189 return buf;
190}
191
192/*
193 * Release a buffer, if any, and try to wake up entities waiting in the buffer
194 * wait queue.
195 */
196static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
197{
198 if (bptr->size) {
199 b_free(bptr);
200 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
201 }
202}
203
204static int h1_avail_streams(struct connection *conn)
205{
206 struct h1c *h1c = conn->mux_ctx;
207
208 return h1c->h1s ? 0 : 1;
209}
210
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100211static int h1_max_streams(struct connection *conn)
212{
213 return 1;
214}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200215
216/*****************************************************************/
217/* functions below are dedicated to the mux setup and management */
218/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100219static struct conn_stream *h1s_new_cs(struct h1s *h1s)
220{
221 struct conn_stream *cs;
222
223 cs = cs_new(h1s->h1c->conn);
224 if (!cs)
225 goto err;
226 h1s->cs = cs;
227 cs->ctx = h1s;
228
229 if (h1s->flags & H1S_F_NOT_FIRST)
230 cs->flags |= CS_FL_NOT_FIRST;
231
232 if (stream_create_from_cs(cs) < 0)
233 goto err;
234 return cs;
235
236 err:
237 cs_free(cs);
238 h1s->cs = NULL;
239 return NULL;
240}
241
Christopher Fauletf2824e62018-10-01 12:12:37 +0200242static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200243{
244 struct h1s *h1s;
245
246 h1s = pool_alloc(pool_head_h1s);
247 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100248 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200249
250 h1s->h1c = h1c;
251 h1c->h1s = h1s;
252
253 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200254 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200255
256 h1s->recv_wait = NULL;
257 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200258
259 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200260 h1s->req.flags |= H1_MF_NO_PHDR;
261
Christopher Faulet129817b2018-09-20 16:14:40 +0200262 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200263 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200264
265 h1s->status = 0;
266 h1s->meth = HTTP_METH_OTHER;
267
Christopher Faulet47365272018-10-31 17:40:50 +0100268 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
269 h1s->flags |= H1S_F_NOT_FIRST;
270 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
271
Christopher Faulet129817b2018-09-20 16:14:40 +0200272 if (!conn_is_back(h1c->conn)) {
273 if (h1c->px->options2 & PR_O2_REQBUG_OK)
274 h1s->req.err_pos = -1;
275 }
276 else {
277 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
278 h1s->res.err_pos = -1;
279 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200280
Christopher Faulet539e0292018-11-19 10:40:09 +0100281 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
282 * create a new one.
283 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200284 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100285 h1s->csinfo.create_date = date;
286 h1s->csinfo.tv_create = now;
287 h1s->csinfo.t_handshake = 0;
288 h1s->csinfo.t_idle = -1;
289
Christopher Fauletf2824e62018-10-01 12:12:37 +0200290 cs->ctx = h1s;
291 h1s->cs = cs;
292 }
Christopher Faulet47365272018-10-31 17:40:50 +0100293 else {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100294 struct session *sess = h1c->conn->owner;
295
296 h1s->csinfo.create_date = sess->accept_date;
297 h1s->csinfo.tv_create = sess->tv_accept;
298 h1s->csinfo.t_handshake = sess->t_handshake;
299 h1s->csinfo.t_idle = -1;
300
Christopher Faulet47365272018-10-31 17:40:50 +0100301 cs = h1s_new_cs(h1s);
302 if (!cs)
303 goto fail;
304 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200305 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100306
307 fail:
308 pool_free(pool_head_h1s, h1s);
309 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200310}
311
312static void h1s_destroy(struct h1s *h1s)
313{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200314 if (h1s) {
315 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200316
Christopher Fauletf2824e62018-10-01 12:12:37 +0200317 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200318
Christopher Fauletf2824e62018-10-01 12:12:37 +0200319 if (h1s->recv_wait != NULL)
320 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
321 if (h1s->send_wait != NULL)
322 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
323
Christopher Fauletcb55f482018-12-10 11:56:47 +0100324 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100325 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
326 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
327 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200328
Christopher Fauletf2824e62018-10-01 12:12:37 +0200329 cs_free(h1s->cs);
330 pool_free(pool_head_h1s, h1s);
331 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200332}
333
Christopher Fauletfeb11742018-11-29 15:12:34 +0100334static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
335{
336 struct h1s *h1s = cs->ctx;
337
338 if (h1s && !conn_is_back(cs->conn))
339 return &h1s->csinfo;
340 return NULL;
341}
342
Christopher Faulet51dbc942018-09-13 09:05:15 +0200343/*
344 * Initialize the mux once it's attached. It is expected that conn->mux_ctx
345 * points to the existing conn_stream (for outgoing connections) or NULL (for
346 * incoming ones). Returns < 0 on error.
347 */
348static int h1_init(struct connection *conn, struct proxy *proxy)
349{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200350 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200351
352 h1c = pool_alloc(pool_head_h1c);
353 if (!h1c)
354 goto fail_h1c;
355 h1c->conn = conn;
356 h1c->px = proxy;
357
358 h1c->flags = H1C_F_NONE;
359 h1c->ibuf = BUF_NULL;
360 h1c->obuf = BUF_NULL;
361 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200362
Christopher Faulet51dbc942018-09-13 09:05:15 +0200363 LIST_INIT(&h1c->buf_wait.list);
364 h1c->wait_event.task = tasklet_new();
365 if (!h1c->wait_event.task)
366 goto fail;
367 h1c->wait_event.task->process = h1_io_cb;
368 h1c->wait_event.task->context = h1c;
369 h1c->wait_event.wait_reason = 0;
370
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200371 if (!(conn->flags & CO_FL_CONNECTED))
372 h1c->flags |= H1C_F_CS_WAIT_CONN;
373
Christopher Fauletf2824e62018-10-01 12:12:37 +0200374 /* Always Create a new H1S */
375 if (!h1s_create(h1c, conn->mux_ctx))
376 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200377
Christopher Faulet129817b2018-09-20 16:14:40 +0200378 conn->mux_ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200379
Christopher Faulet51dbc942018-09-13 09:05:15 +0200380 /* Try to read, if nothing is available yet we'll just subscribe */
381 if (h1_recv(h1c))
382 h1_process(h1c);
383
384 /* mux->wake will be called soon to complete the operation */
385 return 0;
386
387 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100388 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200389 tasklet_free(h1c->wait_event.task);
390 pool_free(pool_head_h1c, h1c);
391 fail_h1c:
392 return -1;
393}
394
395
396/* release function for a connection. This one should be called to free all
397 * resources allocated to the mux.
398 */
399static void h1_release(struct connection *conn)
400{
401 struct h1c *h1c = conn->mux_ctx;
402
403 LIST_DEL(&conn->list);
404
405 if (h1c) {
406 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
407 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
408 LIST_DEL(&h1c->buf_wait.list);
409 LIST_INIT(&h1c->buf_wait.list);
410 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
411 }
412
413 h1_release_buf(h1c, &h1c->ibuf);
414 h1_release_buf(h1c, &h1c->obuf);
415
Christopher Faulet51dbc942018-09-13 09:05:15 +0200416 if (h1c->wait_event.task)
417 tasklet_free(h1c->wait_event.task);
418
Christopher Fauletf2824e62018-10-01 12:12:37 +0200419 h1s_destroy(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200420 if (h1c->wait_event.wait_reason != 0)
421 conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason,
422 &h1c->wait_event);
423 pool_free(pool_head_h1c, h1c);
424 }
425
426 conn->mux = NULL;
427 conn->mux_ctx = NULL;
428
429 conn_stop_tracking(conn);
430 conn_full_close(conn);
431 if (conn->destroy_cb)
432 conn->destroy_cb(conn);
433 conn_free(conn);
434}
435
436/******************************************************/
437/* functions below are for the H1 protocol processing */
438/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200439/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
440 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200441 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100442static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200443{
Christopher Faulet570d1612018-11-26 11:13:57 +0100444 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200445
Christopher Faulet570d1612018-11-26 11:13:57 +0100446 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200447 (*(p + 5) > '1' ||
448 (*(p + 5) == '1' && *(p + 7) >= '1')))
449 h1m->flags |= H1_MF_VER_11;
450}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200451
Christopher Faulet9768c262018-10-22 09:34:31 +0200452/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
453 * greater or equal to 1.1
454 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100455static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200456{
Christopher Faulet570d1612018-11-26 11:13:57 +0100457 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200458
Christopher Faulet570d1612018-11-26 11:13:57 +0100459 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200460 (*(p + 5) > '1' ||
461 (*(p + 5) == '1' && *(p + 7) >= '1')))
462 h1m->flags |= H1_MF_VER_11;
463}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200464
Christopher Faulet9768c262018-10-22 09:34:31 +0200465/*
466 * Check the validity of the request version. If the version is valid, it
467 * returns 1. Otherwise, it returns 0.
468 */
469static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
470{
471 struct h1c *h1c = h1s->h1c;
472
473 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
474 * exactly one digit "." one digit. This check may be disabled using
475 * option accept-invalid-http-request.
476 */
477 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
478 if (sl.rq.v.len != 8)
479 return 0;
480
481 if (*(sl.rq.v.ptr + 4) != '/' ||
482 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
483 *(sl.rq.v.ptr + 6) != '.' ||
484 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
485 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200486 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200487 else if (!sl.rq.v.len) {
488 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200489
Christopher Faulet9768c262018-10-22 09:34:31 +0200490 /* RFC 1945 allows only GET for HTTP/0.9 requests */
491 if (sl.rq.meth != HTTP_METH_GET)
492 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200493
Christopher Faulet9768c262018-10-22 09:34:31 +0200494 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
495 if (!sl.rq.u.len)
496 return 0;
497
498 /* Add HTTP version */
499 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100500 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200501 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100502
503 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100504 ((*(sl.rq.v.ptr + 5) > '1') ||
505 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100506 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200507 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200508}
509
Christopher Faulet9768c262018-10-22 09:34:31 +0200510/*
511 * Check the validity of the response version. If the version is valid, it
512 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200513 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200514static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200515{
Christopher Faulet9768c262018-10-22 09:34:31 +0200516 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200517
Christopher Faulet9768c262018-10-22 09:34:31 +0200518 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
519 * exactly one digit "." one digit. This check may be disabled using
520 * option accept-invalid-http-request.
521 */
522 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
523 if (sl.st.v.len != 8)
524 return 0;
525
526 if (*(sl.st.v.ptr + 4) != '/' ||
527 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
528 *(sl.st.v.ptr + 6) != '.' ||
529 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
530 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200531 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100532
533 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100534 ((*(sl.st.v.ptr + 5) > '1') ||
535 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100536 h1m->flags |= H1_MF_VER_11;
537
Christopher Faulet9768c262018-10-22 09:34:31 +0200538 return 1;
539}
540/* Remove all "Connection:" headers from the HTX message <htx> */
541static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
542{
543 struct ist hdr = {.ptr = "Connection", .len = 10};
544 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200545
Christopher Faulet9768c262018-10-22 09:34:31 +0200546 while (http_find_header(htx, hdr, &ctx, 1))
547 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200548
Christopher Faulet9768c262018-10-22 09:34:31 +0200549 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
550}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200551
Christopher Faulet9768c262018-10-22 09:34:31 +0200552/* Add a "Connection:" header with the value <value> into the HTX message
553 * <htx>.
554 */
555static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
556{
557 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200558
Christopher Faulet9768c262018-10-22 09:34:31 +0200559 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200560}
561
562/* Deduce the connection mode of the client connection, depending on the
563 * configuration and the H1 message flags. This function is called twice, the
564 * first time when the request is parsed and the second time when the response
565 * is parsed.
566 */
567static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
568{
569 struct proxy *fe = h1s->h1c->px;
570 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
571
572 /* Tunnel mode can only by set on the frontend */
573 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
574 flag = H1S_F_WANT_TUN;
575 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
576 flag = H1S_F_WANT_CLO;
577
578 /* flags order: CLO > SCL > TUN > KAL */
579 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
580 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
581
582 if (h1m->flags & H1_MF_RESP) {
583 /* Either we've established an explicit tunnel, or we're
584 * switching the protocol. In both cases, we're very unlikely to
585 * understand the next protocols. We have to switch to tunnel
586 * mode, so that we transfer the request and responses then let
587 * this protocol pass unmodified. When we later implement
588 * specific parsers for such protocols, we'll want to check the
589 * Upgrade header which contains information about that protocol
590 * for responses with status 101 (eg: see RFC2817 about TLS).
591 */
592 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
593 h1s->status == 101)
594 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletbd44ca62018-11-29 09:55:22 +0100595 else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */
596 (h1m->flags & H1_MF_CONN_CLO && h1s->req.state != H1_MSG_DONE)) /*explicit close and unfinished request */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200597 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
598 }
599 else {
600 if (h1s->flags & H1S_F_WANT_KAL &&
601 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
602 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
603 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
604 }
605
606 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
607 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
608 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
609}
610
611/* Deduce the connection mode of the client connection, depending on the
612 * configuration and the H1 message flags. This function is called twice, the
613 * first time when the request is parsed and the second time when the response
614 * is parsed.
615 */
616static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
617{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100618 struct h1c *h1c = h1s->h1c;
619 struct session *sess = h1c->conn->owner;
620 struct proxy *fe = sess->fe;
621 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200622 int flag = H1S_F_WANT_KAL;
623
624 /* Tunnel mode can only by set on the frontend */
625 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
626 flag = H1S_F_WANT_TUN;
627
628 /* For the server connection: server-close == httpclose */
629 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
630 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
631 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
632 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
633 flag = H1S_F_WANT_CLO;
634
635 /* flags order: CLO > SCL > TUN > KAL */
636 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
637 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
638
639 if (h1m->flags & H1_MF_RESP) {
640 /* Either we've established an explicit tunnel, or we're
641 * switching the protocol. In both cases, we're very unlikely to
642 * understand the next protocols. We have to switch to tunnel
643 * mode, so that we transfer the request and responses then let
644 * this protocol pass unmodified. When we later implement
645 * specific parsers for such protocols, we'll want to check the
646 * Upgrade header which contains information about that protocol
647 * for responses with status 101 (eg: see RFC2817 about TLS).
648 */
649 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
650 h1s->status == 101)
651 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
652 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
653 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
654 else if (h1s->flags & H1S_F_WANT_KAL &&
655 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
656 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
657 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
658 }
Christopher Faulet70033782018-12-05 13:50:11 +0100659 else {
660 if (h1s->flags & H1S_F_WANT_KAL &&
661 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
662 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
663 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
664 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200665
666 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
667 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
668 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200669}
670
Christopher Faulet9768c262018-10-22 09:34:31 +0200671static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
672 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200673{
674 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200675
676 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
677 * token is found
678 */
679 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200680 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200681
682 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200683 if (h1m->flags & H1_MF_CONN_CLO) {
684 if (conn_val)
685 *conn_val = ist("");
686 if (htx)
687 h1_remove_conn_hdrs(h1m, htx);
688 }
689 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
690 if (conn_val)
691 *conn_val = ist("keep-alive");
692 if (htx)
693 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
694 }
Christopher Fauletce851492018-12-07 18:06:59 +0100695 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
696 if (conn_val)
697 *conn_val = ist("");
698 if (htx)
699 h1_remove_conn_hdrs(h1m, htx);
700 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200701 }
702 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200703 if (h1m->flags & H1_MF_CONN_KAL) {
704 if (conn_val)
705 *conn_val = ist("");
706 if (htx)
707 h1_remove_conn_hdrs(h1m, htx);
708 }
709 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
710 if (conn_val)
711 *conn_val = ist("close");
712 if (htx)
713 h1_add_conn_hdr(h1m, htx, ist("close"));
714 }
Christopher Fauletce851492018-12-07 18:06:59 +0100715 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
716 if (conn_val)
717 *conn_val = ist("");
718 if (htx)
719 h1_remove_conn_hdrs(h1m, htx);
720 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200721 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200722}
723
Christopher Faulet9768c262018-10-22 09:34:31 +0200724static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
725 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200726{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200727 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
728 * token is found
729 */
730 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200731 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200732
733 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200734 if (h1m->flags & H1_MF_CONN_CLO) {
735 if (conn_val)
736 *conn_val = ist("");
737 if (htx)
738 h1_remove_conn_hdrs(h1m, htx);
739 }
740 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
741 if (conn_val)
742 *conn_val = ist("keep-alive");
743 if (htx)
744 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
745 }
Christopher Fauletce851492018-12-07 18:06:59 +0100746 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
747 if (conn_val)
748 *conn_val = ist("");
749 if (htx)
750 h1_remove_conn_hdrs(h1m, htx);
751 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200752 }
753 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200754 if (h1m->flags & H1_MF_CONN_KAL) {
755 if (conn_val)
756 *conn_val = ist("");
757 if (htx)
758 h1_remove_conn_hdrs(h1m, htx);
759 }
760 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
761 if (conn_val)
762 *conn_val = ist("close");
763 if (htx)
764 h1_add_conn_hdr(h1m, htx, ist("close"));
765 }
Christopher Fauletce851492018-12-07 18:06:59 +0100766 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
767 if (conn_val)
768 *conn_val = ist("");
769 if (htx)
770 h1_remove_conn_hdrs(h1m, htx);
771 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200772 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200773}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200774
Christopher Faulet9768c262018-10-22 09:34:31 +0200775/* Set the right connection mode and update "Connection:" header if
776 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
777 * message is updated accordingly. When <conn_val> is not NULL, it is set with
778 * the new header value.
779 */
780static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
781 struct htx *htx, struct ist *conn_val)
782{
783 if (!conn_is_back(h1s->h1c->conn)) {
784 h1_set_cli_conn_mode(h1s, h1m);
785 if (h1m->flags & H1_MF_RESP)
786 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
787 }
788 else {
789 h1_set_srv_conn_mode(h1s, h1m);
790 if (!(h1m->flags & H1_MF_RESP))
791 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
792 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200793}
794
Christopher Faulete44769b2018-11-29 23:01:45 +0100795
796/* Append the description of what is present in error snapshot <es> into <out>.
797 * The description must be small enough to always fit in a buffer. The output
798 * buffer may be the trash so the trash must not be used inside this function.
799 */
800static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
801{
802 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100803 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
804 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
805 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
806 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
807 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
808 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100809}
810/*
811 * Capture a bad request or response and archive it in the proxy's structure.
812 * By default it tries to report the error position as h1m->err_pos. However if
813 * this one is not set, it will then report h1m->next, which is the last known
814 * parsing point. The function is able to deal with wrapping buffers. It always
815 * displays buffers as a contiguous area starting at buf->p. The direction is
816 * determined thanks to the h1m's flags.
817 */
818static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
819 struct h1m *h1m, struct buffer *buf)
820{
821 struct session *sess = h1c->conn->owner;
822 struct proxy *proxy = h1c->px;
823 struct proxy *other_end = sess->fe;
824 union error_snapshot_ctx ctx;
825
826 if (h1s->cs->data)
827 other_end = si_strm(h1s->cs->data)->be;
828
829 /* http-specific part now */
830 ctx.h1.state = h1m->state;
831 ctx.h1.c_flags = h1c->flags;
832 ctx.h1.s_flags = h1s->flags;
833 ctx.h1.m_flags = h1m->flags;
834 ctx.h1.m_clen = h1m->curr_len;
835 ctx.h1.m_blen = h1m->body_len;
836
837 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
838 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100839 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
840 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100841}
842
Christopher Fauletadb22202018-12-12 10:32:09 +0100843/* Emit the chunksize followed by a CRLF in front of data of the buffer
844 * <buf>. It goes backwards and starts with the byte before the buffer's
845 * head. The caller is responsible for ensuring there is enough room left before
846 * the buffer's head for the string.
847 */
848static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
849{
850 char *beg, *end;
851
852 beg = end = b_head(buf);
853 *--beg = '\n';
854 *--beg = '\r';
855 do {
856 *--beg = hextab[chksz & 0xF];
857 } while (chksz >>= 4);
858 buf->head -= (end - beg);
859 b_add(buf, end - beg);
860}
861
862/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
863 * ensuring there is enough room left in the buffer for the string. */
864static void h1_emit_chunk_crlf(struct buffer *buf)
865{
866 *(b_peek(buf, b_data(buf))) = '\r';
867 *(b_peek(buf, b_data(buf) + 1)) = '\n';
868 b_add(buf, 2);
869}
870
Christopher Faulet129817b2018-09-20 16:14:40 +0200871/*
872 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200873 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
874 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800875 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200876 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200877static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200878 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200879{
880 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100881 union h1_sl h1sl;
882 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200883 int ret = 0;
884
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100885 if (!max)
886 goto end;
887
Christopher Faulet129817b2018-09-20 16:14:40 +0200888 /* Realing input buffer if necessary */
889 if (b_head(buf) + b_data(buf) > b_wrap(buf))
890 b_slow_realign(buf, trash.area, 0);
891
Christopher Fauletf2824e62018-10-01 12:12:37 +0200892 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100893 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200894 if (ret <= 0) {
895 /* Incomplete or invalid message. If the buffer is full, it's an
896 * error because headers are too large to be handled by the
897 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200898 if (ret < 0 || (!ret && b_full(buf)))
899 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200900 goto end;
901 }
902
903 /* messages headers fully parsed, do some checks to prepare the body
904 * parsing.
905 */
906
907 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200908 if (ret > (b_size(buf) - global.tune.maxrewrite))
909 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200910
Christopher Faulet9768c262018-10-22 09:34:31 +0200911 /* Save the request's method or the response's status, check if the body
912 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200913 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100914 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200915
Christopher Faulet129817b2018-09-20 16:14:40 +0200916 /* Request have always a known length */
917 h1m->flags |= H1_MF_XFER_LEN;
918 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
919 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200920
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100921 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
922 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200923 h1m->err_state = h1m->state;
924 goto vsn_error;
925 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200926 }
927 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100928 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200929
930 if ((h1s->meth == HTTP_METH_HEAD) ||
931 (h1s->status >= 100 && h1s->status < 200) ||
932 (h1s->status == 204) || (h1s->status == 304) ||
933 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
934 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
935 h1m->flags |= H1_MF_XFER_LEN;
936 h1m->curr_len = h1m->body_len = 0;
937 h1m->state = H1_MSG_DONE;
938 }
939 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
940 h1m->flags |= H1_MF_XFER_LEN;
941 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
942 h1m->state = H1_MSG_DONE;
943 }
944 else
945 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200946
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100947 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
948 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200949 h1m->err_state = h1m->state;
950 goto vsn_error;
951 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200952 }
953
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100954 /* Set HTX start-line flags */
955 if (h1m->flags & H1_MF_VER_11)
956 flags |= HTX_SL_F_VER_11;
957 if (h1m->flags & H1_MF_XFER_ENC)
958 flags |= HTX_SL_F_XFER_ENC;
959 if (h1m->flags & H1_MF_XFER_LEN) {
960 flags |= HTX_SL_F_XFER_LEN;
961 if (h1m->flags & H1_MF_CHNK)
962 flags |= HTX_SL_F_CHNK;
963 else if (h1m->flags & H1_MF_CLEN)
964 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100965 if (h1m->state == H1_MSG_DONE)
966 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100967 }
968
Christopher Faulet9768c262018-10-22 09:34:31 +0200969 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100970 struct htx_sl *sl;
971
972 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
973 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200974 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100975 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200976 }
977 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100978 struct htx_sl *sl;
979
980 flags |= HTX_SL_F_IS_RESP;
981 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
982 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200983 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100984 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200985 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100986
Christopher Faulet9768c262018-10-22 09:34:31 +0200987 if (h1m->state == H1_MSG_DONE)
988 if (!htx_add_endof(htx, HTX_BLK_EOM))
989 goto error;
990
991 h1_process_conn_mode(h1s, h1m, htx, NULL);
992
993 /* If body length cannot be determined, set htx->extra to
994 * ULLONG_MAX. This value is impossible in other cases.
995 */
996 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
997
998 /* Recheck there is enough space to do headers rewritting */
999 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
1000 goto error;
1001
1002 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001003 end:
1004 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001005
1006 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001007 h1m->err_state = h1m->state;
1008 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001009 vsn_error:
1010 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +01001011 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001012 ret = 0;
1013 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001014}
1015
1016/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001017 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1018 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001019 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001020 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001021 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001022static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001023 struct buffer *buf, size_t *ofs, size_t max,
1024 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001025{
Christopher Faulet9768c262018-10-22 09:34:31 +02001026 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +02001027 size_t total = 0;
1028 int ret = 0;
1029
1030 if (h1m->flags & H1_MF_XFER_LEN) {
1031 if (h1m->flags & H1_MF_CLEN) {
1032 /* content-length: read only h2m->body_len */
1033 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001034 if (ret > data_space)
1035 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001036 if ((uint64_t)ret > h1m->curr_len)
1037 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001038 if (ret > b_contig_data(buf, *ofs))
1039 ret = b_contig_data(buf, *ofs);
1040 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001041 /* very often with large files we'll face the following
1042 * situation :
1043 * - htx is empty and points to <htxbuf>
1044 * - ret == buf->data
1045 * - buf->head == sizeof(struct htx)
1046 * => we can swap the buffers and place an htx header into
1047 * the target buffer instead
1048 */
1049 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1050 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1051 void *raw_area = buf->area;
1052 void *htx_area = htxbuf->area;
1053 struct htx_blk *blk;
1054
1055 buf->area = htx_area;
1056 htxbuf->area = raw_area;
1057 htx = (struct htx *)htxbuf->area;
1058 htx->size = htxbuf->size - sizeof(*htx);
1059 htx_reset(htx);
1060 b_set_data(htxbuf, b_size(htxbuf));
1061
1062 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1063 blk->info += ret;
1064 /* nothing else to do, the old buffer now contains an
1065 * empty pre-initialized HTX header
1066 */
1067 }
1068 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001069 goto end;
1070 h1m->curr_len -= ret;
1071 *ofs += ret;
1072 total += ret;
1073 }
1074
1075 if (!h1m->curr_len) {
1076 if (!htx_add_endof(htx, HTX_BLK_EOM))
1077 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001078 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +02001079 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001080 }
1081 else if (h1m->flags & H1_MF_CHNK) {
1082 new_chunk:
1083 /* te:chunked : parse chunks */
1084 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001085 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001086 if (ret <= 0)
1087 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001088 h1m->state = H1_MSG_CHUNK_SIZE;
1089
Christopher Faulet129817b2018-09-20 16:14:40 +02001090 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001091 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001092 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001093 }
1094
1095 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1096 unsigned int chksz;
1097
Christopher Fauletf2824e62018-10-01 12:12:37 +02001098 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001099 if (ret <= 0)
1100 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001101 if (!chksz) {
1102 if (!htx_add_endof(htx, HTX_BLK_EOD))
1103 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001104 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001105 h1m->state = H1_MSG_TRAILERS;
1106 }
1107 else
1108 h1m->state = H1_MSG_DATA;
1109
Christopher Faulet129817b2018-09-20 16:14:40 +02001110 h1m->curr_len = chksz;
1111 h1m->body_len += chksz;
1112 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001113 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001114 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001115 }
1116
1117 if (h1m->state == H1_MSG_DATA) {
1118 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001119 if (ret > data_space)
1120 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001121 if ((uint64_t)ret > h1m->curr_len)
1122 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001123 if (ret > b_contig_data(buf, *ofs))
1124 ret = b_contig_data(buf, *ofs);
1125 if (ret) {
1126 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1127 goto end;
1128 h1m->curr_len -= ret;
1129 max -= ret;
1130 *ofs += ret;
1131 total += ret;
1132 }
1133 if (!h1m->curr_len) {
1134 h1m->state = H1_MSG_CHUNK_CRLF;
1135 goto new_chunk;
1136 }
1137 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001138 }
1139
1140 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001141 /* Trailers were alread parsed, only the EOM
1142 * need to be added */
1143 if (h1s->flags & H1S_F_HAVE_TLR)
1144 goto skip_tlr_parsing;
1145
Christopher Fauletf2824e62018-10-01 12:12:37 +02001146 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001147 if (ret > data_space)
1148 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001149 if (ret <= 0)
1150 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001151
1152 /* Realing input buffer if tailers wrap. For now
1153 * this is a workaroung. Because trailers are
1154 * not split on CRLF, like headers, there is no
1155 * way to know where to split it when trailers
1156 * wrap. This is a limitation of
1157 * h1_measure_trailers.
1158 */
1159 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1160 b_slow_realign(buf, trash.area, 0);
1161
1162 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1163 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001164 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001165 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001166 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001167 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001168
Christopher Faulet17276482018-11-22 11:44:35 +01001169 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001170 if (!htx_add_endof(htx, HTX_BLK_EOM))
1171 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001172 h1m->state = H1_MSG_DONE;
1173 }
1174 }
1175 else {
1176 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1177 * is no body. Switch the message in DONE state
1178 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001179 if (!htx_add_endof(htx, HTX_BLK_EOM))
1180 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001181 h1m->state = H1_MSG_DONE;
1182 }
1183 }
1184 else {
1185 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001186 ret = max;
1187 if (ret > data_space)
1188 ret = data_space;
1189 if (ret > b_contig_data(buf, *ofs))
1190 ret = b_contig_data(buf, *ofs);
1191 if (ret) {
1192 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1193 goto end;
1194
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001195 *ofs += ret;
1196 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001197 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001198 }
1199
1200 end:
1201 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001202 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001203 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001204 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001205 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001206 return 0;
1207 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001208 /* update htx->extra, only when the body length is known */
1209 if (h1m->flags & H1_MF_XFER_LEN)
1210 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001211 return total;
1212}
1213
1214/*
1215 * Synchronize the request and the response before reseting them. Except for 1xx
1216 * responses, we wait that the request and the response are in DONE state and
1217 * that all data are forwarded for both. For 1xx responses, only the response is
1218 * reset, waiting the final one. Many 1xx messages can be sent.
1219 */
1220static void h1_sync_messages(struct h1c *h1c)
1221{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001222 struct h1s *h1s = h1c->h1s;
1223
1224 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001225 return;
1226
Christopher Fauletf2824e62018-10-01 12:12:37 +02001227 if (h1s->res.state == H1_MSG_DONE &&
1228 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001229 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001230 /* For 100-Continue response or any other informational 1xx
1231 * response which is non-final, don't reset the request, the
1232 * transaction is not finished. We take care the response was
1233 * transferred before.
1234 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001235 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001237 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet129817b2018-09-20 16:14:40 +02001238 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001239 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001240 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1241 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001242 h1m_init_req(&h1s->req);
1243 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001244 h1s->req.state = H1_MSG_TUNNEL;
1245 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001246 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001247 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001248 }
1249}
1250
1251/*
1252 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001253 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1254 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001255 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001256static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001257{
Christopher Faulet539e0292018-11-19 10:40:09 +01001258 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001259 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001261 size_t total = 0;
1262 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001263 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001264 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001265
Christopher Faulet539e0292018-11-19 10:40:09 +01001266 htx = htx_from_buf(buf);
1267 count = b_data(&h1c->ibuf);
1268 max = htx_free_space(htx);
1269 if (flags & CO_RFL_KEEP_RSV) {
1270 if (max < global.tune.maxrewrite)
1271 goto end;
1272 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001273 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001274 if (count > max)
1275 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001276
Christopher Fauletf2824e62018-10-01 12:12:37 +02001277 if (!conn_is_back(h1c->conn)) {
1278 h1m = &h1s->req;
1279 errflag = H1S_F_REQ_ERROR;
1280 }
1281 else {
1282 h1m = &h1s->res;
1283 errflag = H1S_F_RES_ERROR;
1284 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001285
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001286 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001287 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001288 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001289 if (!ret)
1290 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001291 }
1292 else if (h1m->state <= H1_MSG_TRAILERS) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001293 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1294 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 if (!ret)
1296 break;
1297 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001298 else if (h1m->state == H1_MSG_DONE) {
1299 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001300 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001301 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001302 else if (h1m->state == H1_MSG_TUNNEL) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001303 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1304 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001305 if (!ret)
1306 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001307 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001308 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001309 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001310 break;
1311 }
1312
Christopher Faulet539e0292018-11-19 10:40:09 +01001313 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001314 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001315
Christopher Faulet47365272018-10-31 17:40:50 +01001316 if (h1s->flags & errflag)
1317 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001318
Christopher Faulet539e0292018-11-19 10:40:09 +01001319 b_del(&h1c->ibuf, total);
1320
1321 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001322 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001323
Willy Tarreau45f2b892018-12-05 07:59:27 +01001324 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001325 h1c->flags &= ~H1C_F_IN_FULL;
1326 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001327 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001328
Christopher Fauletcf56b992018-12-11 16:12:31 +01001329 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1330
1331 if (!b_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001332 h1_release_buf(h1c, &h1c->ibuf);
1333 h1_sync_messages(h1c);
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001334 }
Christopher Fauletcf56b992018-12-11 16:12:31 +01001335 else if (!htx_is_empty(htx))
1336 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001337
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001338 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1339 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet539e0292018-11-19 10:40:09 +01001340 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001341
Christopher Faulet539e0292018-11-19 10:40:09 +01001342 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001343
1344 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001345 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001346 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001347 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001348 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001349 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001350}
1351
Christopher Faulet129817b2018-09-20 16:14:40 +02001352/*
1353 * Process outgoing data. It parses data and transfer them from the channel buffer into
1354 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1355 * 0 if it couldn't proceed.
1356 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001357static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1358{
Christopher Faulet129817b2018-09-20 16:14:40 +02001359 struct h1s *h1s = h1c->h1s;
1360 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001361 struct htx *chn_htx;
1362 struct htx_blk *blk;
1363 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001364 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001365 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001366 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001367
Christopher Faulet47365272018-10-31 17:40:50 +01001368 if (!count)
1369 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001370 chn_htx = htx_from_buf(buf);
1371
Christopher Faulet51dbc942018-09-13 09:05:15 +02001372 if (!h1_get_buf(h1c, &h1c->obuf)) {
1373 h1c->flags |= H1C_F_OUT_ALLOC;
1374 goto end;
1375 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001376
Christopher Fauletf2824e62018-10-01 12:12:37 +02001377 if (!conn_is_back(h1c->conn)) {
1378 h1m = &h1s->res;
1379 errflag = H1S_F_RES_ERROR;
1380 }
1381 else {
1382 h1m = &h1s->req;
1383 errflag = H1S_F_REQ_ERROR;
1384 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001385
Willy Tarreau3815b222018-12-11 19:50:43 +01001386 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001387
1388 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001389
Willy Tarreau3815b222018-12-11 19:50:43 +01001390 /* Perform some optimizations to reduce the number of buffer copies.
1391 * First, if the mux's buffer is empty and the htx area contains
1392 * exactly one data block of the same size as the requested count,
1393 * then it's possible to simply swap the caller's buffer with the
1394 * mux's output buffer and adjust offsets and length to match the
1395 * entire DATA HTX block in the middle. In this case we perform a
1396 * true zero-copy operation from end-to-end. This is the situation
1397 * that happens all the time with large files. Second, if this is not
1398 * possible, but the mux's output buffer is empty, we still have an
1399 * opportunity to avoid the copy to the intermediary buffer, by making
1400 * the intermediary buffer's area point to the output buffer's area.
1401 * In this case we want to skip the HTX header to make sure that copies
1402 * remain aligned and that this operation remains possible all the
1403 * time. This goes for headers, data blocks and any data extracted from
1404 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001405 */
1406 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001407 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001408
1409 if (chn_htx->used == 1 &&
1410 blk && htx_get_blk_type(blk) == HTX_BLK_DATA &&
1411 htx_get_blk_value(chn_htx, blk).len == count) {
1412 void *old_area = h1c->obuf.area;
1413
1414 h1c->obuf.area = buf->area;
1415 h1c->obuf.data = count;
1416
1417 buf->area = old_area;
1418 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001419
1420 /* The message is chunked. We need to emit the chunk
1421 * size. We have at least the size of the struct htx to
1422 * write the chunk envelope. It should be enough.
1423 */
1424 if (h1m->flags & H1_MF_CHNK) {
1425 h1_emit_chunk_size(&h1c->obuf, count);
1426 h1_emit_chunk_crlf(&h1c->obuf);
1427 }
1428
Willy Tarreau3815b222018-12-11 19:50:43 +01001429 total += count;
1430 goto out;
1431 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001432 tmp->area = h1c->obuf.area + h1c->obuf.head;
1433 }
1434
Christopher Faulet9768c262018-10-22 09:34:31 +02001435 tmp->size = b_room(&h1c->obuf);
1436
Christopher Fauletb2e84162018-12-06 11:39:49 +01001437 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001438 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001439 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001440 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001441 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001442 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001443
Christopher Fauletb2e84162018-12-06 11:39:49 +01001444 vlen = sz;
1445 if (vlen > count) {
1446 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1447 goto copy;
1448 vlen = count;
1449 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001450
Christopher Fauletb2e84162018-12-06 11:39:49 +01001451 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001452 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001453 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001454
1455 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001456 h1m_init_req(h1m);
1457 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001458 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001459 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001460 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001461 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001462 goto copy;
1463 h1m->flags |= H1_MF_XFER_LEN;
1464 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001465 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001466
Christopher Faulet9768c262018-10-22 09:34:31 +02001467 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001468 h1m_init_res(h1m);
1469 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001470 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001471 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001472 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001473 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001474 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001475 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001476 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001477 if (sl->info.res.status < 200 &&
1478 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1479 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001480 h1m->state = H1_MSG_HDR_FIRST;
1481 break;
1482
1483 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001484 h1m->state = H1_MSG_HDR_NAME;
1485 n = htx_get_blk_name(chn_htx, blk);
1486 v = htx_get_blk_value(chn_htx, blk);
1487
1488 if (isteqi(n, ist("transfer-encoding")))
1489 h1_parse_xfer_enc_header(h1m, v);
1490 else if (isteqi(n, ist("connection"))) {
1491 h1_parse_connection_header(h1m, v);
1492 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001493 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001494 if (!v.len)
1495 goto skip_hdr;
1496 }
1497
Christopher Fauletc59ff232018-12-03 13:58:44 +01001498 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001499 goto copy;
1500 skip_hdr:
1501 h1m->state = H1_MSG_HDR_L2_LWS;
1502 break;
1503
1504 case HTX_BLK_PHDR:
1505 /* not implemented yet */
1506 h1m->flags |= errflag;
1507 break;
1508
1509 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001510 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001511 /* There is no "Connection:" header and
1512 * it the conn_mode must be
1513 * processed. So do it */
1514 n = ist("Connection");
1515 v = ist("");
1516 h1_process_conn_mode(h1s, h1m, NULL, &v);
1517 process_conn_mode = 0;
1518 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001519 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001520 goto copy;
1521 }
1522 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001523 h1m->state = H1_MSG_LAST_LF;
1524 if (!chunk_memcat(tmp, "\r\n", 2))
1525 goto copy;
1526
1527 h1m->state = H1_MSG_DATA;
1528 break;
1529
1530 case HTX_BLK_DATA:
1531 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001532 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001533 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001534 goto copy;
1535 break;
1536
1537 case HTX_BLK_EOD:
1538 if (!chunk_memcat(tmp, "0\r\n", 3))
1539 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001540 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001541 h1m->state = H1_MSG_TRAILERS;
1542 break;
1543
1544 case HTX_BLK_TLR:
Christopher Faulet32188212018-11-20 18:21:43 +01001545 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1546 if (!chunk_memcat(tmp, "0\r\n", 3))
1547 goto copy;
1548 h1s->flags |= H1S_F_HAVE_EOD;
1549 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001550 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001551 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001552 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001553 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001554 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001555 break;
1556
1557 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001558 if ((h1m->flags & H1_MF_CHNK)) {
1559 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1560 if (!chunk_memcat(tmp, "0\r\n", 3))
1561 goto copy;
1562 h1s->flags |= H1S_F_HAVE_EOD;
1563 }
1564 if (!(h1s->flags & H1S_F_HAVE_TLR)) {
1565 if (!chunk_memcat(tmp, "\r\n", 2))
1566 goto copy;
1567 h1s->flags |= H1S_F_HAVE_TLR;
1568 }
1569 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001570 h1m->state = H1_MSG_DONE;
1571 break;
1572
1573 case HTX_BLK_OOB:
1574 v = htx_get_blk_value(chn_htx, blk);
1575 if (!chunk_memcat(tmp, v.ptr, v.len))
1576 goto copy;
1577 break;
1578
1579 default:
1580 h1m->flags |= errflag;
1581 break;
1582 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001583 total += vlen;
1584 count -= vlen;
1585 if (sz == vlen)
1586 blk = htx_remove_blk(chn_htx, blk);
1587 else {
1588 htx_cut_data_blk(chn_htx, blk, vlen);
1589 break;
1590 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001591 }
1592
Christopher Faulet9768c262018-10-22 09:34:31 +02001593 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001594 /* when the output buffer is empty, tmp shares the same area so that we
1595 * only have to update pointers and lengths.
1596 */
1597 if (tmp->area == h1c->obuf.area)
1598 h1c->obuf.data = tmp->data;
1599 else
1600 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001601
Willy Tarreau3815b222018-12-11 19:50:43 +01001602 htx_to_buf(chn_htx, buf);
1603 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001604 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001605 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001606 end:
1607 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001608}
1609
Christopher Faulet51dbc942018-09-13 09:05:15 +02001610/*********************************************************/
1611/* functions below are I/O callbacks from the connection */
1612/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001613static void h1_wake_stream_for_recv(struct h1s *h1s)
1614{
1615 if (h1s && h1s->recv_wait) {
1616 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1617 tasklet_wakeup(h1s->recv_wait->task);
1618 h1s->recv_wait = NULL;
1619 }
1620}
1621static void h1_wake_stream_for_send(struct h1s *h1s)
1622{
1623 if (h1s && h1s->send_wait) {
1624 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1625 tasklet_wakeup(h1s->send_wait->task);
1626 h1s->send_wait = NULL;
1627 }
1628}
1629
Christopher Faulet51dbc942018-09-13 09:05:15 +02001630/*
1631 * Attempt to read data, and subscribe if none available
1632 */
1633static int h1_recv(struct h1c *h1c)
1634{
1635 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001636 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001637 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001638 int rcvd = 0;
1639
1640 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001641 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001642
Olivier Houchard75159a92018-12-03 18:46:09 +01001643 if (!h1_recv_allowed(h1c)) {
1644 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001645 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001646 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001647
Christopher Fauletfeb11742018-11-29 15:12:34 +01001648 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 rcvd = 1;
1650 goto end;
1651 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001652
Christopher Faulet51dbc942018-09-13 09:05:15 +02001653 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1654 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001655 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001656 }
1657
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001658 /*
1659 * If we only have a small amount of data, realign it,
1660 * it's probably cheaper than doing 2 recv() calls.
1661 */
1662 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1663 b_slow_realign(&h1c->ibuf, trash.area, 0);
1664
Willy Tarreau45f2b892018-12-05 07:59:27 +01001665 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001666 if (max) {
1667 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001668
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001669 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001670 if (!b_data(&h1c->ibuf)) {
1671 /* try to pre-align the buffer like the rxbufs will be
1672 * to optimize memory copies.
1673 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001674 h1c->ibuf.head = sizeof(struct htx);
1675 }
Willy Tarreauc0960d12018-12-14 10:59:15 +01001676 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001677 }
Christopher Faulet47365272018-10-31 17:40:50 +01001678 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001679 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001680 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001681 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001682 if (h1s->csinfo.t_idle == -1)
1683 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1684 }
Christopher Faulet47365272018-10-31 17:40:50 +01001685 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001686
Christopher Fauletcf56b992018-12-11 16:12:31 +01001687 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001688 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001689 goto end;
1690 }
1691
1692 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001693
Christopher Faulet9768c262018-10-22 09:34:31 +02001694 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001695 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1696 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001697
Christopher Faulete6b39942018-12-07 09:42:49 +01001698 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Olivier Houchard6a2d3342018-12-06 17:41:26 +01001699 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001700 if (!b_data(&h1c->ibuf))
1701 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001702 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001703 h1c->flags |= H1C_F_IN_FULL;
1704 return rcvd;
1705}
1706
1707
1708/*
1709 * Try to send data if possible
1710 */
1711static int h1_send(struct h1c *h1c)
1712{
1713 struct connection *conn = h1c->conn;
1714 unsigned int flags = 0;
1715 size_t ret;
1716 int sent = 0;
1717
1718 if (conn->flags & CO_FL_ERROR)
1719 return 0;
1720
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001721 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1722 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1723 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1724 return 0;
1725 }
1726
Christopher Faulet51dbc942018-09-13 09:05:15 +02001727 if (!b_data(&h1c->obuf))
1728 goto end;
1729
1730 if (h1c->flags & H1C_F_OUT_FULL)
1731 flags |= CO_SFL_MSG_MORE;
1732
1733 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1734 if (ret > 0) {
1735 h1c->flags &= ~H1C_F_OUT_FULL;
1736 b_del(&h1c->obuf, ret);
1737 sent = 1;
1738 }
1739
Christopher Faulet145aa472018-12-06 10:56:20 +01001740 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1741 /* error or output closed, nothing to send, clear the buffer to release it */
1742 b_reset(&h1c->obuf);
1743 }
1744
Christopher Faulet51dbc942018-09-13 09:05:15 +02001745 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001746 if (!(h1c->flags & H1C_F_OUT_FULL))
1747 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001748
Christopher Faulet51dbc942018-09-13 09:05:15 +02001749 /* We're done, no more to send */
1750 if (!b_data(&h1c->obuf)) {
1751 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001752 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001753 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1754 h1_shutw_conn(conn);
1755 }
1756 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1757 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1758
1759 return sent;
1760}
1761
Christopher Faulet51dbc942018-09-13 09:05:15 +02001762
1763/* callback called on any event by the connection handler.
1764 * It applies changes and returns zero, or < 0 if it wants immediate
1765 * destruction of the connection.
1766 */
1767static int h1_process(struct h1c * h1c)
1768{
1769 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001770 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001771
Christopher Faulet51dbc942018-09-13 09:05:15 +02001772 if (!conn->mux_ctx)
1773 return -1;
1774
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001775 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001776 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1777 goto end;
1778 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001779 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001780 }
1781
Christopher Fauletfeb11742018-11-29 15:12:34 +01001782 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001783 if (h1c->flags & H1C_F_CS_ERROR ||
1784 conn->flags & CO_FL_ERROR ||
1785 conn_xprt_read0_pending(conn))
1786 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001787 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001788 if (!h1s_create(h1c, NULL))
1789 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001790 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001791 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001792 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001793 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001794 }
1795
Christopher Fauletfeb11742018-11-29 15:12:34 +01001796 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1797 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1798
Olivier Houchard75159a92018-12-03 18:46:09 +01001799 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1800 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
1801 conn->flags & CO_FL_ERROR)) {
1802 int flags = 0;
1803
1804 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1805 flags |= CS_FL_ERROR;
1806 if (conn_xprt_read0_pending(conn))
Christopher Faulet5f50f5e2018-12-07 11:39:55 +01001807 flags |= CS_FL_EOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001808 h1s->cs->flags |= flags;
1809 h1s->cs->data_cb->wake(h1s->cs);
1810 }
Christopher Faulet47365272018-10-31 17:40:50 +01001811 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001812 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001813
1814 release:
1815 h1_release(conn);
1816 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001817}
1818
1819static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1820{
1821 struct h1c *h1c = ctx;
1822 int ret = 0;
1823
1824 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1825 ret = h1_send(h1c);
1826 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1827 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001828 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001829 h1_process(h1c);
1830 return NULL;
1831}
1832
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001833static void h1_reset(struct connection *conn)
1834{
1835 struct h1c *h1c = conn->mux_ctx;
1836
1837 /* Reset the flags, and let the mux know we're waiting for a connection */
1838 h1c->flags = H1C_F_CS_WAIT_CONN;
1839}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001840
1841static int h1_wake(struct connection *conn)
1842{
1843 struct h1c *h1c = conn->mux_ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001844 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001845
Christopher Faulet539e0292018-11-19 10:40:09 +01001846 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001847 ret = h1_process(h1c);
1848 if (ret == 0) {
1849 struct h1s *h1s = h1c->h1s;
1850
1851 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1852 ret = h1s->cs->data_cb->wake(h1s->cs);
1853 }
1854 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001855}
1856
Christopher Faulet51dbc942018-09-13 09:05:15 +02001857/*******************************************/
1858/* functions below are used by the streams */
1859/*******************************************/
1860/*
1861 * Attach a new stream to a connection
1862 * (Used for outgoing connections)
1863 */
1864static struct conn_stream *h1_attach(struct connection *conn)
1865{
1866 struct h1c *h1c = conn->mux_ctx;
1867 struct conn_stream *cs = NULL;
1868 struct h1s *h1s;
1869
1870 if (h1c->flags & H1C_F_CS_ERROR)
1871 goto end;
1872
1873 cs = cs_new(h1c->conn);
1874 if (!cs)
1875 goto end;
1876
Christopher Fauletf2824e62018-10-01 12:12:37 +02001877 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001878 if (h1s == NULL)
1879 goto end;
1880
1881 return cs;
1882 end:
1883 cs_free(cs);
1884 return NULL;
1885}
1886
1887/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1888 * this mux, it's easy as we can only store a single conn_stream.
1889 */
1890static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1891{
1892 struct h1c *h1c = conn->mux_ctx;
1893 struct h1s *h1s = h1c->h1s;
1894
1895 if (h1s)
1896 return h1s->cs;
1897
1898 return NULL;
1899}
1900
1901static void h1_destroy(struct connection *conn)
1902{
1903 struct h1c *h1c = conn->mux_ctx;
1904
1905 if (!h1c->h1s)
1906 h1_release(conn);
1907}
1908
1909/*
1910 * Detach the stream from the connection and possibly release the connection.
1911 */
1912static void h1_detach(struct conn_stream *cs)
1913{
1914 struct h1s *h1s = cs->ctx;
1915 struct h1c *h1c;
1916
1917 cs->ctx = NULL;
1918 if (!h1s)
1919 return;
1920
1921 h1c = h1s->h1c;
1922 h1s->cs = NULL;
1923
Olivier Houchard44d59142018-12-13 18:46:22 +01001924 if (conn_is_back(h1c->conn) && (h1s->flags & H1S_F_WANT_KAL) &&
1925 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
1926 struct stream_interface *si = cs->data;
1927 struct stream *s = si_strm(si);
1928
Christopher Faulet9400a392018-11-23 23:10:39 +01001929 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01001930 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01001931 h1c->conn->flags |= CO_FL_PRIVATE;
1932
Olivier Houchard44d59142018-12-13 18:46:22 +01001933 if (!(h1c->conn->owner)) {
1934 h1c->conn->owner = s->sess;
1935 session_add_conn(s->sess, h1c->conn, s->target);
1936 }
Christopher Faulet9400a392018-11-23 23:10:39 +01001937 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01001938 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001939 struct server *srv = objt_server(h1c->conn->target);
1940
1941 if (srv) {
1942 if (h1c->conn->flags & CO_FL_PRIVATE)
1943 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
1944 else if (h1s->flags & H1S_F_NOT_FIRST)
1945 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1946 else
1947 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1948 }
1949 }
1950 }
1951
Christopher Faulet51dbc942018-09-13 09:05:15 +02001952 h1s_destroy(h1s);
1953
1954 /* We don't want to close right now unless the connection is in error */
1955 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01001956 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001957 h1_release(h1c->conn);
1958 else
1959 tasklet_wakeup(h1c->wait_event.task);
1960}
1961
1962
1963static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1964{
1965 struct h1s *h1s = cs->ctx;
1966
1967 if (!h1s)
1968 return;
1969
Christopher Fauletf2824e62018-10-01 12:12:37 +02001970 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1971 return;
1972
Christopher Faulet51dbc942018-09-13 09:05:15 +02001973 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1974 if (cs->flags & CS_FL_SHR)
1975 return;
1976 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1977 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1978 if (cs->flags & CS_FL_SHW) {
1979 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1980 conn_full_close(cs->conn);
1981 }
1982}
1983
1984static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1985{
1986 struct h1s *h1s = cs->ctx;
1987 struct h1c *h1c;
1988
1989 if (!h1s)
1990 return;
1991 h1c = h1s->h1c;
1992
Christopher Fauletf2824e62018-10-01 12:12:37 +02001993 if ((h1s->flags & H1S_F_WANT_KAL) &&
1994 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1995 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1996 return;
1997
Christopher Faulet51dbc942018-09-13 09:05:15 +02001998 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1999 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2000 return;
2001
2002 h1_shutw_conn(cs->conn);
2003}
2004
2005static void h1_shutw_conn(struct connection *conn)
2006{
2007 struct h1c *h1c = conn->mux_ctx;
2008
2009 if (conn_xprt_ready(conn) && conn->xprt->shutw)
2010 conn->xprt->shutw(conn, 1);
2011 if (!(conn->flags & CO_FL_SOCK_RD_SH))
2012 conn_sock_shutw(conn, 1);
2013 else {
2014 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
2015 conn_full_close(conn);
2016 }
2017}
2018
2019/* Called from the upper layer, to unsubscribe to events */
2020static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2021{
2022 struct wait_event *sw;
2023 struct h1s *h1s = cs->ctx;
2024
2025 if (!h1s)
2026 return 0;
2027
2028 if (event_type & SUB_CAN_RECV) {
2029 sw = param;
2030 if (h1s->recv_wait == sw) {
2031 sw->wait_reason &= ~SUB_CAN_RECV;
2032 h1s->recv_wait = NULL;
2033 }
2034 }
2035 if (event_type & SUB_CAN_SEND) {
2036 sw = param;
2037 if (h1s->send_wait == sw) {
2038 sw->wait_reason &= ~SUB_CAN_SEND;
2039 h1s->send_wait = NULL;
2040 }
2041 }
2042 return 0;
2043}
2044
2045/* Called from the upper layer, to subscribe to events, such as being able to send */
2046static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2047{
2048 struct wait_event *sw;
2049 struct h1s *h1s = cs->ctx;
2050
2051 if (!h1s)
2052 return -1;
2053
2054 switch (event_type) {
2055 case SUB_CAN_RECV:
2056 sw = param;
2057 if (!(sw->wait_reason & SUB_CAN_RECV)) {
2058 sw->wait_reason |= SUB_CAN_RECV;
2059 sw->handle = h1s;
2060 h1s->recv_wait = sw;
2061 }
2062 return 0;
2063 case SUB_CAN_SEND:
2064 sw = param;
2065 if (!(sw->wait_reason & SUB_CAN_SEND)) {
2066 sw->wait_reason |= SUB_CAN_SEND;
2067 sw->handle = h1s;
2068 h1s->send_wait = sw;
2069 }
2070 return 0;
2071 default:
2072 break;
2073 }
2074 return -1;
2075}
2076
2077/* Called from the upper layer, to receive data */
2078static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2079{
2080 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002081 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002082 size_t ret = 0;
2083
Christopher Faulet539e0292018-11-19 10:40:09 +01002084 if (!(h1c->flags & H1C_F_IN_ALLOC))
2085 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002086
2087 if (flags & CO_RFL_BUF_FLUSH)
2088 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002089 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2090 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01002091 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
2092 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002093 }
2094 return ret;
2095}
2096
2097
2098/* Called from the upper layer, to send data */
2099static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2100{
2101 struct h1s *h1s = cs->ctx;
2102 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002103 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002104
2105 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002106 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002107
2108 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002109 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2110 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002111
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002112 while (total != count) {
2113 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002114
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002115 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2116 ret = h1_process_output(h1c, buf, count);
2117 if (!ret)
2118 break;
2119 total += ret;
2120 if (!h1_send(h1c))
2121 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002122 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002123
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002124 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002125}
2126
Christopher Faulet1be55f92018-10-02 15:59:23 +02002127#if defined(CONFIG_HAP_LINUX_SPLICE)
2128/* Send and get, using splicing */
2129static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2130{
2131 struct h1s *h1s = cs->ctx;
2132 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2133 int ret = 0;
2134
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002135 if (b_data(&h1s->h1c->ibuf)) {
2136 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002137 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002138 }
2139
2140 h1s->flags &= ~H1S_F_BUF_FLUSH;
2141 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002142 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2143 count = h1m->curr_len;
2144 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2145 if (h1m->state == H1_MSG_DATA && ret > 0)
2146 h1m->curr_len -= ret;
2147 end:
2148 return ret;
2149
2150}
2151
2152static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2153{
2154 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002155 int ret = 0;
2156
2157 if (b_data(&h1s->h1c->obuf))
2158 goto end;
2159
2160 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002161 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002162 if (pipe->data) {
2163 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
2164 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
2165 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002166 return ret;
2167}
2168#endif
2169
Christopher Faulet51dbc942018-09-13 09:05:15 +02002170/****************************************/
2171/* MUX initialization and instanciation */
2172/****************************************/
2173
2174/* The mux operations */
2175const struct mux_ops mux_h1_ops = {
2176 .init = h1_init,
2177 .wake = h1_wake,
2178 .attach = h1_attach,
2179 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002180 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002181 .detach = h1_detach,
2182 .destroy = h1_destroy,
2183 .avail_streams = h1_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01002184 .max_streams = h1_max_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002185 .rcv_buf = h1_rcv_buf,
2186 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002187#if defined(CONFIG_HAP_LINUX_SPLICE)
2188 .rcv_pipe = h1_rcv_pipe,
2189 .snd_pipe = h1_snd_pipe,
2190#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002191 .subscribe = h1_subscribe,
2192 .unsubscribe = h1_unsubscribe,
2193 .shutr = h1_shutr,
2194 .shutw = h1_shutw,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002195 .reset = h1_reset,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002196 .flags = MX_FL_NONE,
2197 .name = "h1",
2198};
2199
2200
2201/* this mux registers default HTX proto */
2202static struct mux_proto_list mux_proto_htx =
2203{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2204
Willy Tarreau0108d902018-11-25 19:14:37 +01002205INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2206
Christopher Faulet51dbc942018-09-13 09:05:15 +02002207/*
2208 * Local variables:
2209 * c-indent-level: 8
2210 * c-basic-offset: 8
2211 * End:
2212 */