blob: 37c0f8883b379989db55c9ec685a38783f19d10a [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 Tarreau0108d902018-11-25 19:14:37 +010014#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020015
Christopher Faulet1be55f92018-10-02 15:59:23 +020016#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020017#include <types/proxy.h>
18#include <types/session.h>
19
Christopher Faulet51dbc942018-09-13 09:05:15 +020020#include <proto/connection.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020021#include <proto/h1.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020022#include <proto/http_htx.h>
23#include <proto/htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020024#include <proto/log.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020025#include <proto/stream.h>
26#include <proto/stream_interface.h>
27
28/*
29 * H1 Connection flags (32 bits)
30 */
31#define H1C_F_NONE 0x00000000
32
33/* Flags indicating why writing output data are blocked */
34#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
35#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
36/* 0x00000004 - 0x00000008 unused */
37
38/* Flags indicating why reading input data are blocked. */
39#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
40#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Faulet539e0292018-11-19 10:40:09 +010041/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020042
43#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
44#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
45#define H1C_F_CS_SHUTW 0x00004000 /* connection is already shut down */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020046#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020047
Christopher Fauletf2824e62018-10-01 12:12:37 +020048#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 +020049
Christopher Faulet51dbc942018-09-13 09:05:15 +020050/*
51 * H1 Stream flags (32 bits)
52 */
Christopher Faulet129817b2018-09-20 16:14:40 +020053#define H1S_F_NONE 0x00000000
54#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020055#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
56#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Christopher Faulet539e0292018-11-19 10:40:09 +010057/* 0x00000008 unused */
Christopher Fauletf2824e62018-10-01 12:12:37 +020058#define H1S_F_WANT_KAL 0x00000010
59#define H1S_F_WANT_TUN 0x00000020
60#define H1S_F_WANT_CLO 0x00000040
61#define H1S_F_WANT_MSK 0x00000070
62#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010063#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010064#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Christopher Faulet32188212018-11-20 18:21:43 +010065#define H1S_F_HAVE_EOD 0x00000400 /* Set during input/output process to know the last empty chunk was processed */
66#define H1S_F_HAVE_TLR 0x00000800 /* Set during input/output process to know the trailers were processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020067
68/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020069struct h1c {
70 struct connection *conn;
71 struct proxy *px;
72 uint32_t flags; /* Connection flags: H1C_F_* */
73
74 struct buffer ibuf; /* Input buffer to store data before parsing */
75 struct buffer obuf; /* Output buffer to store data after reformatting */
76
77 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
78 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
79
80 struct h1s *h1s; /* H1 stream descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020081};
82
83/* H1 stream descriptor */
84struct h1s {
85 struct h1c *h1c;
86 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010087 struct cs_info csinfo; /* CS info, only used for client connections */
88 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020089
Christopher Faulet51dbc942018-09-13 09:05:15 +020090 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
91 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020092
93 struct h1m req;
94 struct h1m res;
95
96 enum http_meth_t meth; /* HTTP resquest method */
97 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +020098};
99
100/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100101DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
102DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200103
Christopher Faulet51dbc942018-09-13 09:05:15 +0200104static int h1_recv(struct h1c *h1c);
105static int h1_send(struct h1c *h1c);
106static int h1_process(struct h1c *h1c);
107static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
108static void h1_shutw_conn(struct connection *conn);
109
110/*****************************************************/
111/* functions below are for dynamic buffer management */
112/*****************************************************/
113/*
114 * Indicates whether or not the we may call the h1_recv() function to
115 * attempt to receive data into the buffer and/or parse pending data. The
116 * condition is a bit complex due to some API limits for now. The rules are the
117 * following :
118 * - if an error or a shutdown was detected on the connection and the buffer
119 * is empty, we must not attempt to receive
120 * - if the input buffer failed to be allocated, we must not try to receive
121 * and we know there is nothing pending
122 * - if no flag indicates a blocking condition, we may attempt to receive,
123 * regardless of whether the input buffer is full or not, so that only de
124 * receiving part decides whether or not to block. This is needed because
125 * the connection API indeed prevents us from re-enabling receipt that is
126 * already enabled in a polled state, so we must always immediately stop as
127 * soon as the mux can't proceed so as never to hit an end of read with data
128 * pending in the buffers.
129 * - otherwise must may not attempt to receive
130 */
131static inline int h1_recv_allowed(const struct h1c *h1c)
132{
133 if (b_data(&h1c->ibuf) == 0 &&
Christopher Faulet7e346f32018-11-20 17:13:52 +0100134 (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW) ||
Christopher Faulet51dbc942018-09-13 09:05:15 +0200135 h1c->conn->flags & CO_FL_ERROR ||
136 conn_xprt_read0_pending(h1c->conn)))
137 return 0;
138
139 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL)))
140 return 1;
141
142 return 0;
143}
144
145/*
146 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
147 * flags are used to figure what buffer was requested. It returns 1 if the
148 * allocation succeeds, in which case the connection is woken up, or 0 if it's
149 * impossible to wake up and we prefer to be woken up later.
150 */
151static int h1_buf_available(void *target)
152{
153 struct h1c *h1c = target;
154
155 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
156 h1c->flags &= ~H1C_F_IN_ALLOC;
157 if (h1_recv_allowed(h1c))
158 tasklet_wakeup(h1c->wait_event.task);
159 return 1;
160 }
161
162 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
163 h1c->flags &= ~H1C_F_OUT_ALLOC;
164 tasklet_wakeup(h1c->wait_event.task);
165 return 1;
166 }
167
Christopher Faulet51dbc942018-09-13 09:05:15 +0200168 return 0;
169}
170
171/*
172 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
173 */
174static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
175{
176 struct buffer *buf = NULL;
177
178 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
179 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
180 h1c->buf_wait.target = h1c;
181 h1c->buf_wait.wakeup_cb = h1_buf_available;
182 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
183 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
184 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
185 __conn_xprt_stop_recv(h1c->conn);
186 }
187 return buf;
188}
189
190/*
191 * Release a buffer, if any, and try to wake up entities waiting in the buffer
192 * wait queue.
193 */
194static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
195{
196 if (bptr->size) {
197 b_free(bptr);
198 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
199 }
200}
201
202static int h1_avail_streams(struct connection *conn)
203{
204 struct h1c *h1c = conn->mux_ctx;
205
206 return h1c->h1s ? 0 : 1;
207}
208
209
210/*****************************************************************/
211/* functions below are dedicated to the mux setup and management */
212/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100213static struct conn_stream *h1s_new_cs(struct h1s *h1s)
214{
215 struct conn_stream *cs;
216
217 cs = cs_new(h1s->h1c->conn);
218 if (!cs)
219 goto err;
220 h1s->cs = cs;
221 cs->ctx = h1s;
222
223 if (h1s->flags & H1S_F_NOT_FIRST)
224 cs->flags |= CS_FL_NOT_FIRST;
225
226 if (stream_create_from_cs(cs) < 0)
227 goto err;
228 return cs;
229
230 err:
231 cs_free(cs);
232 h1s->cs = NULL;
233 return NULL;
234}
235
Christopher Fauletf2824e62018-10-01 12:12:37 +0200236static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200237{
238 struct h1s *h1s;
239
240 h1s = pool_alloc(pool_head_h1s);
241 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100242 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200243
244 h1s->h1c = h1c;
245 h1c->h1s = h1s;
246
247 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200248 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200249
250 h1s->recv_wait = NULL;
251 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200252
253 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200254 h1s->req.flags |= H1_MF_NO_PHDR;
255
Christopher Faulet129817b2018-09-20 16:14:40 +0200256 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200257 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200258
259 h1s->status = 0;
260 h1s->meth = HTTP_METH_OTHER;
261
Christopher Faulet47365272018-10-31 17:40:50 +0100262 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
263 h1s->flags |= H1S_F_NOT_FIRST;
264 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
265
Christopher Faulet129817b2018-09-20 16:14:40 +0200266 if (!conn_is_back(h1c->conn)) {
267 if (h1c->px->options2 & PR_O2_REQBUG_OK)
268 h1s->req.err_pos = -1;
269 }
270 else {
271 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
272 h1s->res.err_pos = -1;
273 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200274
Christopher Faulet539e0292018-11-19 10:40:09 +0100275 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
276 * create a new one.
277 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200278 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100279 h1s->csinfo.create_date = date;
280 h1s->csinfo.tv_create = now;
281 h1s->csinfo.t_handshake = 0;
282 h1s->csinfo.t_idle = -1;
283
Christopher Fauletf2824e62018-10-01 12:12:37 +0200284 cs->ctx = h1s;
285 h1s->cs = cs;
286 }
Christopher Faulet47365272018-10-31 17:40:50 +0100287 else {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100288 struct session *sess = h1c->conn->owner;
289
290 h1s->csinfo.create_date = sess->accept_date;
291 h1s->csinfo.tv_create = sess->tv_accept;
292 h1s->csinfo.t_handshake = sess->t_handshake;
293 h1s->csinfo.t_idle = -1;
294
Christopher Faulet47365272018-10-31 17:40:50 +0100295 cs = h1s_new_cs(h1s);
296 if (!cs)
297 goto fail;
298 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200299 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100300
301 fail:
302 pool_free(pool_head_h1s, h1s);
303 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200304}
305
306static void h1s_destroy(struct h1s *h1s)
307{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200308 if (h1s) {
309 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200310
Christopher Fauletf2824e62018-10-01 12:12:37 +0200311 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200312
Christopher Fauletf2824e62018-10-01 12:12:37 +0200313 if (h1s->recv_wait != NULL)
314 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
315 if (h1s->send_wait != NULL)
316 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
317
Christopher Faulet47365272018-10-31 17:40:50 +0100318 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
319 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
320 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200321
Christopher Fauletf2824e62018-10-01 12:12:37 +0200322 cs_free(h1s->cs);
323 pool_free(pool_head_h1s, h1s);
324 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200325}
326
Christopher Fauletfeb11742018-11-29 15:12:34 +0100327static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
328{
329 struct h1s *h1s = cs->ctx;
330
331 if (h1s && !conn_is_back(cs->conn))
332 return &h1s->csinfo;
333 return NULL;
334}
335
Christopher Faulet51dbc942018-09-13 09:05:15 +0200336/*
337 * Initialize the mux once it's attached. It is expected that conn->mux_ctx
338 * points to the existing conn_stream (for outgoing connections) or NULL (for
339 * incoming ones). Returns < 0 on error.
340 */
341static int h1_init(struct connection *conn, struct proxy *proxy)
342{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200343 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200344
345 h1c = pool_alloc(pool_head_h1c);
346 if (!h1c)
347 goto fail_h1c;
348 h1c->conn = conn;
349 h1c->px = proxy;
350
351 h1c->flags = H1C_F_NONE;
352 h1c->ibuf = BUF_NULL;
353 h1c->obuf = BUF_NULL;
354 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200355
Christopher Faulet51dbc942018-09-13 09:05:15 +0200356 LIST_INIT(&h1c->buf_wait.list);
357 h1c->wait_event.task = tasklet_new();
358 if (!h1c->wait_event.task)
359 goto fail;
360 h1c->wait_event.task->process = h1_io_cb;
361 h1c->wait_event.task->context = h1c;
362 h1c->wait_event.wait_reason = 0;
363
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200364 if (!(conn->flags & CO_FL_CONNECTED))
365 h1c->flags |= H1C_F_CS_WAIT_CONN;
366
Christopher Fauletf2824e62018-10-01 12:12:37 +0200367 /* Always Create a new H1S */
368 if (!h1s_create(h1c, conn->mux_ctx))
369 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200370
Christopher Faulet129817b2018-09-20 16:14:40 +0200371 conn->mux_ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200372
Christopher Faulet51dbc942018-09-13 09:05:15 +0200373 /* Try to read, if nothing is available yet we'll just subscribe */
374 if (h1_recv(h1c))
375 h1_process(h1c);
376
377 /* mux->wake will be called soon to complete the operation */
378 return 0;
379
380 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100381 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200382 tasklet_free(h1c->wait_event.task);
383 pool_free(pool_head_h1c, h1c);
384 fail_h1c:
385 return -1;
386}
387
388
389/* release function for a connection. This one should be called to free all
390 * resources allocated to the mux.
391 */
392static void h1_release(struct connection *conn)
393{
394 struct h1c *h1c = conn->mux_ctx;
395
396 LIST_DEL(&conn->list);
397
398 if (h1c) {
399 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
400 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
401 LIST_DEL(&h1c->buf_wait.list);
402 LIST_INIT(&h1c->buf_wait.list);
403 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
404 }
405
406 h1_release_buf(h1c, &h1c->ibuf);
407 h1_release_buf(h1c, &h1c->obuf);
408
Christopher Faulet51dbc942018-09-13 09:05:15 +0200409 if (h1c->wait_event.task)
410 tasklet_free(h1c->wait_event.task);
411
Christopher Fauletf2824e62018-10-01 12:12:37 +0200412 h1s_destroy(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200413 if (h1c->wait_event.wait_reason != 0)
414 conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason,
415 &h1c->wait_event);
416 pool_free(pool_head_h1c, h1c);
417 }
418
419 conn->mux = NULL;
420 conn->mux_ctx = NULL;
421
422 conn_stop_tracking(conn);
423 conn_full_close(conn);
424 if (conn->destroy_cb)
425 conn->destroy_cb(conn);
426 conn_free(conn);
427}
428
429/******************************************************/
430/* functions below are for the H1 protocol processing */
431/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200432/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
433 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200434 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100435static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200436{
Christopher Faulet570d1612018-11-26 11:13:57 +0100437 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200438
Christopher Faulet570d1612018-11-26 11:13:57 +0100439 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200440 (*(p + 5) > '1' ||
441 (*(p + 5) == '1' && *(p + 7) >= '1')))
442 h1m->flags |= H1_MF_VER_11;
443}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200444
Christopher Faulet9768c262018-10-22 09:34:31 +0200445/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
446 * greater or equal to 1.1
447 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100448static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200449{
Christopher Faulet570d1612018-11-26 11:13:57 +0100450 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200451
Christopher Faulet570d1612018-11-26 11:13:57 +0100452 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200453 (*(p + 5) > '1' ||
454 (*(p + 5) == '1' && *(p + 7) >= '1')))
455 h1m->flags |= H1_MF_VER_11;
456}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200457
Christopher Faulet9768c262018-10-22 09:34:31 +0200458/*
459 * Check the validity of the request version. If the version is valid, it
460 * returns 1. Otherwise, it returns 0.
461 */
462static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
463{
464 struct h1c *h1c = h1s->h1c;
465
466 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
467 * exactly one digit "." one digit. This check may be disabled using
468 * option accept-invalid-http-request.
469 */
470 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
471 if (sl.rq.v.len != 8)
472 return 0;
473
474 if (*(sl.rq.v.ptr + 4) != '/' ||
475 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
476 *(sl.rq.v.ptr + 6) != '.' ||
477 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
478 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200479 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200480 else if (!sl.rq.v.len) {
481 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200482
Christopher Faulet9768c262018-10-22 09:34:31 +0200483 /* RFC 1945 allows only GET for HTTP/0.9 requests */
484 if (sl.rq.meth != HTTP_METH_GET)
485 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200486
Christopher Faulet9768c262018-10-22 09:34:31 +0200487 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
488 if (!sl.rq.u.len)
489 return 0;
490
491 /* Add HTTP version */
492 sl.rq.v = ist("HTTP/1.0");
493 }
494 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200495}
496
Christopher Faulet9768c262018-10-22 09:34:31 +0200497/*
498 * Check the validity of the response version. If the version is valid, it
499 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200500 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200501static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200502{
Christopher Faulet9768c262018-10-22 09:34:31 +0200503 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200504
Christopher Faulet9768c262018-10-22 09:34:31 +0200505 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
506 * exactly one digit "." one digit. This check may be disabled using
507 * option accept-invalid-http-request.
508 */
509 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
510 if (sl.st.v.len != 8)
511 return 0;
512
513 if (*(sl.st.v.ptr + 4) != '/' ||
514 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
515 *(sl.st.v.ptr + 6) != '.' ||
516 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
517 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200518 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200519 return 1;
520}
521/* Remove all "Connection:" headers from the HTX message <htx> */
522static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
523{
524 struct ist hdr = {.ptr = "Connection", .len = 10};
525 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200526
Christopher Faulet9768c262018-10-22 09:34:31 +0200527 while (http_find_header(htx, hdr, &ctx, 1))
528 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200529
Christopher Faulet9768c262018-10-22 09:34:31 +0200530 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
531}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200532
Christopher Faulet9768c262018-10-22 09:34:31 +0200533/* Add a "Connection:" header with the value <value> into the HTX message
534 * <htx>.
535 */
536static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
537{
538 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200539
Christopher Faulet9768c262018-10-22 09:34:31 +0200540 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200541}
542
543/* Deduce the connection mode of the client connection, depending on the
544 * configuration and the H1 message flags. This function is called twice, the
545 * first time when the request is parsed and the second time when the response
546 * is parsed.
547 */
548static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
549{
550 struct proxy *fe = h1s->h1c->px;
551 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
552
553 /* Tunnel mode can only by set on the frontend */
554 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
555 flag = H1S_F_WANT_TUN;
556 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
557 flag = H1S_F_WANT_CLO;
558
559 /* flags order: CLO > SCL > TUN > KAL */
560 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
561 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
562
563 if (h1m->flags & H1_MF_RESP) {
564 /* Either we've established an explicit tunnel, or we're
565 * switching the protocol. In both cases, we're very unlikely to
566 * understand the next protocols. We have to switch to tunnel
567 * mode, so that we transfer the request and responses then let
568 * this protocol pass unmodified. When we later implement
569 * specific parsers for such protocols, we'll want to check the
570 * Upgrade header which contains information about that protocol
571 * for responses with status 101 (eg: see RFC2817 about TLS).
572 */
573 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
574 h1s->status == 101)
575 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
576 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
577 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
578 }
579 else {
580 if (h1s->flags & H1S_F_WANT_KAL &&
581 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
582 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
583 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
584 }
585
586 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
587 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
588 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
589}
590
591/* Deduce the connection mode of the client connection, depending on the
592 * configuration and the H1 message flags. This function is called twice, the
593 * first time when the request is parsed and the second time when the response
594 * is parsed.
595 */
596static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
597{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100598 struct h1c *h1c = h1s->h1c;
599 struct session *sess = h1c->conn->owner;
600 struct proxy *fe = sess->fe;
601 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200602 int flag = H1S_F_WANT_KAL;
603
604 /* Tunnel mode can only by set on the frontend */
605 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
606 flag = H1S_F_WANT_TUN;
607
608 /* For the server connection: server-close == httpclose */
609 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
610 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
611 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
612 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
613 flag = H1S_F_WANT_CLO;
614
615 /* flags order: CLO > SCL > TUN > KAL */
616 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
617 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
618
619 if (h1m->flags & H1_MF_RESP) {
620 /* Either we've established an explicit tunnel, or we're
621 * switching the protocol. In both cases, we're very unlikely to
622 * understand the next protocols. We have to switch to tunnel
623 * mode, so that we transfer the request and responses then let
624 * this protocol pass unmodified. When we later implement
625 * specific parsers for such protocols, we'll want to check the
626 * Upgrade header which contains information about that protocol
627 * for responses with status 101 (eg: see RFC2817 about TLS).
628 */
629 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
630 h1s->status == 101)
631 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
632 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
633 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
634 else if (h1s->flags & H1S_F_WANT_KAL &&
635 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
636 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
637 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
638 }
639
640 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
641 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
642 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200643}
644
Christopher Faulet9768c262018-10-22 09:34:31 +0200645static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
646 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200647{
648 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200649
650 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
651 * token is found
652 */
653 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200654 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200655
656 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200657 if (h1m->flags & H1_MF_CONN_CLO) {
658 if (conn_val)
659 *conn_val = ist("");
660 if (htx)
661 h1_remove_conn_hdrs(h1m, htx);
662 }
663 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
664 if (conn_val)
665 *conn_val = ist("keep-alive");
666 if (htx)
667 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
668 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200669 }
670 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200671 if (h1m->flags & H1_MF_CONN_KAL) {
672 if (conn_val)
673 *conn_val = ist("");
674 if (htx)
675 h1_remove_conn_hdrs(h1m, htx);
676 }
677 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
678 if (conn_val)
679 *conn_val = ist("close");
680 if (htx)
681 h1_add_conn_hdr(h1m, htx, ist("close"));
682 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200683 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200684}
685
Christopher Faulet9768c262018-10-22 09:34:31 +0200686static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
687 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200688{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200689 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
690 * token is found
691 */
692 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200693 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200694
695 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200696 if (h1m->flags & H1_MF_CONN_CLO) {
697 if (conn_val)
698 *conn_val = ist("");
699 if (htx)
700 h1_remove_conn_hdrs(h1m, htx);
701 }
702 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
703 if (conn_val)
704 *conn_val = ist("keep-alive");
705 if (htx)
706 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
707 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200708 }
709 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200710 if (h1m->flags & H1_MF_CONN_KAL) {
711 if (conn_val)
712 *conn_val = ist("");
713 if (htx)
714 h1_remove_conn_hdrs(h1m, htx);
715 }
716 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
717 if (conn_val)
718 *conn_val = ist("close");
719 if (htx)
720 h1_add_conn_hdr(h1m, htx, ist("close"));
721 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200722 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200723}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200724
Christopher Faulet9768c262018-10-22 09:34:31 +0200725/* Set the right connection mode and update "Connection:" header if
726 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
727 * message is updated accordingly. When <conn_val> is not NULL, it is set with
728 * the new header value.
729 */
730static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
731 struct htx *htx, struct ist *conn_val)
732{
733 if (!conn_is_back(h1s->h1c->conn)) {
734 h1_set_cli_conn_mode(h1s, h1m);
735 if (h1m->flags & H1_MF_RESP)
736 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
737 }
738 else {
739 h1_set_srv_conn_mode(h1s, h1m);
740 if (!(h1m->flags & H1_MF_RESP))
741 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
742 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200743}
744
Christopher Faulet129817b2018-09-20 16:14:40 +0200745/*
746 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200747 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
748 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet129817b2018-09-20 16:14:40 +0200749 * responsibile to update the parser state <h1m>.
750 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200751static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200752 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200753{
754 struct http_hdr hdrs[MAX_HTTP_HDR];
755 union h1_sl sl;
756 int ret = 0;
757
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100758 if (!max)
759 goto end;
760
Christopher Faulet129817b2018-09-20 16:14:40 +0200761 /* Realing input buffer if necessary */
762 if (b_head(buf) + b_data(buf) > b_wrap(buf))
763 b_slow_realign(buf, trash.area, 0);
764
Christopher Fauletf2824e62018-10-01 12:12:37 +0200765 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Faulet129817b2018-09-20 16:14:40 +0200766 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &sl);
767 if (ret <= 0) {
768 /* Incomplete or invalid message. If the buffer is full, it's an
769 * error because headers are too large to be handled by the
770 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200771 if (ret < 0 || (!ret && b_full(buf)))
772 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200773 goto end;
774 }
775
776 /* messages headers fully parsed, do some checks to prepare the body
777 * parsing.
778 */
779
780 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200781 if (ret > (b_size(buf) - global.tune.maxrewrite))
782 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200783
Christopher Faulet9768c262018-10-22 09:34:31 +0200784 /* Save the request's method or the response's status, check if the body
785 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200786 if (!(h1m->flags & H1_MF_RESP)) {
787 h1s->meth = sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200788
Christopher Faulet129817b2018-09-20 16:14:40 +0200789 /* Request have always a known length */
790 h1m->flags |= H1_MF_XFER_LEN;
791 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
792 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200793
794 if (!h1_process_req_vsn(h1s, h1m, sl)) {
795 h1m->err_pos = sl.rq.v.ptr - b_head(buf);
796 h1m->err_state = h1m->state;
797 goto vsn_error;
798 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200799 }
800 else {
801 h1s->status = sl.st.status;
802
803 if ((h1s->meth == HTTP_METH_HEAD) ||
804 (h1s->status >= 100 && h1s->status < 200) ||
805 (h1s->status == 204) || (h1s->status == 304) ||
806 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
807 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
808 h1m->flags |= H1_MF_XFER_LEN;
809 h1m->curr_len = h1m->body_len = 0;
810 h1m->state = H1_MSG_DONE;
811 }
812 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
813 h1m->flags |= H1_MF_XFER_LEN;
814 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
815 h1m->state = H1_MSG_DONE;
816 }
817 else
818 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200819
820 if (!h1_process_res_vsn(h1s, h1m, sl)) {
821 h1m->err_pos = sl.st.v.ptr - b_head(buf);
822 h1m->err_state = h1m->state;
823 goto vsn_error;
824 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200825 }
826
Christopher Faulet9768c262018-10-22 09:34:31 +0200827 if (!(h1m->flags & H1_MF_RESP)) {
828 if (!htx_add_reqline(htx, sl) || !htx_add_all_headers(htx, hdrs))
829 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200830 }
831 else {
Christopher Faulet9768c262018-10-22 09:34:31 +0200832 if (!htx_add_resline(htx, sl) || !htx_add_all_headers(htx, hdrs))
833 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200834 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200835 if (h1m->state == H1_MSG_DONE)
836 if (!htx_add_endof(htx, HTX_BLK_EOM))
837 goto error;
838
839 h1_process_conn_mode(h1s, h1m, htx, NULL);
840
841 /* If body length cannot be determined, set htx->extra to
842 * ULLONG_MAX. This value is impossible in other cases.
843 */
844 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
845
846 /* Recheck there is enough space to do headers rewritting */
847 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
848 goto error;
849
850 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200851 end:
852 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200853
854 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +0200855 h1m->err_state = h1m->state;
856 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +0200857 vsn_error:
858 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200859 ret = 0;
860 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200861}
862
863/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200864 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
865 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +0200866 * and filling h1s->err_pos and h1s->err_state fields. This functions is
867 * responsibile to update the parser state <h1m>.
868 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200869static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200870 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200871{
Christopher Faulet9768c262018-10-22 09:34:31 +0200872 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +0200873 size_t total = 0;
874 int ret = 0;
875
876 if (h1m->flags & H1_MF_XFER_LEN) {
877 if (h1m->flags & H1_MF_CLEN) {
878 /* content-length: read only h2m->body_len */
879 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200880 if (ret > data_space)
881 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200882 if ((uint64_t)ret > h1m->curr_len)
883 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200884 if (ret > b_contig_data(buf, *ofs))
885 ret = b_contig_data(buf, *ofs);
886 if (ret) {
887 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
888 goto end;
889 h1m->curr_len -= ret;
890 *ofs += ret;
891 total += ret;
892 }
893
894 if (!h1m->curr_len) {
895 if (!htx_add_endof(htx, HTX_BLK_EOM))
896 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200897 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200898 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200899 }
900 else if (h1m->flags & H1_MF_CHNK) {
901 new_chunk:
902 /* te:chunked : parse chunks */
903 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200904 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +0200905 if (ret <= 0)
906 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200907 h1m->state = H1_MSG_CHUNK_SIZE;
908
Christopher Faulet129817b2018-09-20 16:14:40 +0200909 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200910 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200911 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200912 }
913
914 if (h1m->state == H1_MSG_CHUNK_SIZE) {
915 unsigned int chksz;
916
Christopher Fauletf2824e62018-10-01 12:12:37 +0200917 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +0200918 if (ret <= 0)
919 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200920 if (!chksz) {
921 if (!htx_add_endof(htx, HTX_BLK_EOD))
922 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +0100923 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +0200924 h1m->state = H1_MSG_TRAILERS;
925 }
926 else
927 h1m->state = H1_MSG_DATA;
928
Christopher Faulet129817b2018-09-20 16:14:40 +0200929 h1m->curr_len = chksz;
930 h1m->body_len += chksz;
931 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200932 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200933 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200934 }
935
936 if (h1m->state == H1_MSG_DATA) {
937 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200938 if (ret > data_space)
939 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200940 if ((uint64_t)ret > h1m->curr_len)
941 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200942 if (ret > b_contig_data(buf, *ofs))
943 ret = b_contig_data(buf, *ofs);
944 if (ret) {
945 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
946 goto end;
947 h1m->curr_len -= ret;
948 max -= ret;
949 *ofs += ret;
950 total += ret;
951 }
952 if (!h1m->curr_len) {
953 h1m->state = H1_MSG_CHUNK_CRLF;
954 goto new_chunk;
955 }
956 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200957 }
958
959 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +0100960 /* Trailers were alread parsed, only the EOM
961 * need to be added */
962 if (h1s->flags & H1S_F_HAVE_TLR)
963 goto skip_tlr_parsing;
964
Christopher Fauletf2824e62018-10-01 12:12:37 +0200965 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +0200966 if (ret > data_space)
967 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +0200968 if (ret <= 0)
969 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200970
971 /* Realing input buffer if tailers wrap. For now
972 * this is a workaroung. Because trailers are
973 * not split on CRLF, like headers, there is no
974 * way to know where to split it when trailers
975 * wrap. This is a limitation of
976 * h1_measure_trailers.
977 */
978 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
979 b_slow_realign(buf, trash.area, 0);
980
981 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
982 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +0100983 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200984 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200985 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200986 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +0200987
Christopher Faulet17276482018-11-22 11:44:35 +0100988 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +0200989 if (!htx_add_endof(htx, HTX_BLK_EOM))
990 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200991 h1m->state = H1_MSG_DONE;
992 }
993 }
994 else {
995 /* XFER_LEN is set but not CLEN nor CHNK, it means there
996 * is no body. Switch the message in DONE state
997 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200998 if (!htx_add_endof(htx, HTX_BLK_EOM))
999 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001000 h1m->state = H1_MSG_DONE;
1001 }
1002 }
1003 else {
1004 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001005 ret = max;
1006 if (ret > data_space)
1007 ret = data_space;
1008 if (ret > b_contig_data(buf, *ofs))
1009 ret = b_contig_data(buf, *ofs);
1010 if (ret) {
1011 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1012 goto end;
1013
1014 *ofs += max;
1015 total = max;
1016 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001017 }
1018
1019 end:
1020 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001021 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001022 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001023 h1m->err_pos = *ofs + max + ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001024 return 0;
1025 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001026 /* update htx->extra, only when the body length is known */
1027 if (h1m->flags & H1_MF_XFER_LEN)
1028 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001029 return total;
1030}
1031
1032/*
1033 * Synchronize the request and the response before reseting them. Except for 1xx
1034 * responses, we wait that the request and the response are in DONE state and
1035 * that all data are forwarded for both. For 1xx responses, only the response is
1036 * reset, waiting the final one. Many 1xx messages can be sent.
1037 */
1038static void h1_sync_messages(struct h1c *h1c)
1039{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001040 struct h1s *h1s = h1c->h1s;
1041
1042 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001043 return;
1044
Christopher Fauletf2824e62018-10-01 12:12:37 +02001045 if (h1s->res.state == H1_MSG_DONE &&
1046 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001047 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001048 /* For 100-Continue response or any other informational 1xx
1049 * response which is non-final, don't reset the request, the
1050 * transaction is not finished. We take care the response was
1051 * transferred before.
1052 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001053 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001054 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001055 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001056 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001057 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1058 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001059 h1m_init_req(&h1s->req);
1060 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001061 h1s->req.state = H1_MSG_TUNNEL;
1062 h1s->res.state = H1_MSG_TUNNEL;
1063 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001064 }
1065}
1066
1067/*
1068 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001069 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1070 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001071 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001072static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001073{
Christopher Faulet539e0292018-11-19 10:40:09 +01001074 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001075 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001076 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001077 size_t total = 0;
1078 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001079 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001080 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001081
Christopher Faulet539e0292018-11-19 10:40:09 +01001082 htx = htx_from_buf(buf);
1083 count = b_data(&h1c->ibuf);
1084 max = htx_free_space(htx);
1085 if (flags & CO_RFL_KEEP_RSV) {
1086 if (max < global.tune.maxrewrite)
1087 goto end;
1088 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001089 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001090 if (count > max)
1091 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001092
Christopher Fauletf2824e62018-10-01 12:12:37 +02001093 if (!conn_is_back(h1c->conn)) {
1094 h1m = &h1s->req;
1095 errflag = H1S_F_REQ_ERROR;
1096 }
1097 else {
1098 h1m = &h1s->res;
1099 errflag = H1S_F_RES_ERROR;
1100 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001101
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001102 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001103 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001104 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001105 if (!ret)
1106 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001107 }
1108 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001109 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001110 if (!ret)
1111 break;
1112 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001113 else if (h1m->state == H1_MSG_DONE)
1114 break;
1115 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001116 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet9768c262018-10-22 09:34:31 +02001117 if (!ret)
1118 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001119 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001120 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001121 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001122 break;
1123 }
1124
Christopher Faulet539e0292018-11-19 10:40:09 +01001125 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001126 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001127
Christopher Faulet47365272018-10-31 17:40:50 +01001128 if (h1s->flags & errflag)
1129 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001130
Christopher Faulet539e0292018-11-19 10:40:09 +01001131 b_del(&h1c->ibuf, total);
1132
1133 end:
1134 if (htx_is_not_empty(htx))
1135 b_set_data(buf, b_size(buf));
1136 else {
1137 htx_reset(htx);
1138 b_set_data(buf, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001139 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001140
Christopher Faulet539e0292018-11-19 10:40:09 +01001141 if (h1c->flags & H1C_F_IN_FULL && b_room(&h1c->ibuf)) {
1142 h1c->flags &= ~H1C_F_IN_FULL;
1143 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001144 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001145
Christopher Faulet9c388402018-11-19 21:54:26 +01001146 if (b_data(&h1c->ibuf)) {
1147 if (!htx_is_empty(htx))
1148 h1s->cs->flags |= CS_FL_RCV_MORE;
1149 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001150 else {
1151 h1_release_buf(h1c, &h1c->ibuf);
1152 h1_sync_messages(h1c);
1153
1154 h1s->cs->flags &= ~CS_FL_RCV_MORE;
1155 if (h1s->cs->flags & CS_FL_REOS)
1156 h1s->cs->flags |= CS_FL_EOS;
1157 }
1158 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001159
1160 parsing_err:
1161 // FIXME: create an error snapshot here
1162 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001163 htx->flags |= HTX_FL_PARSING_ERROR;
1164 b_set_data(buf, b_size(buf));
1165 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001166 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001167}
1168
Christopher Faulet129817b2018-09-20 16:14:40 +02001169/*
1170 * Process outgoing data. It parses data and transfer them from the channel buffer into
1171 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1172 * 0 if it couldn't proceed.
1173 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001174static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1175{
Christopher Faulet129817b2018-09-20 16:14:40 +02001176 struct h1s *h1s = h1c->h1s;
1177 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001178 struct htx *chn_htx;
1179 struct htx_blk *blk;
1180 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001181 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001182 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001183
Christopher Faulet47365272018-10-31 17:40:50 +01001184 if (!count)
1185 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001186 chn_htx = htx_from_buf(buf);
1187
Christopher Faulet51dbc942018-09-13 09:05:15 +02001188 if (!h1_get_buf(h1c, &h1c->obuf)) {
1189 h1c->flags |= H1C_F_OUT_ALLOC;
1190 goto end;
1191 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001192
Christopher Fauletf2824e62018-10-01 12:12:37 +02001193 if (!conn_is_back(h1c->conn)) {
1194 h1m = &h1s->res;
1195 errflag = H1S_F_RES_ERROR;
1196 }
1197 else {
1198 h1m = &h1s->req;
1199 errflag = H1S_F_REQ_ERROR;
1200 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001201
1202
1203 tmp = get_trash_chunk();
1204 tmp->size = b_room(&h1c->obuf);
1205
1206 blk = htx_get_head_blk(chn_htx);
1207 while (!(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001208 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001209 struct ist n, v;
1210 uint32_t sz = htx_get_blksz(blk);
1211
1212 if (total + sz > count)
1213 goto copy;
1214
1215 switch (htx_get_blk_type(blk)) {
1216 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001217 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001218
1219 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001220 h1m_init_req(h1m);
1221 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001222 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001223 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001224 h1_parse_req_vsn(h1m, sl);
1225 if (!htx_reqline_to_str(sl, tmp))
1226 goto copy;
1227 h1m->flags |= H1_MF_XFER_LEN;
1228 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001229 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001230
Christopher Faulet9768c262018-10-22 09:34:31 +02001231 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001232 h1m_init_res(h1m);
1233 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001234 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001235 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 h1_parse_res_vsn(h1m, sl);
1237 if (!htx_stline_to_str(sl, tmp))
1238 goto copy;
1239 if (chn_htx->extra != ULLONG_MAX)
1240 h1m->flags |= H1_MF_XFER_LEN;
1241 h1m->state = H1_MSG_HDR_FIRST;
1242 break;
1243
1244 case HTX_BLK_HDR:
1245 if (h1m->state == H1_MSG_HDR_FIRST) {
1246 struct http_hdr_ctx ctx;
1247
1248 n = ist("Connection");
1249 v = ist("");
1250
1251 /* If there is no "Connection:" header,
1252 * process conn_mode now and add the
1253 * right one.
1254 */
1255 ctx.blk = blk;
Christopher Faulet5999b862018-11-27 10:46:09 +01001256 ctx.value = ist(NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +02001257 if (http_find_header(chn_htx, n, &ctx, 1))
1258 goto process_hdr;
1259 h1_process_conn_mode(h1s, h1m, NULL, &v);
1260 if (!v.len)
1261 goto process_hdr;
1262
1263 if (!htx_hdr_to_str(n, v, tmp))
1264 goto copy;
1265 }
1266 process_hdr:
1267 h1m->state = H1_MSG_HDR_NAME;
1268 n = htx_get_blk_name(chn_htx, blk);
1269 v = htx_get_blk_value(chn_htx, blk);
1270
1271 if (isteqi(n, ist("transfer-encoding")))
1272 h1_parse_xfer_enc_header(h1m, v);
1273 else if (isteqi(n, ist("connection"))) {
1274 h1_parse_connection_header(h1m, v);
1275 h1_process_conn_mode(h1s, h1m, NULL, &v);
1276 if (!v.len)
1277 goto skip_hdr;
1278 }
1279
1280 if (!htx_hdr_to_str(n, v, tmp))
1281 goto copy;
1282 skip_hdr:
1283 h1m->state = H1_MSG_HDR_L2_LWS;
1284 break;
1285
1286 case HTX_BLK_PHDR:
1287 /* not implemented yet */
1288 h1m->flags |= errflag;
1289 break;
1290
1291 case HTX_BLK_EOH:
1292 h1m->state = H1_MSG_LAST_LF;
1293 if (!chunk_memcat(tmp, "\r\n", 2))
1294 goto copy;
1295
1296 h1m->state = H1_MSG_DATA;
1297 break;
1298
1299 case HTX_BLK_DATA:
1300 v = htx_get_blk_value(chn_htx, blk);
1301 if (!htx_data_to_str(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
1302 goto copy;
1303 break;
1304
1305 case HTX_BLK_EOD:
1306 if (!chunk_memcat(tmp, "0\r\n", 3))
1307 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001308 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001309 h1m->state = H1_MSG_TRAILERS;
1310 break;
1311
1312 case HTX_BLK_TLR:
Christopher Faulet32188212018-11-20 18:21:43 +01001313 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1314 if (!chunk_memcat(tmp, "0\r\n", 3))
1315 goto copy;
1316 h1s->flags |= H1S_F_HAVE_EOD;
1317 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001318 v = htx_get_blk_value(chn_htx, blk);
1319 if (!htx_trailer_to_str(v, tmp))
1320 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001321 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001322 break;
1323
1324 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001325 if ((h1m->flags & H1_MF_CHNK)) {
1326 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1327 if (!chunk_memcat(tmp, "0\r\n", 3))
1328 goto copy;
1329 h1s->flags |= H1S_F_HAVE_EOD;
1330 }
1331 if (!(h1s->flags & H1S_F_HAVE_TLR)) {
1332 if (!chunk_memcat(tmp, "\r\n", 2))
1333 goto copy;
1334 h1s->flags |= H1S_F_HAVE_TLR;
1335 }
1336 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001337 h1m->state = H1_MSG_DONE;
1338 break;
1339
1340 case HTX_BLK_OOB:
1341 v = htx_get_blk_value(chn_htx, blk);
1342 if (!chunk_memcat(tmp, v.ptr, v.len))
1343 goto copy;
1344 break;
1345
1346 default:
1347 h1m->flags |= errflag;
1348 break;
1349 }
1350 total += sz;
1351 blk = htx_remove_blk(chn_htx, blk);
Christopher Faulet129817b2018-09-20 16:14:40 +02001352 }
1353
Christopher Faulet9768c262018-10-22 09:34:31 +02001354 copy:
1355 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001356
1357 if (b_full(&h1c->obuf))
1358 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001359 if (htx_is_empty(chn_htx)) {
1360 htx_reset(chn_htx);
1361 b_set_data(buf, 0);
1362 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001363 end:
1364 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001365}
1366
Christopher Faulet51dbc942018-09-13 09:05:15 +02001367/*********************************************************/
1368/* functions below are I/O callbacks from the connection */
1369/*********************************************************/
1370/*
1371 * Attempt to read data, and subscribe if none available
1372 */
1373static int h1_recv(struct h1c *h1c)
1374{
1375 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001376 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001377 size_t ret, max;
1378 int rcvd = 0;
1379
1380 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
1381 return 0;
1382
Christopher Faulet539e0292018-11-19 10:40:09 +01001383 if (!h1_recv_allowed(h1c))
Christopher Faulet9768c262018-10-22 09:34:31 +02001384 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001385
Christopher Fauletfeb11742018-11-29 15:12:34 +01001386 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001387 rcvd = 1;
1388 goto end;
1389 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001390
Christopher Faulet51dbc942018-09-13 09:05:15 +02001391 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1392 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001393 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001394 }
1395
1396 ret = 0;
1397 max = b_room(&h1c->ibuf);
1398 if (max) {
1399 h1c->flags &= ~H1C_F_IN_FULL;
1400 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
1401 }
Christopher Faulet47365272018-10-31 17:40:50 +01001402 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001403 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001404 if (h1s && h1s->cs) {
1405 h1s->cs->flags |= CS_FL_READ_PARTIAL;
1406 if (h1s->csinfo.t_idle == -1)
1407 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1408 }
Christopher Faulet47365272018-10-31 17:40:50 +01001409 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001410
1411 if (h1_recv_allowed(h1c))
1412 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet81d48432018-11-19 21:22:43 +01001413 else
1414 rcvd = 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001415
Christopher Faulet9768c262018-10-22 09:34:31 +02001416 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001417 if (!b_data(&h1c->ibuf))
1418 h1_release_buf(h1c, &h1c->ibuf);
1419 else if (b_full(&h1c->ibuf))
1420 h1c->flags |= H1C_F_IN_FULL;
1421 return rcvd;
1422}
1423
1424
1425/*
1426 * Try to send data if possible
1427 */
1428static int h1_send(struct h1c *h1c)
1429{
1430 struct connection *conn = h1c->conn;
1431 unsigned int flags = 0;
1432 size_t ret;
1433 int sent = 0;
1434
1435 if (conn->flags & CO_FL_ERROR)
1436 return 0;
1437
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001438 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1439 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1440 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1441 return 0;
1442 }
1443
Christopher Faulet51dbc942018-09-13 09:05:15 +02001444 if (!b_data(&h1c->obuf))
1445 goto end;
1446
1447 if (h1c->flags & H1C_F_OUT_FULL)
1448 flags |= CO_SFL_MSG_MORE;
1449
1450 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1451 if (ret > 0) {
1452 h1c->flags &= ~H1C_F_OUT_FULL;
1453 b_del(&h1c->obuf, ret);
1454 sent = 1;
1455 }
1456
1457 end:
1458 /* We're done, no more to send */
1459 if (!b_data(&h1c->obuf)) {
1460 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001461 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001462 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1463 h1_shutw_conn(conn);
1464 }
1465 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1466 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1467
1468 return sent;
1469}
1470
1471
1472static void h1_wake_stream(struct h1c *h1c)
1473{
1474 struct connection *conn = h1c->conn;
1475 struct h1s *h1s = h1c->h1s;
1476 uint32_t flags = 0;
1477 int dont_wake = 0;
1478
1479 if (!h1s || !h1s->cs)
1480 return;
1481
1482 if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR))
1483 flags |= CS_FL_ERROR;
1484 if (conn_xprt_read0_pending(conn))
1485 flags |= CS_FL_REOS;
1486
1487 h1s->cs->flags |= flags;
1488 if (h1s->recv_wait) {
1489 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1490 tasklet_wakeup(h1s->recv_wait->task);
1491 h1s->recv_wait = NULL;
1492 dont_wake = 1;
1493 }
1494 if (h1s->send_wait) {
1495 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1496 tasklet_wakeup(h1s->send_wait->task);
1497 h1s->send_wait = NULL;
1498 dont_wake = 1;
1499 }
1500 if (!dont_wake && h1s->cs->data_cb->wake)
1501 h1s->cs->data_cb->wake(h1s->cs);
1502}
1503
1504/* callback called on any event by the connection handler.
1505 * It applies changes and returns zero, or < 0 if it wants immediate
1506 * destruction of the connection.
1507 */
1508static int h1_process(struct h1c * h1c)
1509{
1510 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001511 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001512
Christopher Faulet51dbc942018-09-13 09:05:15 +02001513 if (!conn->mux_ctx)
1514 return -1;
1515
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001516 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001517 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1518 goto end;
1519 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001520 }
1521
Christopher Fauletfeb11742018-11-29 15:12:34 +01001522 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001523 if (h1c->flags & H1C_F_CS_ERROR ||
1524 conn->flags & CO_FL_ERROR ||
1525 conn_xprt_read0_pending(conn))
1526 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001527 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001528 if (!h1s_create(h1c, NULL))
1529 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001530 }
Christopher Fauletfeb11742018-11-29 15:12:34 +01001531 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001532 }
1533
Christopher Fauletfeb11742018-11-29 15:12:34 +01001534 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1535 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1536
Christopher Faulet539e0292018-11-19 10:40:09 +01001537 h1_wake_stream(h1c);
Christopher Faulet47365272018-10-31 17:40:50 +01001538 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001539 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001540
1541 release:
1542 h1_release(conn);
1543 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001544}
1545
1546static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1547{
1548 struct h1c *h1c = ctx;
1549 int ret = 0;
1550
1551 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1552 ret = h1_send(h1c);
1553 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1554 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001555 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001556 h1_process(h1c);
1557 return NULL;
1558}
1559
1560
1561static int h1_wake(struct connection *conn)
1562{
1563 struct h1c *h1c = conn->mux_ctx;
1564
Christopher Faulet539e0292018-11-19 10:40:09 +01001565 h1_send(h1c);
1566 return h1_process(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001567}
1568
Christopher Faulet51dbc942018-09-13 09:05:15 +02001569/*******************************************/
1570/* functions below are used by the streams */
1571/*******************************************/
1572/*
1573 * Attach a new stream to a connection
1574 * (Used for outgoing connections)
1575 */
1576static struct conn_stream *h1_attach(struct connection *conn)
1577{
1578 struct h1c *h1c = conn->mux_ctx;
1579 struct conn_stream *cs = NULL;
1580 struct h1s *h1s;
1581
1582 if (h1c->flags & H1C_F_CS_ERROR)
1583 goto end;
1584
1585 cs = cs_new(h1c->conn);
1586 if (!cs)
1587 goto end;
1588
Christopher Fauletf2824e62018-10-01 12:12:37 +02001589 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001590 if (h1s == NULL)
1591 goto end;
1592
1593 return cs;
1594 end:
1595 cs_free(cs);
1596 return NULL;
1597}
1598
1599/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1600 * this mux, it's easy as we can only store a single conn_stream.
1601 */
1602static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1603{
1604 struct h1c *h1c = conn->mux_ctx;
1605 struct h1s *h1s = h1c->h1s;
1606
1607 if (h1s)
1608 return h1s->cs;
1609
1610 return NULL;
1611}
1612
1613static void h1_destroy(struct connection *conn)
1614{
1615 struct h1c *h1c = conn->mux_ctx;
1616
1617 if (!h1c->h1s)
1618 h1_release(conn);
1619}
1620
1621/*
1622 * Detach the stream from the connection and possibly release the connection.
1623 */
1624static void h1_detach(struct conn_stream *cs)
1625{
1626 struct h1s *h1s = cs->ctx;
1627 struct h1c *h1c;
1628
1629 cs->ctx = NULL;
1630 if (!h1s)
1631 return;
1632
1633 h1c = h1s->h1c;
1634 h1s->cs = NULL;
1635
Christopher Faulet9400a392018-11-23 23:10:39 +01001636 if (conn_is_back(h1c->conn) && (h1s->flags & H1S_F_WANT_KAL)) {
1637 /* Never ever allow to reuse a connection from a non-reuse backend */
1638 if (h1c->conn && (h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
1639 h1c->conn->flags |= CO_FL_PRIVATE;
1640
1641 /* we're in keep-alive with an idle connection, monitor it if not already done */
1642 if (h1c->conn && LIST_ISEMPTY(&h1c->conn->list)) {
1643 struct server *srv = objt_server(h1c->conn->target);
1644
1645 if (srv) {
1646 if (h1c->conn->flags & CO_FL_PRIVATE)
1647 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
1648 else if (h1s->flags & H1S_F_NOT_FIRST)
1649 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1650 else
1651 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1652 }
1653 }
1654 }
1655
Christopher Faulet51dbc942018-09-13 09:05:15 +02001656 h1s_destroy(h1s);
1657
1658 /* We don't want to close right now unless the connection is in error */
1659 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
1660 (h1c->conn->flags & CO_FL_ERROR))
1661 h1_release(h1c->conn);
1662 else
1663 tasklet_wakeup(h1c->wait_event.task);
1664}
1665
1666
1667static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1668{
1669 struct h1s *h1s = cs->ctx;
1670
1671 if (!h1s)
1672 return;
1673
Christopher Fauletf2824e62018-10-01 12:12:37 +02001674 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1675 return;
1676
Christopher Faulet51dbc942018-09-13 09:05:15 +02001677 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1678 if (cs->flags & CS_FL_SHR)
1679 return;
1680 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1681 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1682 if (cs->flags & CS_FL_SHW) {
1683 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1684 conn_full_close(cs->conn);
1685 }
1686}
1687
1688static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1689{
1690 struct h1s *h1s = cs->ctx;
1691 struct h1c *h1c;
1692
1693 if (!h1s)
1694 return;
1695 h1c = h1s->h1c;
1696
Christopher Fauletf2824e62018-10-01 12:12:37 +02001697 if ((h1s->flags & H1S_F_WANT_KAL) &&
1698 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1699 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1700 return;
1701
Christopher Faulet51dbc942018-09-13 09:05:15 +02001702 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1703 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1704 return;
1705
1706 h1_shutw_conn(cs->conn);
1707}
1708
1709static void h1_shutw_conn(struct connection *conn)
1710{
1711 struct h1c *h1c = conn->mux_ctx;
1712
1713 if (conn_xprt_ready(conn) && conn->xprt->shutw)
1714 conn->xprt->shutw(conn, 1);
1715 if (!(conn->flags & CO_FL_SOCK_RD_SH))
1716 conn_sock_shutw(conn, 1);
1717 else {
1718 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1719 conn_full_close(conn);
1720 }
1721}
1722
1723/* Called from the upper layer, to unsubscribe to events */
1724static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1725{
1726 struct wait_event *sw;
1727 struct h1s *h1s = cs->ctx;
1728
1729 if (!h1s)
1730 return 0;
1731
1732 if (event_type & SUB_CAN_RECV) {
1733 sw = param;
1734 if (h1s->recv_wait == sw) {
1735 sw->wait_reason &= ~SUB_CAN_RECV;
1736 h1s->recv_wait = NULL;
1737 }
1738 }
1739 if (event_type & SUB_CAN_SEND) {
1740 sw = param;
1741 if (h1s->send_wait == sw) {
1742 sw->wait_reason &= ~SUB_CAN_SEND;
1743 h1s->send_wait = NULL;
1744 }
1745 }
1746 return 0;
1747}
1748
1749/* Called from the upper layer, to subscribe to events, such as being able to send */
1750static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1751{
1752 struct wait_event *sw;
1753 struct h1s *h1s = cs->ctx;
1754
1755 if (!h1s)
1756 return -1;
1757
1758 switch (event_type) {
1759 case SUB_CAN_RECV:
1760 sw = param;
1761 if (!(sw->wait_reason & SUB_CAN_RECV)) {
1762 sw->wait_reason |= SUB_CAN_RECV;
1763 sw->handle = h1s;
1764 h1s->recv_wait = sw;
1765 }
1766 return 0;
1767 case SUB_CAN_SEND:
1768 sw = param;
1769 if (!(sw->wait_reason & SUB_CAN_SEND)) {
1770 sw->wait_reason |= SUB_CAN_SEND;
1771 sw->handle = h1s;
1772 h1s->send_wait = sw;
1773 }
1774 return 0;
1775 default:
1776 break;
1777 }
1778 return -1;
1779}
1780
1781/* Called from the upper layer, to receive data */
1782static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1783{
1784 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01001785 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001786 size_t ret = 0;
1787
Christopher Faulet539e0292018-11-19 10:40:09 +01001788 if (!(h1c->flags & H1C_F_IN_ALLOC))
1789 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001790
1791 if (flags & CO_RFL_BUF_FLUSH)
1792 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001793 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
1794 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01001795 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1796 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001797 }
1798 return ret;
1799}
1800
1801
1802/* Called from the upper layer, to send data */
1803static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1804{
1805 struct h1s *h1s = cs->ctx;
1806 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001807 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001808
1809 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001810 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001811
1812 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001813 if (h1c->flags & H1C_F_CS_WAIT_CONN)
1814 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001815
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001816 while (total != count) {
1817 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001818
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001819 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
1820 ret = h1_process_output(h1c, buf, count);
1821 if (!ret)
1822 break;
1823 total += ret;
1824 if (!h1_send(h1c))
1825 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001826 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01001827
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001828 /* We need to do that because of the infinite forwarding. <buf>
1829 * contains HTX messages so when infinite forwarding is enabled,
1830 * count is equal to the buffer size. From outside, the buffer
1831 * appears as full.
1832 */
1833 if (!b_data(buf))
1834 total = count;
1835 else if (total != count) {
Christopher Fauletf96c3222018-11-20 18:38:01 +01001836 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1837 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1c->wait_event);
1838 }
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001839 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001840}
1841
Christopher Faulet1be55f92018-10-02 15:59:23 +02001842#if defined(CONFIG_HAP_LINUX_SPLICE)
1843/* Send and get, using splicing */
1844static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
1845{
1846 struct h1s *h1s = cs->ctx;
1847 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
1848 int ret = 0;
1849
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001850 if (b_data(&h1s->h1c->ibuf)) {
1851 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001852 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001853 }
1854
1855 h1s->flags &= ~H1S_F_BUF_FLUSH;
1856 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001857 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
1858 count = h1m->curr_len;
1859 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
1860 if (h1m->state == H1_MSG_DATA && ret > 0)
1861 h1m->curr_len -= ret;
1862 end:
1863 return ret;
1864
1865}
1866
1867static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
1868{
1869 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001870 int ret = 0;
1871
1872 if (b_data(&h1s->h1c->obuf))
1873 goto end;
1874
1875 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001876 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001877 if (pipe->data) {
1878 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
1879 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
1880 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001881 return ret;
1882}
1883#endif
1884
Christopher Faulet51dbc942018-09-13 09:05:15 +02001885/****************************************/
1886/* MUX initialization and instanciation */
1887/****************************************/
1888
1889/* The mux operations */
1890const struct mux_ops mux_h1_ops = {
1891 .init = h1_init,
1892 .wake = h1_wake,
1893 .attach = h1_attach,
1894 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01001895 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02001896 .detach = h1_detach,
1897 .destroy = h1_destroy,
1898 .avail_streams = h1_avail_streams,
1899 .rcv_buf = h1_rcv_buf,
1900 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02001901#if defined(CONFIG_HAP_LINUX_SPLICE)
1902 .rcv_pipe = h1_rcv_pipe,
1903 .snd_pipe = h1_snd_pipe,
1904#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02001905 .subscribe = h1_subscribe,
1906 .unsubscribe = h1_unsubscribe,
1907 .shutr = h1_shutr,
1908 .shutw = h1_shutw,
1909 .flags = MX_FL_NONE,
1910 .name = "h1",
1911};
1912
1913
1914/* this mux registers default HTX proto */
1915static struct mux_proto_list mux_proto_htx =
1916{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
1917
Willy Tarreau0108d902018-11-25 19:14:37 +01001918INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
1919
Christopher Faulet51dbc942018-09-13 09:05:15 +02001920/*
1921 * Local variables:
1922 * c-indent-level: 8
1923 * c-basic-offset: 8
1924 * End:
1925 */