blob: c3293409dcc0aac55364355579d3710dbea9af25 [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) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100501 ((*(sl.rq.v.ptr + 5) > '1') ||
502 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100503 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) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100531 ((*(sl.st.v.ptr + 5) > '1') ||
532 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100533 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 }
Christopher Faulet70033782018-12-05 13:50:11 +0100656 else {
657 if (h1s->flags & H1S_F_WANT_KAL &&
658 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
659 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
660 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
661 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200662
663 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
664 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
665 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200666}
667
Christopher Faulet9768c262018-10-22 09:34:31 +0200668static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
669 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200670{
671 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200672
673 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
674 * token is found
675 */
676 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200677 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200678
679 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200680 if (h1m->flags & H1_MF_CONN_CLO) {
681 if (conn_val)
682 *conn_val = ist("");
683 if (htx)
684 h1_remove_conn_hdrs(h1m, htx);
685 }
686 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
687 if (conn_val)
688 *conn_val = ist("keep-alive");
689 if (htx)
690 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
691 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200692 }
693 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200694 if (h1m->flags & H1_MF_CONN_KAL) {
695 if (conn_val)
696 *conn_val = ist("");
697 if (htx)
698 h1_remove_conn_hdrs(h1m, htx);
699 }
700 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
701 if (conn_val)
702 *conn_val = ist("close");
703 if (htx)
704 h1_add_conn_hdr(h1m, htx, ist("close"));
705 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200706 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200707}
708
Christopher Faulet9768c262018-10-22 09:34:31 +0200709static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
710 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200711{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200712 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
713 * token is found
714 */
715 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200716 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200717
718 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200719 if (h1m->flags & H1_MF_CONN_CLO) {
720 if (conn_val)
721 *conn_val = ist("");
722 if (htx)
723 h1_remove_conn_hdrs(h1m, htx);
724 }
725 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
726 if (conn_val)
727 *conn_val = ist("keep-alive");
728 if (htx)
729 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
730 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200731 }
732 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200733 if (h1m->flags & H1_MF_CONN_KAL) {
734 if (conn_val)
735 *conn_val = ist("");
736 if (htx)
737 h1_remove_conn_hdrs(h1m, htx);
738 }
739 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
740 if (conn_val)
741 *conn_val = ist("close");
742 if (htx)
743 h1_add_conn_hdr(h1m, htx, ist("close"));
744 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200745 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200746}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200747
Christopher Faulet9768c262018-10-22 09:34:31 +0200748/* Set the right connection mode and update "Connection:" header if
749 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
750 * message is updated accordingly. When <conn_val> is not NULL, it is set with
751 * the new header value.
752 */
753static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
754 struct htx *htx, struct ist *conn_val)
755{
756 if (!conn_is_back(h1s->h1c->conn)) {
757 h1_set_cli_conn_mode(h1s, h1m);
758 if (h1m->flags & H1_MF_RESP)
759 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
760 }
761 else {
762 h1_set_srv_conn_mode(h1s, h1m);
763 if (!(h1m->flags & H1_MF_RESP))
764 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
765 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200766}
767
Christopher Faulete44769b2018-11-29 23:01:45 +0100768
769/* Append the description of what is present in error snapshot <es> into <out>.
770 * The description must be small enough to always fit in a buffer. The output
771 * buffer may be the trash so the trash must not be used inside this function.
772 */
773static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
774{
775 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100776 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
777 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
778 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
779 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
780 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
781 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100782}
783/*
784 * Capture a bad request or response and archive it in the proxy's structure.
785 * By default it tries to report the error position as h1m->err_pos. However if
786 * this one is not set, it will then report h1m->next, which is the last known
787 * parsing point. The function is able to deal with wrapping buffers. It always
788 * displays buffers as a contiguous area starting at buf->p. The direction is
789 * determined thanks to the h1m's flags.
790 */
791static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
792 struct h1m *h1m, struct buffer *buf)
793{
794 struct session *sess = h1c->conn->owner;
795 struct proxy *proxy = h1c->px;
796 struct proxy *other_end = sess->fe;
797 union error_snapshot_ctx ctx;
798
799 if (h1s->cs->data)
800 other_end = si_strm(h1s->cs->data)->be;
801
802 /* http-specific part now */
803 ctx.h1.state = h1m->state;
804 ctx.h1.c_flags = h1c->flags;
805 ctx.h1.s_flags = h1s->flags;
806 ctx.h1.m_flags = h1m->flags;
807 ctx.h1.m_clen = h1m->curr_len;
808 ctx.h1.m_blen = h1m->body_len;
809
810 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
811 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100812 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
813 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100814}
815
Christopher Faulet129817b2018-09-20 16:14:40 +0200816/*
817 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200818 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
819 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800820 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200821 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200822static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200823 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200824{
825 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100826 union h1_sl h1sl;
827 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200828 int ret = 0;
829
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100830 if (!max)
831 goto end;
832
Christopher Faulet129817b2018-09-20 16:14:40 +0200833 /* Realing input buffer if necessary */
834 if (b_head(buf) + b_data(buf) > b_wrap(buf))
835 b_slow_realign(buf, trash.area, 0);
836
Christopher Fauletf2824e62018-10-01 12:12:37 +0200837 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100838 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200839 if (ret <= 0) {
840 /* Incomplete or invalid message. If the buffer is full, it's an
841 * error because headers are too large to be handled by the
842 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200843 if (ret < 0 || (!ret && b_full(buf)))
844 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200845 goto end;
846 }
847
848 /* messages headers fully parsed, do some checks to prepare the body
849 * parsing.
850 */
851
852 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200853 if (ret > (b_size(buf) - global.tune.maxrewrite))
854 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200855
Christopher Faulet9768c262018-10-22 09:34:31 +0200856 /* Save the request's method or the response's status, check if the body
857 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200858 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100859 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200860
Christopher Faulet129817b2018-09-20 16:14:40 +0200861 /* Request have always a known length */
862 h1m->flags |= H1_MF_XFER_LEN;
863 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
864 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200865
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100866 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
867 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200868 h1m->err_state = h1m->state;
869 goto vsn_error;
870 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200871 }
872 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100873 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200874
875 if ((h1s->meth == HTTP_METH_HEAD) ||
876 (h1s->status >= 100 && h1s->status < 200) ||
877 (h1s->status == 204) || (h1s->status == 304) ||
878 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
879 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
880 h1m->flags |= H1_MF_XFER_LEN;
881 h1m->curr_len = h1m->body_len = 0;
882 h1m->state = H1_MSG_DONE;
883 }
884 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
885 h1m->flags |= H1_MF_XFER_LEN;
886 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
887 h1m->state = H1_MSG_DONE;
888 }
889 else
890 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200891
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100892 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
893 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200894 h1m->err_state = h1m->state;
895 goto vsn_error;
896 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200897 }
898
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100899 /* Set HTX start-line flags */
900 if (h1m->flags & H1_MF_VER_11)
901 flags |= HTX_SL_F_VER_11;
902 if (h1m->flags & H1_MF_XFER_ENC)
903 flags |= HTX_SL_F_XFER_ENC;
904 if (h1m->flags & H1_MF_XFER_LEN) {
905 flags |= HTX_SL_F_XFER_LEN;
906 if (h1m->flags & H1_MF_CHNK)
907 flags |= HTX_SL_F_CHNK;
908 else if (h1m->flags & H1_MF_CLEN)
909 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100910 if (h1m->state == H1_MSG_DONE)
911 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100912 }
913
Christopher Faulet9768c262018-10-22 09:34:31 +0200914 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100915 struct htx_sl *sl;
916
917 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
918 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200919 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100920 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200921 }
922 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100923 struct htx_sl *sl;
924
925 flags |= HTX_SL_F_IS_RESP;
926 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
927 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200928 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100929 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200930 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100931
Christopher Faulet9768c262018-10-22 09:34:31 +0200932 if (h1m->state == H1_MSG_DONE)
933 if (!htx_add_endof(htx, HTX_BLK_EOM))
934 goto error;
935
936 h1_process_conn_mode(h1s, h1m, htx, NULL);
937
938 /* If body length cannot be determined, set htx->extra to
939 * ULLONG_MAX. This value is impossible in other cases.
940 */
941 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
942
943 /* Recheck there is enough space to do headers rewritting */
944 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
945 goto error;
946
947 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200948 end:
949 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200950
951 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +0200952 h1m->err_state = h1m->state;
953 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +0200954 vsn_error:
955 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +0100956 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200957 ret = 0;
958 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200959}
960
961/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200962 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
963 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +0200964 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800965 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200966 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200967static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100968 struct buffer *buf, size_t *ofs, size_t max,
969 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +0200970{
Christopher Faulet9768c262018-10-22 09:34:31 +0200971 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +0200972 size_t total = 0;
973 int ret = 0;
974
975 if (h1m->flags & H1_MF_XFER_LEN) {
976 if (h1m->flags & H1_MF_CLEN) {
977 /* content-length: read only h2m->body_len */
978 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200979 if (ret > data_space)
980 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200981 if ((uint64_t)ret > h1m->curr_len)
982 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200983 if (ret > b_contig_data(buf, *ofs))
984 ret = b_contig_data(buf, *ofs);
985 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +0100986 /* very often with large files we'll face the following
987 * situation :
988 * - htx is empty and points to <htxbuf>
989 * - ret == buf->data
990 * - buf->head == sizeof(struct htx)
991 * => we can swap the buffers and place an htx header into
992 * the target buffer instead
993 */
994 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
995 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
996 void *raw_area = buf->area;
997 void *htx_area = htxbuf->area;
998 struct htx_blk *blk;
999
1000 buf->area = htx_area;
1001 htxbuf->area = raw_area;
1002 htx = (struct htx *)htxbuf->area;
1003 htx->size = htxbuf->size - sizeof(*htx);
1004 htx_reset(htx);
1005 b_set_data(htxbuf, b_size(htxbuf));
1006
1007 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1008 blk->info += ret;
1009 /* nothing else to do, the old buffer now contains an
1010 * empty pre-initialized HTX header
1011 */
1012 }
1013 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001014 goto end;
1015 h1m->curr_len -= ret;
1016 *ofs += ret;
1017 total += ret;
1018 }
1019
1020 if (!h1m->curr_len) {
1021 if (!htx_add_endof(htx, HTX_BLK_EOM))
1022 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001023 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +02001024 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 }
1026 else if (h1m->flags & H1_MF_CHNK) {
1027 new_chunk:
1028 /* te:chunked : parse chunks */
1029 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001030 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001031 if (ret <= 0)
1032 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001033 h1m->state = H1_MSG_CHUNK_SIZE;
1034
Christopher Faulet129817b2018-09-20 16:14:40 +02001035 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001036 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001037 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001038 }
1039
1040 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1041 unsigned int chksz;
1042
Christopher Fauletf2824e62018-10-01 12:12:37 +02001043 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001044 if (ret <= 0)
1045 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001046 if (!chksz) {
1047 if (!htx_add_endof(htx, HTX_BLK_EOD))
1048 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001049 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001050 h1m->state = H1_MSG_TRAILERS;
1051 }
1052 else
1053 h1m->state = H1_MSG_DATA;
1054
Christopher Faulet129817b2018-09-20 16:14:40 +02001055 h1m->curr_len = chksz;
1056 h1m->body_len += chksz;
1057 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001058 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001059 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001060 }
1061
1062 if (h1m->state == H1_MSG_DATA) {
1063 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001064 if (ret > data_space)
1065 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001066 if ((uint64_t)ret > h1m->curr_len)
1067 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001068 if (ret > b_contig_data(buf, *ofs))
1069 ret = b_contig_data(buf, *ofs);
1070 if (ret) {
1071 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1072 goto end;
1073 h1m->curr_len -= ret;
1074 max -= ret;
1075 *ofs += ret;
1076 total += ret;
1077 }
1078 if (!h1m->curr_len) {
1079 h1m->state = H1_MSG_CHUNK_CRLF;
1080 goto new_chunk;
1081 }
1082 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001083 }
1084
1085 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001086 /* Trailers were alread parsed, only the EOM
1087 * need to be added */
1088 if (h1s->flags & H1S_F_HAVE_TLR)
1089 goto skip_tlr_parsing;
1090
Christopher Fauletf2824e62018-10-01 12:12:37 +02001091 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001092 if (ret > data_space)
1093 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001094 if (ret <= 0)
1095 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001096
1097 /* Realing input buffer if tailers wrap. For now
1098 * this is a workaroung. Because trailers are
1099 * not split on CRLF, like headers, there is no
1100 * way to know where to split it when trailers
1101 * wrap. This is a limitation of
1102 * h1_measure_trailers.
1103 */
1104 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1105 b_slow_realign(buf, trash.area, 0);
1106
1107 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1108 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001109 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001110 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001111 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001112 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001113
Christopher Faulet17276482018-11-22 11:44:35 +01001114 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001115 if (!htx_add_endof(htx, HTX_BLK_EOM))
1116 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001117 h1m->state = H1_MSG_DONE;
1118 }
1119 }
1120 else {
1121 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1122 * is no body. Switch the message in DONE state
1123 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001124 if (!htx_add_endof(htx, HTX_BLK_EOM))
1125 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001126 h1m->state = H1_MSG_DONE;
1127 }
1128 }
1129 else {
1130 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001131 ret = max;
1132 if (ret > data_space)
1133 ret = data_space;
1134 if (ret > b_contig_data(buf, *ofs))
1135 ret = b_contig_data(buf, *ofs);
1136 if (ret) {
1137 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1138 goto end;
1139
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001140 *ofs += ret;
1141 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001142 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001143 }
1144
1145 end:
1146 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001147 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001148 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001149 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001150 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001151 return 0;
1152 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001153 /* update htx->extra, only when the body length is known */
1154 if (h1m->flags & H1_MF_XFER_LEN)
1155 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001156 return total;
1157}
1158
1159/*
1160 * Synchronize the request and the response before reseting them. Except for 1xx
1161 * responses, we wait that the request and the response are in DONE state and
1162 * that all data are forwarded for both. For 1xx responses, only the response is
1163 * reset, waiting the final one. Many 1xx messages can be sent.
1164 */
1165static void h1_sync_messages(struct h1c *h1c)
1166{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001167 struct h1s *h1s = h1c->h1s;
1168
1169 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001170 return;
1171
Christopher Fauletf2824e62018-10-01 12:12:37 +02001172 if (h1s->res.state == H1_MSG_DONE &&
1173 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001174 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001175 /* For 100-Continue response or any other informational 1xx
1176 * response which is non-final, don't reset the request, the
1177 * transaction is not finished. We take care the response was
1178 * transferred before.
1179 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001180 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001181 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001182 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001183 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001184 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1185 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001186 h1m_init_req(&h1s->req);
1187 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001188 h1s->req.state = H1_MSG_TUNNEL;
1189 h1s->res.state = H1_MSG_TUNNEL;
1190 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001191 }
1192}
1193
1194/*
1195 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001196 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1197 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001198 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001199static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001200{
Christopher Faulet539e0292018-11-19 10:40:09 +01001201 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001202 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001203 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001204 size_t total = 0;
1205 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001206 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001207 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001208
Christopher Faulet539e0292018-11-19 10:40:09 +01001209 htx = htx_from_buf(buf);
1210 count = b_data(&h1c->ibuf);
1211 max = htx_free_space(htx);
1212 if (flags & CO_RFL_KEEP_RSV) {
1213 if (max < global.tune.maxrewrite)
1214 goto end;
1215 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001216 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001217 if (count > max)
1218 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001219
Christopher Fauletf2824e62018-10-01 12:12:37 +02001220 if (!conn_is_back(h1c->conn)) {
1221 h1m = &h1s->req;
1222 errflag = H1S_F_REQ_ERROR;
1223 }
1224 else {
1225 h1m = &h1s->res;
1226 errflag = H1S_F_RES_ERROR;
1227 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001228
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001229 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001230 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001231 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001232 if (!ret)
1233 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001234 }
1235 else if (h1m->state <= H1_MSG_TRAILERS) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001236 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1237 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001238 if (!ret)
1239 break;
1240 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001241 else if (h1m->state == H1_MSG_DONE)
1242 break;
1243 else if (h1m->state == H1_MSG_TUNNEL) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001244 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1245 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001246 if (!ret)
1247 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001248 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001249 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001250 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001251 break;
1252 }
1253
Christopher Faulet539e0292018-11-19 10:40:09 +01001254 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001255 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001256
Christopher Faulet47365272018-10-31 17:40:50 +01001257 if (h1s->flags & errflag)
1258 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001259
Christopher Faulet539e0292018-11-19 10:40:09 +01001260 b_del(&h1c->ibuf, total);
1261
1262 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001263 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001264
Willy Tarreau45f2b892018-12-05 07:59:27 +01001265 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001266 h1c->flags &= ~H1C_F_IN_FULL;
1267 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001268 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001269
Christopher Faulet9c388402018-11-19 21:54:26 +01001270 if (b_data(&h1c->ibuf)) {
1271 if (!htx_is_empty(htx))
Olivier Houchardd247be02018-12-06 16:22:29 +01001272 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet9c388402018-11-19 21:54:26 +01001273 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001274 else {
1275 h1_release_buf(h1c, &h1c->ibuf);
1276 h1_sync_messages(h1c);
1277
Olivier Houchardd247be02018-12-06 16:22:29 +01001278 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet539e0292018-11-19 10:40:09 +01001279 if (h1s->cs->flags & CS_FL_REOS)
1280 h1s->cs->flags |= CS_FL_EOS;
1281 }
1282 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001283
1284 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001285 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001286 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001287 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001288 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001289 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001290}
1291
Christopher Faulet129817b2018-09-20 16:14:40 +02001292/*
1293 * Process outgoing data. It parses data and transfer them from the channel buffer into
1294 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1295 * 0 if it couldn't proceed.
1296 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001297static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1298{
Christopher Faulet129817b2018-09-20 16:14:40 +02001299 struct h1s *h1s = h1c->h1s;
1300 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001301 struct htx *chn_htx;
1302 struct htx_blk *blk;
1303 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001304 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001305 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001306 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001307
Christopher Faulet47365272018-10-31 17:40:50 +01001308 if (!count)
1309 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001310 chn_htx = htx_from_buf(buf);
1311
Christopher Faulet51dbc942018-09-13 09:05:15 +02001312 if (!h1_get_buf(h1c, &h1c->obuf)) {
1313 h1c->flags |= H1C_F_OUT_ALLOC;
1314 goto end;
1315 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001316
Christopher Fauletf2824e62018-10-01 12:12:37 +02001317 if (!conn_is_back(h1c->conn)) {
1318 h1m = &h1s->res;
1319 errflag = H1S_F_RES_ERROR;
1320 }
1321 else {
1322 h1m = &h1s->req;
1323 errflag = H1S_F_REQ_ERROR;
1324 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001325
1326
1327 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001328
1329 /* pre-align the output buffer like the HTX in case it's empty. In this
1330 * case since it's aligned we don't need to use the temporary trash.
1331 */
1332 if (!b_data(&h1c->obuf)) {
1333 h1c->obuf.head = sizeof(struct htx);
1334 tmp->area = h1c->obuf.area + h1c->obuf.head;
1335 }
1336
Christopher Faulet9768c262018-10-22 09:34:31 +02001337 tmp->size = b_room(&h1c->obuf);
1338
1339 blk = htx_get_head_blk(chn_htx);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001340 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001341 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001342 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001343 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001344 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001345 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001346
Christopher Fauletb2e84162018-12-06 11:39:49 +01001347 vlen = sz;
1348 if (vlen > count) {
1349 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1350 goto copy;
1351 vlen = count;
1352 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001353
Christopher Fauletb2e84162018-12-06 11:39:49 +01001354 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001355 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001356 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001357
1358 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001359 h1m_init_req(h1m);
1360 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001361 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001362 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001363 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001364 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001365 goto copy;
1366 h1m->flags |= H1_MF_XFER_LEN;
1367 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001368 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001369
Christopher Faulet9768c262018-10-22 09:34:31 +02001370 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001371 h1m_init_res(h1m);
1372 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001373 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001374 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001375 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001376 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001377 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001378 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001379 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001380 if (sl->info.res.status < 200 &&
1381 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1382 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001383 h1m->state = H1_MSG_HDR_FIRST;
1384 break;
1385
1386 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001387 h1m->state = H1_MSG_HDR_NAME;
1388 n = htx_get_blk_name(chn_htx, blk);
1389 v = htx_get_blk_value(chn_htx, blk);
1390
1391 if (isteqi(n, ist("transfer-encoding")))
1392 h1_parse_xfer_enc_header(h1m, v);
1393 else if (isteqi(n, ist("connection"))) {
1394 h1_parse_connection_header(h1m, v);
1395 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001396 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001397 if (!v.len)
1398 goto skip_hdr;
1399 }
1400
Christopher Fauletc59ff232018-12-03 13:58:44 +01001401 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001402 goto copy;
1403 skip_hdr:
1404 h1m->state = H1_MSG_HDR_L2_LWS;
1405 break;
1406
1407 case HTX_BLK_PHDR:
1408 /* not implemented yet */
1409 h1m->flags |= errflag;
1410 break;
1411
1412 case HTX_BLK_EOH:
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001413 if (h1m->state == H1_MSG_HDR_L2_LWS && process_conn_mode) {
1414 /* There is no "Connection:" header and
1415 * it the conn_mode must be
1416 * processed. So do it */
1417 n = ist("Connection");
1418 v = ist("");
1419 h1_process_conn_mode(h1s, h1m, NULL, &v);
1420 process_conn_mode = 0;
1421 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001422 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001423 goto copy;
1424 }
1425 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001426 h1m->state = H1_MSG_LAST_LF;
1427 if (!chunk_memcat(tmp, "\r\n", 2))
1428 goto copy;
1429
1430 h1m->state = H1_MSG_DATA;
1431 break;
1432
1433 case HTX_BLK_DATA:
1434 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001435 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001436 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001437 goto copy;
1438 break;
1439
1440 case HTX_BLK_EOD:
1441 if (!chunk_memcat(tmp, "0\r\n", 3))
1442 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001443 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001444 h1m->state = H1_MSG_TRAILERS;
1445 break;
1446
1447 case HTX_BLK_TLR:
Christopher Faulet32188212018-11-20 18:21:43 +01001448 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1449 if (!chunk_memcat(tmp, "0\r\n", 3))
1450 goto copy;
1451 h1s->flags |= H1S_F_HAVE_EOD;
1452 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001453 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001454 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001455 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001456 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001457 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001458 break;
1459
1460 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001461 if ((h1m->flags & H1_MF_CHNK)) {
1462 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1463 if (!chunk_memcat(tmp, "0\r\n", 3))
1464 goto copy;
1465 h1s->flags |= H1S_F_HAVE_EOD;
1466 }
1467 if (!(h1s->flags & H1S_F_HAVE_TLR)) {
1468 if (!chunk_memcat(tmp, "\r\n", 2))
1469 goto copy;
1470 h1s->flags |= H1S_F_HAVE_TLR;
1471 }
1472 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001473 h1m->state = H1_MSG_DONE;
1474 break;
1475
1476 case HTX_BLK_OOB:
1477 v = htx_get_blk_value(chn_htx, blk);
1478 if (!chunk_memcat(tmp, v.ptr, v.len))
1479 goto copy;
1480 break;
1481
1482 default:
1483 h1m->flags |= errflag;
1484 break;
1485 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001486 total += vlen;
1487 count -= vlen;
1488 if (sz == vlen)
1489 blk = htx_remove_blk(chn_htx, blk);
1490 else {
1491 htx_cut_data_blk(chn_htx, blk, vlen);
1492 break;
1493 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001494 }
1495
Christopher Faulet9768c262018-10-22 09:34:31 +02001496 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001497 /* when the output buffer is empty, tmp shares the same area so that we
1498 * only have to update pointers and lengths.
1499 */
1500 if (tmp->area == h1c->obuf.area)
1501 h1c->obuf.data = tmp->data;
1502 else
1503 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001504
Willy Tarreau45f2b892018-12-05 07:59:27 +01001505 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001506 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001507 htx_to_buf(chn_htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001508 end:
1509 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001510}
1511
Christopher Faulet51dbc942018-09-13 09:05:15 +02001512/*********************************************************/
1513/* functions below are I/O callbacks from the connection */
1514/*********************************************************/
1515/*
1516 * Attempt to read data, and subscribe if none available
1517 */
1518static int h1_recv(struct h1c *h1c)
1519{
1520 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001521 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001522 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001523 int rcvd = 0;
1524
1525 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001526 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001527
Olivier Houchard75159a92018-12-03 18:46:09 +01001528 if (!h1_recv_allowed(h1c)) {
1529 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001530 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001531 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001532
Christopher Fauletfeb11742018-11-29 15:12:34 +01001533 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001534 rcvd = 1;
1535 goto end;
1536 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001537
Christopher Faulet51dbc942018-09-13 09:05:15 +02001538 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1539 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001540 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001541 }
1542
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001543 /*
1544 * If we only have a small amount of data, realign it,
1545 * it's probably cheaper than doing 2 recv() calls.
1546 */
1547 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1548 b_slow_realign(&h1c->ibuf, trash.area, 0);
1549
Willy Tarreau45f2b892018-12-05 07:59:27 +01001550 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001551 if (max) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001552 int aligned = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001553 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001554
1555 if (!b_data(&h1c->ibuf)) {
1556 /* try to pre-align the buffer like the rxbufs will be
1557 * to optimize memory copies.
1558 */
1559 h1c->ibuf.data = sizeof(struct htx);
1560 aligned = 1;
1561 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001562 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001563 if (aligned) {
1564 h1c->ibuf.data -= sizeof(struct htx);
1565 h1c->ibuf.head = sizeof(struct htx);
1566 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001567 }
Christopher Faulet47365272018-10-31 17:40:50 +01001568 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001569 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001570 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001571 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001572 if (h1s->csinfo.t_idle == -1)
1573 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1574 }
Christopher Faulet47365272018-10-31 17:40:50 +01001575 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001576
Willy Tarreau45f2b892018-12-05 07:59:27 +01001577 if (h1_recv_allowed(h1c) && buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001578 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet81d48432018-11-19 21:22:43 +01001579 else
1580 rcvd = 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001581
Christopher Faulet9768c262018-10-22 09:34:31 +02001582 end:
Olivier Houchard75159a92018-12-03 18:46:09 +01001583 if ((ret > 0 || (conn->flags & CO_FL_ERROR) ||
1584 conn_xprt_read0_pending(conn)) && h1s && h1s->recv_wait) {
1585 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1586 tasklet_wakeup(h1s->recv_wait->task);
1587 h1s->recv_wait = NULL;
1588
1589 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001590 if (!b_data(&h1c->ibuf))
1591 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001592 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001593 h1c->flags |= H1C_F_IN_FULL;
1594 return rcvd;
1595}
1596
1597
1598/*
1599 * Try to send data if possible
1600 */
1601static int h1_send(struct h1c *h1c)
1602{
1603 struct connection *conn = h1c->conn;
1604 unsigned int flags = 0;
1605 size_t ret;
1606 int sent = 0;
1607
1608 if (conn->flags & CO_FL_ERROR)
1609 return 0;
1610
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001611 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1612 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1613 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1614 return 0;
1615 }
1616
Christopher Faulet51dbc942018-09-13 09:05:15 +02001617 if (!b_data(&h1c->obuf))
1618 goto end;
1619
1620 if (h1c->flags & H1C_F_OUT_FULL)
1621 flags |= CO_SFL_MSG_MORE;
1622
1623 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1624 if (ret > 0) {
1625 h1c->flags &= ~H1C_F_OUT_FULL;
1626 b_del(&h1c->obuf, ret);
1627 sent = 1;
1628 }
1629
Christopher Faulet145aa472018-12-06 10:56:20 +01001630 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1631 /* error or output closed, nothing to send, clear the buffer to release it */
1632 b_reset(&h1c->obuf);
1633 }
1634
Christopher Faulet51dbc942018-09-13 09:05:15 +02001635 end:
Olivier Houchard75159a92018-12-03 18:46:09 +01001636 if (!(h1c->flags & H1C_F_OUT_FULL) && h1c->h1s && h1c->h1s->send_wait) {
1637 struct h1s *h1s = h1c->h1s;
1638
1639 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1640 tasklet_wakeup(h1s->send_wait->task);
1641 h1s->send_wait = NULL;
1642 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001643 /* We're done, no more to send */
1644 if (!b_data(&h1c->obuf)) {
1645 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001646 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001647 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1648 h1_shutw_conn(conn);
1649 }
1650 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1651 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1652
1653 return sent;
1654}
1655
Christopher Faulet51dbc942018-09-13 09:05:15 +02001656
1657/* callback called on any event by the connection handler.
1658 * It applies changes and returns zero, or < 0 if it wants immediate
1659 * destruction of the connection.
1660 */
1661static int h1_process(struct h1c * h1c)
1662{
1663 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001664 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001665
Christopher Faulet51dbc942018-09-13 09:05:15 +02001666 if (!conn->mux_ctx)
1667 return -1;
1668
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001669 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001670 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1671 goto end;
1672 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001673 }
1674
Christopher Fauletfeb11742018-11-29 15:12:34 +01001675 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001676 if (h1c->flags & H1C_F_CS_ERROR ||
1677 conn->flags & CO_FL_ERROR ||
1678 conn_xprt_read0_pending(conn))
1679 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001680 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001681 if (!h1s_create(h1c, NULL))
1682 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001683 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001684 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001685 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001686 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001687 }
1688
Christopher Fauletfeb11742018-11-29 15:12:34 +01001689 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1690 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1691
Olivier Houchard75159a92018-12-03 18:46:09 +01001692 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1693 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
1694 conn->flags & CO_FL_ERROR)) {
1695 int flags = 0;
1696
1697 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1698 flags |= CS_FL_ERROR;
1699 if (conn_xprt_read0_pending(conn))
1700 flags |= CS_FL_REOS;
1701 h1s->cs->flags |= flags;
1702 h1s->cs->data_cb->wake(h1s->cs);
1703 }
Christopher Faulet47365272018-10-31 17:40:50 +01001704 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001705 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001706
1707 release:
1708 h1_release(conn);
1709 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001710}
1711
1712static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1713{
1714 struct h1c *h1c = ctx;
1715 int ret = 0;
1716
1717 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1718 ret = h1_send(h1c);
1719 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1720 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001721 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001722 h1_process(h1c);
1723 return NULL;
1724}
1725
1726
1727static int h1_wake(struct connection *conn)
1728{
1729 struct h1c *h1c = conn->mux_ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001730 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001731
Christopher Faulet539e0292018-11-19 10:40:09 +01001732 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001733 ret = h1_process(h1c);
1734 if (ret == 0) {
1735 struct h1s *h1s = h1c->h1s;
1736
1737 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1738 ret = h1s->cs->data_cb->wake(h1s->cs);
1739 }
1740 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001741}
1742
Christopher Faulet51dbc942018-09-13 09:05:15 +02001743/*******************************************/
1744/* functions below are used by the streams */
1745/*******************************************/
1746/*
1747 * Attach a new stream to a connection
1748 * (Used for outgoing connections)
1749 */
1750static struct conn_stream *h1_attach(struct connection *conn)
1751{
1752 struct h1c *h1c = conn->mux_ctx;
1753 struct conn_stream *cs = NULL;
1754 struct h1s *h1s;
1755
1756 if (h1c->flags & H1C_F_CS_ERROR)
1757 goto end;
1758
1759 cs = cs_new(h1c->conn);
1760 if (!cs)
1761 goto end;
1762
Christopher Fauletf2824e62018-10-01 12:12:37 +02001763 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001764 if (h1s == NULL)
1765 goto end;
1766
1767 return cs;
1768 end:
1769 cs_free(cs);
1770 return NULL;
1771}
1772
1773/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1774 * this mux, it's easy as we can only store a single conn_stream.
1775 */
1776static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1777{
1778 struct h1c *h1c = conn->mux_ctx;
1779 struct h1s *h1s = h1c->h1s;
1780
1781 if (h1s)
1782 return h1s->cs;
1783
1784 return NULL;
1785}
1786
1787static void h1_destroy(struct connection *conn)
1788{
1789 struct h1c *h1c = conn->mux_ctx;
1790
1791 if (!h1c->h1s)
1792 h1_release(conn);
1793}
1794
1795/*
1796 * Detach the stream from the connection and possibly release the connection.
1797 */
1798static void h1_detach(struct conn_stream *cs)
1799{
1800 struct h1s *h1s = cs->ctx;
1801 struct h1c *h1c;
1802
1803 cs->ctx = NULL;
1804 if (!h1s)
1805 return;
1806
1807 h1c = h1s->h1c;
1808 h1s->cs = NULL;
1809
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01001810 if (conn_is_back(h1c->conn) && (h1s->flags & H1S_F_WANT_KAL) && h1c->conn->owner) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001811 /* Never ever allow to reuse a connection from a non-reuse backend */
1812 if (h1c->conn && (h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
1813 h1c->conn->flags |= CO_FL_PRIVATE;
1814
1815 /* we're in keep-alive with an idle connection, monitor it if not already done */
1816 if (h1c->conn && LIST_ISEMPTY(&h1c->conn->list)) {
1817 struct server *srv = objt_server(h1c->conn->target);
1818
1819 if (srv) {
1820 if (h1c->conn->flags & CO_FL_PRIVATE)
1821 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
1822 else if (h1s->flags & H1S_F_NOT_FIRST)
1823 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1824 else
1825 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1826 }
1827 }
1828 }
1829
Christopher Faulet51dbc942018-09-13 09:05:15 +02001830 h1s_destroy(h1s);
1831
1832 /* We don't want to close right now unless the connection is in error */
1833 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01001834 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001835 h1_release(h1c->conn);
1836 else
1837 tasklet_wakeup(h1c->wait_event.task);
1838}
1839
1840
1841static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1842{
1843 struct h1s *h1s = cs->ctx;
1844
1845 if (!h1s)
1846 return;
1847
Christopher Fauletf2824e62018-10-01 12:12:37 +02001848 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1849 return;
1850
Christopher Faulet51dbc942018-09-13 09:05:15 +02001851 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1852 if (cs->flags & CS_FL_SHR)
1853 return;
1854 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1855 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1856 if (cs->flags & CS_FL_SHW) {
1857 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1858 conn_full_close(cs->conn);
1859 }
1860}
1861
1862static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1863{
1864 struct h1s *h1s = cs->ctx;
1865 struct h1c *h1c;
1866
1867 if (!h1s)
1868 return;
1869 h1c = h1s->h1c;
1870
Christopher Fauletf2824e62018-10-01 12:12:37 +02001871 if ((h1s->flags & H1S_F_WANT_KAL) &&
1872 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1873 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1874 return;
1875
Christopher Faulet51dbc942018-09-13 09:05:15 +02001876 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1877 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1878 return;
1879
1880 h1_shutw_conn(cs->conn);
1881}
1882
1883static void h1_shutw_conn(struct connection *conn)
1884{
1885 struct h1c *h1c = conn->mux_ctx;
1886
1887 if (conn_xprt_ready(conn) && conn->xprt->shutw)
1888 conn->xprt->shutw(conn, 1);
1889 if (!(conn->flags & CO_FL_SOCK_RD_SH))
1890 conn_sock_shutw(conn, 1);
1891 else {
1892 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1893 conn_full_close(conn);
1894 }
1895}
1896
1897/* Called from the upper layer, to unsubscribe to events */
1898static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1899{
1900 struct wait_event *sw;
1901 struct h1s *h1s = cs->ctx;
1902
1903 if (!h1s)
1904 return 0;
1905
1906 if (event_type & SUB_CAN_RECV) {
1907 sw = param;
1908 if (h1s->recv_wait == sw) {
1909 sw->wait_reason &= ~SUB_CAN_RECV;
1910 h1s->recv_wait = NULL;
1911 }
1912 }
1913 if (event_type & SUB_CAN_SEND) {
1914 sw = param;
1915 if (h1s->send_wait == sw) {
1916 sw->wait_reason &= ~SUB_CAN_SEND;
1917 h1s->send_wait = NULL;
1918 }
1919 }
1920 return 0;
1921}
1922
1923/* Called from the upper layer, to subscribe to events, such as being able to send */
1924static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1925{
1926 struct wait_event *sw;
1927 struct h1s *h1s = cs->ctx;
1928
1929 if (!h1s)
1930 return -1;
1931
1932 switch (event_type) {
1933 case SUB_CAN_RECV:
1934 sw = param;
1935 if (!(sw->wait_reason & SUB_CAN_RECV)) {
1936 sw->wait_reason |= SUB_CAN_RECV;
1937 sw->handle = h1s;
1938 h1s->recv_wait = sw;
1939 }
1940 return 0;
1941 case SUB_CAN_SEND:
1942 sw = param;
1943 if (!(sw->wait_reason & SUB_CAN_SEND)) {
1944 sw->wait_reason |= SUB_CAN_SEND;
1945 sw->handle = h1s;
1946 h1s->send_wait = sw;
1947 }
1948 return 0;
1949 default:
1950 break;
1951 }
1952 return -1;
1953}
1954
1955/* Called from the upper layer, to receive data */
1956static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1957{
1958 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01001959 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001960 size_t ret = 0;
1961
Christopher Faulet539e0292018-11-19 10:40:09 +01001962 if (!(h1c->flags & H1C_F_IN_ALLOC))
1963 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001964
1965 if (flags & CO_RFL_BUF_FLUSH)
1966 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001967 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
1968 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01001969 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1970 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001971 }
1972 return ret;
1973}
1974
1975
1976/* Called from the upper layer, to send data */
1977static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1978{
1979 struct h1s *h1s = cs->ctx;
1980 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001981 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001982
1983 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001984 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001985
1986 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001987 if (h1c->flags & H1C_F_CS_WAIT_CONN)
1988 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001989
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001990 while (total != count) {
1991 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001992
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001993 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
1994 ret = h1_process_output(h1c, buf, count);
1995 if (!ret)
1996 break;
1997 total += ret;
1998 if (!h1_send(h1c))
1999 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002000 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002001
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002002 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002003}
2004
Christopher Faulet1be55f92018-10-02 15:59:23 +02002005#if defined(CONFIG_HAP_LINUX_SPLICE)
2006/* Send and get, using splicing */
2007static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2008{
2009 struct h1s *h1s = cs->ctx;
2010 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2011 int ret = 0;
2012
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002013 if (b_data(&h1s->h1c->ibuf)) {
2014 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002015 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002016 }
2017
2018 h1s->flags &= ~H1S_F_BUF_FLUSH;
2019 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002020 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2021 count = h1m->curr_len;
2022 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2023 if (h1m->state == H1_MSG_DATA && ret > 0)
2024 h1m->curr_len -= ret;
2025 end:
2026 return ret;
2027
2028}
2029
2030static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2031{
2032 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002033 int ret = 0;
2034
2035 if (b_data(&h1s->h1c->obuf))
2036 goto end;
2037
2038 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002039 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002040 if (pipe->data) {
2041 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
2042 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
2043 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002044 return ret;
2045}
2046#endif
2047
Christopher Faulet51dbc942018-09-13 09:05:15 +02002048/****************************************/
2049/* MUX initialization and instanciation */
2050/****************************************/
2051
2052/* The mux operations */
2053const struct mux_ops mux_h1_ops = {
2054 .init = h1_init,
2055 .wake = h1_wake,
2056 .attach = h1_attach,
2057 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002058 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002059 .detach = h1_detach,
2060 .destroy = h1_destroy,
2061 .avail_streams = h1_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01002062 .max_streams = h1_max_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002063 .rcv_buf = h1_rcv_buf,
2064 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002065#if defined(CONFIG_HAP_LINUX_SPLICE)
2066 .rcv_pipe = h1_rcv_pipe,
2067 .snd_pipe = h1_snd_pipe,
2068#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002069 .subscribe = h1_subscribe,
2070 .unsubscribe = h1_unsubscribe,
2071 .shutr = h1_shutr,
2072 .shutw = h1_shutw,
2073 .flags = MX_FL_NONE,
2074 .name = "h1",
2075};
2076
2077
2078/* this mux registers default HTX proto */
2079static struct mux_proto_list mux_proto_htx =
2080{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2081
Willy Tarreau0108d902018-11-25 19:14:37 +01002082INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2083
Christopher Faulet51dbc942018-09-13 09:05:15 +02002084/*
2085 * Local variables:
2086 * c-indent-level: 8
2087 * c-basic-offset: 8
2088 * End:
2089 */