blob: b01e5d768402f6e8e13fe90ce22c8e4f5b6964fe [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
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100209static int h1_max_streams(struct connection *conn)
210{
211 return 1;
212}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200213
214/*****************************************************************/
215/* functions below are dedicated to the mux setup and management */
216/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100217static struct conn_stream *h1s_new_cs(struct h1s *h1s)
218{
219 struct conn_stream *cs;
220
221 cs = cs_new(h1s->h1c->conn);
222 if (!cs)
223 goto err;
224 h1s->cs = cs;
225 cs->ctx = h1s;
226
227 if (h1s->flags & H1S_F_NOT_FIRST)
228 cs->flags |= CS_FL_NOT_FIRST;
229
230 if (stream_create_from_cs(cs) < 0)
231 goto err;
232 return cs;
233
234 err:
235 cs_free(cs);
236 h1s->cs = NULL;
237 return NULL;
238}
239
Christopher Fauletf2824e62018-10-01 12:12:37 +0200240static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200241{
242 struct h1s *h1s;
243
244 h1s = pool_alloc(pool_head_h1s);
245 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100246 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200247
248 h1s->h1c = h1c;
249 h1c->h1s = h1s;
250
251 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200252 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200253
254 h1s->recv_wait = NULL;
255 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200256
257 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200258 h1s->req.flags |= H1_MF_NO_PHDR;
259
Christopher Faulet129817b2018-09-20 16:14:40 +0200260 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200261 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200262
263 h1s->status = 0;
264 h1s->meth = HTTP_METH_OTHER;
265
Christopher Faulet47365272018-10-31 17:40:50 +0100266 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
267 h1s->flags |= H1S_F_NOT_FIRST;
268 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
269
Christopher Faulet129817b2018-09-20 16:14:40 +0200270 if (!conn_is_back(h1c->conn)) {
271 if (h1c->px->options2 & PR_O2_REQBUG_OK)
272 h1s->req.err_pos = -1;
273 }
274 else {
275 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
276 h1s->res.err_pos = -1;
277 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200278
Christopher Faulet539e0292018-11-19 10:40:09 +0100279 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
280 * create a new one.
281 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200282 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100283 h1s->csinfo.create_date = date;
284 h1s->csinfo.tv_create = now;
285 h1s->csinfo.t_handshake = 0;
286 h1s->csinfo.t_idle = -1;
287
Christopher Fauletf2824e62018-10-01 12:12:37 +0200288 cs->ctx = h1s;
289 h1s->cs = cs;
290 }
Christopher Faulet47365272018-10-31 17:40:50 +0100291 else {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100292 struct session *sess = h1c->conn->owner;
293
294 h1s->csinfo.create_date = sess->accept_date;
295 h1s->csinfo.tv_create = sess->tv_accept;
296 h1s->csinfo.t_handshake = sess->t_handshake;
297 h1s->csinfo.t_idle = -1;
298
Christopher Faulet47365272018-10-31 17:40:50 +0100299 cs = h1s_new_cs(h1s);
300 if (!cs)
301 goto fail;
302 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200303 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100304
305 fail:
306 pool_free(pool_head_h1s, h1s);
307 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200308}
309
310static void h1s_destroy(struct h1s *h1s)
311{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200312 if (h1s) {
313 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200314
Christopher Fauletf2824e62018-10-01 12:12:37 +0200315 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200316
Christopher Fauletf2824e62018-10-01 12:12:37 +0200317 if (h1s->recv_wait != NULL)
318 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
319 if (h1s->send_wait != NULL)
320 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
321
Christopher Faulet47365272018-10-31 17:40:50 +0100322 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
323 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
324 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200325
Christopher Fauletf2824e62018-10-01 12:12:37 +0200326 cs_free(h1s->cs);
327 pool_free(pool_head_h1s, h1s);
328 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200329}
330
Christopher Fauletfeb11742018-11-29 15:12:34 +0100331static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
332{
333 struct h1s *h1s = cs->ctx;
334
335 if (h1s && !conn_is_back(cs->conn))
336 return &h1s->csinfo;
337 return NULL;
338}
339
Christopher Faulet51dbc942018-09-13 09:05:15 +0200340/*
341 * Initialize the mux once it's attached. It is expected that conn->mux_ctx
342 * points to the existing conn_stream (for outgoing connections) or NULL (for
343 * incoming ones). Returns < 0 on error.
344 */
345static int h1_init(struct connection *conn, struct proxy *proxy)
346{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200347 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200348
349 h1c = pool_alloc(pool_head_h1c);
350 if (!h1c)
351 goto fail_h1c;
352 h1c->conn = conn;
353 h1c->px = proxy;
354
355 h1c->flags = H1C_F_NONE;
356 h1c->ibuf = BUF_NULL;
357 h1c->obuf = BUF_NULL;
358 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200359
Christopher Faulet51dbc942018-09-13 09:05:15 +0200360 LIST_INIT(&h1c->buf_wait.list);
361 h1c->wait_event.task = tasklet_new();
362 if (!h1c->wait_event.task)
363 goto fail;
364 h1c->wait_event.task->process = h1_io_cb;
365 h1c->wait_event.task->context = h1c;
366 h1c->wait_event.wait_reason = 0;
367
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200368 if (!(conn->flags & CO_FL_CONNECTED))
369 h1c->flags |= H1C_F_CS_WAIT_CONN;
370
Christopher Fauletf2824e62018-10-01 12:12:37 +0200371 /* Always Create a new H1S */
372 if (!h1s_create(h1c, conn->mux_ctx))
373 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200374
Christopher Faulet129817b2018-09-20 16:14:40 +0200375 conn->mux_ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200376
Christopher Faulet51dbc942018-09-13 09:05:15 +0200377 /* Try to read, if nothing is available yet we'll just subscribe */
378 if (h1_recv(h1c))
379 h1_process(h1c);
380
381 /* mux->wake will be called soon to complete the operation */
382 return 0;
383
384 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100385 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200386 tasklet_free(h1c->wait_event.task);
387 pool_free(pool_head_h1c, h1c);
388 fail_h1c:
389 return -1;
390}
391
392
393/* release function for a connection. This one should be called to free all
394 * resources allocated to the mux.
395 */
396static void h1_release(struct connection *conn)
397{
398 struct h1c *h1c = conn->mux_ctx;
399
400 LIST_DEL(&conn->list);
401
402 if (h1c) {
403 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
404 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
405 LIST_DEL(&h1c->buf_wait.list);
406 LIST_INIT(&h1c->buf_wait.list);
407 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
408 }
409
410 h1_release_buf(h1c, &h1c->ibuf);
411 h1_release_buf(h1c, &h1c->obuf);
412
Christopher Faulet51dbc942018-09-13 09:05:15 +0200413 if (h1c->wait_event.task)
414 tasklet_free(h1c->wait_event.task);
415
Christopher Fauletf2824e62018-10-01 12:12:37 +0200416 h1s_destroy(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200417 if (h1c->wait_event.wait_reason != 0)
418 conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason,
419 &h1c->wait_event);
420 pool_free(pool_head_h1c, h1c);
421 }
422
423 conn->mux = NULL;
424 conn->mux_ctx = NULL;
425
426 conn_stop_tracking(conn);
427 conn_full_close(conn);
428 if (conn->destroy_cb)
429 conn->destroy_cb(conn);
430 conn_free(conn);
431}
432
433/******************************************************/
434/* functions below are for the H1 protocol processing */
435/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200436/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
437 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200438 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100439static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200440{
Christopher Faulet570d1612018-11-26 11:13:57 +0100441 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200442
Christopher Faulet570d1612018-11-26 11:13:57 +0100443 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200444 (*(p + 5) > '1' ||
445 (*(p + 5) == '1' && *(p + 7) >= '1')))
446 h1m->flags |= H1_MF_VER_11;
447}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200448
Christopher Faulet9768c262018-10-22 09:34:31 +0200449/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
450 * greater or equal to 1.1
451 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100452static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200453{
Christopher Faulet570d1612018-11-26 11:13:57 +0100454 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200455
Christopher Faulet570d1612018-11-26 11:13:57 +0100456 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200457 (*(p + 5) > '1' ||
458 (*(p + 5) == '1' && *(p + 7) >= '1')))
459 h1m->flags |= H1_MF_VER_11;
460}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200461
Christopher Faulet9768c262018-10-22 09:34:31 +0200462/*
463 * Check the validity of the request version. If the version is valid, it
464 * returns 1. Otherwise, it returns 0.
465 */
466static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
467{
468 struct h1c *h1c = h1s->h1c;
469
470 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
471 * exactly one digit "." one digit. This check may be disabled using
472 * option accept-invalid-http-request.
473 */
474 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
475 if (sl.rq.v.len != 8)
476 return 0;
477
478 if (*(sl.rq.v.ptr + 4) != '/' ||
479 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
480 *(sl.rq.v.ptr + 6) != '.' ||
481 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
482 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200483 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200484 else if (!sl.rq.v.len) {
485 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200486
Christopher Faulet9768c262018-10-22 09:34:31 +0200487 /* RFC 1945 allows only GET for HTTP/0.9 requests */
488 if (sl.rq.meth != HTTP_METH_GET)
489 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200490
Christopher Faulet9768c262018-10-22 09:34:31 +0200491 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
492 if (!sl.rq.u.len)
493 return 0;
494
495 /* Add HTTP version */
496 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100497 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200498 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100499
500 if ((sl.rq.v.len == 8) &&
501 ((*(sl.rq.v.ptr + 5) > '1') ||
502 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
503 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200504 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200505}
506
Christopher Faulet9768c262018-10-22 09:34:31 +0200507/*
508 * Check the validity of the response version. If the version is valid, it
509 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200510 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200511static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200512{
Christopher Faulet9768c262018-10-22 09:34:31 +0200513 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200514
Christopher Faulet9768c262018-10-22 09:34:31 +0200515 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
516 * exactly one digit "." one digit. This check may be disabled using
517 * option accept-invalid-http-request.
518 */
519 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
520 if (sl.st.v.len != 8)
521 return 0;
522
523 if (*(sl.st.v.ptr + 4) != '/' ||
524 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
525 *(sl.st.v.ptr + 6) != '.' ||
526 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
527 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200528 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100529
530 if ((sl.st.v.len == 8) &&
531 ((*(sl.st.v.ptr + 5) > '1') ||
532 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
533 h1m->flags |= H1_MF_VER_11;
534
Christopher Faulet9768c262018-10-22 09:34:31 +0200535 return 1;
536}
537/* Remove all "Connection:" headers from the HTX message <htx> */
538static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
539{
540 struct ist hdr = {.ptr = "Connection", .len = 10};
541 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200542
Christopher Faulet9768c262018-10-22 09:34:31 +0200543 while (http_find_header(htx, hdr, &ctx, 1))
544 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200545
Christopher Faulet9768c262018-10-22 09:34:31 +0200546 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
547}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200548
Christopher Faulet9768c262018-10-22 09:34:31 +0200549/* Add a "Connection:" header with the value <value> into the HTX message
550 * <htx>.
551 */
552static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
553{
554 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200555
Christopher Faulet9768c262018-10-22 09:34:31 +0200556 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200557}
558
559/* Deduce the connection mode of the client connection, depending on the
560 * configuration and the H1 message flags. This function is called twice, the
561 * first time when the request is parsed and the second time when the response
562 * is parsed.
563 */
564static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
565{
566 struct proxy *fe = h1s->h1c->px;
567 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
568
569 /* Tunnel mode can only by set on the frontend */
570 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
571 flag = H1S_F_WANT_TUN;
572 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
573 flag = H1S_F_WANT_CLO;
574
575 /* flags order: CLO > SCL > TUN > KAL */
576 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
577 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
578
579 if (h1m->flags & H1_MF_RESP) {
580 /* Either we've established an explicit tunnel, or we're
581 * switching the protocol. In both cases, we're very unlikely to
582 * understand the next protocols. We have to switch to tunnel
583 * mode, so that we transfer the request and responses then let
584 * this protocol pass unmodified. When we later implement
585 * specific parsers for such protocols, we'll want to check the
586 * Upgrade header which contains information about that protocol
587 * for responses with status 101 (eg: see RFC2817 about TLS).
588 */
589 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
590 h1s->status == 101)
591 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletbd44ca62018-11-29 09:55:22 +0100592 else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */
593 (h1m->flags & H1_MF_CONN_CLO && h1s->req.state != H1_MSG_DONE)) /*explicit close and unfinished request */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200594 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
595 }
596 else {
597 if (h1s->flags & H1S_F_WANT_KAL &&
598 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
599 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
600 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
601 }
602
603 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
604 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
605 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
606}
607
608/* Deduce the connection mode of the client connection, depending on the
609 * configuration and the H1 message flags. This function is called twice, the
610 * first time when the request is parsed and the second time when the response
611 * is parsed.
612 */
613static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
614{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100615 struct h1c *h1c = h1s->h1c;
616 struct session *sess = h1c->conn->owner;
617 struct proxy *fe = sess->fe;
618 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200619 int flag = H1S_F_WANT_KAL;
620
621 /* Tunnel mode can only by set on the frontend */
622 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
623 flag = H1S_F_WANT_TUN;
624
625 /* For the server connection: server-close == httpclose */
626 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
627 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
628 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
629 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
630 flag = H1S_F_WANT_CLO;
631
632 /* flags order: CLO > SCL > TUN > KAL */
633 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
634 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
635
636 if (h1m->flags & H1_MF_RESP) {
637 /* Either we've established an explicit tunnel, or we're
638 * switching the protocol. In both cases, we're very unlikely to
639 * understand the next protocols. We have to switch to tunnel
640 * mode, so that we transfer the request and responses then let
641 * this protocol pass unmodified. When we later implement
642 * specific parsers for such protocols, we'll want to check the
643 * Upgrade header which contains information about that protocol
644 * for responses with status 101 (eg: see RFC2817 about TLS).
645 */
646 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
647 h1s->status == 101)
648 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
649 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
650 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
651 else if (h1s->flags & H1S_F_WANT_KAL &&
652 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
653 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
654 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
655 }
656
657 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
658 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
659 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200660}
661
Christopher Faulet9768c262018-10-22 09:34:31 +0200662static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
663 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200664{
665 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200666
667 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
668 * token is found
669 */
670 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200671 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200672
673 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200674 if (h1m->flags & H1_MF_CONN_CLO) {
675 if (conn_val)
676 *conn_val = ist("");
677 if (htx)
678 h1_remove_conn_hdrs(h1m, htx);
679 }
680 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
681 if (conn_val)
682 *conn_val = ist("keep-alive");
683 if (htx)
684 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
685 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200686 }
687 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200688 if (h1m->flags & H1_MF_CONN_KAL) {
689 if (conn_val)
690 *conn_val = ist("");
691 if (htx)
692 h1_remove_conn_hdrs(h1m, htx);
693 }
694 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
695 if (conn_val)
696 *conn_val = ist("close");
697 if (htx)
698 h1_add_conn_hdr(h1m, htx, ist("close"));
699 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200700 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200701}
702
Christopher Faulet9768c262018-10-22 09:34:31 +0200703static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
704 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200705{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200706 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
707 * token is found
708 */
709 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200710 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200711
712 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200713 if (h1m->flags & H1_MF_CONN_CLO) {
714 if (conn_val)
715 *conn_val = ist("");
716 if (htx)
717 h1_remove_conn_hdrs(h1m, htx);
718 }
719 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
720 if (conn_val)
721 *conn_val = ist("keep-alive");
722 if (htx)
723 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
724 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200725 }
726 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200727 if (h1m->flags & H1_MF_CONN_KAL) {
728 if (conn_val)
729 *conn_val = ist("");
730 if (htx)
731 h1_remove_conn_hdrs(h1m, htx);
732 }
733 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
734 if (conn_val)
735 *conn_val = ist("close");
736 if (htx)
737 h1_add_conn_hdr(h1m, htx, ist("close"));
738 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200739 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200740}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200741
Christopher Faulet9768c262018-10-22 09:34:31 +0200742/* Set the right connection mode and update "Connection:" header if
743 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
744 * message is updated accordingly. When <conn_val> is not NULL, it is set with
745 * the new header value.
746 */
747static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
748 struct htx *htx, struct ist *conn_val)
749{
750 if (!conn_is_back(h1s->h1c->conn)) {
751 h1_set_cli_conn_mode(h1s, h1m);
752 if (h1m->flags & H1_MF_RESP)
753 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
754 }
755 else {
756 h1_set_srv_conn_mode(h1s, h1m);
757 if (!(h1m->flags & H1_MF_RESP))
758 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
759 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200760}
761
Christopher Faulete44769b2018-11-29 23:01:45 +0100762
763/* Append the description of what is present in error snapshot <es> into <out>.
764 * The description must be small enough to always fit in a buffer. The output
765 * buffer may be the trash so the trash must not be used inside this function.
766 */
767static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
768{
769 chunk_appendf(out,
770 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
771 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
772 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
773 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
774 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
775 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
776}
777/*
778 * Capture a bad request or response and archive it in the proxy's structure.
779 * By default it tries to report the error position as h1m->err_pos. However if
780 * this one is not set, it will then report h1m->next, which is the last known
781 * parsing point. The function is able to deal with wrapping buffers. It always
782 * displays buffers as a contiguous area starting at buf->p. The direction is
783 * determined thanks to the h1m's flags.
784 */
785static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
786 struct h1m *h1m, struct buffer *buf)
787{
788 struct session *sess = h1c->conn->owner;
789 struct proxy *proxy = h1c->px;
790 struct proxy *other_end = sess->fe;
791 union error_snapshot_ctx ctx;
792
793 if (h1s->cs->data)
794 other_end = si_strm(h1s->cs->data)->be;
795
796 /* http-specific part now */
797 ctx.h1.state = h1m->state;
798 ctx.h1.c_flags = h1c->flags;
799 ctx.h1.s_flags = h1s->flags;
800 ctx.h1.m_flags = h1m->flags;
801 ctx.h1.m_clen = h1m->curr_len;
802 ctx.h1.m_blen = h1m->body_len;
803
804 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
805 h1c->conn->target, sess, buf, 0, 0,
806 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
807 &ctx, h1_show_error_snapshot);
808}
809
Christopher Faulet129817b2018-09-20 16:14:40 +0200810/*
811 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200812 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
813 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800814 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200815 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200816static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200817 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200818{
819 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100820 union h1_sl h1sl;
821 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200822 int ret = 0;
823
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100824 if (!max)
825 goto end;
826
Christopher Faulet129817b2018-09-20 16:14:40 +0200827 /* Realing input buffer if necessary */
828 if (b_head(buf) + b_data(buf) > b_wrap(buf))
829 b_slow_realign(buf, trash.area, 0);
830
Christopher Fauletf2824e62018-10-01 12:12:37 +0200831 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100832 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200833 if (ret <= 0) {
834 /* Incomplete or invalid message. If the buffer is full, it's an
835 * error because headers are too large to be handled by the
836 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200837 if (ret < 0 || (!ret && b_full(buf)))
838 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200839 goto end;
840 }
841
842 /* messages headers fully parsed, do some checks to prepare the body
843 * parsing.
844 */
845
846 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200847 if (ret > (b_size(buf) - global.tune.maxrewrite))
848 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200849
Christopher Faulet9768c262018-10-22 09:34:31 +0200850 /* Save the request's method or the response's status, check if the body
851 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200852 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100853 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200854
Christopher Faulet129817b2018-09-20 16:14:40 +0200855 /* Request have always a known length */
856 h1m->flags |= H1_MF_XFER_LEN;
857 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
858 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200859
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100860 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
861 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200862 h1m->err_state = h1m->state;
863 goto vsn_error;
864 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200865 }
866 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100867 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200868
869 if ((h1s->meth == HTTP_METH_HEAD) ||
870 (h1s->status >= 100 && h1s->status < 200) ||
871 (h1s->status == 204) || (h1s->status == 304) ||
872 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
873 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
874 h1m->flags |= H1_MF_XFER_LEN;
875 h1m->curr_len = h1m->body_len = 0;
876 h1m->state = H1_MSG_DONE;
877 }
878 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
879 h1m->flags |= H1_MF_XFER_LEN;
880 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
881 h1m->state = H1_MSG_DONE;
882 }
883 else
884 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200885
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100886 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
887 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200888 h1m->err_state = h1m->state;
889 goto vsn_error;
890 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200891 }
892
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100893 /* Set HTX start-line flags */
894 if (h1m->flags & H1_MF_VER_11)
895 flags |= HTX_SL_F_VER_11;
896 if (h1m->flags & H1_MF_XFER_ENC)
897 flags |= HTX_SL_F_XFER_ENC;
898 if (h1m->flags & H1_MF_XFER_LEN) {
899 flags |= HTX_SL_F_XFER_LEN;
900 if (h1m->flags & H1_MF_CHNK)
901 flags |= HTX_SL_F_CHNK;
902 else if (h1m->flags & H1_MF_CLEN)
903 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100904 if (h1m->state == H1_MSG_DONE)
905 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100906 }
907
Christopher Faulet9768c262018-10-22 09:34:31 +0200908 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100909 struct htx_sl *sl;
910
911 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
912 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200913 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100914 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200915 }
916 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100917 struct htx_sl *sl;
918
919 flags |= HTX_SL_F_IS_RESP;
920 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
921 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200922 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100923 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200924 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100925
Christopher Faulet9768c262018-10-22 09:34:31 +0200926 if (h1m->state == H1_MSG_DONE)
927 if (!htx_add_endof(htx, HTX_BLK_EOM))
928 goto error;
929
930 h1_process_conn_mode(h1s, h1m, htx, NULL);
931
932 /* If body length cannot be determined, set htx->extra to
933 * ULLONG_MAX. This value is impossible in other cases.
934 */
935 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
936
937 /* Recheck there is enough space to do headers rewritting */
938 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
939 goto error;
940
941 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200942 end:
943 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200944
945 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +0200946 h1m->err_state = h1m->state;
947 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +0200948 vsn_error:
949 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +0100950 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200951 ret = 0;
952 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200953}
954
955/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200956 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
957 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +0200958 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800959 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200960 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200961static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200962 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200963{
Christopher Faulet9768c262018-10-22 09:34:31 +0200964 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +0200965 size_t total = 0;
966 int ret = 0;
967
968 if (h1m->flags & H1_MF_XFER_LEN) {
969 if (h1m->flags & H1_MF_CLEN) {
970 /* content-length: read only h2m->body_len */
971 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200972 if (ret > data_space)
973 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200974 if ((uint64_t)ret > h1m->curr_len)
975 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200976 if (ret > b_contig_data(buf, *ofs))
977 ret = b_contig_data(buf, *ofs);
978 if (ret) {
979 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
980 goto end;
981 h1m->curr_len -= ret;
982 *ofs += ret;
983 total += ret;
984 }
985
986 if (!h1m->curr_len) {
987 if (!htx_add_endof(htx, HTX_BLK_EOM))
988 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200989 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200990 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200991 }
992 else if (h1m->flags & H1_MF_CHNK) {
993 new_chunk:
994 /* te:chunked : parse chunks */
995 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200996 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +0200997 if (ret <= 0)
998 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200999 h1m->state = H1_MSG_CHUNK_SIZE;
1000
Christopher Faulet129817b2018-09-20 16:14:40 +02001001 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001002 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001003 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001004 }
1005
1006 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1007 unsigned int chksz;
1008
Christopher Fauletf2824e62018-10-01 12:12:37 +02001009 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001010 if (ret <= 0)
1011 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001012 if (!chksz) {
1013 if (!htx_add_endof(htx, HTX_BLK_EOD))
1014 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001015 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001016 h1m->state = H1_MSG_TRAILERS;
1017 }
1018 else
1019 h1m->state = H1_MSG_DATA;
1020
Christopher Faulet129817b2018-09-20 16:14:40 +02001021 h1m->curr_len = chksz;
1022 h1m->body_len += chksz;
1023 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001024 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001026 }
1027
1028 if (h1m->state == H1_MSG_DATA) {
1029 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001030 if (ret > data_space)
1031 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001032 if ((uint64_t)ret > h1m->curr_len)
1033 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001034 if (ret > b_contig_data(buf, *ofs))
1035 ret = b_contig_data(buf, *ofs);
1036 if (ret) {
1037 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1038 goto end;
1039 h1m->curr_len -= ret;
1040 max -= ret;
1041 *ofs += ret;
1042 total += ret;
1043 }
1044 if (!h1m->curr_len) {
1045 h1m->state = H1_MSG_CHUNK_CRLF;
1046 goto new_chunk;
1047 }
1048 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001049 }
1050
1051 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001052 /* Trailers were alread parsed, only the EOM
1053 * need to be added */
1054 if (h1s->flags & H1S_F_HAVE_TLR)
1055 goto skip_tlr_parsing;
1056
Christopher Fauletf2824e62018-10-01 12:12:37 +02001057 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001058 if (ret > data_space)
1059 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001060 if (ret <= 0)
1061 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001062
1063 /* Realing input buffer if tailers wrap. For now
1064 * this is a workaroung. Because trailers are
1065 * not split on CRLF, like headers, there is no
1066 * way to know where to split it when trailers
1067 * wrap. This is a limitation of
1068 * h1_measure_trailers.
1069 */
1070 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1071 b_slow_realign(buf, trash.area, 0);
1072
1073 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1074 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001075 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001076 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001077 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001078 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001079
Christopher Faulet17276482018-11-22 11:44:35 +01001080 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001081 if (!htx_add_endof(htx, HTX_BLK_EOM))
1082 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001083 h1m->state = H1_MSG_DONE;
1084 }
1085 }
1086 else {
1087 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1088 * is no body. Switch the message in DONE state
1089 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001090 if (!htx_add_endof(htx, HTX_BLK_EOM))
1091 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001092 h1m->state = H1_MSG_DONE;
1093 }
1094 }
1095 else {
1096 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001097 ret = max;
1098 if (ret > data_space)
1099 ret = data_space;
1100 if (ret > b_contig_data(buf, *ofs))
1101 ret = b_contig_data(buf, *ofs);
1102 if (ret) {
1103 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1104 goto end;
1105
1106 *ofs += max;
1107 total = max;
1108 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001109 }
1110
1111 end:
1112 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001113 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001114 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001115 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001116 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001117 return 0;
1118 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001119 /* update htx->extra, only when the body length is known */
1120 if (h1m->flags & H1_MF_XFER_LEN)
1121 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001122 return total;
1123}
1124
1125/*
1126 * Synchronize the request and the response before reseting them. Except for 1xx
1127 * responses, we wait that the request and the response are in DONE state and
1128 * that all data are forwarded for both. For 1xx responses, only the response is
1129 * reset, waiting the final one. Many 1xx messages can be sent.
1130 */
1131static void h1_sync_messages(struct h1c *h1c)
1132{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001133 struct h1s *h1s = h1c->h1s;
1134
1135 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001136 return;
1137
Christopher Fauletf2824e62018-10-01 12:12:37 +02001138 if (h1s->res.state == H1_MSG_DONE &&
1139 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001140 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001141 /* For 100-Continue response or any other informational 1xx
1142 * response which is non-final, don't reset the request, the
1143 * transaction is not finished. We take care the response was
1144 * transferred before.
1145 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001146 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001147 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001148 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001149 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001150 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1151 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001152 h1m_init_req(&h1s->req);
1153 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001154 h1s->req.state = H1_MSG_TUNNEL;
1155 h1s->res.state = H1_MSG_TUNNEL;
1156 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001157 }
1158}
1159
1160/*
1161 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001162 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1163 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001164 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001165static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001166{
Christopher Faulet539e0292018-11-19 10:40:09 +01001167 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001168 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001169 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001170 size_t total = 0;
1171 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001172 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001173 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001174
Christopher Faulet539e0292018-11-19 10:40:09 +01001175 htx = htx_from_buf(buf);
1176 count = b_data(&h1c->ibuf);
1177 max = htx_free_space(htx);
1178 if (flags & CO_RFL_KEEP_RSV) {
1179 if (max < global.tune.maxrewrite)
1180 goto end;
1181 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001182 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001183 if (count > max)
1184 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001185
Christopher Fauletf2824e62018-10-01 12:12:37 +02001186 if (!conn_is_back(h1c->conn)) {
1187 h1m = &h1s->req;
1188 errflag = H1S_F_REQ_ERROR;
1189 }
1190 else {
1191 h1m = &h1s->res;
1192 errflag = H1S_F_RES_ERROR;
1193 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001194
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001195 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001196 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001197 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001198 if (!ret)
1199 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001200 }
1201 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001202 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001203 if (!ret)
1204 break;
1205 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001206 else if (h1m->state == H1_MSG_DONE)
1207 break;
1208 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001209 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet9768c262018-10-22 09:34:31 +02001210 if (!ret)
1211 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001212 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001213 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001214 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001215 break;
1216 }
1217
Christopher Faulet539e0292018-11-19 10:40:09 +01001218 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001219 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001220
Christopher Faulet47365272018-10-31 17:40:50 +01001221 if (h1s->flags & errflag)
1222 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001223
Christopher Faulet539e0292018-11-19 10:40:09 +01001224 b_del(&h1c->ibuf, total);
1225
1226 end:
1227 if (htx_is_not_empty(htx))
1228 b_set_data(buf, b_size(buf));
1229 else {
1230 htx_reset(htx);
1231 b_set_data(buf, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001232 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001233
Christopher Faulet539e0292018-11-19 10:40:09 +01001234 if (h1c->flags & H1C_F_IN_FULL && b_room(&h1c->ibuf)) {
1235 h1c->flags &= ~H1C_F_IN_FULL;
1236 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001237 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001238
Christopher Faulet9c388402018-11-19 21:54:26 +01001239 if (b_data(&h1c->ibuf)) {
1240 if (!htx_is_empty(htx))
1241 h1s->cs->flags |= CS_FL_RCV_MORE;
1242 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001243 else {
1244 h1_release_buf(h1c, &h1c->ibuf);
1245 h1_sync_messages(h1c);
1246
1247 h1s->cs->flags &= ~CS_FL_RCV_MORE;
1248 if (h1s->cs->flags & CS_FL_REOS)
1249 h1s->cs->flags |= CS_FL_EOS;
1250 }
1251 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001252
1253 parsing_err:
1254 // FIXME: create an error snapshot here
1255 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001256 htx->flags |= HTX_FL_PARSING_ERROR;
1257 b_set_data(buf, b_size(buf));
1258 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001259 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001260}
1261
Christopher Faulet129817b2018-09-20 16:14:40 +02001262/*
1263 * Process outgoing data. It parses data and transfer them from the channel buffer into
1264 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1265 * 0 if it couldn't proceed.
1266 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001267static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1268{
Christopher Faulet129817b2018-09-20 16:14:40 +02001269 struct h1s *h1s = h1c->h1s;
1270 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001271 struct htx *chn_htx;
1272 struct htx_blk *blk;
1273 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001274 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001275 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001276 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001277
Christopher Faulet47365272018-10-31 17:40:50 +01001278 if (!count)
1279 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001280 chn_htx = htx_from_buf(buf);
1281
Christopher Faulet51dbc942018-09-13 09:05:15 +02001282 if (!h1_get_buf(h1c, &h1c->obuf)) {
1283 h1c->flags |= H1C_F_OUT_ALLOC;
1284 goto end;
1285 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001286
Christopher Fauletf2824e62018-10-01 12:12:37 +02001287 if (!conn_is_back(h1c->conn)) {
1288 h1m = &h1s->res;
1289 errflag = H1S_F_RES_ERROR;
1290 }
1291 else {
1292 h1m = &h1s->req;
1293 errflag = H1S_F_REQ_ERROR;
1294 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001295
1296
1297 tmp = get_trash_chunk();
1298 tmp->size = b_room(&h1c->obuf);
1299
1300 blk = htx_get_head_blk(chn_htx);
1301 while (!(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001302 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001303 struct ist n, v;
1304 uint32_t sz = htx_get_blksz(blk);
1305
1306 if (total + sz > count)
1307 goto copy;
1308
1309 switch (htx_get_blk_type(blk)) {
1310 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001311 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001312
1313 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001314 h1m_init_req(h1m);
1315 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001316 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001317 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001318 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001319 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001320 goto copy;
1321 h1m->flags |= H1_MF_XFER_LEN;
1322 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001323 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001324
Christopher Faulet9768c262018-10-22 09:34:31 +02001325 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001326 h1m_init_res(h1m);
1327 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001328 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001329 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001330 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001331 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001332 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001333 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001335 if (sl->info.res.status < 200 &&
1336 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1337 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001338 h1m->state = H1_MSG_HDR_FIRST;
1339 break;
1340
1341 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001342 h1m->state = H1_MSG_HDR_NAME;
1343 n = htx_get_blk_name(chn_htx, blk);
1344 v = htx_get_blk_value(chn_htx, blk);
1345
1346 if (isteqi(n, ist("transfer-encoding")))
1347 h1_parse_xfer_enc_header(h1m, v);
1348 else if (isteqi(n, ist("connection"))) {
1349 h1_parse_connection_header(h1m, v);
1350 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001351 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001352 if (!v.len)
1353 goto skip_hdr;
1354 }
1355
Christopher Fauletc59ff232018-12-03 13:58:44 +01001356 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001357 goto copy;
1358 skip_hdr:
1359 h1m->state = H1_MSG_HDR_L2_LWS;
1360 break;
1361
1362 case HTX_BLK_PHDR:
1363 /* not implemented yet */
1364 h1m->flags |= errflag;
1365 break;
1366
1367 case HTX_BLK_EOH:
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001368 if (h1m->state == H1_MSG_HDR_L2_LWS && process_conn_mode) {
1369 /* There is no "Connection:" header and
1370 * it the conn_mode must be
1371 * processed. So do it */
1372 n = ist("Connection");
1373 v = ist("");
1374 h1_process_conn_mode(h1s, h1m, NULL, &v);
1375 process_conn_mode = 0;
1376 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001377 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001378 goto copy;
1379 }
1380 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001381 h1m->state = H1_MSG_LAST_LF;
1382 if (!chunk_memcat(tmp, "\r\n", 2))
1383 goto copy;
1384
1385 h1m->state = H1_MSG_DATA;
1386 break;
1387
1388 case HTX_BLK_DATA:
1389 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001390 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001391 goto copy;
1392 break;
1393
1394 case HTX_BLK_EOD:
1395 if (!chunk_memcat(tmp, "0\r\n", 3))
1396 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001397 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001398 h1m->state = H1_MSG_TRAILERS;
1399 break;
1400
1401 case HTX_BLK_TLR:
Christopher Faulet32188212018-11-20 18:21:43 +01001402 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1403 if (!chunk_memcat(tmp, "0\r\n", 3))
1404 goto copy;
1405 h1s->flags |= H1S_F_HAVE_EOD;
1406 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001407 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001408 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001409 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001410 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001411 break;
1412
1413 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001414 if ((h1m->flags & H1_MF_CHNK)) {
1415 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1416 if (!chunk_memcat(tmp, "0\r\n", 3))
1417 goto copy;
1418 h1s->flags |= H1S_F_HAVE_EOD;
1419 }
1420 if (!(h1s->flags & H1S_F_HAVE_TLR)) {
1421 if (!chunk_memcat(tmp, "\r\n", 2))
1422 goto copy;
1423 h1s->flags |= H1S_F_HAVE_TLR;
1424 }
1425 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001426 h1m->state = H1_MSG_DONE;
1427 break;
1428
1429 case HTX_BLK_OOB:
1430 v = htx_get_blk_value(chn_htx, blk);
1431 if (!chunk_memcat(tmp, v.ptr, v.len))
1432 goto copy;
1433 break;
1434
1435 default:
1436 h1m->flags |= errflag;
1437 break;
1438 }
1439 total += sz;
1440 blk = htx_remove_blk(chn_htx, blk);
Christopher Faulet129817b2018-09-20 16:14:40 +02001441 }
1442
Christopher Faulet9768c262018-10-22 09:34:31 +02001443 copy:
1444 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001445
1446 if (b_full(&h1c->obuf))
1447 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001448 if (htx_is_empty(chn_htx)) {
1449 htx_reset(chn_htx);
1450 b_set_data(buf, 0);
1451 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001452 end:
1453 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001454}
1455
Christopher Faulet51dbc942018-09-13 09:05:15 +02001456/*********************************************************/
1457/* functions below are I/O callbacks from the connection */
1458/*********************************************************/
1459/*
1460 * Attempt to read data, and subscribe if none available
1461 */
1462static int h1_recv(struct h1c *h1c)
1463{
1464 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001465 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001466 size_t ret, max;
1467 int rcvd = 0;
1468
1469 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
1470 return 0;
1471
Christopher Faulet539e0292018-11-19 10:40:09 +01001472 if (!h1_recv_allowed(h1c))
Christopher Faulet9768c262018-10-22 09:34:31 +02001473 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001474
Christopher Fauletfeb11742018-11-29 15:12:34 +01001475 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001476 rcvd = 1;
1477 goto end;
1478 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001479
Christopher Faulet51dbc942018-09-13 09:05:15 +02001480 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1481 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001482 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001483 }
1484
1485 ret = 0;
1486 max = b_room(&h1c->ibuf);
1487 if (max) {
1488 h1c->flags &= ~H1C_F_IN_FULL;
1489 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
1490 }
Christopher Faulet47365272018-10-31 17:40:50 +01001491 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001492 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001493 if (h1s && h1s->cs) {
1494 h1s->cs->flags |= CS_FL_READ_PARTIAL;
1495 if (h1s->csinfo.t_idle == -1)
1496 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1497 }
Christopher Faulet47365272018-10-31 17:40:50 +01001498 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001499
1500 if (h1_recv_allowed(h1c))
1501 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet81d48432018-11-19 21:22:43 +01001502 else
1503 rcvd = 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001504
Christopher Faulet9768c262018-10-22 09:34:31 +02001505 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001506 if (!b_data(&h1c->ibuf))
1507 h1_release_buf(h1c, &h1c->ibuf);
1508 else if (b_full(&h1c->ibuf))
1509 h1c->flags |= H1C_F_IN_FULL;
1510 return rcvd;
1511}
1512
1513
1514/*
1515 * Try to send data if possible
1516 */
1517static int h1_send(struct h1c *h1c)
1518{
1519 struct connection *conn = h1c->conn;
1520 unsigned int flags = 0;
1521 size_t ret;
1522 int sent = 0;
1523
1524 if (conn->flags & CO_FL_ERROR)
1525 return 0;
1526
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001527 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1528 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1529 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1530 return 0;
1531 }
1532
Christopher Faulet51dbc942018-09-13 09:05:15 +02001533 if (!b_data(&h1c->obuf))
1534 goto end;
1535
1536 if (h1c->flags & H1C_F_OUT_FULL)
1537 flags |= CO_SFL_MSG_MORE;
1538
1539 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1540 if (ret > 0) {
1541 h1c->flags &= ~H1C_F_OUT_FULL;
1542 b_del(&h1c->obuf, ret);
1543 sent = 1;
1544 }
1545
1546 end:
1547 /* We're done, no more to send */
1548 if (!b_data(&h1c->obuf)) {
1549 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001550 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001551 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1552 h1_shutw_conn(conn);
1553 }
1554 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1555 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1556
1557 return sent;
1558}
1559
1560
1561static void h1_wake_stream(struct h1c *h1c)
1562{
1563 struct connection *conn = h1c->conn;
1564 struct h1s *h1s = h1c->h1s;
1565 uint32_t flags = 0;
1566 int dont_wake = 0;
1567
1568 if (!h1s || !h1s->cs)
1569 return;
1570
1571 if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR))
1572 flags |= CS_FL_ERROR;
1573 if (conn_xprt_read0_pending(conn))
1574 flags |= CS_FL_REOS;
1575
1576 h1s->cs->flags |= flags;
1577 if (h1s->recv_wait) {
1578 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1579 tasklet_wakeup(h1s->recv_wait->task);
1580 h1s->recv_wait = NULL;
1581 dont_wake = 1;
1582 }
1583 if (h1s->send_wait) {
1584 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1585 tasklet_wakeup(h1s->send_wait->task);
1586 h1s->send_wait = NULL;
1587 dont_wake = 1;
1588 }
1589 if (!dont_wake && h1s->cs->data_cb->wake)
1590 h1s->cs->data_cb->wake(h1s->cs);
1591}
1592
1593/* callback called on any event by the connection handler.
1594 * It applies changes and returns zero, or < 0 if it wants immediate
1595 * destruction of the connection.
1596 */
1597static int h1_process(struct h1c * h1c)
1598{
1599 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001600 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001601
Christopher Faulet51dbc942018-09-13 09:05:15 +02001602 if (!conn->mux_ctx)
1603 return -1;
1604
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001605 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001606 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1607 goto end;
1608 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001609 }
1610
Christopher Fauletfeb11742018-11-29 15:12:34 +01001611 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001612 if (h1c->flags & H1C_F_CS_ERROR ||
1613 conn->flags & CO_FL_ERROR ||
1614 conn_xprt_read0_pending(conn))
1615 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001616 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001617 if (!h1s_create(h1c, NULL))
1618 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001619 }
Christopher Fauletfeb11742018-11-29 15:12:34 +01001620 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001621 }
1622
Christopher Fauletfeb11742018-11-29 15:12:34 +01001623 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1624 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1625
Christopher Faulet539e0292018-11-19 10:40:09 +01001626 h1_wake_stream(h1c);
Christopher Faulet47365272018-10-31 17:40:50 +01001627 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001628 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001629
1630 release:
1631 h1_release(conn);
1632 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001633}
1634
1635static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1636{
1637 struct h1c *h1c = ctx;
1638 int ret = 0;
1639
1640 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1641 ret = h1_send(h1c);
1642 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1643 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001644 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001645 h1_process(h1c);
1646 return NULL;
1647}
1648
1649
1650static int h1_wake(struct connection *conn)
1651{
1652 struct h1c *h1c = conn->mux_ctx;
1653
Christopher Faulet539e0292018-11-19 10:40:09 +01001654 h1_send(h1c);
1655 return h1_process(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001656}
1657
Christopher Faulet51dbc942018-09-13 09:05:15 +02001658/*******************************************/
1659/* functions below are used by the streams */
1660/*******************************************/
1661/*
1662 * Attach a new stream to a connection
1663 * (Used for outgoing connections)
1664 */
1665static struct conn_stream *h1_attach(struct connection *conn)
1666{
1667 struct h1c *h1c = conn->mux_ctx;
1668 struct conn_stream *cs = NULL;
1669 struct h1s *h1s;
1670
1671 if (h1c->flags & H1C_F_CS_ERROR)
1672 goto end;
1673
1674 cs = cs_new(h1c->conn);
1675 if (!cs)
1676 goto end;
1677
Christopher Fauletf2824e62018-10-01 12:12:37 +02001678 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001679 if (h1s == NULL)
1680 goto end;
1681
1682 return cs;
1683 end:
1684 cs_free(cs);
1685 return NULL;
1686}
1687
1688/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1689 * this mux, it's easy as we can only store a single conn_stream.
1690 */
1691static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1692{
1693 struct h1c *h1c = conn->mux_ctx;
1694 struct h1s *h1s = h1c->h1s;
1695
1696 if (h1s)
1697 return h1s->cs;
1698
1699 return NULL;
1700}
1701
1702static void h1_destroy(struct connection *conn)
1703{
1704 struct h1c *h1c = conn->mux_ctx;
1705
1706 if (!h1c->h1s)
1707 h1_release(conn);
1708}
1709
1710/*
1711 * Detach the stream from the connection and possibly release the connection.
1712 */
1713static void h1_detach(struct conn_stream *cs)
1714{
1715 struct h1s *h1s = cs->ctx;
1716 struct h1c *h1c;
1717
1718 cs->ctx = NULL;
1719 if (!h1s)
1720 return;
1721
1722 h1c = h1s->h1c;
1723 h1s->cs = NULL;
1724
Christopher Faulet9400a392018-11-23 23:10:39 +01001725 if (conn_is_back(h1c->conn) && (h1s->flags & H1S_F_WANT_KAL)) {
1726 /* Never ever allow to reuse a connection from a non-reuse backend */
1727 if (h1c->conn && (h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
1728 h1c->conn->flags |= CO_FL_PRIVATE;
1729
1730 /* we're in keep-alive with an idle connection, monitor it if not already done */
1731 if (h1c->conn && LIST_ISEMPTY(&h1c->conn->list)) {
1732 struct server *srv = objt_server(h1c->conn->target);
1733
1734 if (srv) {
1735 if (h1c->conn->flags & CO_FL_PRIVATE)
1736 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
1737 else if (h1s->flags & H1S_F_NOT_FIRST)
1738 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1739 else
1740 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1741 }
1742 }
1743 }
1744
Christopher Faulet51dbc942018-09-13 09:05:15 +02001745 h1s_destroy(h1s);
1746
1747 /* We don't want to close right now unless the connection is in error */
1748 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
1749 (h1c->conn->flags & CO_FL_ERROR))
1750 h1_release(h1c->conn);
1751 else
1752 tasklet_wakeup(h1c->wait_event.task);
1753}
1754
1755
1756static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1757{
1758 struct h1s *h1s = cs->ctx;
1759
1760 if (!h1s)
1761 return;
1762
Christopher Fauletf2824e62018-10-01 12:12:37 +02001763 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1764 return;
1765
Christopher Faulet51dbc942018-09-13 09:05:15 +02001766 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1767 if (cs->flags & CS_FL_SHR)
1768 return;
1769 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1770 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1771 if (cs->flags & CS_FL_SHW) {
1772 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1773 conn_full_close(cs->conn);
1774 }
1775}
1776
1777static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1778{
1779 struct h1s *h1s = cs->ctx;
1780 struct h1c *h1c;
1781
1782 if (!h1s)
1783 return;
1784 h1c = h1s->h1c;
1785
Christopher Fauletf2824e62018-10-01 12:12:37 +02001786 if ((h1s->flags & H1S_F_WANT_KAL) &&
1787 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1788 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1789 return;
1790
Christopher Faulet51dbc942018-09-13 09:05:15 +02001791 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1792 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1793 return;
1794
1795 h1_shutw_conn(cs->conn);
1796}
1797
1798static void h1_shutw_conn(struct connection *conn)
1799{
1800 struct h1c *h1c = conn->mux_ctx;
1801
1802 if (conn_xprt_ready(conn) && conn->xprt->shutw)
1803 conn->xprt->shutw(conn, 1);
1804 if (!(conn->flags & CO_FL_SOCK_RD_SH))
1805 conn_sock_shutw(conn, 1);
1806 else {
1807 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1808 conn_full_close(conn);
1809 }
1810}
1811
1812/* Called from the upper layer, to unsubscribe to events */
1813static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1814{
1815 struct wait_event *sw;
1816 struct h1s *h1s = cs->ctx;
1817
1818 if (!h1s)
1819 return 0;
1820
1821 if (event_type & SUB_CAN_RECV) {
1822 sw = param;
1823 if (h1s->recv_wait == sw) {
1824 sw->wait_reason &= ~SUB_CAN_RECV;
1825 h1s->recv_wait = NULL;
1826 }
1827 }
1828 if (event_type & SUB_CAN_SEND) {
1829 sw = param;
1830 if (h1s->send_wait == sw) {
1831 sw->wait_reason &= ~SUB_CAN_SEND;
1832 h1s->send_wait = NULL;
1833 }
1834 }
1835 return 0;
1836}
1837
1838/* Called from the upper layer, to subscribe to events, such as being able to send */
1839static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1840{
1841 struct wait_event *sw;
1842 struct h1s *h1s = cs->ctx;
1843
1844 if (!h1s)
1845 return -1;
1846
1847 switch (event_type) {
1848 case SUB_CAN_RECV:
1849 sw = param;
1850 if (!(sw->wait_reason & SUB_CAN_RECV)) {
1851 sw->wait_reason |= SUB_CAN_RECV;
1852 sw->handle = h1s;
1853 h1s->recv_wait = sw;
1854 }
1855 return 0;
1856 case SUB_CAN_SEND:
1857 sw = param;
1858 if (!(sw->wait_reason & SUB_CAN_SEND)) {
1859 sw->wait_reason |= SUB_CAN_SEND;
1860 sw->handle = h1s;
1861 h1s->send_wait = sw;
1862 }
1863 return 0;
1864 default:
1865 break;
1866 }
1867 return -1;
1868}
1869
1870/* Called from the upper layer, to receive data */
1871static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1872{
1873 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01001874 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001875 size_t ret = 0;
1876
Christopher Faulet539e0292018-11-19 10:40:09 +01001877 if (!(h1c->flags & H1C_F_IN_ALLOC))
1878 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001879
1880 if (flags & CO_RFL_BUF_FLUSH)
1881 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001882 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
1883 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01001884 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1885 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001886 }
1887 return ret;
1888}
1889
1890
1891/* Called from the upper layer, to send data */
1892static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1893{
1894 struct h1s *h1s = cs->ctx;
1895 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001896 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001897
1898 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001899 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001900
1901 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001902 if (h1c->flags & H1C_F_CS_WAIT_CONN)
1903 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001904
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001905 while (total != count) {
1906 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001907
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001908 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
1909 ret = h1_process_output(h1c, buf, count);
1910 if (!ret)
1911 break;
1912 total += ret;
1913 if (!h1_send(h1c))
1914 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001915 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01001916
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001917 /* We need to do that because of the infinite forwarding. <buf>
1918 * contains HTX messages so when infinite forwarding is enabled,
1919 * count is equal to the buffer size. From outside, the buffer
1920 * appears as full.
1921 */
1922 if (!b_data(buf))
1923 total = count;
1924 else if (total != count) {
Christopher Fauletf96c3222018-11-20 18:38:01 +01001925 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1926 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1c->wait_event);
1927 }
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001928 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001929}
1930
Christopher Faulet1be55f92018-10-02 15:59:23 +02001931#if defined(CONFIG_HAP_LINUX_SPLICE)
1932/* Send and get, using splicing */
1933static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
1934{
1935 struct h1s *h1s = cs->ctx;
1936 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
1937 int ret = 0;
1938
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001939 if (b_data(&h1s->h1c->ibuf)) {
1940 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001941 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001942 }
1943
1944 h1s->flags &= ~H1S_F_BUF_FLUSH;
1945 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001946 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
1947 count = h1m->curr_len;
1948 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
1949 if (h1m->state == H1_MSG_DATA && ret > 0)
1950 h1m->curr_len -= ret;
1951 end:
1952 return ret;
1953
1954}
1955
1956static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
1957{
1958 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001959 int ret = 0;
1960
1961 if (b_data(&h1s->h1c->obuf))
1962 goto end;
1963
1964 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001965 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001966 if (pipe->data) {
1967 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
1968 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
1969 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001970 return ret;
1971}
1972#endif
1973
Christopher Faulet51dbc942018-09-13 09:05:15 +02001974/****************************************/
1975/* MUX initialization and instanciation */
1976/****************************************/
1977
1978/* The mux operations */
1979const struct mux_ops mux_h1_ops = {
1980 .init = h1_init,
1981 .wake = h1_wake,
1982 .attach = h1_attach,
1983 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01001984 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02001985 .detach = h1_detach,
1986 .destroy = h1_destroy,
1987 .avail_streams = h1_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01001988 .max_streams = h1_max_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02001989 .rcv_buf = h1_rcv_buf,
1990 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02001991#if defined(CONFIG_HAP_LINUX_SPLICE)
1992 .rcv_pipe = h1_rcv_pipe,
1993 .snd_pipe = h1_snd_pipe,
1994#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02001995 .subscribe = h1_subscribe,
1996 .unsubscribe = h1_unsubscribe,
1997 .shutr = h1_shutr,
1998 .shutw = h1_shutw,
1999 .flags = MX_FL_NONE,
2000 .name = "h1",
2001};
2002
2003
2004/* this mux registers default HTX proto */
2005static struct mux_proto_list mux_proto_htx =
2006{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2007
Willy Tarreau0108d902018-11-25 19:14:37 +01002008INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2009
Christopher Faulet51dbc942018-09-13 09:05:15 +02002010/*
2011 * Local variables:
2012 * c-indent-level: 8
2013 * c-basic-offset: 8
2014 * End:
2015 */