blob: 74e1bb724aa0f3a3167aceca5f3a902cf8f305e5 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <common/cfgparse.h>
13#include <common/config.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010014#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020015
Christopher Faulet1be55f92018-10-02 15:59:23 +020016#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020017#include <types/proxy.h>
18#include <types/session.h>
19
Christopher Faulet51dbc942018-09-13 09:05:15 +020020#include <proto/connection.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020021#include <proto/h1.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020022#include <proto/http_htx.h>
23#include <proto/htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020024#include <proto/log.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020025#include <proto/stream.h>
26#include <proto/stream_interface.h>
27
28/*
29 * H1 Connection flags (32 bits)
30 */
31#define H1C_F_NONE 0x00000000
32
33/* Flags indicating why writing output data are blocked */
34#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
35#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
36/* 0x00000004 - 0x00000008 unused */
37
38/* Flags indicating why reading input data are blocked. */
39#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
40#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Faulet539e0292018-11-19 10:40:09 +010041/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020042
43#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
44#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
45#define H1C_F_CS_SHUTW 0x00004000 /* connection is already shut down */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020046#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020047
Christopher Fauletf2824e62018-10-01 12:12:37 +020048#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet129817b2018-09-20 16:14:40 +020049
Christopher Faulet51dbc942018-09-13 09:05:15 +020050/*
51 * H1 Stream flags (32 bits)
52 */
Christopher Faulet129817b2018-09-20 16:14:40 +020053#define H1S_F_NONE 0x00000000
54#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020055#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
56#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Christopher Faulet539e0292018-11-19 10:40:09 +010057/* 0x00000008 unused */
Christopher Fauletf2824e62018-10-01 12:12:37 +020058#define H1S_F_WANT_KAL 0x00000010
59#define H1S_F_WANT_TUN 0x00000020
60#define H1S_F_WANT_CLO 0x00000040
61#define H1S_F_WANT_MSK 0x00000070
62#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010063#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010064#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Christopher Faulet32188212018-11-20 18:21:43 +010065#define H1S_F_HAVE_EOD 0x00000400 /* Set during input/output process to know the last empty chunk was processed */
66#define H1S_F_HAVE_TLR 0x00000800 /* Set during input/output process to know the trailers were processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020067
68/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020069struct h1c {
70 struct connection *conn;
71 struct proxy *px;
72 uint32_t flags; /* Connection flags: H1C_F_* */
73
74 struct buffer ibuf; /* Input buffer to store data before parsing */
75 struct buffer obuf; /* Output buffer to store data after reformatting */
76
77 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
78 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
79
80 struct h1s *h1s; /* H1 stream descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020081};
82
83/* H1 stream descriptor */
84struct h1s {
85 struct h1c *h1c;
86 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010087 struct cs_info csinfo; /* CS info, only used for client connections */
88 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020089
Christopher Faulet51dbc942018-09-13 09:05:15 +020090 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
91 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020092
93 struct h1m req;
94 struct h1m res;
95
96 enum http_meth_t meth; /* HTTP resquest method */
97 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +020098};
99
100/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100101DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
102DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200103
Christopher Faulet51dbc942018-09-13 09:05:15 +0200104static int h1_recv(struct h1c *h1c);
105static int h1_send(struct h1c *h1c);
106static int h1_process(struct h1c *h1c);
107static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
108static void h1_shutw_conn(struct connection *conn);
109
110/*****************************************************/
111/* functions below are for dynamic buffer management */
112/*****************************************************/
113/*
114 * Indicates whether or not the we may call the h1_recv() function to
115 * attempt to receive data into the buffer and/or parse pending data. The
116 * condition is a bit complex due to some API limits for now. The rules are the
117 * following :
118 * - if an error or a shutdown was detected on the connection and the buffer
119 * is empty, we must not attempt to receive
120 * - if the input buffer failed to be allocated, we must not try to receive
121 * and we know there is nothing pending
122 * - if no flag indicates a blocking condition, we may attempt to receive,
123 * regardless of whether the input buffer is full or not, so that only de
124 * receiving part decides whether or not to block. This is needed because
125 * the connection API indeed prevents us from re-enabling receipt that is
126 * already enabled in a polled state, so we must always immediately stop as
127 * soon as the mux can't proceed so as never to hit an end of read with data
128 * pending in the buffers.
129 * - otherwise must may not attempt to receive
130 */
131static inline int h1_recv_allowed(const struct h1c *h1c)
132{
133 if (b_data(&h1c->ibuf) == 0 &&
Christopher Faulet7e346f32018-11-20 17:13:52 +0100134 (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW) ||
Christopher Faulet51dbc942018-09-13 09:05:15 +0200135 h1c->conn->flags & CO_FL_ERROR ||
136 conn_xprt_read0_pending(h1c->conn)))
137 return 0;
138
139 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL)))
140 return 1;
141
142 return 0;
143}
144
145/*
146 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
147 * flags are used to figure what buffer was requested. It returns 1 if the
148 * allocation succeeds, in which case the connection is woken up, or 0 if it's
149 * impossible to wake up and we prefer to be woken up later.
150 */
151static int h1_buf_available(void *target)
152{
153 struct h1c *h1c = target;
154
155 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
156 h1c->flags &= ~H1C_F_IN_ALLOC;
157 if (h1_recv_allowed(h1c))
158 tasklet_wakeup(h1c->wait_event.task);
159 return 1;
160 }
161
162 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
163 h1c->flags &= ~H1C_F_OUT_ALLOC;
164 tasklet_wakeup(h1c->wait_event.task);
165 return 1;
166 }
167
Christopher Faulet51dbc942018-09-13 09:05:15 +0200168 return 0;
169}
170
171/*
172 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
173 */
174static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
175{
176 struct buffer *buf = NULL;
177
178 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
179 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
180 h1c->buf_wait.target = h1c;
181 h1c->buf_wait.wakeup_cb = h1_buf_available;
182 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
183 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
184 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
185 __conn_xprt_stop_recv(h1c->conn);
186 }
187 return buf;
188}
189
190/*
191 * Release a buffer, if any, and try to wake up entities waiting in the buffer
192 * wait queue.
193 */
194static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
195{
196 if (bptr->size) {
197 b_free(bptr);
198 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
199 }
200}
201
202static int h1_avail_streams(struct connection *conn)
203{
204 struct h1c *h1c = conn->mux_ctx;
205
206 return h1c->h1s ? 0 : 1;
207}
208
209
210/*****************************************************************/
211/* functions below are dedicated to the mux setup and management */
212/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100213static struct conn_stream *h1s_new_cs(struct h1s *h1s)
214{
215 struct conn_stream *cs;
216
217 cs = cs_new(h1s->h1c->conn);
218 if (!cs)
219 goto err;
220 h1s->cs = cs;
221 cs->ctx = h1s;
222
223 if (h1s->flags & H1S_F_NOT_FIRST)
224 cs->flags |= CS_FL_NOT_FIRST;
225
226 if (stream_create_from_cs(cs) < 0)
227 goto err;
228 return cs;
229
230 err:
231 cs_free(cs);
232 h1s->cs = NULL;
233 return NULL;
234}
235
Christopher Fauletf2824e62018-10-01 12:12:37 +0200236static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200237{
238 struct h1s *h1s;
239
240 h1s = pool_alloc(pool_head_h1s);
241 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100242 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200243
244 h1s->h1c = h1c;
245 h1c->h1s = h1s;
246
247 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200248 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200249
250 h1s->recv_wait = NULL;
251 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200252
253 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200254 h1s->req.flags |= H1_MF_NO_PHDR;
255
Christopher Faulet129817b2018-09-20 16:14:40 +0200256 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200257 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200258
259 h1s->status = 0;
260 h1s->meth = HTTP_METH_OTHER;
261
Christopher Faulet47365272018-10-31 17:40:50 +0100262 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
263 h1s->flags |= H1S_F_NOT_FIRST;
264 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
265
Christopher Faulet129817b2018-09-20 16:14:40 +0200266 if (!conn_is_back(h1c->conn)) {
267 if (h1c->px->options2 & PR_O2_REQBUG_OK)
268 h1s->req.err_pos = -1;
269 }
270 else {
271 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
272 h1s->res.err_pos = -1;
273 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200274
Christopher Faulet539e0292018-11-19 10:40:09 +0100275 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
276 * create a new one.
277 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200278 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100279 h1s->csinfo.create_date = date;
280 h1s->csinfo.tv_create = now;
281 h1s->csinfo.t_handshake = 0;
282 h1s->csinfo.t_idle = -1;
283
Christopher Fauletf2824e62018-10-01 12:12:37 +0200284 cs->ctx = h1s;
285 h1s->cs = cs;
286 }
Christopher Faulet47365272018-10-31 17:40:50 +0100287 else {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100288 struct session *sess = h1c->conn->owner;
289
290 h1s->csinfo.create_date = sess->accept_date;
291 h1s->csinfo.tv_create = sess->tv_accept;
292 h1s->csinfo.t_handshake = sess->t_handshake;
293 h1s->csinfo.t_idle = -1;
294
Christopher Faulet47365272018-10-31 17:40:50 +0100295 cs = h1s_new_cs(h1s);
296 if (!cs)
297 goto fail;
298 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200299 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100300
301 fail:
302 pool_free(pool_head_h1s, h1s);
303 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200304}
305
306static void h1s_destroy(struct h1s *h1s)
307{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200308 if (h1s) {
309 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200310
Christopher Fauletf2824e62018-10-01 12:12:37 +0200311 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200312
Christopher Fauletf2824e62018-10-01 12:12:37 +0200313 if (h1s->recv_wait != NULL)
314 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
315 if (h1s->send_wait != NULL)
316 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
317
Christopher Faulet47365272018-10-31 17:40:50 +0100318 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
319 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
320 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200321
Christopher Fauletf2824e62018-10-01 12:12:37 +0200322 cs_free(h1s->cs);
323 pool_free(pool_head_h1s, h1s);
324 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200325}
326
Christopher Fauletfeb11742018-11-29 15:12:34 +0100327static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
328{
329 struct h1s *h1s = cs->ctx;
330
331 if (h1s && !conn_is_back(cs->conn))
332 return &h1s->csinfo;
333 return NULL;
334}
335
Christopher Faulet51dbc942018-09-13 09:05:15 +0200336/*
337 * Initialize the mux once it's attached. It is expected that conn->mux_ctx
338 * points to the existing conn_stream (for outgoing connections) or NULL (for
339 * incoming ones). Returns < 0 on error.
340 */
341static int h1_init(struct connection *conn, struct proxy *proxy)
342{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200343 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200344
345 h1c = pool_alloc(pool_head_h1c);
346 if (!h1c)
347 goto fail_h1c;
348 h1c->conn = conn;
349 h1c->px = proxy;
350
351 h1c->flags = H1C_F_NONE;
352 h1c->ibuf = BUF_NULL;
353 h1c->obuf = BUF_NULL;
354 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200355
Christopher Faulet51dbc942018-09-13 09:05:15 +0200356 LIST_INIT(&h1c->buf_wait.list);
357 h1c->wait_event.task = tasklet_new();
358 if (!h1c->wait_event.task)
359 goto fail;
360 h1c->wait_event.task->process = h1_io_cb;
361 h1c->wait_event.task->context = h1c;
362 h1c->wait_event.wait_reason = 0;
363
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200364 if (!(conn->flags & CO_FL_CONNECTED))
365 h1c->flags |= H1C_F_CS_WAIT_CONN;
366
Christopher Fauletf2824e62018-10-01 12:12:37 +0200367 /* Always Create a new H1S */
368 if (!h1s_create(h1c, conn->mux_ctx))
369 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200370
Christopher Faulet129817b2018-09-20 16:14:40 +0200371 conn->mux_ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200372
Christopher Faulet51dbc942018-09-13 09:05:15 +0200373 /* Try to read, if nothing is available yet we'll just subscribe */
374 if (h1_recv(h1c))
375 h1_process(h1c);
376
377 /* mux->wake will be called soon to complete the operation */
378 return 0;
379
380 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100381 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200382 tasklet_free(h1c->wait_event.task);
383 pool_free(pool_head_h1c, h1c);
384 fail_h1c:
385 return -1;
386}
387
388
389/* release function for a connection. This one should be called to free all
390 * resources allocated to the mux.
391 */
392static void h1_release(struct connection *conn)
393{
394 struct h1c *h1c = conn->mux_ctx;
395
396 LIST_DEL(&conn->list);
397
398 if (h1c) {
399 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
400 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
401 LIST_DEL(&h1c->buf_wait.list);
402 LIST_INIT(&h1c->buf_wait.list);
403 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
404 }
405
406 h1_release_buf(h1c, &h1c->ibuf);
407 h1_release_buf(h1c, &h1c->obuf);
408
Christopher Faulet51dbc942018-09-13 09:05:15 +0200409 if (h1c->wait_event.task)
410 tasklet_free(h1c->wait_event.task);
411
Christopher Fauletf2824e62018-10-01 12:12:37 +0200412 h1s_destroy(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200413 if (h1c->wait_event.wait_reason != 0)
414 conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason,
415 &h1c->wait_event);
416 pool_free(pool_head_h1c, h1c);
417 }
418
419 conn->mux = NULL;
420 conn->mux_ctx = NULL;
421
422 conn_stop_tracking(conn);
423 conn_full_close(conn);
424 if (conn->destroy_cb)
425 conn->destroy_cb(conn);
426 conn_free(conn);
427}
428
429/******************************************************/
430/* functions below are for the H1 protocol processing */
431/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200432/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
433 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200434 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100435static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200436{
Christopher Faulet570d1612018-11-26 11:13:57 +0100437 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200438
Christopher Faulet570d1612018-11-26 11:13:57 +0100439 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200440 (*(p + 5) > '1' ||
441 (*(p + 5) == '1' && *(p + 7) >= '1')))
442 h1m->flags |= H1_MF_VER_11;
443}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200444
Christopher Faulet9768c262018-10-22 09:34:31 +0200445/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
446 * greater or equal to 1.1
447 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100448static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200449{
Christopher Faulet570d1612018-11-26 11:13:57 +0100450 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200451
Christopher Faulet570d1612018-11-26 11:13:57 +0100452 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200453 (*(p + 5) > '1' ||
454 (*(p + 5) == '1' && *(p + 7) >= '1')))
455 h1m->flags |= H1_MF_VER_11;
456}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200457
Christopher Faulet9768c262018-10-22 09:34:31 +0200458/*
459 * Check the validity of the request version. If the version is valid, it
460 * returns 1. Otherwise, it returns 0.
461 */
462static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
463{
464 struct h1c *h1c = h1s->h1c;
465
466 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
467 * exactly one digit "." one digit. This check may be disabled using
468 * option accept-invalid-http-request.
469 */
470 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
471 if (sl.rq.v.len != 8)
472 return 0;
473
474 if (*(sl.rq.v.ptr + 4) != '/' ||
475 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
476 *(sl.rq.v.ptr + 6) != '.' ||
477 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
478 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200479 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200480 else if (!sl.rq.v.len) {
481 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200482
Christopher Faulet9768c262018-10-22 09:34:31 +0200483 /* RFC 1945 allows only GET for HTTP/0.9 requests */
484 if (sl.rq.meth != HTTP_METH_GET)
485 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200486
Christopher Faulet9768c262018-10-22 09:34:31 +0200487 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
488 if (!sl.rq.u.len)
489 return 0;
490
491 /* Add HTTP version */
492 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100493 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200494 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100495
496 if ((sl.rq.v.len == 8) &&
497 ((*(sl.rq.v.ptr + 5) > '1') ||
498 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
499 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200500 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200501}
502
Christopher Faulet9768c262018-10-22 09:34:31 +0200503/*
504 * Check the validity of the response version. If the version is valid, it
505 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200506 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200507static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200508{
Christopher Faulet9768c262018-10-22 09:34:31 +0200509 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200510
Christopher Faulet9768c262018-10-22 09:34:31 +0200511 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
512 * exactly one digit "." one digit. This check may be disabled using
513 * option accept-invalid-http-request.
514 */
515 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
516 if (sl.st.v.len != 8)
517 return 0;
518
519 if (*(sl.st.v.ptr + 4) != '/' ||
520 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
521 *(sl.st.v.ptr + 6) != '.' ||
522 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
523 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200524 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100525
526 if ((sl.st.v.len == 8) &&
527 ((*(sl.st.v.ptr + 5) > '1') ||
528 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
529 h1m->flags |= H1_MF_VER_11;
530
Christopher Faulet9768c262018-10-22 09:34:31 +0200531 return 1;
532}
533/* Remove all "Connection:" headers from the HTX message <htx> */
534static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
535{
536 struct ist hdr = {.ptr = "Connection", .len = 10};
537 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200538
Christopher Faulet9768c262018-10-22 09:34:31 +0200539 while (http_find_header(htx, hdr, &ctx, 1))
540 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200541
Christopher Faulet9768c262018-10-22 09:34:31 +0200542 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
543}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200544
Christopher Faulet9768c262018-10-22 09:34:31 +0200545/* Add a "Connection:" header with the value <value> into the HTX message
546 * <htx>.
547 */
548static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
549{
550 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200551
Christopher Faulet9768c262018-10-22 09:34:31 +0200552 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200553}
554
555/* Deduce the connection mode of the client connection, depending on the
556 * configuration and the H1 message flags. This function is called twice, the
557 * first time when the request is parsed and the second time when the response
558 * is parsed.
559 */
560static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
561{
562 struct proxy *fe = h1s->h1c->px;
563 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
564
565 /* Tunnel mode can only by set on the frontend */
566 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
567 flag = H1S_F_WANT_TUN;
568 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
569 flag = H1S_F_WANT_CLO;
570
571 /* flags order: CLO > SCL > TUN > KAL */
572 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
573 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
574
575 if (h1m->flags & H1_MF_RESP) {
576 /* Either we've established an explicit tunnel, or we're
577 * switching the protocol. In both cases, we're very unlikely to
578 * understand the next protocols. We have to switch to tunnel
579 * mode, so that we transfer the request and responses then let
580 * this protocol pass unmodified. When we later implement
581 * specific parsers for such protocols, we'll want to check the
582 * Upgrade header which contains information about that protocol
583 * for responses with status 101 (eg: see RFC2817 about TLS).
584 */
585 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
586 h1s->status == 101)
587 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletbd44ca62018-11-29 09:55:22 +0100588 else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */
589 (h1m->flags & H1_MF_CONN_CLO && h1s->req.state != H1_MSG_DONE)) /*explicit close and unfinished request */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200590 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
591 }
592 else {
593 if (h1s->flags & H1S_F_WANT_KAL &&
594 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
595 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
596 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
597 }
598
599 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
600 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
601 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
602}
603
604/* Deduce the connection mode of the client connection, depending on the
605 * configuration and the H1 message flags. This function is called twice, the
606 * first time when the request is parsed and the second time when the response
607 * is parsed.
608 */
609static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
610{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100611 struct h1c *h1c = h1s->h1c;
612 struct session *sess = h1c->conn->owner;
613 struct proxy *fe = sess->fe;
614 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200615 int flag = H1S_F_WANT_KAL;
616
617 /* Tunnel mode can only by set on the frontend */
618 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
619 flag = H1S_F_WANT_TUN;
620
621 /* For the server connection: server-close == httpclose */
622 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
623 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
624 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
625 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
626 flag = H1S_F_WANT_CLO;
627
628 /* flags order: CLO > SCL > TUN > KAL */
629 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
630 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
631
632 if (h1m->flags & H1_MF_RESP) {
633 /* Either we've established an explicit tunnel, or we're
634 * switching the protocol. In both cases, we're very unlikely to
635 * understand the next protocols. We have to switch to tunnel
636 * mode, so that we transfer the request and responses then let
637 * this protocol pass unmodified. When we later implement
638 * specific parsers for such protocols, we'll want to check the
639 * Upgrade header which contains information about that protocol
640 * for responses with status 101 (eg: see RFC2817 about TLS).
641 */
642 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
643 h1s->status == 101)
644 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
645 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
646 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
647 else if (h1s->flags & H1S_F_WANT_KAL &&
648 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
649 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
650 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
651 }
652
653 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
654 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
655 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200656}
657
Christopher Faulet9768c262018-10-22 09:34:31 +0200658static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
659 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200660{
661 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200662
663 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
664 * token is found
665 */
666 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200667 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200668
669 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200670 if (h1m->flags & H1_MF_CONN_CLO) {
671 if (conn_val)
672 *conn_val = ist("");
673 if (htx)
674 h1_remove_conn_hdrs(h1m, htx);
675 }
676 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
677 if (conn_val)
678 *conn_val = ist("keep-alive");
679 if (htx)
680 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
681 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200682 }
683 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200684 if (h1m->flags & H1_MF_CONN_KAL) {
685 if (conn_val)
686 *conn_val = ist("");
687 if (htx)
688 h1_remove_conn_hdrs(h1m, htx);
689 }
690 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
691 if (conn_val)
692 *conn_val = ist("close");
693 if (htx)
694 h1_add_conn_hdr(h1m, htx, ist("close"));
695 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200696 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200697}
698
Christopher Faulet9768c262018-10-22 09:34:31 +0200699static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
700 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200701{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200702 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
703 * token is found
704 */
705 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200706 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200707
708 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200709 if (h1m->flags & H1_MF_CONN_CLO) {
710 if (conn_val)
711 *conn_val = ist("");
712 if (htx)
713 h1_remove_conn_hdrs(h1m, htx);
714 }
715 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
716 if (conn_val)
717 *conn_val = ist("keep-alive");
718 if (htx)
719 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
720 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200721 }
722 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200723 if (h1m->flags & H1_MF_CONN_KAL) {
724 if (conn_val)
725 *conn_val = ist("");
726 if (htx)
727 h1_remove_conn_hdrs(h1m, htx);
728 }
729 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
730 if (conn_val)
731 *conn_val = ist("close");
732 if (htx)
733 h1_add_conn_hdr(h1m, htx, ist("close"));
734 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200735 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200736}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200737
Christopher Faulet9768c262018-10-22 09:34:31 +0200738/* Set the right connection mode and update "Connection:" header if
739 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
740 * message is updated accordingly. When <conn_val> is not NULL, it is set with
741 * the new header value.
742 */
743static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
744 struct htx *htx, struct ist *conn_val)
745{
746 if (!conn_is_back(h1s->h1c->conn)) {
747 h1_set_cli_conn_mode(h1s, h1m);
748 if (h1m->flags & H1_MF_RESP)
749 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
750 }
751 else {
752 h1_set_srv_conn_mode(h1s, h1m);
753 if (!(h1m->flags & H1_MF_RESP))
754 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
755 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200756}
757
Christopher Faulete44769b2018-11-29 23:01:45 +0100758
759/* Append the description of what is present in error snapshot <es> into <out>.
760 * The description must be small enough to always fit in a buffer. The output
761 * buffer may be the trash so the trash must not be used inside this function.
762 */
763static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
764{
765 chunk_appendf(out,
766 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
767 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
768 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
769 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
770 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
771 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
772}
773/*
774 * Capture a bad request or response and archive it in the proxy's structure.
775 * By default it tries to report the error position as h1m->err_pos. However if
776 * this one is not set, it will then report h1m->next, which is the last known
777 * parsing point. The function is able to deal with wrapping buffers. It always
778 * displays buffers as a contiguous area starting at buf->p. The direction is
779 * determined thanks to the h1m's flags.
780 */
781static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
782 struct h1m *h1m, struct buffer *buf)
783{
784 struct session *sess = h1c->conn->owner;
785 struct proxy *proxy = h1c->px;
786 struct proxy *other_end = sess->fe;
787 union error_snapshot_ctx ctx;
788
789 if (h1s->cs->data)
790 other_end = si_strm(h1s->cs->data)->be;
791
792 /* http-specific part now */
793 ctx.h1.state = h1m->state;
794 ctx.h1.c_flags = h1c->flags;
795 ctx.h1.s_flags = h1s->flags;
796 ctx.h1.m_flags = h1m->flags;
797 ctx.h1.m_clen = h1m->curr_len;
798 ctx.h1.m_blen = h1m->body_len;
799
800 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
801 h1c->conn->target, sess, buf, 0, 0,
802 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
803 &ctx, h1_show_error_snapshot);
804}
805
Christopher Faulet129817b2018-09-20 16:14:40 +0200806/*
807 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200808 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
809 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet129817b2018-09-20 16:14:40 +0200810 * responsibile to update the parser state <h1m>.
811 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200812static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200813 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200814{
815 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100816 union h1_sl h1sl;
817 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200818 int ret = 0;
819
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100820 if (!max)
821 goto end;
822
Christopher Faulet129817b2018-09-20 16:14:40 +0200823 /* Realing input buffer if necessary */
824 if (b_head(buf) + b_data(buf) > b_wrap(buf))
825 b_slow_realign(buf, trash.area, 0);
826
Christopher Fauletf2824e62018-10-01 12:12:37 +0200827 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100828 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200829 if (ret <= 0) {
830 /* Incomplete or invalid message. If the buffer is full, it's an
831 * error because headers are too large to be handled by the
832 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200833 if (ret < 0 || (!ret && b_full(buf)))
834 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200835 goto end;
836 }
837
838 /* messages headers fully parsed, do some checks to prepare the body
839 * parsing.
840 */
841
842 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200843 if (ret > (b_size(buf) - global.tune.maxrewrite))
844 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200845
Christopher Faulet9768c262018-10-22 09:34:31 +0200846 /* Save the request's method or the response's status, check if the body
847 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200848 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100849 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200850
Christopher Faulet129817b2018-09-20 16:14:40 +0200851 /* Request have always a known length */
852 h1m->flags |= H1_MF_XFER_LEN;
853 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
854 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200855
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100856 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
857 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200858 h1m->err_state = h1m->state;
859 goto vsn_error;
860 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200861 }
862 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100863 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200864
865 if ((h1s->meth == HTTP_METH_HEAD) ||
866 (h1s->status >= 100 && h1s->status < 200) ||
867 (h1s->status == 204) || (h1s->status == 304) ||
868 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
869 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
870 h1m->flags |= H1_MF_XFER_LEN;
871 h1m->curr_len = h1m->body_len = 0;
872 h1m->state = H1_MSG_DONE;
873 }
874 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
875 h1m->flags |= H1_MF_XFER_LEN;
876 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
877 h1m->state = H1_MSG_DONE;
878 }
879 else
880 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200881
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100882 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
883 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200884 h1m->err_state = h1m->state;
885 goto vsn_error;
886 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200887 }
888
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100889 /* Set HTX start-line flags */
890 if (h1m->flags & H1_MF_VER_11)
891 flags |= HTX_SL_F_VER_11;
892 if (h1m->flags & H1_MF_XFER_ENC)
893 flags |= HTX_SL_F_XFER_ENC;
894 if (h1m->flags & H1_MF_XFER_LEN) {
895 flags |= HTX_SL_F_XFER_LEN;
896 if (h1m->flags & H1_MF_CHNK)
897 flags |= HTX_SL_F_CHNK;
898 else if (h1m->flags & H1_MF_CLEN)
899 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100900 if (h1m->state == H1_MSG_DONE)
901 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100902 }
903
Christopher Faulet9768c262018-10-22 09:34:31 +0200904 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100905 struct htx_sl *sl;
906
907 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
908 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200909 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100910 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200911 }
912 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100913 struct htx_sl *sl;
914
915 flags |= HTX_SL_F_IS_RESP;
916 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
917 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200918 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100919 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200920 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100921
Christopher Faulet9768c262018-10-22 09:34:31 +0200922 if (h1m->state == H1_MSG_DONE)
923 if (!htx_add_endof(htx, HTX_BLK_EOM))
924 goto error;
925
926 h1_process_conn_mode(h1s, h1m, htx, NULL);
927
928 /* If body length cannot be determined, set htx->extra to
929 * ULLONG_MAX. This value is impossible in other cases.
930 */
931 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
932
933 /* Recheck there is enough space to do headers rewritting */
934 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
935 goto error;
936
937 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200938 end:
939 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200940
941 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +0200942 h1m->err_state = h1m->state;
943 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +0200944 vsn_error:
945 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +0100946 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200947 ret = 0;
948 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200949}
950
951/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200952 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
953 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +0200954 * and filling h1s->err_pos and h1s->err_state fields. This functions is
955 * responsibile to update the parser state <h1m>.
956 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200957static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200958 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200959{
Christopher Faulet9768c262018-10-22 09:34:31 +0200960 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +0200961 size_t total = 0;
962 int ret = 0;
963
964 if (h1m->flags & H1_MF_XFER_LEN) {
965 if (h1m->flags & H1_MF_CLEN) {
966 /* content-length: read only h2m->body_len */
967 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200968 if (ret > data_space)
969 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200970 if ((uint64_t)ret > h1m->curr_len)
971 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200972 if (ret > b_contig_data(buf, *ofs))
973 ret = b_contig_data(buf, *ofs);
974 if (ret) {
975 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
976 goto end;
977 h1m->curr_len -= ret;
978 *ofs += ret;
979 total += ret;
980 }
981
982 if (!h1m->curr_len) {
983 if (!htx_add_endof(htx, HTX_BLK_EOM))
984 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200985 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200986 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200987 }
988 else if (h1m->flags & H1_MF_CHNK) {
989 new_chunk:
990 /* te:chunked : parse chunks */
991 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200992 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +0200993 if (ret <= 0)
994 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200995 h1m->state = H1_MSG_CHUNK_SIZE;
996
Christopher Faulet129817b2018-09-20 16:14:40 +0200997 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200998 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200999 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001000 }
1001
1002 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1003 unsigned int chksz;
1004
Christopher Fauletf2824e62018-10-01 12:12:37 +02001005 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001006 if (ret <= 0)
1007 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001008 if (!chksz) {
1009 if (!htx_add_endof(htx, HTX_BLK_EOD))
1010 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001011 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001012 h1m->state = H1_MSG_TRAILERS;
1013 }
1014 else
1015 h1m->state = H1_MSG_DATA;
1016
Christopher Faulet129817b2018-09-20 16:14:40 +02001017 h1m->curr_len = chksz;
1018 h1m->body_len += chksz;
1019 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001020 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001021 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001022 }
1023
1024 if (h1m->state == H1_MSG_DATA) {
1025 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001026 if (ret > data_space)
1027 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001028 if ((uint64_t)ret > h1m->curr_len)
1029 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001030 if (ret > b_contig_data(buf, *ofs))
1031 ret = b_contig_data(buf, *ofs);
1032 if (ret) {
1033 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1034 goto end;
1035 h1m->curr_len -= ret;
1036 max -= ret;
1037 *ofs += ret;
1038 total += ret;
1039 }
1040 if (!h1m->curr_len) {
1041 h1m->state = H1_MSG_CHUNK_CRLF;
1042 goto new_chunk;
1043 }
1044 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001045 }
1046
1047 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001048 /* Trailers were alread parsed, only the EOM
1049 * need to be added */
1050 if (h1s->flags & H1S_F_HAVE_TLR)
1051 goto skip_tlr_parsing;
1052
Christopher Fauletf2824e62018-10-01 12:12:37 +02001053 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001054 if (ret > data_space)
1055 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001056 if (ret <= 0)
1057 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001058
1059 /* Realing input buffer if tailers wrap. For now
1060 * this is a workaroung. Because trailers are
1061 * not split on CRLF, like headers, there is no
1062 * way to know where to split it when trailers
1063 * wrap. This is a limitation of
1064 * h1_measure_trailers.
1065 */
1066 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1067 b_slow_realign(buf, trash.area, 0);
1068
1069 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1070 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001071 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001072 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001073 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001074 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001075
Christopher Faulet17276482018-11-22 11:44:35 +01001076 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001077 if (!htx_add_endof(htx, HTX_BLK_EOM))
1078 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001079 h1m->state = H1_MSG_DONE;
1080 }
1081 }
1082 else {
1083 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1084 * is no body. Switch the message in DONE state
1085 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001086 if (!htx_add_endof(htx, HTX_BLK_EOM))
1087 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001088 h1m->state = H1_MSG_DONE;
1089 }
1090 }
1091 else {
1092 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001093 ret = max;
1094 if (ret > data_space)
1095 ret = data_space;
1096 if (ret > b_contig_data(buf, *ofs))
1097 ret = b_contig_data(buf, *ofs);
1098 if (ret) {
1099 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1100 goto end;
1101
1102 *ofs += max;
1103 total = max;
1104 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001105 }
1106
1107 end:
1108 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001109 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001110 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001111 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001112 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001113 return 0;
1114 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001115 /* update htx->extra, only when the body length is known */
1116 if (h1m->flags & H1_MF_XFER_LEN)
1117 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001118 return total;
1119}
1120
1121/*
1122 * Synchronize the request and the response before reseting them. Except for 1xx
1123 * responses, we wait that the request and the response are in DONE state and
1124 * that all data are forwarded for both. For 1xx responses, only the response is
1125 * reset, waiting the final one. Many 1xx messages can be sent.
1126 */
1127static void h1_sync_messages(struct h1c *h1c)
1128{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001129 struct h1s *h1s = h1c->h1s;
1130
1131 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001132 return;
1133
Christopher Fauletf2824e62018-10-01 12:12:37 +02001134 if (h1s->res.state == H1_MSG_DONE &&
1135 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001136 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001137 /* For 100-Continue response or any other informational 1xx
1138 * response which is non-final, don't reset the request, the
1139 * transaction is not finished. We take care the response was
1140 * transferred before.
1141 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001142 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001143 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001144 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001145 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001146 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1147 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001148 h1m_init_req(&h1s->req);
1149 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001150 h1s->req.state = H1_MSG_TUNNEL;
1151 h1s->res.state = H1_MSG_TUNNEL;
1152 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001153 }
1154}
1155
1156/*
1157 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001158 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1159 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001160 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001161static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001162{
Christopher Faulet539e0292018-11-19 10:40:09 +01001163 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001164 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001165 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001166 size_t total = 0;
1167 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001168 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001169 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001170
Christopher Faulet539e0292018-11-19 10:40:09 +01001171 htx = htx_from_buf(buf);
1172 count = b_data(&h1c->ibuf);
1173 max = htx_free_space(htx);
1174 if (flags & CO_RFL_KEEP_RSV) {
1175 if (max < global.tune.maxrewrite)
1176 goto end;
1177 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001178 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001179 if (count > max)
1180 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001181
Christopher Fauletf2824e62018-10-01 12:12:37 +02001182 if (!conn_is_back(h1c->conn)) {
1183 h1m = &h1s->req;
1184 errflag = H1S_F_REQ_ERROR;
1185 }
1186 else {
1187 h1m = &h1s->res;
1188 errflag = H1S_F_RES_ERROR;
1189 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001190
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001191 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001192 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001193 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001194 if (!ret)
1195 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001196 }
1197 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001198 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001199 if (!ret)
1200 break;
1201 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001202 else if (h1m->state == H1_MSG_DONE)
1203 break;
1204 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001205 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet9768c262018-10-22 09:34:31 +02001206 if (!ret)
1207 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001208 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001210 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001211 break;
1212 }
1213
Christopher Faulet539e0292018-11-19 10:40:09 +01001214 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001215 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001216
Christopher Faulet47365272018-10-31 17:40:50 +01001217 if (h1s->flags & errflag)
1218 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001219
Christopher Faulet539e0292018-11-19 10:40:09 +01001220 b_del(&h1c->ibuf, total);
1221
1222 end:
1223 if (htx_is_not_empty(htx))
1224 b_set_data(buf, b_size(buf));
1225 else {
1226 htx_reset(htx);
1227 b_set_data(buf, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001228 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001229
Christopher Faulet539e0292018-11-19 10:40:09 +01001230 if (h1c->flags & H1C_F_IN_FULL && b_room(&h1c->ibuf)) {
1231 h1c->flags &= ~H1C_F_IN_FULL;
1232 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001233 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001234
Christopher Faulet9c388402018-11-19 21:54:26 +01001235 if (b_data(&h1c->ibuf)) {
1236 if (!htx_is_empty(htx))
1237 h1s->cs->flags |= CS_FL_RCV_MORE;
1238 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001239 else {
1240 h1_release_buf(h1c, &h1c->ibuf);
1241 h1_sync_messages(h1c);
1242
1243 h1s->cs->flags &= ~CS_FL_RCV_MORE;
1244 if (h1s->cs->flags & CS_FL_REOS)
1245 h1s->cs->flags |= CS_FL_EOS;
1246 }
1247 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001248
1249 parsing_err:
1250 // FIXME: create an error snapshot here
1251 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001252 htx->flags |= HTX_FL_PARSING_ERROR;
1253 b_set_data(buf, b_size(buf));
1254 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001255 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001256}
1257
Christopher Faulet129817b2018-09-20 16:14:40 +02001258/*
1259 * Process outgoing data. It parses data and transfer them from the channel buffer into
1260 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1261 * 0 if it couldn't proceed.
1262 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001263static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1264{
Christopher Faulet129817b2018-09-20 16:14:40 +02001265 struct h1s *h1s = h1c->h1s;
1266 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001267 struct htx *chn_htx;
1268 struct htx_blk *blk;
1269 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001270 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001271 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001272 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001273
Christopher Faulet47365272018-10-31 17:40:50 +01001274 if (!count)
1275 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001276 chn_htx = htx_from_buf(buf);
1277
Christopher Faulet51dbc942018-09-13 09:05:15 +02001278 if (!h1_get_buf(h1c, &h1c->obuf)) {
1279 h1c->flags |= H1C_F_OUT_ALLOC;
1280 goto end;
1281 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001282
Christopher Fauletf2824e62018-10-01 12:12:37 +02001283 if (!conn_is_back(h1c->conn)) {
1284 h1m = &h1s->res;
1285 errflag = H1S_F_RES_ERROR;
1286 }
1287 else {
1288 h1m = &h1s->req;
1289 errflag = H1S_F_REQ_ERROR;
1290 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001291
1292
1293 tmp = get_trash_chunk();
1294 tmp->size = b_room(&h1c->obuf);
1295
1296 blk = htx_get_head_blk(chn_htx);
1297 while (!(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001298 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001299 struct ist n, v;
1300 uint32_t sz = htx_get_blksz(blk);
1301
1302 if (total + sz > count)
1303 goto copy;
1304
1305 switch (htx_get_blk_type(blk)) {
1306 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001307 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001308
1309 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001310 h1m_init_req(h1m);
1311 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001312 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001313 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001314 h1_parse_req_vsn(h1m, sl);
1315 if (!htx_reqline_to_str(sl, tmp))
1316 goto copy;
1317 h1m->flags |= H1_MF_XFER_LEN;
1318 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001319 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001320
Christopher Faulet9768c262018-10-22 09:34:31 +02001321 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001322 h1m_init_res(h1m);
1323 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001324 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001325 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001326 h1_parse_res_vsn(h1m, sl);
1327 if (!htx_stline_to_str(sl, tmp))
1328 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001329 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001330 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001331 if (sl->info.res.status < 200 &&
1332 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1333 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 h1m->state = H1_MSG_HDR_FIRST;
1335 break;
1336
1337 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001338 h1m->state = H1_MSG_HDR_NAME;
1339 n = htx_get_blk_name(chn_htx, blk);
1340 v = htx_get_blk_value(chn_htx, blk);
1341
1342 if (isteqi(n, ist("transfer-encoding")))
1343 h1_parse_xfer_enc_header(h1m, v);
1344 else if (isteqi(n, ist("connection"))) {
1345 h1_parse_connection_header(h1m, v);
1346 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001347 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001348 if (!v.len)
1349 goto skip_hdr;
1350 }
1351
1352 if (!htx_hdr_to_str(n, v, tmp))
1353 goto copy;
1354 skip_hdr:
1355 h1m->state = H1_MSG_HDR_L2_LWS;
1356 break;
1357
1358 case HTX_BLK_PHDR:
1359 /* not implemented yet */
1360 h1m->flags |= errflag;
1361 break;
1362
1363 case HTX_BLK_EOH:
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001364 if (h1m->state == H1_MSG_HDR_L2_LWS && process_conn_mode) {
1365 /* There is no "Connection:" header and
1366 * it the conn_mode must be
1367 * processed. So do it */
1368 n = ist("Connection");
1369 v = ist("");
1370 h1_process_conn_mode(h1s, h1m, NULL, &v);
1371 process_conn_mode = 0;
1372 if (v.len) {
1373 if (!htx_hdr_to_str(n, v, tmp))
1374 goto copy;
1375 }
1376 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001377 h1m->state = H1_MSG_LAST_LF;
1378 if (!chunk_memcat(tmp, "\r\n", 2))
1379 goto copy;
1380
1381 h1m->state = H1_MSG_DATA;
1382 break;
1383
1384 case HTX_BLK_DATA:
1385 v = htx_get_blk_value(chn_htx, blk);
1386 if (!htx_data_to_str(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
1387 goto copy;
1388 break;
1389
1390 case HTX_BLK_EOD:
1391 if (!chunk_memcat(tmp, "0\r\n", 3))
1392 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001393 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001394 h1m->state = H1_MSG_TRAILERS;
1395 break;
1396
1397 case HTX_BLK_TLR:
Christopher Faulet32188212018-11-20 18:21:43 +01001398 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1399 if (!chunk_memcat(tmp, "0\r\n", 3))
1400 goto copy;
1401 h1s->flags |= H1S_F_HAVE_EOD;
1402 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001403 v = htx_get_blk_value(chn_htx, blk);
1404 if (!htx_trailer_to_str(v, tmp))
1405 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001406 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001407 break;
1408
1409 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001410 if ((h1m->flags & H1_MF_CHNK)) {
1411 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1412 if (!chunk_memcat(tmp, "0\r\n", 3))
1413 goto copy;
1414 h1s->flags |= H1S_F_HAVE_EOD;
1415 }
1416 if (!(h1s->flags & H1S_F_HAVE_TLR)) {
1417 if (!chunk_memcat(tmp, "\r\n", 2))
1418 goto copy;
1419 h1s->flags |= H1S_F_HAVE_TLR;
1420 }
1421 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001422 h1m->state = H1_MSG_DONE;
1423 break;
1424
1425 case HTX_BLK_OOB:
1426 v = htx_get_blk_value(chn_htx, blk);
1427 if (!chunk_memcat(tmp, v.ptr, v.len))
1428 goto copy;
1429 break;
1430
1431 default:
1432 h1m->flags |= errflag;
1433 break;
1434 }
1435 total += sz;
1436 blk = htx_remove_blk(chn_htx, blk);
Christopher Faulet129817b2018-09-20 16:14:40 +02001437 }
1438
Christopher Faulet9768c262018-10-22 09:34:31 +02001439 copy:
1440 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001441
1442 if (b_full(&h1c->obuf))
1443 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001444 if (htx_is_empty(chn_htx)) {
1445 htx_reset(chn_htx);
1446 b_set_data(buf, 0);
1447 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001448 end:
1449 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001450}
1451
Christopher Faulet51dbc942018-09-13 09:05:15 +02001452/*********************************************************/
1453/* functions below are I/O callbacks from the connection */
1454/*********************************************************/
1455/*
1456 * Attempt to read data, and subscribe if none available
1457 */
1458static int h1_recv(struct h1c *h1c)
1459{
1460 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001461 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001462 size_t ret, max;
1463 int rcvd = 0;
1464
1465 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
1466 return 0;
1467
Christopher Faulet539e0292018-11-19 10:40:09 +01001468 if (!h1_recv_allowed(h1c))
Christopher Faulet9768c262018-10-22 09:34:31 +02001469 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001470
Christopher Fauletfeb11742018-11-29 15:12:34 +01001471 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001472 rcvd = 1;
1473 goto end;
1474 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001475
Christopher Faulet51dbc942018-09-13 09:05:15 +02001476 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1477 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001479 }
1480
1481 ret = 0;
1482 max = b_room(&h1c->ibuf);
1483 if (max) {
1484 h1c->flags &= ~H1C_F_IN_FULL;
1485 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
1486 }
Christopher Faulet47365272018-10-31 17:40:50 +01001487 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001488 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001489 if (h1s && h1s->cs) {
1490 h1s->cs->flags |= CS_FL_READ_PARTIAL;
1491 if (h1s->csinfo.t_idle == -1)
1492 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1493 }
Christopher Faulet47365272018-10-31 17:40:50 +01001494 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001495
1496 if (h1_recv_allowed(h1c))
1497 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet81d48432018-11-19 21:22:43 +01001498 else
1499 rcvd = 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001500
Christopher Faulet9768c262018-10-22 09:34:31 +02001501 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001502 if (!b_data(&h1c->ibuf))
1503 h1_release_buf(h1c, &h1c->ibuf);
1504 else if (b_full(&h1c->ibuf))
1505 h1c->flags |= H1C_F_IN_FULL;
1506 return rcvd;
1507}
1508
1509
1510/*
1511 * Try to send data if possible
1512 */
1513static int h1_send(struct h1c *h1c)
1514{
1515 struct connection *conn = h1c->conn;
1516 unsigned int flags = 0;
1517 size_t ret;
1518 int sent = 0;
1519
1520 if (conn->flags & CO_FL_ERROR)
1521 return 0;
1522
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001523 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1524 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1525 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1526 return 0;
1527 }
1528
Christopher Faulet51dbc942018-09-13 09:05:15 +02001529 if (!b_data(&h1c->obuf))
1530 goto end;
1531
1532 if (h1c->flags & H1C_F_OUT_FULL)
1533 flags |= CO_SFL_MSG_MORE;
1534
1535 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1536 if (ret > 0) {
1537 h1c->flags &= ~H1C_F_OUT_FULL;
1538 b_del(&h1c->obuf, ret);
1539 sent = 1;
1540 }
1541
1542 end:
1543 /* We're done, no more to send */
1544 if (!b_data(&h1c->obuf)) {
1545 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001546 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001547 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1548 h1_shutw_conn(conn);
1549 }
1550 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1551 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1552
1553 return sent;
1554}
1555
1556
1557static void h1_wake_stream(struct h1c *h1c)
1558{
1559 struct connection *conn = h1c->conn;
1560 struct h1s *h1s = h1c->h1s;
1561 uint32_t flags = 0;
1562 int dont_wake = 0;
1563
1564 if (!h1s || !h1s->cs)
1565 return;
1566
1567 if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR))
1568 flags |= CS_FL_ERROR;
1569 if (conn_xprt_read0_pending(conn))
1570 flags |= CS_FL_REOS;
1571
1572 h1s->cs->flags |= flags;
1573 if (h1s->recv_wait) {
1574 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1575 tasklet_wakeup(h1s->recv_wait->task);
1576 h1s->recv_wait = NULL;
1577 dont_wake = 1;
1578 }
1579 if (h1s->send_wait) {
1580 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1581 tasklet_wakeup(h1s->send_wait->task);
1582 h1s->send_wait = NULL;
1583 dont_wake = 1;
1584 }
1585 if (!dont_wake && h1s->cs->data_cb->wake)
1586 h1s->cs->data_cb->wake(h1s->cs);
1587}
1588
1589/* callback called on any event by the connection handler.
1590 * It applies changes and returns zero, or < 0 if it wants immediate
1591 * destruction of the connection.
1592 */
1593static int h1_process(struct h1c * h1c)
1594{
1595 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001596 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001597
Christopher Faulet51dbc942018-09-13 09:05:15 +02001598 if (!conn->mux_ctx)
1599 return -1;
1600
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001601 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001602 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1603 goto end;
1604 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001605 }
1606
Christopher Fauletfeb11742018-11-29 15:12:34 +01001607 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001608 if (h1c->flags & H1C_F_CS_ERROR ||
1609 conn->flags & CO_FL_ERROR ||
1610 conn_xprt_read0_pending(conn))
1611 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001612 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001613 if (!h1s_create(h1c, NULL))
1614 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001615 }
Christopher Fauletfeb11742018-11-29 15:12:34 +01001616 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001617 }
1618
Christopher Fauletfeb11742018-11-29 15:12:34 +01001619 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1620 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1621
Christopher Faulet539e0292018-11-19 10:40:09 +01001622 h1_wake_stream(h1c);
Christopher Faulet47365272018-10-31 17:40:50 +01001623 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001624 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001625
1626 release:
1627 h1_release(conn);
1628 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001629}
1630
1631static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1632{
1633 struct h1c *h1c = ctx;
1634 int ret = 0;
1635
1636 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1637 ret = h1_send(h1c);
1638 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1639 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001640 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001641 h1_process(h1c);
1642 return NULL;
1643}
1644
1645
1646static int h1_wake(struct connection *conn)
1647{
1648 struct h1c *h1c = conn->mux_ctx;
1649
Christopher Faulet539e0292018-11-19 10:40:09 +01001650 h1_send(h1c);
1651 return h1_process(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001652}
1653
Christopher Faulet51dbc942018-09-13 09:05:15 +02001654/*******************************************/
1655/* functions below are used by the streams */
1656/*******************************************/
1657/*
1658 * Attach a new stream to a connection
1659 * (Used for outgoing connections)
1660 */
1661static struct conn_stream *h1_attach(struct connection *conn)
1662{
1663 struct h1c *h1c = conn->mux_ctx;
1664 struct conn_stream *cs = NULL;
1665 struct h1s *h1s;
1666
1667 if (h1c->flags & H1C_F_CS_ERROR)
1668 goto end;
1669
1670 cs = cs_new(h1c->conn);
1671 if (!cs)
1672 goto end;
1673
Christopher Fauletf2824e62018-10-01 12:12:37 +02001674 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001675 if (h1s == NULL)
1676 goto end;
1677
1678 return cs;
1679 end:
1680 cs_free(cs);
1681 return NULL;
1682}
1683
1684/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1685 * this mux, it's easy as we can only store a single conn_stream.
1686 */
1687static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1688{
1689 struct h1c *h1c = conn->mux_ctx;
1690 struct h1s *h1s = h1c->h1s;
1691
1692 if (h1s)
1693 return h1s->cs;
1694
1695 return NULL;
1696}
1697
1698static void h1_destroy(struct connection *conn)
1699{
1700 struct h1c *h1c = conn->mux_ctx;
1701
1702 if (!h1c->h1s)
1703 h1_release(conn);
1704}
1705
1706/*
1707 * Detach the stream from the connection and possibly release the connection.
1708 */
1709static void h1_detach(struct conn_stream *cs)
1710{
1711 struct h1s *h1s = cs->ctx;
1712 struct h1c *h1c;
1713
1714 cs->ctx = NULL;
1715 if (!h1s)
1716 return;
1717
1718 h1c = h1s->h1c;
1719 h1s->cs = NULL;
1720
Christopher Faulet9400a392018-11-23 23:10:39 +01001721 if (conn_is_back(h1c->conn) && (h1s->flags & H1S_F_WANT_KAL)) {
1722 /* Never ever allow to reuse a connection from a non-reuse backend */
1723 if (h1c->conn && (h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
1724 h1c->conn->flags |= CO_FL_PRIVATE;
1725
1726 /* we're in keep-alive with an idle connection, monitor it if not already done */
1727 if (h1c->conn && LIST_ISEMPTY(&h1c->conn->list)) {
1728 struct server *srv = objt_server(h1c->conn->target);
1729
1730 if (srv) {
1731 if (h1c->conn->flags & CO_FL_PRIVATE)
1732 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
1733 else if (h1s->flags & H1S_F_NOT_FIRST)
1734 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1735 else
1736 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1737 }
1738 }
1739 }
1740
Christopher Faulet51dbc942018-09-13 09:05:15 +02001741 h1s_destroy(h1s);
1742
1743 /* We don't want to close right now unless the connection is in error */
1744 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
1745 (h1c->conn->flags & CO_FL_ERROR))
1746 h1_release(h1c->conn);
1747 else
1748 tasklet_wakeup(h1c->wait_event.task);
1749}
1750
1751
1752static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1753{
1754 struct h1s *h1s = cs->ctx;
1755
1756 if (!h1s)
1757 return;
1758
Christopher Fauletf2824e62018-10-01 12:12:37 +02001759 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1760 return;
1761
Christopher Faulet51dbc942018-09-13 09:05:15 +02001762 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1763 if (cs->flags & CS_FL_SHR)
1764 return;
1765 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1766 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1767 if (cs->flags & CS_FL_SHW) {
1768 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1769 conn_full_close(cs->conn);
1770 }
1771}
1772
1773static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1774{
1775 struct h1s *h1s = cs->ctx;
1776 struct h1c *h1c;
1777
1778 if (!h1s)
1779 return;
1780 h1c = h1s->h1c;
1781
Christopher Fauletf2824e62018-10-01 12:12:37 +02001782 if ((h1s->flags & H1S_F_WANT_KAL) &&
1783 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1784 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1785 return;
1786
Christopher Faulet51dbc942018-09-13 09:05:15 +02001787 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1788 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1789 return;
1790
1791 h1_shutw_conn(cs->conn);
1792}
1793
1794static void h1_shutw_conn(struct connection *conn)
1795{
1796 struct h1c *h1c = conn->mux_ctx;
1797
1798 if (conn_xprt_ready(conn) && conn->xprt->shutw)
1799 conn->xprt->shutw(conn, 1);
1800 if (!(conn->flags & CO_FL_SOCK_RD_SH))
1801 conn_sock_shutw(conn, 1);
1802 else {
1803 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1804 conn_full_close(conn);
1805 }
1806}
1807
1808/* Called from the upper layer, to unsubscribe to events */
1809static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1810{
1811 struct wait_event *sw;
1812 struct h1s *h1s = cs->ctx;
1813
1814 if (!h1s)
1815 return 0;
1816
1817 if (event_type & SUB_CAN_RECV) {
1818 sw = param;
1819 if (h1s->recv_wait == sw) {
1820 sw->wait_reason &= ~SUB_CAN_RECV;
1821 h1s->recv_wait = NULL;
1822 }
1823 }
1824 if (event_type & SUB_CAN_SEND) {
1825 sw = param;
1826 if (h1s->send_wait == sw) {
1827 sw->wait_reason &= ~SUB_CAN_SEND;
1828 h1s->send_wait = NULL;
1829 }
1830 }
1831 return 0;
1832}
1833
1834/* Called from the upper layer, to subscribe to events, such as being able to send */
1835static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1836{
1837 struct wait_event *sw;
1838 struct h1s *h1s = cs->ctx;
1839
1840 if (!h1s)
1841 return -1;
1842
1843 switch (event_type) {
1844 case SUB_CAN_RECV:
1845 sw = param;
1846 if (!(sw->wait_reason & SUB_CAN_RECV)) {
1847 sw->wait_reason |= SUB_CAN_RECV;
1848 sw->handle = h1s;
1849 h1s->recv_wait = sw;
1850 }
1851 return 0;
1852 case SUB_CAN_SEND:
1853 sw = param;
1854 if (!(sw->wait_reason & SUB_CAN_SEND)) {
1855 sw->wait_reason |= SUB_CAN_SEND;
1856 sw->handle = h1s;
1857 h1s->send_wait = sw;
1858 }
1859 return 0;
1860 default:
1861 break;
1862 }
1863 return -1;
1864}
1865
1866/* Called from the upper layer, to receive data */
1867static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1868{
1869 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01001870 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001871 size_t ret = 0;
1872
Christopher Faulet539e0292018-11-19 10:40:09 +01001873 if (!(h1c->flags & H1C_F_IN_ALLOC))
1874 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001875
1876 if (flags & CO_RFL_BUF_FLUSH)
1877 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001878 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
1879 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01001880 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1881 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001882 }
1883 return ret;
1884}
1885
1886
1887/* Called from the upper layer, to send data */
1888static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1889{
1890 struct h1s *h1s = cs->ctx;
1891 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001892 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001893
1894 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001895 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001896
1897 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001898 if (h1c->flags & H1C_F_CS_WAIT_CONN)
1899 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001900
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001901 while (total != count) {
1902 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001903
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001904 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
1905 ret = h1_process_output(h1c, buf, count);
1906 if (!ret)
1907 break;
1908 total += ret;
1909 if (!h1_send(h1c))
1910 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001911 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01001912
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001913 /* We need to do that because of the infinite forwarding. <buf>
1914 * contains HTX messages so when infinite forwarding is enabled,
1915 * count is equal to the buffer size. From outside, the buffer
1916 * appears as full.
1917 */
1918 if (!b_data(buf))
1919 total = count;
1920 else if (total != count) {
Christopher Fauletf96c3222018-11-20 18:38:01 +01001921 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1922 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1c->wait_event);
1923 }
Christopher Faulet5d37dac2018-11-22 10:58:42 +01001924 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001925}
1926
Christopher Faulet1be55f92018-10-02 15:59:23 +02001927#if defined(CONFIG_HAP_LINUX_SPLICE)
1928/* Send and get, using splicing */
1929static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
1930{
1931 struct h1s *h1s = cs->ctx;
1932 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
1933 int ret = 0;
1934
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001935 if (b_data(&h1s->h1c->ibuf)) {
1936 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001937 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001938 }
1939
1940 h1s->flags &= ~H1S_F_BUF_FLUSH;
1941 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001942 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
1943 count = h1m->curr_len;
1944 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
1945 if (h1m->state == H1_MSG_DATA && ret > 0)
1946 h1m->curr_len -= ret;
1947 end:
1948 return ret;
1949
1950}
1951
1952static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
1953{
1954 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001955 int ret = 0;
1956
1957 if (b_data(&h1s->h1c->obuf))
1958 goto end;
1959
1960 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001961 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001962 if (pipe->data) {
1963 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
1964 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
1965 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001966 return ret;
1967}
1968#endif
1969
Christopher Faulet51dbc942018-09-13 09:05:15 +02001970/****************************************/
1971/* MUX initialization and instanciation */
1972/****************************************/
1973
1974/* The mux operations */
1975const struct mux_ops mux_h1_ops = {
1976 .init = h1_init,
1977 .wake = h1_wake,
1978 .attach = h1_attach,
1979 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01001980 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02001981 .detach = h1_detach,
1982 .destroy = h1_destroy,
1983 .avail_streams = h1_avail_streams,
1984 .rcv_buf = h1_rcv_buf,
1985 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02001986#if defined(CONFIG_HAP_LINUX_SPLICE)
1987 .rcv_pipe = h1_rcv_pipe,
1988 .snd_pipe = h1_snd_pipe,
1989#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02001990 .subscribe = h1_subscribe,
1991 .unsubscribe = h1_unsubscribe,
1992 .shutr = h1_shutr,
1993 .shutw = h1_shutw,
1994 .flags = MX_FL_NONE,
1995 .name = "h1",
1996};
1997
1998
1999/* this mux registers default HTX proto */
2000static struct mux_proto_list mux_proto_htx =
2001{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2002
Willy Tarreau0108d902018-11-25 19:14:37 +01002003INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2004
Christopher Faulet51dbc942018-09-13 09:05:15 +02002005/*
2006 * Local variables:
2007 * c-indent-level: 8
2008 * c-basic-offset: 8
2009 * End:
2010 */