blob: bade571a7d035e5bdbe4a88c07ad97df980dd738 [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 Faulet129817b2018-09-20 16:14:40 +020065
Christopher Faulet51dbc942018-09-13 09:05:15 +020066
67/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020068struct h1c {
69 struct connection *conn;
70 struct proxy *px;
71 uint32_t flags; /* Connection flags: H1C_F_* */
72
73 struct buffer ibuf; /* Input buffer to store data before parsing */
74 struct buffer obuf; /* Output buffer to store data after reformatting */
75
76 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
77 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
78
79 struct h1s *h1s; /* H1 stream descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020080};
81
82/* H1 stream descriptor */
83struct h1s {
84 struct h1c *h1c;
85 struct conn_stream *cs;
86 uint32_t flags; /* Connection flags: H1S_F_* */
87
Christopher Faulet51dbc942018-09-13 09:05:15 +020088 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
89 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020090
91 struct h1m req;
92 struct h1m res;
93
94 enum http_meth_t meth; /* HTTP resquest method */
95 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +020096};
97
98/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +010099DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
100DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200101
Christopher Faulet51dbc942018-09-13 09:05:15 +0200102static int h1_recv(struct h1c *h1c);
103static int h1_send(struct h1c *h1c);
104static int h1_process(struct h1c *h1c);
105static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
106static void h1_shutw_conn(struct connection *conn);
107
108/*****************************************************/
109/* functions below are for dynamic buffer management */
110/*****************************************************/
111/*
112 * Indicates whether or not the we may call the h1_recv() function to
113 * attempt to receive data into the buffer and/or parse pending data. The
114 * condition is a bit complex due to some API limits for now. The rules are the
115 * following :
116 * - if an error or a shutdown was detected on the connection and the buffer
117 * is empty, we must not attempt to receive
118 * - if the input buffer failed to be allocated, we must not try to receive
119 * and we know there is nothing pending
120 * - if no flag indicates a blocking condition, we may attempt to receive,
121 * regardless of whether the input buffer is full or not, so that only de
122 * receiving part decides whether or not to block. This is needed because
123 * the connection API indeed prevents us from re-enabling receipt that is
124 * already enabled in a polled state, so we must always immediately stop as
125 * soon as the mux can't proceed so as never to hit an end of read with data
126 * pending in the buffers.
127 * - otherwise must may not attempt to receive
128 */
129static inline int h1_recv_allowed(const struct h1c *h1c)
130{
131 if (b_data(&h1c->ibuf) == 0 &&
Christopher Faulet7e346f32018-11-20 17:13:52 +0100132 (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW) ||
Christopher Faulet51dbc942018-09-13 09:05:15 +0200133 h1c->conn->flags & CO_FL_ERROR ||
134 conn_xprt_read0_pending(h1c->conn)))
135 return 0;
136
137 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL)))
138 return 1;
139
140 return 0;
141}
142
143/*
144 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
145 * flags are used to figure what buffer was requested. It returns 1 if the
146 * allocation succeeds, in which case the connection is woken up, or 0 if it's
147 * impossible to wake up and we prefer to be woken up later.
148 */
149static int h1_buf_available(void *target)
150{
151 struct h1c *h1c = target;
152
153 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
154 h1c->flags &= ~H1C_F_IN_ALLOC;
155 if (h1_recv_allowed(h1c))
156 tasklet_wakeup(h1c->wait_event.task);
157 return 1;
158 }
159
160 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
161 h1c->flags &= ~H1C_F_OUT_ALLOC;
162 tasklet_wakeup(h1c->wait_event.task);
163 return 1;
164 }
165
Christopher Faulet51dbc942018-09-13 09:05:15 +0200166 return 0;
167}
168
169/*
170 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
171 */
172static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
173{
174 struct buffer *buf = NULL;
175
176 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
177 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
178 h1c->buf_wait.target = h1c;
179 h1c->buf_wait.wakeup_cb = h1_buf_available;
180 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
181 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
182 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
183 __conn_xprt_stop_recv(h1c->conn);
184 }
185 return buf;
186}
187
188/*
189 * Release a buffer, if any, and try to wake up entities waiting in the buffer
190 * wait queue.
191 */
192static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
193{
194 if (bptr->size) {
195 b_free(bptr);
196 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
197 }
198}
199
200static int h1_avail_streams(struct connection *conn)
201{
202 struct h1c *h1c = conn->mux_ctx;
203
204 return h1c->h1s ? 0 : 1;
205}
206
207
208/*****************************************************************/
209/* functions below are dedicated to the mux setup and management */
210/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100211static struct conn_stream *h1s_new_cs(struct h1s *h1s)
212{
213 struct conn_stream *cs;
214
215 cs = cs_new(h1s->h1c->conn);
216 if (!cs)
217 goto err;
218 h1s->cs = cs;
219 cs->ctx = h1s;
220
221 if (h1s->flags & H1S_F_NOT_FIRST)
222 cs->flags |= CS_FL_NOT_FIRST;
223
224 if (stream_create_from_cs(cs) < 0)
225 goto err;
226 return cs;
227
228 err:
229 cs_free(cs);
230 h1s->cs = NULL;
231 return NULL;
232}
233
Christopher Fauletf2824e62018-10-01 12:12:37 +0200234static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200235{
236 struct h1s *h1s;
237
238 h1s = pool_alloc(pool_head_h1s);
239 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100240 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200241
242 h1s->h1c = h1c;
243 h1c->h1s = h1s;
244
245 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200246 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200247
248 h1s->recv_wait = NULL;
249 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200250
251 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200252 h1s->req.flags |= H1_MF_NO_PHDR;
253
Christopher Faulet129817b2018-09-20 16:14:40 +0200254 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200255 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200256
257 h1s->status = 0;
258 h1s->meth = HTTP_METH_OTHER;
259
Christopher Faulet47365272018-10-31 17:40:50 +0100260 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
261 h1s->flags |= H1S_F_NOT_FIRST;
262 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
263
Christopher Faulet129817b2018-09-20 16:14:40 +0200264 if (!conn_is_back(h1c->conn)) {
265 if (h1c->px->options2 & PR_O2_REQBUG_OK)
266 h1s->req.err_pos = -1;
267 }
268 else {
269 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
270 h1s->res.err_pos = -1;
271 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200272
Christopher Faulet539e0292018-11-19 10:40:09 +0100273 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
274 * create a new one.
275 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200276 if (cs) {
277 cs->ctx = h1s;
278 h1s->cs = cs;
279 }
Christopher Faulet47365272018-10-31 17:40:50 +0100280 else {
281 cs = h1s_new_cs(h1s);
282 if (!cs)
283 goto fail;
284 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200285 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100286
287 fail:
288 pool_free(pool_head_h1s, h1s);
289 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200290}
291
292static void h1s_destroy(struct h1s *h1s)
293{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200294 if (h1s) {
295 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200296
Christopher Fauletf2824e62018-10-01 12:12:37 +0200297 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200298
Christopher Fauletf2824e62018-10-01 12:12:37 +0200299 if (h1s->recv_wait != NULL)
300 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
301 if (h1s->send_wait != NULL)
302 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
303
Christopher Faulet47365272018-10-31 17:40:50 +0100304 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
305 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
306 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200307
Christopher Fauletf2824e62018-10-01 12:12:37 +0200308 cs_free(h1s->cs);
309 pool_free(pool_head_h1s, h1s);
310 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200311}
312
313/*
314 * Initialize the mux once it's attached. It is expected that conn->mux_ctx
315 * points to the existing conn_stream (for outgoing connections) or NULL (for
316 * incoming ones). Returns < 0 on error.
317 */
318static int h1_init(struct connection *conn, struct proxy *proxy)
319{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200320 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200321
322 h1c = pool_alloc(pool_head_h1c);
323 if (!h1c)
324 goto fail_h1c;
325 h1c->conn = conn;
326 h1c->px = proxy;
327
328 h1c->flags = H1C_F_NONE;
329 h1c->ibuf = BUF_NULL;
330 h1c->obuf = BUF_NULL;
331 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200332
Christopher Faulet51dbc942018-09-13 09:05:15 +0200333 LIST_INIT(&h1c->buf_wait.list);
334 h1c->wait_event.task = tasklet_new();
335 if (!h1c->wait_event.task)
336 goto fail;
337 h1c->wait_event.task->process = h1_io_cb;
338 h1c->wait_event.task->context = h1c;
339 h1c->wait_event.wait_reason = 0;
340
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200341 if (!(conn->flags & CO_FL_CONNECTED))
342 h1c->flags |= H1C_F_CS_WAIT_CONN;
343
Christopher Fauletf2824e62018-10-01 12:12:37 +0200344 /* Always Create a new H1S */
345 if (!h1s_create(h1c, conn->mux_ctx))
346 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200347
Christopher Faulet129817b2018-09-20 16:14:40 +0200348 conn->mux_ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200349
Christopher Faulet51dbc942018-09-13 09:05:15 +0200350 /* Try to read, if nothing is available yet we'll just subscribe */
351 if (h1_recv(h1c))
352 h1_process(h1c);
353
354 /* mux->wake will be called soon to complete the operation */
355 return 0;
356
357 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100358 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200359 tasklet_free(h1c->wait_event.task);
360 pool_free(pool_head_h1c, h1c);
361 fail_h1c:
362 return -1;
363}
364
365
366/* release function for a connection. This one should be called to free all
367 * resources allocated to the mux.
368 */
369static void h1_release(struct connection *conn)
370{
371 struct h1c *h1c = conn->mux_ctx;
372
373 LIST_DEL(&conn->list);
374
375 if (h1c) {
376 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
377 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
378 LIST_DEL(&h1c->buf_wait.list);
379 LIST_INIT(&h1c->buf_wait.list);
380 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
381 }
382
383 h1_release_buf(h1c, &h1c->ibuf);
384 h1_release_buf(h1c, &h1c->obuf);
385
Christopher Faulet51dbc942018-09-13 09:05:15 +0200386 if (h1c->wait_event.task)
387 tasklet_free(h1c->wait_event.task);
388
Christopher Fauletf2824e62018-10-01 12:12:37 +0200389 h1s_destroy(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200390 if (h1c->wait_event.wait_reason != 0)
391 conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason,
392 &h1c->wait_event);
393 pool_free(pool_head_h1c, h1c);
394 }
395
396 conn->mux = NULL;
397 conn->mux_ctx = NULL;
398
399 conn_stop_tracking(conn);
400 conn_full_close(conn);
401 if (conn->destroy_cb)
402 conn->destroy_cb(conn);
403 conn_free(conn);
404}
405
406/******************************************************/
407/* functions below are for the H1 protocol processing */
408/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200409/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
410 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200411 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200412static void h1_parse_req_vsn(struct h1m *h1m, const union htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200413{
Christopher Faulet9768c262018-10-22 09:34:31 +0200414 const char *p = sl->rq.l + sl->rq.m_len + sl->rq.u_len;
415
416 if ((sl->rq.v_len == 8) &&
417 (*(p + 5) > '1' ||
418 (*(p + 5) == '1' && *(p + 7) >= '1')))
419 h1m->flags |= H1_MF_VER_11;
420}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200421
Christopher Faulet9768c262018-10-22 09:34:31 +0200422/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
423 * greater or equal to 1.1
424 */
425static void h1_parse_res_vsn(struct h1m *h1m, const union htx_sl *sl)
426{
427 const char *p = sl->rq.l;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200428
Christopher Faulet9768c262018-10-22 09:34:31 +0200429 if ((sl->st.v_len == 8) &&
430 (*(p + 5) > '1' ||
431 (*(p + 5) == '1' && *(p + 7) >= '1')))
432 h1m->flags |= H1_MF_VER_11;
433}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200434
Christopher Faulet9768c262018-10-22 09:34:31 +0200435/*
436 * Check the validity of the request version. If the version is valid, it
437 * returns 1. Otherwise, it returns 0.
438 */
439static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
440{
441 struct h1c *h1c = h1s->h1c;
442
443 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
444 * exactly one digit "." one digit. This check may be disabled using
445 * option accept-invalid-http-request.
446 */
447 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
448 if (sl.rq.v.len != 8)
449 return 0;
450
451 if (*(sl.rq.v.ptr + 4) != '/' ||
452 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
453 *(sl.rq.v.ptr + 6) != '.' ||
454 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
455 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200456 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200457 else if (!sl.rq.v.len) {
458 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200459
Christopher Faulet9768c262018-10-22 09:34:31 +0200460 /* RFC 1945 allows only GET for HTTP/0.9 requests */
461 if (sl.rq.meth != HTTP_METH_GET)
462 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200463
Christopher Faulet9768c262018-10-22 09:34:31 +0200464 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
465 if (!sl.rq.u.len)
466 return 0;
467
468 /* Add HTTP version */
469 sl.rq.v = ist("HTTP/1.0");
470 }
471 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200472}
473
Christopher Faulet9768c262018-10-22 09:34:31 +0200474/*
475 * Check the validity of the response version. If the version is valid, it
476 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200477 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200478static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200479{
Christopher Faulet9768c262018-10-22 09:34:31 +0200480 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200481
Christopher Faulet9768c262018-10-22 09:34:31 +0200482 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
483 * exactly one digit "." one digit. This check may be disabled using
484 * option accept-invalid-http-request.
485 */
486 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
487 if (sl.st.v.len != 8)
488 return 0;
489
490 if (*(sl.st.v.ptr + 4) != '/' ||
491 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
492 *(sl.st.v.ptr + 6) != '.' ||
493 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
494 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200495 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200496 return 1;
497}
498/* Remove all "Connection:" headers from the HTX message <htx> */
499static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
500{
501 struct ist hdr = {.ptr = "Connection", .len = 10};
502 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200503
Christopher Faulet9768c262018-10-22 09:34:31 +0200504 while (http_find_header(htx, hdr, &ctx, 1))
505 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200506
Christopher Faulet9768c262018-10-22 09:34:31 +0200507 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
508}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200509
Christopher Faulet9768c262018-10-22 09:34:31 +0200510/* Add a "Connection:" header with the value <value> into the HTX message
511 * <htx>.
512 */
513static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
514{
515 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200516
Christopher Faulet9768c262018-10-22 09:34:31 +0200517 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200518}
519
520/* Deduce the connection mode of the client connection, depending on the
521 * configuration and the H1 message flags. This function is called twice, the
522 * first time when the request is parsed and the second time when the response
523 * is parsed.
524 */
525static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
526{
527 struct proxy *fe = h1s->h1c->px;
528 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
529
530 /* Tunnel mode can only by set on the frontend */
531 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
532 flag = H1S_F_WANT_TUN;
533 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
534 flag = H1S_F_WANT_CLO;
535
536 /* flags order: CLO > SCL > TUN > KAL */
537 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
538 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
539
540 if (h1m->flags & H1_MF_RESP) {
541 /* Either we've established an explicit tunnel, or we're
542 * switching the protocol. In both cases, we're very unlikely to
543 * understand the next protocols. We have to switch to tunnel
544 * mode, so that we transfer the request and responses then let
545 * this protocol pass unmodified. When we later implement
546 * specific parsers for such protocols, we'll want to check the
547 * Upgrade header which contains information about that protocol
548 * for responses with status 101 (eg: see RFC2817 about TLS).
549 */
550 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
551 h1s->status == 101)
552 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
553 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
554 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
555 }
556 else {
557 if (h1s->flags & H1S_F_WANT_KAL &&
558 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
559 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
560 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
561 }
562
563 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
564 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
565 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
566}
567
568/* Deduce the connection mode of the client connection, depending on the
569 * configuration and the H1 message flags. This function is called twice, the
570 * first time when the request is parsed and the second time when the response
571 * is parsed.
572 */
573static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
574{
575 struct proxy *be = h1s->h1c->px;
576 struct proxy *fe = strm_fe(si_strm(h1s->cs->data));
577 int flag = H1S_F_WANT_KAL;
578
579 /* Tunnel mode can only by set on the frontend */
580 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
581 flag = H1S_F_WANT_TUN;
582
583 /* For the server connection: server-close == httpclose */
584 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
585 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
586 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
587 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
588 flag = H1S_F_WANT_CLO;
589
590 /* flags order: CLO > SCL > TUN > KAL */
591 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
592 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
593
594 if (h1m->flags & H1_MF_RESP) {
595 /* Either we've established an explicit tunnel, or we're
596 * switching the protocol. In both cases, we're very unlikely to
597 * understand the next protocols. We have to switch to tunnel
598 * mode, so that we transfer the request and responses then let
599 * this protocol pass unmodified. When we later implement
600 * specific parsers for such protocols, we'll want to check the
601 * Upgrade header which contains information about that protocol
602 * for responses with status 101 (eg: see RFC2817 about TLS).
603 */
604 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
605 h1s->status == 101)
606 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
607 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
608 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
609 else if (h1s->flags & H1S_F_WANT_KAL &&
610 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
611 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
612 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
613 }
614
615 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
616 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
617 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200618}
619
Christopher Faulet9768c262018-10-22 09:34:31 +0200620static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
621 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200622{
623 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200624
625 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
626 * token is found
627 */
628 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200629 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200630
631 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200632 if (h1m->flags & H1_MF_CONN_CLO) {
633 if (conn_val)
634 *conn_val = ist("");
635 if (htx)
636 h1_remove_conn_hdrs(h1m, htx);
637 }
638 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
639 if (conn_val)
640 *conn_val = ist("keep-alive");
641 if (htx)
642 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
643 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200644 }
645 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200646 if (h1m->flags & H1_MF_CONN_KAL) {
647 if (conn_val)
648 *conn_val = ist("");
649 if (htx)
650 h1_remove_conn_hdrs(h1m, htx);
651 }
652 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
653 if (conn_val)
654 *conn_val = ist("close");
655 if (htx)
656 h1_add_conn_hdr(h1m, htx, ist("close"));
657 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200658 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200659}
660
Christopher Faulet9768c262018-10-22 09:34:31 +0200661static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
662 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200663{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200664 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
665 * token is found
666 */
667 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200668 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200669
670 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200671 if (h1m->flags & H1_MF_CONN_CLO) {
672 if (conn_val)
673 *conn_val = ist("");
674 if (htx)
675 h1_remove_conn_hdrs(h1m, htx);
676 }
677 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
678 if (conn_val)
679 *conn_val = ist("keep-alive");
680 if (htx)
681 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
682 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200683 }
684 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200685 if (h1m->flags & H1_MF_CONN_KAL) {
686 if (conn_val)
687 *conn_val = ist("");
688 if (htx)
689 h1_remove_conn_hdrs(h1m, htx);
690 }
691 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
692 if (conn_val)
693 *conn_val = ist("close");
694 if (htx)
695 h1_add_conn_hdr(h1m, htx, ist("close"));
696 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200697 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200698}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200699
Christopher Faulet9768c262018-10-22 09:34:31 +0200700/* Set the right connection mode and update "Connection:" header if
701 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
702 * message is updated accordingly. When <conn_val> is not NULL, it is set with
703 * the new header value.
704 */
705static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
706 struct htx *htx, struct ist *conn_val)
707{
708 if (!conn_is_back(h1s->h1c->conn)) {
709 h1_set_cli_conn_mode(h1s, h1m);
710 if (h1m->flags & H1_MF_RESP)
711 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
712 }
713 else {
714 h1_set_srv_conn_mode(h1s, h1m);
715 if (!(h1m->flags & H1_MF_RESP))
716 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
717 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200718}
719
Christopher Faulet129817b2018-09-20 16:14:40 +0200720/*
721 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200722 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
723 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet129817b2018-09-20 16:14:40 +0200724 * responsibile to update the parser state <h1m>.
725 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200726static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200727 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200728{
729 struct http_hdr hdrs[MAX_HTTP_HDR];
730 union h1_sl sl;
731 int ret = 0;
732
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100733 if (!max)
734 goto end;
735
Christopher Faulet129817b2018-09-20 16:14:40 +0200736 /* Realing input buffer if necessary */
737 if (b_head(buf) + b_data(buf) > b_wrap(buf))
738 b_slow_realign(buf, trash.area, 0);
739
Christopher Fauletf2824e62018-10-01 12:12:37 +0200740 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Faulet129817b2018-09-20 16:14:40 +0200741 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &sl);
742 if (ret <= 0) {
743 /* Incomplete or invalid message. If the buffer is full, it's an
744 * error because headers are too large to be handled by the
745 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200746 if (ret < 0 || (!ret && b_full(buf)))
747 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200748 goto end;
749 }
750
751 /* messages headers fully parsed, do some checks to prepare the body
752 * parsing.
753 */
754
755 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200756 if (ret > (b_size(buf) - global.tune.maxrewrite))
757 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200758
Christopher Faulet9768c262018-10-22 09:34:31 +0200759 /* Save the request's method or the response's status, check if the body
760 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200761 if (!(h1m->flags & H1_MF_RESP)) {
762 h1s->meth = sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200763
Christopher Faulet129817b2018-09-20 16:14:40 +0200764 /* Request have always a known length */
765 h1m->flags |= H1_MF_XFER_LEN;
766 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
767 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200768
769 if (!h1_process_req_vsn(h1s, h1m, sl)) {
770 h1m->err_pos = sl.rq.v.ptr - b_head(buf);
771 h1m->err_state = h1m->state;
772 goto vsn_error;
773 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200774 }
775 else {
776 h1s->status = sl.st.status;
777
778 if ((h1s->meth == HTTP_METH_HEAD) ||
779 (h1s->status >= 100 && h1s->status < 200) ||
780 (h1s->status == 204) || (h1s->status == 304) ||
781 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
782 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
783 h1m->flags |= H1_MF_XFER_LEN;
784 h1m->curr_len = h1m->body_len = 0;
785 h1m->state = H1_MSG_DONE;
786 }
787 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
788 h1m->flags |= H1_MF_XFER_LEN;
789 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
790 h1m->state = H1_MSG_DONE;
791 }
792 else
793 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200794
795 if (!h1_process_res_vsn(h1s, h1m, sl)) {
796 h1m->err_pos = sl.st.v.ptr - b_head(buf);
797 h1m->err_state = h1m->state;
798 goto vsn_error;
799 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200800 }
801
Christopher Faulet9768c262018-10-22 09:34:31 +0200802 if (!(h1m->flags & H1_MF_RESP)) {
803 if (!htx_add_reqline(htx, sl) || !htx_add_all_headers(htx, hdrs))
804 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200805 }
806 else {
Christopher Faulet9768c262018-10-22 09:34:31 +0200807 if (!htx_add_resline(htx, sl) || !htx_add_all_headers(htx, hdrs))
808 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200809 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200810 if (h1m->state == H1_MSG_DONE)
811 if (!htx_add_endof(htx, HTX_BLK_EOM))
812 goto error;
813
814 h1_process_conn_mode(h1s, h1m, htx, NULL);
815
816 /* If body length cannot be determined, set htx->extra to
817 * ULLONG_MAX. This value is impossible in other cases.
818 */
819 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
820
821 /* Recheck there is enough space to do headers rewritting */
822 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
823 goto error;
824
825 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200826 end:
827 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200828
829 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +0200830 h1m->err_state = h1m->state;
831 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +0200832 vsn_error:
833 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200834 ret = 0;
835 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200836}
837
838/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200839 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
840 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +0200841 * and filling h1s->err_pos and h1s->err_state fields. This functions is
842 * responsibile to update the parser state <h1m>.
843 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200844static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200845 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200846{
Christopher Faulet9768c262018-10-22 09:34:31 +0200847 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +0200848 size_t total = 0;
849 int ret = 0;
850
851 if (h1m->flags & H1_MF_XFER_LEN) {
852 if (h1m->flags & H1_MF_CLEN) {
853 /* content-length: read only h2m->body_len */
854 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200855 if (ret > data_space)
856 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200857 if ((uint64_t)ret > h1m->curr_len)
858 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200859 if (ret > b_contig_data(buf, *ofs))
860 ret = b_contig_data(buf, *ofs);
861 if (ret) {
862 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
863 goto end;
864 h1m->curr_len -= ret;
865 *ofs += ret;
866 total += ret;
867 }
868
869 if (!h1m->curr_len) {
870 if (!htx_add_endof(htx, HTX_BLK_EOM))
871 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200872 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200873 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200874 }
875 else if (h1m->flags & H1_MF_CHNK) {
876 new_chunk:
877 /* te:chunked : parse chunks */
878 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200879 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +0200880 if (ret <= 0)
881 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200882 h1m->state = H1_MSG_CHUNK_SIZE;
883
Christopher Faulet129817b2018-09-20 16:14:40 +0200884 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200885 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200886 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200887 }
888
889 if (h1m->state == H1_MSG_CHUNK_SIZE) {
890 unsigned int chksz;
891
Christopher Fauletf2824e62018-10-01 12:12:37 +0200892 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +0200893 if (ret <= 0)
894 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200895 if (!chksz) {
896 if (!htx_add_endof(htx, HTX_BLK_EOD))
897 goto end;
898 h1m->state = H1_MSG_TRAILERS;
899 }
900 else
901 h1m->state = H1_MSG_DATA;
902
Christopher Faulet129817b2018-09-20 16:14:40 +0200903 h1m->curr_len = chksz;
904 h1m->body_len += chksz;
905 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200906 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200907 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200908 }
909
910 if (h1m->state == H1_MSG_DATA) {
911 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200912 if (ret > data_space)
913 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200914 if ((uint64_t)ret > h1m->curr_len)
915 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200916 if (ret > b_contig_data(buf, *ofs))
917 ret = b_contig_data(buf, *ofs);
918 if (ret) {
919 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
920 goto end;
921 h1m->curr_len -= ret;
922 max -= ret;
923 *ofs += ret;
924 total += ret;
925 }
926 if (!h1m->curr_len) {
927 h1m->state = H1_MSG_CHUNK_CRLF;
928 goto new_chunk;
929 }
930 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200931 }
932
933 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200934 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +0200935 if (ret > data_space)
936 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +0200937 if (ret <= 0)
938 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200939
940 /* Realing input buffer if tailers wrap. For now
941 * this is a workaroung. Because trailers are
942 * not split on CRLF, like headers, there is no
943 * way to know where to split it when trailers
944 * wrap. This is a limitation of
945 * h1_measure_trailers.
946 */
947 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
948 b_slow_realign(buf, trash.area, 0);
949
950 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
951 goto end;
952
Christopher Faulet129817b2018-09-20 16:14:40 +0200953 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200954 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200955 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +0200956
957 /* FIXME: if it fails here, this is a problem,
958 * because there is no way to return here. */
959 if (!htx_add_endof(htx, HTX_BLK_EOM))
960 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200961 h1m->state = H1_MSG_DONE;
962 }
963 }
964 else {
965 /* XFER_LEN is set but not CLEN nor CHNK, it means there
966 * is no body. Switch the message in DONE state
967 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200968 if (!htx_add_endof(htx, HTX_BLK_EOM))
969 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200970 h1m->state = H1_MSG_DONE;
971 }
972 }
973 else {
974 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +0200975 ret = max;
976 if (ret > data_space)
977 ret = data_space;
978 if (ret > b_contig_data(buf, *ofs))
979 ret = b_contig_data(buf, *ofs);
980 if (ret) {
981 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
982 goto end;
983
984 *ofs += max;
985 total = max;
986 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200987 }
988
989 end:
990 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200991 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200992 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200993 h1m->err_pos = *ofs + max + ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200994 return 0;
995 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200996 /* update htx->extra, only when the body length is known */
997 if (h1m->flags & H1_MF_XFER_LEN)
998 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +0200999 return total;
1000}
1001
1002/*
1003 * Synchronize the request and the response before reseting them. Except for 1xx
1004 * responses, we wait that the request and the response are in DONE state and
1005 * that all data are forwarded for both. For 1xx responses, only the response is
1006 * reset, waiting the final one. Many 1xx messages can be sent.
1007 */
1008static void h1_sync_messages(struct h1c *h1c)
1009{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001010 struct h1s *h1s = h1c->h1s;
1011
1012 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001013 return;
1014
Christopher Fauletf2824e62018-10-01 12:12:37 +02001015 if (h1s->res.state == H1_MSG_DONE &&
1016 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001017 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001018 /* For 100-Continue response or any other informational 1xx
1019 * response which is non-final, don't reset the request, the
1020 * transaction is not finished. We take care the response was
1021 * transferred before.
1022 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001023 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001024 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001026 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001027 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1028 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001029 h1m_init_req(&h1s->req);
1030 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001031 h1s->req.state = H1_MSG_TUNNEL;
1032 h1s->res.state = H1_MSG_TUNNEL;
1033 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001034 }
1035}
1036
1037/*
1038 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001039 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1040 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001041 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001042static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001043{
Christopher Faulet539e0292018-11-19 10:40:09 +01001044 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001045 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001046 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001047 size_t total = 0;
1048 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001049 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001050 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001051
Christopher Faulet539e0292018-11-19 10:40:09 +01001052 htx = htx_from_buf(buf);
1053 count = b_data(&h1c->ibuf);
1054 max = htx_free_space(htx);
1055 if (flags & CO_RFL_KEEP_RSV) {
1056 if (max < global.tune.maxrewrite)
1057 goto end;
1058 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001059 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001060 if (count > max)
1061 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001062
Christopher Fauletf2824e62018-10-01 12:12:37 +02001063 if (!conn_is_back(h1c->conn)) {
1064 h1m = &h1s->req;
1065 errflag = H1S_F_REQ_ERROR;
1066 }
1067 else {
1068 h1m = &h1s->res;
1069 errflag = H1S_F_RES_ERROR;
1070 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001071
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001072 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001073 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001074 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001075 if (!ret)
1076 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001077 }
1078 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001079 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001080 if (!ret)
1081 break;
1082 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001083 else if (h1m->state == H1_MSG_DONE)
1084 break;
1085 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001086 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet9768c262018-10-22 09:34:31 +02001087 if (!ret)
1088 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001089 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001090 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001091 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001092 break;
1093 }
1094
Christopher Faulet539e0292018-11-19 10:40:09 +01001095 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001096 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001097
Christopher Faulet47365272018-10-31 17:40:50 +01001098 if (h1s->flags & errflag)
1099 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001100
Christopher Faulet539e0292018-11-19 10:40:09 +01001101 b_del(&h1c->ibuf, total);
1102
1103 end:
1104 if (htx_is_not_empty(htx))
1105 b_set_data(buf, b_size(buf));
1106 else {
1107 htx_reset(htx);
1108 b_set_data(buf, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001109 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001110
Christopher Faulet539e0292018-11-19 10:40:09 +01001111 if (h1c->flags & H1C_F_IN_FULL && b_room(&h1c->ibuf)) {
1112 h1c->flags &= ~H1C_F_IN_FULL;
1113 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001114 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001115
Christopher Faulet9c388402018-11-19 21:54:26 +01001116 if (b_data(&h1c->ibuf)) {
1117 if (!htx_is_empty(htx))
1118 h1s->cs->flags |= CS_FL_RCV_MORE;
1119 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001120 else {
1121 h1_release_buf(h1c, &h1c->ibuf);
1122 h1_sync_messages(h1c);
1123
1124 h1s->cs->flags &= ~CS_FL_RCV_MORE;
1125 if (h1s->cs->flags & CS_FL_REOS)
1126 h1s->cs->flags |= CS_FL_EOS;
1127 }
1128 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001129
1130 parsing_err:
1131 // FIXME: create an error snapshot here
1132 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001133 htx->flags |= HTX_FL_PARSING_ERROR;
1134 b_set_data(buf, b_size(buf));
1135 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001136 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001137}
1138
Christopher Faulet129817b2018-09-20 16:14:40 +02001139/*
1140 * Process outgoing data. It parses data and transfer them from the channel buffer into
1141 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1142 * 0 if it couldn't proceed.
1143 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001144static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1145{
Christopher Faulet129817b2018-09-20 16:14:40 +02001146 struct h1s *h1s = h1c->h1s;
1147 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001148 struct htx *chn_htx;
1149 struct htx_blk *blk;
1150 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001151 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001152 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001153
Christopher Faulet47365272018-10-31 17:40:50 +01001154 if (!count)
1155 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001156 chn_htx = htx_from_buf(buf);
1157
Christopher Faulet51dbc942018-09-13 09:05:15 +02001158 if (!h1_get_buf(h1c, &h1c->obuf)) {
1159 h1c->flags |= H1C_F_OUT_ALLOC;
1160 goto end;
1161 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001162
Christopher Fauletf2824e62018-10-01 12:12:37 +02001163 if (!conn_is_back(h1c->conn)) {
1164 h1m = &h1s->res;
1165 errflag = H1S_F_RES_ERROR;
1166 }
1167 else {
1168 h1m = &h1s->req;
1169 errflag = H1S_F_REQ_ERROR;
1170 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001171
1172
1173 tmp = get_trash_chunk();
1174 tmp->size = b_room(&h1c->obuf);
1175
1176 blk = htx_get_head_blk(chn_htx);
1177 while (!(h1s->flags & errflag) && blk) {
1178 union htx_sl *sl;
1179 struct ist n, v;
1180 uint32_t sz = htx_get_blksz(blk);
1181
1182 if (total + sz > count)
1183 goto copy;
1184
1185 switch (htx_get_blk_type(blk)) {
1186 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001187 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001188
1189 case HTX_BLK_REQ_SL:
1190 sl = htx_get_blk_ptr(chn_htx, blk);
1191 h1s->meth = sl->rq.meth;
1192 h1_parse_req_vsn(h1m, sl);
1193 if (!htx_reqline_to_str(sl, tmp))
1194 goto copy;
1195 h1m->flags |= H1_MF_XFER_LEN;
1196 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001197 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001198
Christopher Faulet9768c262018-10-22 09:34:31 +02001199 case HTX_BLK_RES_SL:
1200 sl = htx_get_blk_ptr(chn_htx, blk);
1201 h1s->status = sl->st.status;
1202 h1_parse_res_vsn(h1m, sl);
1203 if (!htx_stline_to_str(sl, tmp))
1204 goto copy;
1205 if (chn_htx->extra != ULLONG_MAX)
1206 h1m->flags |= H1_MF_XFER_LEN;
1207 h1m->state = H1_MSG_HDR_FIRST;
1208 break;
1209
1210 case HTX_BLK_HDR:
1211 if (h1m->state == H1_MSG_HDR_FIRST) {
1212 struct http_hdr_ctx ctx;
1213
1214 n = ist("Connection");
1215 v = ist("");
1216
1217 /* If there is no "Connection:" header,
1218 * process conn_mode now and add the
1219 * right one.
1220 */
1221 ctx.blk = blk;
1222 if (http_find_header(chn_htx, n, &ctx, 1))
1223 goto process_hdr;
1224 h1_process_conn_mode(h1s, h1m, NULL, &v);
1225 if (!v.len)
1226 goto process_hdr;
1227
1228 if (!htx_hdr_to_str(n, v, tmp))
1229 goto copy;
1230 }
1231 process_hdr:
1232 h1m->state = H1_MSG_HDR_NAME;
1233 n = htx_get_blk_name(chn_htx, blk);
1234 v = htx_get_blk_value(chn_htx, blk);
1235
1236 if (isteqi(n, ist("transfer-encoding")))
1237 h1_parse_xfer_enc_header(h1m, v);
1238 else if (isteqi(n, ist("connection"))) {
1239 h1_parse_connection_header(h1m, v);
1240 h1_process_conn_mode(h1s, h1m, NULL, &v);
1241 if (!v.len)
1242 goto skip_hdr;
1243 }
1244
1245 if (!htx_hdr_to_str(n, v, tmp))
1246 goto copy;
1247 skip_hdr:
1248 h1m->state = H1_MSG_HDR_L2_LWS;
1249 break;
1250
1251 case HTX_BLK_PHDR:
1252 /* not implemented yet */
1253 h1m->flags |= errflag;
1254 break;
1255
1256 case HTX_BLK_EOH:
1257 h1m->state = H1_MSG_LAST_LF;
1258 if (!chunk_memcat(tmp, "\r\n", 2))
1259 goto copy;
1260
1261 h1m->state = H1_MSG_DATA;
1262 break;
1263
1264 case HTX_BLK_DATA:
1265 v = htx_get_blk_value(chn_htx, blk);
1266 if (!htx_data_to_str(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
1267 goto copy;
1268 break;
1269
1270 case HTX_BLK_EOD:
1271 if (!chunk_memcat(tmp, "0\r\n", 3))
1272 goto copy;
1273 h1m->state = H1_MSG_TRAILERS;
1274 break;
1275
1276 case HTX_BLK_TLR:
1277 v = htx_get_blk_value(chn_htx, blk);
1278 if (!htx_trailer_to_str(v, tmp))
1279 goto copy;
1280 break;
1281
1282 case HTX_BLK_EOM:
1283 /* if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(tmp, "\r\n", 2)) */
1284 /* goto copy; */
1285 h1m->state = H1_MSG_DONE;
1286 break;
1287
1288 case HTX_BLK_OOB:
1289 v = htx_get_blk_value(chn_htx, blk);
1290 if (!chunk_memcat(tmp, v.ptr, v.len))
1291 goto copy;
1292 break;
1293
1294 default:
1295 h1m->flags |= errflag;
1296 break;
1297 }
1298 total += sz;
1299 blk = htx_remove_blk(chn_htx, blk);
Christopher Faulet129817b2018-09-20 16:14:40 +02001300 }
1301
Christopher Faulet9768c262018-10-22 09:34:31 +02001302 copy:
1303 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001304
1305 if (b_full(&h1c->obuf))
1306 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001307 if (htx_is_empty(chn_htx)) {
1308 htx_reset(chn_htx);
1309 b_set_data(buf, 0);
1310 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001311 end:
1312 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001313}
1314
Christopher Faulet51dbc942018-09-13 09:05:15 +02001315/*********************************************************/
1316/* functions below are I/O callbacks from the connection */
1317/*********************************************************/
1318/*
1319 * Attempt to read data, and subscribe if none available
1320 */
1321static int h1_recv(struct h1c *h1c)
1322{
1323 struct connection *conn = h1c->conn;
1324 size_t ret, max;
1325 int rcvd = 0;
1326
1327 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
1328 return 0;
1329
Christopher Faulet539e0292018-11-19 10:40:09 +01001330 if (!h1_recv_allowed(h1c))
Christopher Faulet9768c262018-10-22 09:34:31 +02001331 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001332
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001333 if (h1c->h1s && (h1c->h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 rcvd = 1;
1335 goto end;
1336 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001337
Christopher Faulet51dbc942018-09-13 09:05:15 +02001338 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1339 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001340 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001341 }
1342
1343 ret = 0;
1344 max = b_room(&h1c->ibuf);
1345 if (max) {
1346 h1c->flags &= ~H1C_F_IN_FULL;
1347 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
1348 }
Christopher Faulet47365272018-10-31 17:40:50 +01001349 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001350 rcvd = 1;
Christopher Faulet47365272018-10-31 17:40:50 +01001351 if (h1c->h1s && h1c->h1s->cs)
1352 h1c->h1s->cs->flags |= CS_FL_READ_PARTIAL;
1353 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001354
1355 if (h1_recv_allowed(h1c))
1356 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet81d48432018-11-19 21:22:43 +01001357 else
1358 rcvd = 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001359
Christopher Faulet9768c262018-10-22 09:34:31 +02001360 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001361 if (!b_data(&h1c->ibuf))
1362 h1_release_buf(h1c, &h1c->ibuf);
1363 else if (b_full(&h1c->ibuf))
1364 h1c->flags |= H1C_F_IN_FULL;
1365 return rcvd;
1366}
1367
1368
1369/*
1370 * Try to send data if possible
1371 */
1372static int h1_send(struct h1c *h1c)
1373{
1374 struct connection *conn = h1c->conn;
1375 unsigned int flags = 0;
1376 size_t ret;
1377 int sent = 0;
1378
1379 if (conn->flags & CO_FL_ERROR)
1380 return 0;
1381
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001382 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1383 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1384 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1385 return 0;
1386 }
1387
Christopher Faulet51dbc942018-09-13 09:05:15 +02001388 if (!b_data(&h1c->obuf))
1389 goto end;
1390
1391 if (h1c->flags & H1C_F_OUT_FULL)
1392 flags |= CO_SFL_MSG_MORE;
1393
1394 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1395 if (ret > 0) {
1396 h1c->flags &= ~H1C_F_OUT_FULL;
1397 b_del(&h1c->obuf, ret);
1398 sent = 1;
1399 }
1400
1401 end:
1402 /* We're done, no more to send */
1403 if (!b_data(&h1c->obuf)) {
1404 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001405 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001406 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1407 h1_shutw_conn(conn);
1408 }
1409 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1410 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1411
1412 return sent;
1413}
1414
1415
1416static void h1_wake_stream(struct h1c *h1c)
1417{
1418 struct connection *conn = h1c->conn;
1419 struct h1s *h1s = h1c->h1s;
1420 uint32_t flags = 0;
1421 int dont_wake = 0;
1422
1423 if (!h1s || !h1s->cs)
1424 return;
1425
1426 if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR))
1427 flags |= CS_FL_ERROR;
1428 if (conn_xprt_read0_pending(conn))
1429 flags |= CS_FL_REOS;
1430
1431 h1s->cs->flags |= flags;
1432 if (h1s->recv_wait) {
1433 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1434 tasklet_wakeup(h1s->recv_wait->task);
1435 h1s->recv_wait = NULL;
1436 dont_wake = 1;
1437 }
1438 if (h1s->send_wait) {
1439 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1440 tasklet_wakeup(h1s->send_wait->task);
1441 h1s->send_wait = NULL;
1442 dont_wake = 1;
1443 }
1444 if (!dont_wake && h1s->cs->data_cb->wake)
1445 h1s->cs->data_cb->wake(h1s->cs);
1446}
1447
1448/* callback called on any event by the connection handler.
1449 * It applies changes and returns zero, or < 0 if it wants immediate
1450 * destruction of the connection.
1451 */
1452static int h1_process(struct h1c * h1c)
1453{
1454 struct connection *conn = h1c->conn;
1455
Christopher Faulet51dbc942018-09-13 09:05:15 +02001456 if (!conn->mux_ctx)
1457 return -1;
1458
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001459 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001460 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1461 goto end;
1462 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001463 }
1464
Christopher Faulet539e0292018-11-19 10:40:09 +01001465 if (!h1c->h1s) {
1466 if (h1c->flags & H1C_F_CS_ERROR ||
1467 conn->flags & CO_FL_ERROR ||
1468 conn_xprt_read0_pending(conn))
1469 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001470 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001471 if (!h1s_create(h1c, NULL))
1472 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001473 }
1474 }
1475
Christopher Faulet539e0292018-11-19 10:40:09 +01001476 h1_wake_stream(h1c);
Christopher Faulet47365272018-10-31 17:40:50 +01001477 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001478 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001479
1480 release:
1481 h1_release(conn);
1482 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001483}
1484
1485static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1486{
1487 struct h1c *h1c = ctx;
1488 int ret = 0;
1489
1490 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1491 ret = h1_send(h1c);
1492 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1493 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001494 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001495 h1_process(h1c);
1496 return NULL;
1497}
1498
1499
1500static int h1_wake(struct connection *conn)
1501{
1502 struct h1c *h1c = conn->mux_ctx;
1503
Christopher Faulet539e0292018-11-19 10:40:09 +01001504 h1_send(h1c);
1505 return h1_process(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001506}
1507
Christopher Faulet51dbc942018-09-13 09:05:15 +02001508/*******************************************/
1509/* functions below are used by the streams */
1510/*******************************************/
1511/*
1512 * Attach a new stream to a connection
1513 * (Used for outgoing connections)
1514 */
1515static struct conn_stream *h1_attach(struct connection *conn)
1516{
1517 struct h1c *h1c = conn->mux_ctx;
1518 struct conn_stream *cs = NULL;
1519 struct h1s *h1s;
1520
1521 if (h1c->flags & H1C_F_CS_ERROR)
1522 goto end;
1523
1524 cs = cs_new(h1c->conn);
1525 if (!cs)
1526 goto end;
1527
Christopher Fauletf2824e62018-10-01 12:12:37 +02001528 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001529 if (h1s == NULL)
1530 goto end;
1531
1532 return cs;
1533 end:
1534 cs_free(cs);
1535 return NULL;
1536}
1537
1538/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1539 * this mux, it's easy as we can only store a single conn_stream.
1540 */
1541static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1542{
1543 struct h1c *h1c = conn->mux_ctx;
1544 struct h1s *h1s = h1c->h1s;
1545
1546 if (h1s)
1547 return h1s->cs;
1548
1549 return NULL;
1550}
1551
1552static void h1_destroy(struct connection *conn)
1553{
1554 struct h1c *h1c = conn->mux_ctx;
1555
1556 if (!h1c->h1s)
1557 h1_release(conn);
1558}
1559
1560/*
1561 * Detach the stream from the connection and possibly release the connection.
1562 */
1563static void h1_detach(struct conn_stream *cs)
1564{
1565 struct h1s *h1s = cs->ctx;
1566 struct h1c *h1c;
1567
1568 cs->ctx = NULL;
1569 if (!h1s)
1570 return;
1571
1572 h1c = h1s->h1c;
1573 h1s->cs = NULL;
1574
1575 h1s_destroy(h1s);
1576
1577 /* We don't want to close right now unless the connection is in error */
1578 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
1579 (h1c->conn->flags & CO_FL_ERROR))
1580 h1_release(h1c->conn);
1581 else
1582 tasklet_wakeup(h1c->wait_event.task);
1583}
1584
1585
1586static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1587{
1588 struct h1s *h1s = cs->ctx;
1589
1590 if (!h1s)
1591 return;
1592
Christopher Fauletf2824e62018-10-01 12:12:37 +02001593 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1594 return;
1595
Christopher Faulet51dbc942018-09-13 09:05:15 +02001596 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1597 if (cs->flags & CS_FL_SHR)
1598 return;
1599 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1600 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1601 if (cs->flags & CS_FL_SHW) {
1602 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1603 conn_full_close(cs->conn);
1604 }
1605}
1606
1607static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1608{
1609 struct h1s *h1s = cs->ctx;
1610 struct h1c *h1c;
1611
1612 if (!h1s)
1613 return;
1614 h1c = h1s->h1c;
1615
Christopher Fauletf2824e62018-10-01 12:12:37 +02001616 if ((h1s->flags & H1S_F_WANT_KAL) &&
1617 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1618 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1619 return;
1620
Christopher Faulet51dbc942018-09-13 09:05:15 +02001621 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1622 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1623 return;
1624
1625 h1_shutw_conn(cs->conn);
1626}
1627
1628static void h1_shutw_conn(struct connection *conn)
1629{
1630 struct h1c *h1c = conn->mux_ctx;
1631
1632 if (conn_xprt_ready(conn) && conn->xprt->shutw)
1633 conn->xprt->shutw(conn, 1);
1634 if (!(conn->flags & CO_FL_SOCK_RD_SH))
1635 conn_sock_shutw(conn, 1);
1636 else {
1637 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1638 conn_full_close(conn);
1639 }
1640}
1641
1642/* Called from the upper layer, to unsubscribe to events */
1643static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1644{
1645 struct wait_event *sw;
1646 struct h1s *h1s = cs->ctx;
1647
1648 if (!h1s)
1649 return 0;
1650
1651 if (event_type & SUB_CAN_RECV) {
1652 sw = param;
1653 if (h1s->recv_wait == sw) {
1654 sw->wait_reason &= ~SUB_CAN_RECV;
1655 h1s->recv_wait = NULL;
1656 }
1657 }
1658 if (event_type & SUB_CAN_SEND) {
1659 sw = param;
1660 if (h1s->send_wait == sw) {
1661 sw->wait_reason &= ~SUB_CAN_SEND;
1662 h1s->send_wait = NULL;
1663 }
1664 }
1665 return 0;
1666}
1667
1668/* Called from the upper layer, to subscribe to events, such as being able to send */
1669static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1670{
1671 struct wait_event *sw;
1672 struct h1s *h1s = cs->ctx;
1673
1674 if (!h1s)
1675 return -1;
1676
1677 switch (event_type) {
1678 case SUB_CAN_RECV:
1679 sw = param;
1680 if (!(sw->wait_reason & SUB_CAN_RECV)) {
1681 sw->wait_reason |= SUB_CAN_RECV;
1682 sw->handle = h1s;
1683 h1s->recv_wait = sw;
1684 }
1685 return 0;
1686 case SUB_CAN_SEND:
1687 sw = param;
1688 if (!(sw->wait_reason & SUB_CAN_SEND)) {
1689 sw->wait_reason |= SUB_CAN_SEND;
1690 sw->handle = h1s;
1691 h1s->send_wait = sw;
1692 }
1693 return 0;
1694 default:
1695 break;
1696 }
1697 return -1;
1698}
1699
1700/* Called from the upper layer, to receive data */
1701static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1702{
1703 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01001704 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001705 size_t ret = 0;
1706
Christopher Faulet539e0292018-11-19 10:40:09 +01001707 if (!(h1c->flags & H1C_F_IN_ALLOC))
1708 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001709
1710 if (flags & CO_RFL_BUF_FLUSH)
1711 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001712 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
1713 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01001714 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1715 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001716 }
1717 return ret;
1718}
1719
1720
1721/* Called from the upper layer, to send data */
1722static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1723{
1724 struct h1s *h1s = cs->ctx;
1725 struct h1c *h1c;
1726 size_t ret = 0;
1727
1728 if (!h1s)
1729 return ret;
1730
1731 h1c = h1s->h1c;
1732
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001733 if (h1c->flags & H1C_F_CS_WAIT_CONN)
1734 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001735
Christopher Faulet539e0292018-11-19 10:40:09 +01001736 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001737 ret = h1_process_output(h1c, buf, count);
1738 if (ret > 0) {
1739 h1_send(h1c);
1740
Christopher Faulet539e0292018-11-19 10:40:09 +01001741 /* We need to do that because of the infinite forwarding. <buf>
1742 * contains HTX messages so when infinite forwarding is enabled,
1743 * count is equal to the buffer size. From outside, the buffer
1744 * appears as full.
1745 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001746 if (!b_data(buf))
1747 ret = count;
1748 }
1749 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001750}
1751
Christopher Faulet1be55f92018-10-02 15:59:23 +02001752#if defined(CONFIG_HAP_LINUX_SPLICE)
1753/* Send and get, using splicing */
1754static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
1755{
1756 struct h1s *h1s = cs->ctx;
1757 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
1758 int ret = 0;
1759
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001760 if (b_data(&h1s->h1c->ibuf)) {
1761 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001762 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001763 }
1764
1765 h1s->flags &= ~H1S_F_BUF_FLUSH;
1766 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001767 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
1768 count = h1m->curr_len;
1769 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
1770 if (h1m->state == H1_MSG_DATA && ret > 0)
1771 h1m->curr_len -= ret;
1772 end:
1773 return ret;
1774
1775}
1776
1777static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
1778{
1779 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001780 int ret = 0;
1781
1782 if (b_data(&h1s->h1c->obuf))
1783 goto end;
1784
1785 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001786 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001787 if (pipe->data) {
1788 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
1789 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
1790 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001791 return ret;
1792}
1793#endif
1794
Christopher Faulet51dbc942018-09-13 09:05:15 +02001795/****************************************/
1796/* MUX initialization and instanciation */
1797/****************************************/
1798
1799/* The mux operations */
1800const struct mux_ops mux_h1_ops = {
1801 .init = h1_init,
1802 .wake = h1_wake,
1803 .attach = h1_attach,
1804 .get_first_cs = h1_get_first_cs,
1805 .detach = h1_detach,
1806 .destroy = h1_destroy,
1807 .avail_streams = h1_avail_streams,
1808 .rcv_buf = h1_rcv_buf,
1809 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02001810#if defined(CONFIG_HAP_LINUX_SPLICE)
1811 .rcv_pipe = h1_rcv_pipe,
1812 .snd_pipe = h1_snd_pipe,
1813#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02001814 .subscribe = h1_subscribe,
1815 .unsubscribe = h1_unsubscribe,
1816 .shutr = h1_shutr,
1817 .shutw = h1_shutw,
1818 .flags = MX_FL_NONE,
1819 .name = "h1",
1820};
1821
1822
1823/* this mux registers default HTX proto */
1824static struct mux_proto_list mux_proto_htx =
1825{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
1826
Willy Tarreau0108d902018-11-25 19:14:37 +01001827INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
1828
Christopher Faulet51dbc942018-09-13 09:05:15 +02001829/*
1830 * Local variables:
1831 * c-indent-level: 8
1832 * c-basic-offset: 8
1833 * End:
1834 */