blob: 87b58415740ffba9f1eb2e0c28f2cbced1fec35c [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>
14
Christopher Faulet1be55f92018-10-02 15:59:23 +020015#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020016#include <types/proxy.h>
17#include <types/session.h>
18
Christopher Faulet51dbc942018-09-13 09:05:15 +020019#include <proto/connection.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020020#include <proto/h1.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020021#include <proto/http_htx.h>
22#include <proto/htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020023#include <proto/log.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020024#include <proto/stream.h>
25#include <proto/stream_interface.h>
26
27/*
28 * H1 Connection flags (32 bits)
29 */
30#define H1C_F_NONE 0x00000000
31
32/* Flags indicating why writing output data are blocked */
33#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
34#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
35/* 0x00000004 - 0x00000008 unused */
36
37/* Flags indicating why reading input data are blocked. */
38#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
39#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Faulet539e0292018-11-19 10:40:09 +010040/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020041
42#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
43#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
44#define H1C_F_CS_SHUTW 0x00004000 /* connection is already shut down */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020045#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Fauletf2824e62018-10-01 12:12:37 +020047#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 +020048
Christopher Faulet51dbc942018-09-13 09:05:15 +020049/*
50 * H1 Stream flags (32 bits)
51 */
Christopher Faulet129817b2018-09-20 16:14:40 +020052#define H1S_F_NONE 0x00000000
53#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020054#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
55#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Christopher Faulet539e0292018-11-19 10:40:09 +010056/* 0x00000008 unused */
Christopher Fauletf2824e62018-10-01 12:12:37 +020057#define H1S_F_WANT_KAL 0x00000010
58#define H1S_F_WANT_TUN 0x00000020
59#define H1S_F_WANT_CLO 0x00000040
60#define H1S_F_WANT_MSK 0x00000070
61#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010062#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010063#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Christopher Faulet129817b2018-09-20 16:14:40 +020064
Christopher Faulet51dbc942018-09-13 09:05:15 +020065
66/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020067struct h1c {
68 struct connection *conn;
69 struct proxy *px;
70 uint32_t flags; /* Connection flags: H1C_F_* */
71
72 struct buffer ibuf; /* Input buffer to store data before parsing */
73 struct buffer obuf; /* Output buffer to store data after reformatting */
74
75 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
76 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
77
78 struct h1s *h1s; /* H1 stream descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020079};
80
81/* H1 stream descriptor */
82struct h1s {
83 struct h1c *h1c;
84 struct conn_stream *cs;
85 uint32_t flags; /* Connection flags: H1S_F_* */
86
Christopher Faulet51dbc942018-09-13 09:05:15 +020087 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
88 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020089
90 struct h1m req;
91 struct h1m res;
92
93 enum http_meth_t meth; /* HTTP resquest method */
94 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +020095};
96
97/* the h1c and h1s pools */
98static struct pool_head *pool_head_h1c;
99static struct pool_head *pool_head_h1s;
100
Christopher Faulet51dbc942018-09-13 09:05:15 +0200101static int h1_recv(struct h1c *h1c);
102static int h1_send(struct h1c *h1c);
103static int h1_process(struct h1c *h1c);
104static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
105static void h1_shutw_conn(struct connection *conn);
106
107/*****************************************************/
108/* functions below are for dynamic buffer management */
109/*****************************************************/
110/*
111 * Indicates whether or not the we may call the h1_recv() function to
112 * attempt to receive data into the buffer and/or parse pending data. The
113 * condition is a bit complex due to some API limits for now. The rules are the
114 * following :
115 * - if an error or a shutdown was detected on the connection and the buffer
116 * is empty, we must not attempt to receive
117 * - if the input buffer failed to be allocated, we must not try to receive
118 * and we know there is nothing pending
119 * - if no flag indicates a blocking condition, we may attempt to receive,
120 * regardless of whether the input buffer is full or not, so that only de
121 * receiving part decides whether or not to block. This is needed because
122 * the connection API indeed prevents us from re-enabling receipt that is
123 * already enabled in a polled state, so we must always immediately stop as
124 * soon as the mux can't proceed so as never to hit an end of read with data
125 * pending in the buffers.
126 * - otherwise must may not attempt to receive
127 */
128static inline int h1_recv_allowed(const struct h1c *h1c)
129{
130 if (b_data(&h1c->ibuf) == 0 &&
131 (h1c->flags & (H1C_F_CS_ERROR||H1C_F_CS_SHUTW) ||
132 h1c->conn->flags & CO_FL_ERROR ||
133 conn_xprt_read0_pending(h1c->conn)))
134 return 0;
135
136 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL)))
137 return 1;
138
139 return 0;
140}
141
142/*
143 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
144 * flags are used to figure what buffer was requested. It returns 1 if the
145 * allocation succeeds, in which case the connection is woken up, or 0 if it's
146 * impossible to wake up and we prefer to be woken up later.
147 */
148static int h1_buf_available(void *target)
149{
150 struct h1c *h1c = target;
151
152 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
153 h1c->flags &= ~H1C_F_IN_ALLOC;
154 if (h1_recv_allowed(h1c))
155 tasklet_wakeup(h1c->wait_event.task);
156 return 1;
157 }
158
159 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
160 h1c->flags &= ~H1C_F_OUT_ALLOC;
161 tasklet_wakeup(h1c->wait_event.task);
162 return 1;
163 }
164
Christopher Faulet51dbc942018-09-13 09:05:15 +0200165 return 0;
166}
167
168/*
169 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
170 */
171static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
172{
173 struct buffer *buf = NULL;
174
175 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
176 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
177 h1c->buf_wait.target = h1c;
178 h1c->buf_wait.wakeup_cb = h1_buf_available;
179 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
180 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
181 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
182 __conn_xprt_stop_recv(h1c->conn);
183 }
184 return buf;
185}
186
187/*
188 * Release a buffer, if any, and try to wake up entities waiting in the buffer
189 * wait queue.
190 */
191static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
192{
193 if (bptr->size) {
194 b_free(bptr);
195 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
196 }
197}
198
199static int h1_avail_streams(struct connection *conn)
200{
201 struct h1c *h1c = conn->mux_ctx;
202
203 return h1c->h1s ? 0 : 1;
204}
205
206
207/*****************************************************************/
208/* functions below are dedicated to the mux setup and management */
209/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100210static struct conn_stream *h1s_new_cs(struct h1s *h1s)
211{
212 struct conn_stream *cs;
213
214 cs = cs_new(h1s->h1c->conn);
215 if (!cs)
216 goto err;
217 h1s->cs = cs;
218 cs->ctx = h1s;
219
220 if (h1s->flags & H1S_F_NOT_FIRST)
221 cs->flags |= CS_FL_NOT_FIRST;
222
223 if (stream_create_from_cs(cs) < 0)
224 goto err;
225 return cs;
226
227 err:
228 cs_free(cs);
229 h1s->cs = NULL;
230 return NULL;
231}
232
Christopher Fauletf2824e62018-10-01 12:12:37 +0200233static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200234{
235 struct h1s *h1s;
236
237 h1s = pool_alloc(pool_head_h1s);
238 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100239 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200240
241 h1s->h1c = h1c;
242 h1c->h1s = h1s;
243
244 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200245 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200246
247 h1s->recv_wait = NULL;
248 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200249
250 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200251 h1s->req.flags |= H1_MF_NO_PHDR;
252
Christopher Faulet129817b2018-09-20 16:14:40 +0200253 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200254 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200255
256 h1s->status = 0;
257 h1s->meth = HTTP_METH_OTHER;
258
Christopher Faulet47365272018-10-31 17:40:50 +0100259 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
260 h1s->flags |= H1S_F_NOT_FIRST;
261 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
262
Christopher Faulet129817b2018-09-20 16:14:40 +0200263 if (!conn_is_back(h1c->conn)) {
264 if (h1c->px->options2 & PR_O2_REQBUG_OK)
265 h1s->req.err_pos = -1;
266 }
267 else {
268 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
269 h1s->res.err_pos = -1;
270 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200271
Christopher Faulet539e0292018-11-19 10:40:09 +0100272 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
273 * create a new one.
274 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200275 if (cs) {
276 cs->ctx = h1s;
277 h1s->cs = cs;
278 }
Christopher Faulet47365272018-10-31 17:40:50 +0100279 else {
280 cs = h1s_new_cs(h1s);
281 if (!cs)
282 goto fail;
283 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200284 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100285
286 fail:
287 pool_free(pool_head_h1s, h1s);
288 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200289}
290
291static void h1s_destroy(struct h1s *h1s)
292{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200293 if (h1s) {
294 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200295
Christopher Fauletf2824e62018-10-01 12:12:37 +0200296 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200297
Christopher Fauletf2824e62018-10-01 12:12:37 +0200298 if (h1s->recv_wait != NULL)
299 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
300 if (h1s->send_wait != NULL)
301 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
302
Christopher Faulet47365272018-10-31 17:40:50 +0100303 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
304 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
305 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200306
Christopher Fauletf2824e62018-10-01 12:12:37 +0200307 cs_free(h1s->cs);
308 pool_free(pool_head_h1s, h1s);
309 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200310}
311
312/*
313 * Initialize the mux once it's attached. It is expected that conn->mux_ctx
314 * points to the existing conn_stream (for outgoing connections) or NULL (for
315 * incoming ones). Returns < 0 on error.
316 */
317static int h1_init(struct connection *conn, struct proxy *proxy)
318{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200319 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200320
321 h1c = pool_alloc(pool_head_h1c);
322 if (!h1c)
323 goto fail_h1c;
324 h1c->conn = conn;
325 h1c->px = proxy;
326
327 h1c->flags = H1C_F_NONE;
328 h1c->ibuf = BUF_NULL;
329 h1c->obuf = BUF_NULL;
330 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200331
Christopher Faulet51dbc942018-09-13 09:05:15 +0200332 LIST_INIT(&h1c->buf_wait.list);
333 h1c->wait_event.task = tasklet_new();
334 if (!h1c->wait_event.task)
335 goto fail;
336 h1c->wait_event.task->process = h1_io_cb;
337 h1c->wait_event.task->context = h1c;
338 h1c->wait_event.wait_reason = 0;
339
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200340 if (!(conn->flags & CO_FL_CONNECTED))
341 h1c->flags |= H1C_F_CS_WAIT_CONN;
342
Christopher Fauletf2824e62018-10-01 12:12:37 +0200343 /* Always Create a new H1S */
344 if (!h1s_create(h1c, conn->mux_ctx))
345 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200346
Christopher Faulet129817b2018-09-20 16:14:40 +0200347 conn->mux_ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200348
Christopher Faulet51dbc942018-09-13 09:05:15 +0200349 /* Try to read, if nothing is available yet we'll just subscribe */
350 if (h1_recv(h1c))
351 h1_process(h1c);
352
353 /* mux->wake will be called soon to complete the operation */
354 return 0;
355
356 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100357 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200358 tasklet_free(h1c->wait_event.task);
359 pool_free(pool_head_h1c, h1c);
360 fail_h1c:
361 return -1;
362}
363
364
365/* release function for a connection. This one should be called to free all
366 * resources allocated to the mux.
367 */
368static void h1_release(struct connection *conn)
369{
370 struct h1c *h1c = conn->mux_ctx;
371
372 LIST_DEL(&conn->list);
373
374 if (h1c) {
375 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
376 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
377 LIST_DEL(&h1c->buf_wait.list);
378 LIST_INIT(&h1c->buf_wait.list);
379 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
380 }
381
382 h1_release_buf(h1c, &h1c->ibuf);
383 h1_release_buf(h1c, &h1c->obuf);
384
Christopher Faulet51dbc942018-09-13 09:05:15 +0200385 if (h1c->wait_event.task)
386 tasklet_free(h1c->wait_event.task);
387
Christopher Fauletf2824e62018-10-01 12:12:37 +0200388 h1s_destroy(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200389 if (h1c->wait_event.wait_reason != 0)
390 conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason,
391 &h1c->wait_event);
392 pool_free(pool_head_h1c, h1c);
393 }
394
395 conn->mux = NULL;
396 conn->mux_ctx = NULL;
397
398 conn_stop_tracking(conn);
399 conn_full_close(conn);
400 if (conn->destroy_cb)
401 conn->destroy_cb(conn);
402 conn_free(conn);
403}
404
405/******************************************************/
406/* functions below are for the H1 protocol processing */
407/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200408/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
409 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200410 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200411static void h1_parse_req_vsn(struct h1m *h1m, const union htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200412{
Christopher Faulet9768c262018-10-22 09:34:31 +0200413 const char *p = sl->rq.l + sl->rq.m_len + sl->rq.u_len;
414
415 if ((sl->rq.v_len == 8) &&
416 (*(p + 5) > '1' ||
417 (*(p + 5) == '1' && *(p + 7) >= '1')))
418 h1m->flags |= H1_MF_VER_11;
419}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200420
Christopher Faulet9768c262018-10-22 09:34:31 +0200421/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
422 * greater or equal to 1.1
423 */
424static void h1_parse_res_vsn(struct h1m *h1m, const union htx_sl *sl)
425{
426 const char *p = sl->rq.l;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200427
Christopher Faulet9768c262018-10-22 09:34:31 +0200428 if ((sl->st.v_len == 8) &&
429 (*(p + 5) > '1' ||
430 (*(p + 5) == '1' && *(p + 7) >= '1')))
431 h1m->flags |= H1_MF_VER_11;
432}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200433
Christopher Faulet9768c262018-10-22 09:34:31 +0200434/*
435 * Check the validity of the request version. If the version is valid, it
436 * returns 1. Otherwise, it returns 0.
437 */
438static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
439{
440 struct h1c *h1c = h1s->h1c;
441
442 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
443 * exactly one digit "." one digit. This check may be disabled using
444 * option accept-invalid-http-request.
445 */
446 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
447 if (sl.rq.v.len != 8)
448 return 0;
449
450 if (*(sl.rq.v.ptr + 4) != '/' ||
451 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
452 *(sl.rq.v.ptr + 6) != '.' ||
453 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
454 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200455 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200456 else if (!sl.rq.v.len) {
457 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200458
Christopher Faulet9768c262018-10-22 09:34:31 +0200459 /* RFC 1945 allows only GET for HTTP/0.9 requests */
460 if (sl.rq.meth != HTTP_METH_GET)
461 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200462
Christopher Faulet9768c262018-10-22 09:34:31 +0200463 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
464 if (!sl.rq.u.len)
465 return 0;
466
467 /* Add HTTP version */
468 sl.rq.v = ist("HTTP/1.0");
469 }
470 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200471}
472
Christopher Faulet9768c262018-10-22 09:34:31 +0200473/*
474 * Check the validity of the response version. If the version is valid, it
475 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200476 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200477static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200478{
Christopher Faulet9768c262018-10-22 09:34:31 +0200479 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200480
Christopher Faulet9768c262018-10-22 09:34:31 +0200481 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
482 * exactly one digit "." one digit. This check may be disabled using
483 * option accept-invalid-http-request.
484 */
485 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
486 if (sl.st.v.len != 8)
487 return 0;
488
489 if (*(sl.st.v.ptr + 4) != '/' ||
490 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
491 *(sl.st.v.ptr + 6) != '.' ||
492 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
493 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200494 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200495 return 1;
496}
497/* Remove all "Connection:" headers from the HTX message <htx> */
498static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
499{
500 struct ist hdr = {.ptr = "Connection", .len = 10};
501 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200502
Christopher Faulet9768c262018-10-22 09:34:31 +0200503 while (http_find_header(htx, hdr, &ctx, 1))
504 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200505
Christopher Faulet9768c262018-10-22 09:34:31 +0200506 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
507}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200508
Christopher Faulet9768c262018-10-22 09:34:31 +0200509/* Add a "Connection:" header with the value <value> into the HTX message
510 * <htx>.
511 */
512static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
513{
514 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200515
Christopher Faulet9768c262018-10-22 09:34:31 +0200516 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200517}
518
519/* Deduce the connection mode of the client connection, depending on the
520 * configuration and the H1 message flags. This function is called twice, the
521 * first time when the request is parsed and the second time when the response
522 * is parsed.
523 */
524static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
525{
526 struct proxy *fe = h1s->h1c->px;
527 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
528
529 /* Tunnel mode can only by set on the frontend */
530 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
531 flag = H1S_F_WANT_TUN;
532 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
533 flag = H1S_F_WANT_CLO;
534
535 /* flags order: CLO > SCL > TUN > KAL */
536 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
537 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
538
539 if (h1m->flags & H1_MF_RESP) {
540 /* Either we've established an explicit tunnel, or we're
541 * switching the protocol. In both cases, we're very unlikely to
542 * understand the next protocols. We have to switch to tunnel
543 * mode, so that we transfer the request and responses then let
544 * this protocol pass unmodified. When we later implement
545 * specific parsers for such protocols, we'll want to check the
546 * Upgrade header which contains information about that protocol
547 * for responses with status 101 (eg: see RFC2817 about TLS).
548 */
549 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
550 h1s->status == 101)
551 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
552 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
553 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
554 }
555 else {
556 if (h1s->flags & H1S_F_WANT_KAL &&
557 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
558 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
559 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
560 }
561
562 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
563 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
564 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
565}
566
567/* Deduce the connection mode of the client connection, depending on the
568 * configuration and the H1 message flags. This function is called twice, the
569 * first time when the request is parsed and the second time when the response
570 * is parsed.
571 */
572static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
573{
574 struct proxy *be = h1s->h1c->px;
575 struct proxy *fe = strm_fe(si_strm(h1s->cs->data));
576 int flag = H1S_F_WANT_KAL;
577
578 /* Tunnel mode can only by set on the frontend */
579 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
580 flag = H1S_F_WANT_TUN;
581
582 /* For the server connection: server-close == httpclose */
583 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
584 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
585 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
586 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
587 flag = H1S_F_WANT_CLO;
588
589 /* flags order: CLO > SCL > TUN > KAL */
590 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
591 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
592
593 if (h1m->flags & H1_MF_RESP) {
594 /* Either we've established an explicit tunnel, or we're
595 * switching the protocol. In both cases, we're very unlikely to
596 * understand the next protocols. We have to switch to tunnel
597 * mode, so that we transfer the request and responses then let
598 * this protocol pass unmodified. When we later implement
599 * specific parsers for such protocols, we'll want to check the
600 * Upgrade header which contains information about that protocol
601 * for responses with status 101 (eg: see RFC2817 about TLS).
602 */
603 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
604 h1s->status == 101)
605 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
606 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
607 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
608 else if (h1s->flags & H1S_F_WANT_KAL &&
609 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
610 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
611 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
612 }
613
614 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
615 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
616 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200617}
618
Christopher Faulet9768c262018-10-22 09:34:31 +0200619static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
620 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200621{
622 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200623
624 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
625 * token is found
626 */
627 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200628 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200629
630 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200631 if (h1m->flags & H1_MF_CONN_CLO) {
632 if (conn_val)
633 *conn_val = ist("");
634 if (htx)
635 h1_remove_conn_hdrs(h1m, htx);
636 }
637 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
638 if (conn_val)
639 *conn_val = ist("keep-alive");
640 if (htx)
641 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
642 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200643 }
644 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200645 if (h1m->flags & H1_MF_CONN_KAL) {
646 if (conn_val)
647 *conn_val = ist("");
648 if (htx)
649 h1_remove_conn_hdrs(h1m, htx);
650 }
651 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
652 if (conn_val)
653 *conn_val = ist("close");
654 if (htx)
655 h1_add_conn_hdr(h1m, htx, ist("close"));
656 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200657 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200658}
659
Christopher Faulet9768c262018-10-22 09:34:31 +0200660static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
661 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200662{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200663 /* 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) {
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 */
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 Faulet9768c262018-10-22 09:34:31 +0200697}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200698
Christopher Faulet9768c262018-10-22 09:34:31 +0200699/* Set the right connection mode and update "Connection:" header if
700 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
701 * message is updated accordingly. When <conn_val> is not NULL, it is set with
702 * the new header value.
703 */
704static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
705 struct htx *htx, struct ist *conn_val)
706{
707 if (!conn_is_back(h1s->h1c->conn)) {
708 h1_set_cli_conn_mode(h1s, h1m);
709 if (h1m->flags & H1_MF_RESP)
710 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
711 }
712 else {
713 h1_set_srv_conn_mode(h1s, h1m);
714 if (!(h1m->flags & H1_MF_RESP))
715 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
716 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200717}
718
Christopher Faulet129817b2018-09-20 16:14:40 +0200719/*
720 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200721 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
722 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet129817b2018-09-20 16:14:40 +0200723 * responsibile to update the parser state <h1m>.
724 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200725static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200726 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200727{
728 struct http_hdr hdrs[MAX_HTTP_HDR];
729 union h1_sl sl;
730 int ret = 0;
731
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100732 if (!max)
733 goto end;
734
Christopher Faulet129817b2018-09-20 16:14:40 +0200735 /* Realing input buffer if necessary */
736 if (b_head(buf) + b_data(buf) > b_wrap(buf))
737 b_slow_realign(buf, trash.area, 0);
738
Christopher Fauletf2824e62018-10-01 12:12:37 +0200739 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Faulet129817b2018-09-20 16:14:40 +0200740 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &sl);
741 if (ret <= 0) {
742 /* Incomplete or invalid message. If the buffer is full, it's an
743 * error because headers are too large to be handled by the
744 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200745 if (ret < 0 || (!ret && b_full(buf)))
746 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200747 goto end;
748 }
749
750 /* messages headers fully parsed, do some checks to prepare the body
751 * parsing.
752 */
753
754 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200755 if (ret > (b_size(buf) - global.tune.maxrewrite))
756 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200757
Christopher Faulet9768c262018-10-22 09:34:31 +0200758 /* Save the request's method or the response's status, check if the body
759 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200760 if (!(h1m->flags & H1_MF_RESP)) {
761 h1s->meth = sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200762
Christopher Faulet129817b2018-09-20 16:14:40 +0200763 /* Request have always a known length */
764 h1m->flags |= H1_MF_XFER_LEN;
765 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
766 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200767
768 if (!h1_process_req_vsn(h1s, h1m, sl)) {
769 h1m->err_pos = sl.rq.v.ptr - b_head(buf);
770 h1m->err_state = h1m->state;
771 goto vsn_error;
772 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200773 }
774 else {
775 h1s->status = sl.st.status;
776
777 if ((h1s->meth == HTTP_METH_HEAD) ||
778 (h1s->status >= 100 && h1s->status < 200) ||
779 (h1s->status == 204) || (h1s->status == 304) ||
780 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
781 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
782 h1m->flags |= H1_MF_XFER_LEN;
783 h1m->curr_len = h1m->body_len = 0;
784 h1m->state = H1_MSG_DONE;
785 }
786 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
787 h1m->flags |= H1_MF_XFER_LEN;
788 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
789 h1m->state = H1_MSG_DONE;
790 }
791 else
792 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200793
794 if (!h1_process_res_vsn(h1s, h1m, sl)) {
795 h1m->err_pos = sl.st.v.ptr - b_head(buf);
796 h1m->err_state = h1m->state;
797 goto vsn_error;
798 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200799 }
800
Christopher Faulet9768c262018-10-22 09:34:31 +0200801 if (!(h1m->flags & H1_MF_RESP)) {
802 if (!htx_add_reqline(htx, sl) || !htx_add_all_headers(htx, hdrs))
803 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200804 }
805 else {
Christopher Faulet9768c262018-10-22 09:34:31 +0200806 if (!htx_add_resline(htx, sl) || !htx_add_all_headers(htx, hdrs))
807 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200808 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200809 if (h1m->state == H1_MSG_DONE)
810 if (!htx_add_endof(htx, HTX_BLK_EOM))
811 goto error;
812
813 h1_process_conn_mode(h1s, h1m, htx, NULL);
814
815 /* If body length cannot be determined, set htx->extra to
816 * ULLONG_MAX. This value is impossible in other cases.
817 */
818 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
819
820 /* Recheck there is enough space to do headers rewritting */
821 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
822 goto error;
823
824 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200825 end:
826 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200827
828 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +0200829 h1m->err_state = h1m->state;
830 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +0200831 vsn_error:
832 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200833 ret = 0;
834 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200835}
836
837/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200838 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
839 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +0200840 * and filling h1s->err_pos and h1s->err_state fields. This functions is
841 * responsibile to update the parser state <h1m>.
842 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200843static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200844 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200845{
Christopher Faulet9768c262018-10-22 09:34:31 +0200846 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +0200847 size_t total = 0;
848 int ret = 0;
849
850 if (h1m->flags & H1_MF_XFER_LEN) {
851 if (h1m->flags & H1_MF_CLEN) {
852 /* content-length: read only h2m->body_len */
853 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200854 if (ret > data_space)
855 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200856 if ((uint64_t)ret > h1m->curr_len)
857 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200858 if (ret > b_contig_data(buf, *ofs))
859 ret = b_contig_data(buf, *ofs);
860 if (ret) {
861 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
862 goto end;
863 h1m->curr_len -= ret;
864 *ofs += ret;
865 total += ret;
866 }
867
868 if (!h1m->curr_len) {
869 if (!htx_add_endof(htx, HTX_BLK_EOM))
870 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200871 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200872 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200873 }
874 else if (h1m->flags & H1_MF_CHNK) {
875 new_chunk:
876 /* te:chunked : parse chunks */
877 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200878 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +0200879 if (ret <= 0)
880 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200881 h1m->state = H1_MSG_CHUNK_SIZE;
882
Christopher Faulet129817b2018-09-20 16:14:40 +0200883 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200884 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200885 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200886 }
887
888 if (h1m->state == H1_MSG_CHUNK_SIZE) {
889 unsigned int chksz;
890
Christopher Fauletf2824e62018-10-01 12:12:37 +0200891 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +0200892 if (ret <= 0)
893 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200894 if (!chksz) {
895 if (!htx_add_endof(htx, HTX_BLK_EOD))
896 goto end;
897 h1m->state = H1_MSG_TRAILERS;
898 }
899 else
900 h1m->state = H1_MSG_DATA;
901
Christopher Faulet129817b2018-09-20 16:14:40 +0200902 h1m->curr_len = chksz;
903 h1m->body_len += chksz;
904 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200905 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200906 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200907 }
908
909 if (h1m->state == H1_MSG_DATA) {
910 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200911 if (ret > data_space)
912 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200913 if ((uint64_t)ret > h1m->curr_len)
914 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200915 if (ret > b_contig_data(buf, *ofs))
916 ret = b_contig_data(buf, *ofs);
917 if (ret) {
918 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
919 goto end;
920 h1m->curr_len -= ret;
921 max -= ret;
922 *ofs += ret;
923 total += ret;
924 }
925 if (!h1m->curr_len) {
926 h1m->state = H1_MSG_CHUNK_CRLF;
927 goto new_chunk;
928 }
929 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200930 }
931
932 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200933 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +0200934 if (ret > data_space)
935 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +0200936 if (ret <= 0)
937 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200938
939 /* Realing input buffer if tailers wrap. For now
940 * this is a workaroung. Because trailers are
941 * not split on CRLF, like headers, there is no
942 * way to know where to split it when trailers
943 * wrap. This is a limitation of
944 * h1_measure_trailers.
945 */
946 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
947 b_slow_realign(buf, trash.area, 0);
948
949 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
950 goto end;
951
Christopher Faulet129817b2018-09-20 16:14:40 +0200952 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200953 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200954 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +0200955
956 /* FIXME: if it fails here, this is a problem,
957 * because there is no way to return here. */
958 if (!htx_add_endof(htx, HTX_BLK_EOM))
959 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200960 h1m->state = H1_MSG_DONE;
961 }
962 }
963 else {
964 /* XFER_LEN is set but not CLEN nor CHNK, it means there
965 * is no body. Switch the message in DONE state
966 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200967 if (!htx_add_endof(htx, HTX_BLK_EOM))
968 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200969 h1m->state = H1_MSG_DONE;
970 }
971 }
972 else {
973 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +0200974 ret = max;
975 if (ret > data_space)
976 ret = data_space;
977 if (ret > b_contig_data(buf, *ofs))
978 ret = b_contig_data(buf, *ofs);
979 if (ret) {
980 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
981 goto end;
982
983 *ofs += max;
984 total = max;
985 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200986 }
987
988 end:
989 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200990 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200991 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200992 h1m->err_pos = *ofs + max + ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200993 return 0;
994 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200995 /* update htx->extra, only when the body length is known */
996 if (h1m->flags & H1_MF_XFER_LEN)
997 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +0200998 return total;
999}
1000
1001/*
1002 * Synchronize the request and the response before reseting them. Except for 1xx
1003 * responses, we wait that the request and the response are in DONE state and
1004 * that all data are forwarded for both. For 1xx responses, only the response is
1005 * reset, waiting the final one. Many 1xx messages can be sent.
1006 */
1007static void h1_sync_messages(struct h1c *h1c)
1008{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001009 struct h1s *h1s = h1c->h1s;
1010
1011 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001012 return;
1013
Christopher Fauletf2824e62018-10-01 12:12:37 +02001014 if (h1s->res.state == H1_MSG_DONE &&
1015 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001016 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001017 /* For 100-Continue response or any other informational 1xx
1018 * response which is non-final, don't reset the request, the
1019 * transaction is not finished. We take care the response was
1020 * transferred before.
1021 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001022 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001023 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001024 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001025 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001026 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1027 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001028 h1m_init_req(&h1s->req);
1029 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001030 h1s->req.state = H1_MSG_TUNNEL;
1031 h1s->res.state = H1_MSG_TUNNEL;
1032 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001033 }
1034}
1035
1036/*
1037 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001038 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1039 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001040 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001041static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001042{
Christopher Faulet539e0292018-11-19 10:40:09 +01001043 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001044 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001045 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001046 size_t total = 0;
1047 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001048 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001049 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001050
Christopher Faulet539e0292018-11-19 10:40:09 +01001051 htx = htx_from_buf(buf);
1052 count = b_data(&h1c->ibuf);
1053 max = htx_free_space(htx);
1054 if (flags & CO_RFL_KEEP_RSV) {
1055 if (max < global.tune.maxrewrite)
1056 goto end;
1057 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001058 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001059 if (count > max)
1060 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001061
Christopher Fauletf2824e62018-10-01 12:12:37 +02001062 if (!conn_is_back(h1c->conn)) {
1063 h1m = &h1s->req;
1064 errflag = H1S_F_REQ_ERROR;
1065 }
1066 else {
1067 h1m = &h1s->res;
1068 errflag = H1S_F_RES_ERROR;
1069 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001070
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001071 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001072 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001073 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001074 if (!ret)
1075 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001076 }
1077 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001078 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001079 if (!ret)
1080 break;
1081 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001082 else if (h1m->state == H1_MSG_DONE)
1083 break;
1084 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001085 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet9768c262018-10-22 09:34:31 +02001086 if (!ret)
1087 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001088 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001089 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001090 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001091 break;
1092 }
1093
Christopher Faulet539e0292018-11-19 10:40:09 +01001094 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001095 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001096
Christopher Faulet47365272018-10-31 17:40:50 +01001097 if (h1s->flags & errflag)
1098 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001099
Christopher Faulet539e0292018-11-19 10:40:09 +01001100 b_del(&h1c->ibuf, total);
1101
1102 end:
1103 if (htx_is_not_empty(htx))
1104 b_set_data(buf, b_size(buf));
1105 else {
1106 htx_reset(htx);
1107 b_set_data(buf, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001108 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001109
Christopher Faulet539e0292018-11-19 10:40:09 +01001110 if (h1c->flags & H1C_F_IN_FULL && b_room(&h1c->ibuf)) {
1111 h1c->flags &= ~H1C_F_IN_FULL;
1112 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001113 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001114
Christopher Faulet9c388402018-11-19 21:54:26 +01001115 if (b_data(&h1c->ibuf)) {
1116 if (!htx_is_empty(htx))
1117 h1s->cs->flags |= CS_FL_RCV_MORE;
1118 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001119 else {
1120 h1_release_buf(h1c, &h1c->ibuf);
1121 h1_sync_messages(h1c);
1122
1123 h1s->cs->flags &= ~CS_FL_RCV_MORE;
1124 if (h1s->cs->flags & CS_FL_REOS)
1125 h1s->cs->flags |= CS_FL_EOS;
1126 }
1127 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001128
1129 parsing_err:
1130 // FIXME: create an error snapshot here
1131 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001132 htx->flags |= HTX_FL_PARSING_ERROR;
1133 b_set_data(buf, b_size(buf));
1134 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001135 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001136}
1137
Christopher Faulet129817b2018-09-20 16:14:40 +02001138/*
1139 * Process outgoing data. It parses data and transfer them from the channel buffer into
1140 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1141 * 0 if it couldn't proceed.
1142 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001143static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1144{
Christopher Faulet129817b2018-09-20 16:14:40 +02001145 struct h1s *h1s = h1c->h1s;
1146 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001147 struct htx *chn_htx;
1148 struct htx_blk *blk;
1149 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001150 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001151 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001152
Christopher Faulet47365272018-10-31 17:40:50 +01001153 if (!count)
1154 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001155 chn_htx = htx_from_buf(buf);
1156
Christopher Faulet51dbc942018-09-13 09:05:15 +02001157 if (!h1_get_buf(h1c, &h1c->obuf)) {
1158 h1c->flags |= H1C_F_OUT_ALLOC;
1159 goto end;
1160 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001161
Christopher Fauletf2824e62018-10-01 12:12:37 +02001162 if (!conn_is_back(h1c->conn)) {
1163 h1m = &h1s->res;
1164 errflag = H1S_F_RES_ERROR;
1165 }
1166 else {
1167 h1m = &h1s->req;
1168 errflag = H1S_F_REQ_ERROR;
1169 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001170
1171
1172 tmp = get_trash_chunk();
1173 tmp->size = b_room(&h1c->obuf);
1174
1175 blk = htx_get_head_blk(chn_htx);
1176 while (!(h1s->flags & errflag) && blk) {
1177 union htx_sl *sl;
1178 struct ist n, v;
1179 uint32_t sz = htx_get_blksz(blk);
1180
1181 if (total + sz > count)
1182 goto copy;
1183
1184 switch (htx_get_blk_type(blk)) {
1185 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001186 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001187
1188 case HTX_BLK_REQ_SL:
1189 sl = htx_get_blk_ptr(chn_htx, blk);
1190 h1s->meth = sl->rq.meth;
1191 h1_parse_req_vsn(h1m, sl);
1192 if (!htx_reqline_to_str(sl, tmp))
1193 goto copy;
1194 h1m->flags |= H1_MF_XFER_LEN;
1195 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001196 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001197
Christopher Faulet9768c262018-10-22 09:34:31 +02001198 case HTX_BLK_RES_SL:
1199 sl = htx_get_blk_ptr(chn_htx, blk);
1200 h1s->status = sl->st.status;
1201 h1_parse_res_vsn(h1m, sl);
1202 if (!htx_stline_to_str(sl, tmp))
1203 goto copy;
1204 if (chn_htx->extra != ULLONG_MAX)
1205 h1m->flags |= H1_MF_XFER_LEN;
1206 h1m->state = H1_MSG_HDR_FIRST;
1207 break;
1208
1209 case HTX_BLK_HDR:
1210 if (h1m->state == H1_MSG_HDR_FIRST) {
1211 struct http_hdr_ctx ctx;
1212
1213 n = ist("Connection");
1214 v = ist("");
1215
1216 /* If there is no "Connection:" header,
1217 * process conn_mode now and add the
1218 * right one.
1219 */
1220 ctx.blk = blk;
1221 if (http_find_header(chn_htx, n, &ctx, 1))
1222 goto process_hdr;
1223 h1_process_conn_mode(h1s, h1m, NULL, &v);
1224 if (!v.len)
1225 goto process_hdr;
1226
1227 if (!htx_hdr_to_str(n, v, tmp))
1228 goto copy;
1229 }
1230 process_hdr:
1231 h1m->state = H1_MSG_HDR_NAME;
1232 n = htx_get_blk_name(chn_htx, blk);
1233 v = htx_get_blk_value(chn_htx, blk);
1234
1235 if (isteqi(n, ist("transfer-encoding")))
1236 h1_parse_xfer_enc_header(h1m, v);
1237 else if (isteqi(n, ist("connection"))) {
1238 h1_parse_connection_header(h1m, v);
1239 h1_process_conn_mode(h1s, h1m, NULL, &v);
1240 if (!v.len)
1241 goto skip_hdr;
1242 }
1243
1244 if (!htx_hdr_to_str(n, v, tmp))
1245 goto copy;
1246 skip_hdr:
1247 h1m->state = H1_MSG_HDR_L2_LWS;
1248 break;
1249
1250 case HTX_BLK_PHDR:
1251 /* not implemented yet */
1252 h1m->flags |= errflag;
1253 break;
1254
1255 case HTX_BLK_EOH:
1256 h1m->state = H1_MSG_LAST_LF;
1257 if (!chunk_memcat(tmp, "\r\n", 2))
1258 goto copy;
1259
1260 h1m->state = H1_MSG_DATA;
1261 break;
1262
1263 case HTX_BLK_DATA:
1264 v = htx_get_blk_value(chn_htx, blk);
1265 if (!htx_data_to_str(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
1266 goto copy;
1267 break;
1268
1269 case HTX_BLK_EOD:
1270 if (!chunk_memcat(tmp, "0\r\n", 3))
1271 goto copy;
1272 h1m->state = H1_MSG_TRAILERS;
1273 break;
1274
1275 case HTX_BLK_TLR:
1276 v = htx_get_blk_value(chn_htx, blk);
1277 if (!htx_trailer_to_str(v, tmp))
1278 goto copy;
1279 break;
1280
1281 case HTX_BLK_EOM:
1282 /* if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(tmp, "\r\n", 2)) */
1283 /* goto copy; */
1284 h1m->state = H1_MSG_DONE;
1285 break;
1286
1287 case HTX_BLK_OOB:
1288 v = htx_get_blk_value(chn_htx, blk);
1289 if (!chunk_memcat(tmp, v.ptr, v.len))
1290 goto copy;
1291 break;
1292
1293 default:
1294 h1m->flags |= errflag;
1295 break;
1296 }
1297 total += sz;
1298 blk = htx_remove_blk(chn_htx, blk);
Christopher Faulet129817b2018-09-20 16:14:40 +02001299 }
1300
Christopher Faulet9768c262018-10-22 09:34:31 +02001301 copy:
1302 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001303
1304 if (b_full(&h1c->obuf))
1305 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001306 if (htx_is_empty(chn_htx)) {
1307 htx_reset(chn_htx);
1308 b_set_data(buf, 0);
1309 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001310 end:
1311 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001312}
1313
Christopher Faulet51dbc942018-09-13 09:05:15 +02001314/*********************************************************/
1315/* functions below are I/O callbacks from the connection */
1316/*********************************************************/
1317/*
1318 * Attempt to read data, and subscribe if none available
1319 */
1320static int h1_recv(struct h1c *h1c)
1321{
1322 struct connection *conn = h1c->conn;
1323 size_t ret, max;
1324 int rcvd = 0;
1325
1326 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
1327 return 0;
1328
Christopher Faulet539e0292018-11-19 10:40:09 +01001329 if (!h1_recv_allowed(h1c))
Christopher Faulet9768c262018-10-22 09:34:31 +02001330 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001331
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001332 if (h1c->h1s && (h1c->h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001333 rcvd = 1;
1334 goto end;
1335 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001336
Christopher Faulet51dbc942018-09-13 09:05:15 +02001337 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1338 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001339 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001340 }
1341
1342 ret = 0;
1343 max = b_room(&h1c->ibuf);
1344 if (max) {
1345 h1c->flags &= ~H1C_F_IN_FULL;
1346 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
1347 }
Christopher Faulet47365272018-10-31 17:40:50 +01001348 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001349 rcvd = 1;
Christopher Faulet47365272018-10-31 17:40:50 +01001350 if (h1c->h1s && h1c->h1s->cs)
1351 h1c->h1s->cs->flags |= CS_FL_READ_PARTIAL;
1352 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001353
1354 if (h1_recv_allowed(h1c))
1355 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet81d48432018-11-19 21:22:43 +01001356 else
1357 rcvd = 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001358
Christopher Faulet9768c262018-10-22 09:34:31 +02001359 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001360 if (!b_data(&h1c->ibuf))
1361 h1_release_buf(h1c, &h1c->ibuf);
1362 else if (b_full(&h1c->ibuf))
1363 h1c->flags |= H1C_F_IN_FULL;
1364 return rcvd;
1365}
1366
1367
1368/*
1369 * Try to send data if possible
1370 */
1371static int h1_send(struct h1c *h1c)
1372{
1373 struct connection *conn = h1c->conn;
1374 unsigned int flags = 0;
1375 size_t ret;
1376 int sent = 0;
1377
1378 if (conn->flags & CO_FL_ERROR)
1379 return 0;
1380
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001381 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1382 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1383 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1384 return 0;
1385 }
1386
Christopher Faulet51dbc942018-09-13 09:05:15 +02001387 if (!b_data(&h1c->obuf))
1388 goto end;
1389
1390 if (h1c->flags & H1C_F_OUT_FULL)
1391 flags |= CO_SFL_MSG_MORE;
1392
1393 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1394 if (ret > 0) {
1395 h1c->flags &= ~H1C_F_OUT_FULL;
1396 b_del(&h1c->obuf, ret);
1397 sent = 1;
1398 }
1399
1400 end:
1401 /* We're done, no more to send */
1402 if (!b_data(&h1c->obuf)) {
1403 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001404 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001405 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1406 h1_shutw_conn(conn);
1407 }
1408 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1409 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1410
1411 return sent;
1412}
1413
1414
1415static void h1_wake_stream(struct h1c *h1c)
1416{
1417 struct connection *conn = h1c->conn;
1418 struct h1s *h1s = h1c->h1s;
1419 uint32_t flags = 0;
1420 int dont_wake = 0;
1421
1422 if (!h1s || !h1s->cs)
1423 return;
1424
1425 if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR))
1426 flags |= CS_FL_ERROR;
1427 if (conn_xprt_read0_pending(conn))
1428 flags |= CS_FL_REOS;
1429
1430 h1s->cs->flags |= flags;
1431 if (h1s->recv_wait) {
1432 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1433 tasklet_wakeup(h1s->recv_wait->task);
1434 h1s->recv_wait = NULL;
1435 dont_wake = 1;
1436 }
1437 if (h1s->send_wait) {
1438 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1439 tasklet_wakeup(h1s->send_wait->task);
1440 h1s->send_wait = NULL;
1441 dont_wake = 1;
1442 }
1443 if (!dont_wake && h1s->cs->data_cb->wake)
1444 h1s->cs->data_cb->wake(h1s->cs);
1445}
1446
1447/* callback called on any event by the connection handler.
1448 * It applies changes and returns zero, or < 0 if it wants immediate
1449 * destruction of the connection.
1450 */
1451static int h1_process(struct h1c * h1c)
1452{
1453 struct connection *conn = h1c->conn;
1454
Christopher Faulet51dbc942018-09-13 09:05:15 +02001455 if (!conn->mux_ctx)
1456 return -1;
1457
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001458 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001459 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1460 goto end;
1461 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001462 }
1463
Christopher Faulet539e0292018-11-19 10:40:09 +01001464 if (!h1c->h1s) {
1465 if (h1c->flags & H1C_F_CS_ERROR ||
1466 conn->flags & CO_FL_ERROR ||
1467 conn_xprt_read0_pending(conn))
1468 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001469 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001470 if (!h1s_create(h1c, NULL))
1471 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001472 }
1473 }
1474
Christopher Faulet539e0292018-11-19 10:40:09 +01001475 h1_wake_stream(h1c);
Christopher Faulet47365272018-10-31 17:40:50 +01001476 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001477 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001478
1479 release:
1480 h1_release(conn);
1481 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001482}
1483
1484static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1485{
1486 struct h1c *h1c = ctx;
1487 int ret = 0;
1488
1489 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1490 ret = h1_send(h1c);
1491 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1492 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001493 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001494 h1_process(h1c);
1495 return NULL;
1496}
1497
1498
1499static int h1_wake(struct connection *conn)
1500{
1501 struct h1c *h1c = conn->mux_ctx;
1502
Christopher Faulet539e0292018-11-19 10:40:09 +01001503 h1_send(h1c);
1504 return h1_process(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001505}
1506
Christopher Faulet51dbc942018-09-13 09:05:15 +02001507/*******************************************/
1508/* functions below are used by the streams */
1509/*******************************************/
1510/*
1511 * Attach a new stream to a connection
1512 * (Used for outgoing connections)
1513 */
1514static struct conn_stream *h1_attach(struct connection *conn)
1515{
1516 struct h1c *h1c = conn->mux_ctx;
1517 struct conn_stream *cs = NULL;
1518 struct h1s *h1s;
1519
1520 if (h1c->flags & H1C_F_CS_ERROR)
1521 goto end;
1522
1523 cs = cs_new(h1c->conn);
1524 if (!cs)
1525 goto end;
1526
Christopher Fauletf2824e62018-10-01 12:12:37 +02001527 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001528 if (h1s == NULL)
1529 goto end;
1530
1531 return cs;
1532 end:
1533 cs_free(cs);
1534 return NULL;
1535}
1536
1537/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1538 * this mux, it's easy as we can only store a single conn_stream.
1539 */
1540static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1541{
1542 struct h1c *h1c = conn->mux_ctx;
1543 struct h1s *h1s = h1c->h1s;
1544
1545 if (h1s)
1546 return h1s->cs;
1547
1548 return NULL;
1549}
1550
1551static void h1_destroy(struct connection *conn)
1552{
1553 struct h1c *h1c = conn->mux_ctx;
1554
1555 if (!h1c->h1s)
1556 h1_release(conn);
1557}
1558
1559/*
1560 * Detach the stream from the connection and possibly release the connection.
1561 */
1562static void h1_detach(struct conn_stream *cs)
1563{
1564 struct h1s *h1s = cs->ctx;
1565 struct h1c *h1c;
1566
1567 cs->ctx = NULL;
1568 if (!h1s)
1569 return;
1570
1571 h1c = h1s->h1c;
1572 h1s->cs = NULL;
1573
1574 h1s_destroy(h1s);
1575
1576 /* We don't want to close right now unless the connection is in error */
1577 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
1578 (h1c->conn->flags & CO_FL_ERROR))
1579 h1_release(h1c->conn);
1580 else
1581 tasklet_wakeup(h1c->wait_event.task);
1582}
1583
1584
1585static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1586{
1587 struct h1s *h1s = cs->ctx;
1588
1589 if (!h1s)
1590 return;
1591
Christopher Fauletf2824e62018-10-01 12:12:37 +02001592 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1593 return;
1594
Christopher Faulet51dbc942018-09-13 09:05:15 +02001595 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1596 if (cs->flags & CS_FL_SHR)
1597 return;
1598 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1599 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1600 if (cs->flags & CS_FL_SHW) {
1601 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1602 conn_full_close(cs->conn);
1603 }
1604}
1605
1606static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1607{
1608 struct h1s *h1s = cs->ctx;
1609 struct h1c *h1c;
1610
1611 if (!h1s)
1612 return;
1613 h1c = h1s->h1c;
1614
Christopher Fauletf2824e62018-10-01 12:12:37 +02001615 if ((h1s->flags & H1S_F_WANT_KAL) &&
1616 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1617 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1618 return;
1619
Christopher Faulet51dbc942018-09-13 09:05:15 +02001620 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1621 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1622 return;
1623
1624 h1_shutw_conn(cs->conn);
1625}
1626
1627static void h1_shutw_conn(struct connection *conn)
1628{
1629 struct h1c *h1c = conn->mux_ctx;
1630
1631 if (conn_xprt_ready(conn) && conn->xprt->shutw)
1632 conn->xprt->shutw(conn, 1);
1633 if (!(conn->flags & CO_FL_SOCK_RD_SH))
1634 conn_sock_shutw(conn, 1);
1635 else {
1636 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1637 conn_full_close(conn);
1638 }
1639}
1640
1641/* Called from the upper layer, to unsubscribe to events */
1642static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1643{
1644 struct wait_event *sw;
1645 struct h1s *h1s = cs->ctx;
1646
1647 if (!h1s)
1648 return 0;
1649
1650 if (event_type & SUB_CAN_RECV) {
1651 sw = param;
1652 if (h1s->recv_wait == sw) {
1653 sw->wait_reason &= ~SUB_CAN_RECV;
1654 h1s->recv_wait = NULL;
1655 }
1656 }
1657 if (event_type & SUB_CAN_SEND) {
1658 sw = param;
1659 if (h1s->send_wait == sw) {
1660 sw->wait_reason &= ~SUB_CAN_SEND;
1661 h1s->send_wait = NULL;
1662 }
1663 }
1664 return 0;
1665}
1666
1667/* Called from the upper layer, to subscribe to events, such as being able to send */
1668static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1669{
1670 struct wait_event *sw;
1671 struct h1s *h1s = cs->ctx;
1672
1673 if (!h1s)
1674 return -1;
1675
1676 switch (event_type) {
1677 case SUB_CAN_RECV:
1678 sw = param;
1679 if (!(sw->wait_reason & SUB_CAN_RECV)) {
1680 sw->wait_reason |= SUB_CAN_RECV;
1681 sw->handle = h1s;
1682 h1s->recv_wait = sw;
1683 }
1684 return 0;
1685 case SUB_CAN_SEND:
1686 sw = param;
1687 if (!(sw->wait_reason & SUB_CAN_SEND)) {
1688 sw->wait_reason |= SUB_CAN_SEND;
1689 sw->handle = h1s;
1690 h1s->send_wait = sw;
1691 }
1692 return 0;
1693 default:
1694 break;
1695 }
1696 return -1;
1697}
1698
1699/* Called from the upper layer, to receive data */
1700static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1701{
1702 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01001703 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001704 size_t ret = 0;
1705
Christopher Faulet539e0292018-11-19 10:40:09 +01001706 if (!(h1c->flags & H1C_F_IN_ALLOC))
1707 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001708
1709 if (flags & CO_RFL_BUF_FLUSH)
1710 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001711 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
1712 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01001713 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1714 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001715 }
1716 return ret;
1717}
1718
1719
1720/* Called from the upper layer, to send data */
1721static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1722{
1723 struct h1s *h1s = cs->ctx;
1724 struct h1c *h1c;
1725 size_t ret = 0;
1726
1727 if (!h1s)
1728 return ret;
1729
1730 h1c = h1s->h1c;
1731
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001732 if (h1c->flags & H1C_F_CS_WAIT_CONN)
1733 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001734
Christopher Faulet539e0292018-11-19 10:40:09 +01001735 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001736 ret = h1_process_output(h1c, buf, count);
1737 if (ret > 0) {
1738 h1_send(h1c);
1739
Christopher Faulet539e0292018-11-19 10:40:09 +01001740 /* We need to do that because of the infinite forwarding. <buf>
1741 * contains HTX messages so when infinite forwarding is enabled,
1742 * count is equal to the buffer size. From outside, the buffer
1743 * appears as full.
1744 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001745 if (!b_data(buf))
1746 ret = count;
1747 }
1748 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001749}
1750
Christopher Faulet1be55f92018-10-02 15:59:23 +02001751#if defined(CONFIG_HAP_LINUX_SPLICE)
1752/* Send and get, using splicing */
1753static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
1754{
1755 struct h1s *h1s = cs->ctx;
1756 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
1757 int ret = 0;
1758
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001759 if (b_data(&h1s->h1c->ibuf)) {
1760 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001761 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001762 }
1763
1764 h1s->flags &= ~H1S_F_BUF_FLUSH;
1765 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001766 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
1767 count = h1m->curr_len;
1768 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
1769 if (h1m->state == H1_MSG_DATA && ret > 0)
1770 h1m->curr_len -= ret;
1771 end:
1772 return ret;
1773
1774}
1775
1776static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
1777{
1778 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001779 int ret = 0;
1780
1781 if (b_data(&h1s->h1c->obuf))
1782 goto end;
1783
1784 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001785 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001786 if (pipe->data) {
1787 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
1788 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
1789 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001790 return ret;
1791}
1792#endif
1793
Christopher Faulet51dbc942018-09-13 09:05:15 +02001794/****************************************/
1795/* MUX initialization and instanciation */
1796/****************************************/
1797
1798/* The mux operations */
1799const struct mux_ops mux_h1_ops = {
1800 .init = h1_init,
1801 .wake = h1_wake,
1802 .attach = h1_attach,
1803 .get_first_cs = h1_get_first_cs,
1804 .detach = h1_detach,
1805 .destroy = h1_destroy,
1806 .avail_streams = h1_avail_streams,
1807 .rcv_buf = h1_rcv_buf,
1808 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02001809#if defined(CONFIG_HAP_LINUX_SPLICE)
1810 .rcv_pipe = h1_rcv_pipe,
1811 .snd_pipe = h1_snd_pipe,
1812#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02001813 .subscribe = h1_subscribe,
1814 .unsubscribe = h1_unsubscribe,
1815 .shutr = h1_shutr,
1816 .shutw = h1_shutw,
1817 .flags = MX_FL_NONE,
1818 .name = "h1",
1819};
1820
1821
1822/* this mux registers default HTX proto */
1823static struct mux_proto_list mux_proto_htx =
1824{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
1825
1826static void __h1_deinit(void)
1827{
1828 pool_destroy(pool_head_h1c);
1829 pool_destroy(pool_head_h1s);
1830}
1831
1832__attribute__((constructor))
1833static void __h1_init(void)
1834{
1835 register_mux_proto(&mux_proto_htx);
1836 hap_register_post_deinit(__h1_deinit);
1837 pool_head_h1c = create_pool("h1c", sizeof(struct h1c), MEM_F_SHARED);
1838 pool_head_h1s = create_pool("h1s", sizeof(struct h1s), MEM_F_SHARED);
1839}
1840/*
1841 * Local variables:
1842 * c-indent-level: 8
1843 * c-basic-offset: 8
1844 * End:
1845 */