blob: 1ceac128aabacbc199a237fad805d08109c959d9 [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;
Christopher Faulet5999b862018-11-27 10:46:09 +01001222 ctx.value = ist(NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +02001223 if (http_find_header(chn_htx, n, &ctx, 1))
1224 goto process_hdr;
1225 h1_process_conn_mode(h1s, h1m, NULL, &v);
1226 if (!v.len)
1227 goto process_hdr;
1228
1229 if (!htx_hdr_to_str(n, v, tmp))
1230 goto copy;
1231 }
1232 process_hdr:
1233 h1m->state = H1_MSG_HDR_NAME;
1234 n = htx_get_blk_name(chn_htx, blk);
1235 v = htx_get_blk_value(chn_htx, blk);
1236
1237 if (isteqi(n, ist("transfer-encoding")))
1238 h1_parse_xfer_enc_header(h1m, v);
1239 else if (isteqi(n, ist("connection"))) {
1240 h1_parse_connection_header(h1m, v);
1241 h1_process_conn_mode(h1s, h1m, NULL, &v);
1242 if (!v.len)
1243 goto skip_hdr;
1244 }
1245
1246 if (!htx_hdr_to_str(n, v, tmp))
1247 goto copy;
1248 skip_hdr:
1249 h1m->state = H1_MSG_HDR_L2_LWS;
1250 break;
1251
1252 case HTX_BLK_PHDR:
1253 /* not implemented yet */
1254 h1m->flags |= errflag;
1255 break;
1256
1257 case HTX_BLK_EOH:
1258 h1m->state = H1_MSG_LAST_LF;
1259 if (!chunk_memcat(tmp, "\r\n", 2))
1260 goto copy;
1261
1262 h1m->state = H1_MSG_DATA;
1263 break;
1264
1265 case HTX_BLK_DATA:
1266 v = htx_get_blk_value(chn_htx, blk);
1267 if (!htx_data_to_str(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
1268 goto copy;
1269 break;
1270
1271 case HTX_BLK_EOD:
1272 if (!chunk_memcat(tmp, "0\r\n", 3))
1273 goto copy;
1274 h1m->state = H1_MSG_TRAILERS;
1275 break;
1276
1277 case HTX_BLK_TLR:
1278 v = htx_get_blk_value(chn_htx, blk);
1279 if (!htx_trailer_to_str(v, tmp))
1280 goto copy;
1281 break;
1282
1283 case HTX_BLK_EOM:
1284 /* if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(tmp, "\r\n", 2)) */
1285 /* goto copy; */
1286 h1m->state = H1_MSG_DONE;
1287 break;
1288
1289 case HTX_BLK_OOB:
1290 v = htx_get_blk_value(chn_htx, blk);
1291 if (!chunk_memcat(tmp, v.ptr, v.len))
1292 goto copy;
1293 break;
1294
1295 default:
1296 h1m->flags |= errflag;
1297 break;
1298 }
1299 total += sz;
1300 blk = htx_remove_blk(chn_htx, blk);
Christopher Faulet129817b2018-09-20 16:14:40 +02001301 }
1302
Christopher Faulet9768c262018-10-22 09:34:31 +02001303 copy:
1304 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001305
1306 if (b_full(&h1c->obuf))
1307 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001308 if (htx_is_empty(chn_htx)) {
1309 htx_reset(chn_htx);
1310 b_set_data(buf, 0);
1311 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001312 end:
1313 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001314}
1315
Christopher Faulet51dbc942018-09-13 09:05:15 +02001316/*********************************************************/
1317/* functions below are I/O callbacks from the connection */
1318/*********************************************************/
1319/*
1320 * Attempt to read data, and subscribe if none available
1321 */
1322static int h1_recv(struct h1c *h1c)
1323{
1324 struct connection *conn = h1c->conn;
1325 size_t ret, max;
1326 int rcvd = 0;
1327
1328 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
1329 return 0;
1330
Christopher Faulet539e0292018-11-19 10:40:09 +01001331 if (!h1_recv_allowed(h1c))
Christopher Faulet9768c262018-10-22 09:34:31 +02001332 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001333
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001334 if (h1c->h1s && (h1c->h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001335 rcvd = 1;
1336 goto end;
1337 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001338
Christopher Faulet51dbc942018-09-13 09:05:15 +02001339 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1340 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001341 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001342 }
1343
1344 ret = 0;
1345 max = b_room(&h1c->ibuf);
1346 if (max) {
1347 h1c->flags &= ~H1C_F_IN_FULL;
1348 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
1349 }
Christopher Faulet47365272018-10-31 17:40:50 +01001350 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001351 rcvd = 1;
Christopher Faulet47365272018-10-31 17:40:50 +01001352 if (h1c->h1s && h1c->h1s->cs)
1353 h1c->h1s->cs->flags |= CS_FL_READ_PARTIAL;
1354 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001355
1356 if (h1_recv_allowed(h1c))
1357 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet81d48432018-11-19 21:22:43 +01001358 else
1359 rcvd = 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001360
Christopher Faulet9768c262018-10-22 09:34:31 +02001361 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001362 if (!b_data(&h1c->ibuf))
1363 h1_release_buf(h1c, &h1c->ibuf);
1364 else if (b_full(&h1c->ibuf))
1365 h1c->flags |= H1C_F_IN_FULL;
1366 return rcvd;
1367}
1368
1369
1370/*
1371 * Try to send data if possible
1372 */
1373static int h1_send(struct h1c *h1c)
1374{
1375 struct connection *conn = h1c->conn;
1376 unsigned int flags = 0;
1377 size_t ret;
1378 int sent = 0;
1379
1380 if (conn->flags & CO_FL_ERROR)
1381 return 0;
1382
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001383 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1384 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1385 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1386 return 0;
1387 }
1388
Christopher Faulet51dbc942018-09-13 09:05:15 +02001389 if (!b_data(&h1c->obuf))
1390 goto end;
1391
1392 if (h1c->flags & H1C_F_OUT_FULL)
1393 flags |= CO_SFL_MSG_MORE;
1394
1395 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1396 if (ret > 0) {
1397 h1c->flags &= ~H1C_F_OUT_FULL;
1398 b_del(&h1c->obuf, ret);
1399 sent = 1;
1400 }
1401
1402 end:
1403 /* We're done, no more to send */
1404 if (!b_data(&h1c->obuf)) {
1405 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001406 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001407 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1408 h1_shutw_conn(conn);
1409 }
1410 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1411 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1412
1413 return sent;
1414}
1415
1416
1417static void h1_wake_stream(struct h1c *h1c)
1418{
1419 struct connection *conn = h1c->conn;
1420 struct h1s *h1s = h1c->h1s;
1421 uint32_t flags = 0;
1422 int dont_wake = 0;
1423
1424 if (!h1s || !h1s->cs)
1425 return;
1426
1427 if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR))
1428 flags |= CS_FL_ERROR;
1429 if (conn_xprt_read0_pending(conn))
1430 flags |= CS_FL_REOS;
1431
1432 h1s->cs->flags |= flags;
1433 if (h1s->recv_wait) {
1434 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1435 tasklet_wakeup(h1s->recv_wait->task);
1436 h1s->recv_wait = NULL;
1437 dont_wake = 1;
1438 }
1439 if (h1s->send_wait) {
1440 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1441 tasklet_wakeup(h1s->send_wait->task);
1442 h1s->send_wait = NULL;
1443 dont_wake = 1;
1444 }
1445 if (!dont_wake && h1s->cs->data_cb->wake)
1446 h1s->cs->data_cb->wake(h1s->cs);
1447}
1448
1449/* callback called on any event by the connection handler.
1450 * It applies changes and returns zero, or < 0 if it wants immediate
1451 * destruction of the connection.
1452 */
1453static int h1_process(struct h1c * h1c)
1454{
1455 struct connection *conn = h1c->conn;
1456
Christopher Faulet51dbc942018-09-13 09:05:15 +02001457 if (!conn->mux_ctx)
1458 return -1;
1459
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001460 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001461 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1462 goto end;
1463 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001464 }
1465
Christopher Faulet539e0292018-11-19 10:40:09 +01001466 if (!h1c->h1s) {
1467 if (h1c->flags & H1C_F_CS_ERROR ||
1468 conn->flags & CO_FL_ERROR ||
1469 conn_xprt_read0_pending(conn))
1470 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001471 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001472 if (!h1s_create(h1c, NULL))
1473 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001474 }
1475 }
1476
Christopher Faulet539e0292018-11-19 10:40:09 +01001477 h1_wake_stream(h1c);
Christopher Faulet47365272018-10-31 17:40:50 +01001478 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001479 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001480
1481 release:
1482 h1_release(conn);
1483 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001484}
1485
1486static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1487{
1488 struct h1c *h1c = ctx;
1489 int ret = 0;
1490
1491 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1492 ret = h1_send(h1c);
1493 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1494 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001495 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001496 h1_process(h1c);
1497 return NULL;
1498}
1499
1500
1501static int h1_wake(struct connection *conn)
1502{
1503 struct h1c *h1c = conn->mux_ctx;
1504
Christopher Faulet539e0292018-11-19 10:40:09 +01001505 h1_send(h1c);
1506 return h1_process(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001507}
1508
Christopher Faulet51dbc942018-09-13 09:05:15 +02001509/*******************************************/
1510/* functions below are used by the streams */
1511/*******************************************/
1512/*
1513 * Attach a new stream to a connection
1514 * (Used for outgoing connections)
1515 */
1516static struct conn_stream *h1_attach(struct connection *conn)
1517{
1518 struct h1c *h1c = conn->mux_ctx;
1519 struct conn_stream *cs = NULL;
1520 struct h1s *h1s;
1521
1522 if (h1c->flags & H1C_F_CS_ERROR)
1523 goto end;
1524
1525 cs = cs_new(h1c->conn);
1526 if (!cs)
1527 goto end;
1528
Christopher Fauletf2824e62018-10-01 12:12:37 +02001529 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001530 if (h1s == NULL)
1531 goto end;
1532
1533 return cs;
1534 end:
1535 cs_free(cs);
1536 return NULL;
1537}
1538
1539/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1540 * this mux, it's easy as we can only store a single conn_stream.
1541 */
1542static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1543{
1544 struct h1c *h1c = conn->mux_ctx;
1545 struct h1s *h1s = h1c->h1s;
1546
1547 if (h1s)
1548 return h1s->cs;
1549
1550 return NULL;
1551}
1552
1553static void h1_destroy(struct connection *conn)
1554{
1555 struct h1c *h1c = conn->mux_ctx;
1556
1557 if (!h1c->h1s)
1558 h1_release(conn);
1559}
1560
1561/*
1562 * Detach the stream from the connection and possibly release the connection.
1563 */
1564static void h1_detach(struct conn_stream *cs)
1565{
1566 struct h1s *h1s = cs->ctx;
1567 struct h1c *h1c;
1568
1569 cs->ctx = NULL;
1570 if (!h1s)
1571 return;
1572
1573 h1c = h1s->h1c;
1574 h1s->cs = NULL;
1575
1576 h1s_destroy(h1s);
1577
1578 /* We don't want to close right now unless the connection is in error */
1579 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
1580 (h1c->conn->flags & CO_FL_ERROR))
1581 h1_release(h1c->conn);
1582 else
1583 tasklet_wakeup(h1c->wait_event.task);
1584}
1585
1586
1587static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1588{
1589 struct h1s *h1s = cs->ctx;
1590
1591 if (!h1s)
1592 return;
1593
Christopher Fauletf2824e62018-10-01 12:12:37 +02001594 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1595 return;
1596
Christopher Faulet51dbc942018-09-13 09:05:15 +02001597 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1598 if (cs->flags & CS_FL_SHR)
1599 return;
1600 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1601 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1602 if (cs->flags & CS_FL_SHW) {
1603 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1604 conn_full_close(cs->conn);
1605 }
1606}
1607
1608static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1609{
1610 struct h1s *h1s = cs->ctx;
1611 struct h1c *h1c;
1612
1613 if (!h1s)
1614 return;
1615 h1c = h1s->h1c;
1616
Christopher Fauletf2824e62018-10-01 12:12:37 +02001617 if ((h1s->flags & H1S_F_WANT_KAL) &&
1618 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1619 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1620 return;
1621
Christopher Faulet51dbc942018-09-13 09:05:15 +02001622 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1623 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1624 return;
1625
1626 h1_shutw_conn(cs->conn);
1627}
1628
1629static void h1_shutw_conn(struct connection *conn)
1630{
1631 struct h1c *h1c = conn->mux_ctx;
1632
1633 if (conn_xprt_ready(conn) && conn->xprt->shutw)
1634 conn->xprt->shutw(conn, 1);
1635 if (!(conn->flags & CO_FL_SOCK_RD_SH))
1636 conn_sock_shutw(conn, 1);
1637 else {
1638 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1639 conn_full_close(conn);
1640 }
1641}
1642
1643/* Called from the upper layer, to unsubscribe to events */
1644static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1645{
1646 struct wait_event *sw;
1647 struct h1s *h1s = cs->ctx;
1648
1649 if (!h1s)
1650 return 0;
1651
1652 if (event_type & SUB_CAN_RECV) {
1653 sw = param;
1654 if (h1s->recv_wait == sw) {
1655 sw->wait_reason &= ~SUB_CAN_RECV;
1656 h1s->recv_wait = NULL;
1657 }
1658 }
1659 if (event_type & SUB_CAN_SEND) {
1660 sw = param;
1661 if (h1s->send_wait == sw) {
1662 sw->wait_reason &= ~SUB_CAN_SEND;
1663 h1s->send_wait = NULL;
1664 }
1665 }
1666 return 0;
1667}
1668
1669/* Called from the upper layer, to subscribe to events, such as being able to send */
1670static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1671{
1672 struct wait_event *sw;
1673 struct h1s *h1s = cs->ctx;
1674
1675 if (!h1s)
1676 return -1;
1677
1678 switch (event_type) {
1679 case SUB_CAN_RECV:
1680 sw = param;
1681 if (!(sw->wait_reason & SUB_CAN_RECV)) {
1682 sw->wait_reason |= SUB_CAN_RECV;
1683 sw->handle = h1s;
1684 h1s->recv_wait = sw;
1685 }
1686 return 0;
1687 case SUB_CAN_SEND:
1688 sw = param;
1689 if (!(sw->wait_reason & SUB_CAN_SEND)) {
1690 sw->wait_reason |= SUB_CAN_SEND;
1691 sw->handle = h1s;
1692 h1s->send_wait = sw;
1693 }
1694 return 0;
1695 default:
1696 break;
1697 }
1698 return -1;
1699}
1700
1701/* Called from the upper layer, to receive data */
1702static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1703{
1704 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01001705 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001706 size_t ret = 0;
1707
Christopher Faulet539e0292018-11-19 10:40:09 +01001708 if (!(h1c->flags & H1C_F_IN_ALLOC))
1709 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001710
1711 if (flags & CO_RFL_BUF_FLUSH)
1712 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001713 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
1714 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01001715 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1716 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001717 }
1718 return ret;
1719}
1720
1721
1722/* Called from the upper layer, to send data */
1723static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1724{
1725 struct h1s *h1s = cs->ctx;
1726 struct h1c *h1c;
1727 size_t ret = 0;
1728
1729 if (!h1s)
1730 return ret;
1731
1732 h1c = h1s->h1c;
1733
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001734 if (h1c->flags & H1C_F_CS_WAIT_CONN)
1735 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001736
Christopher Faulet539e0292018-11-19 10:40:09 +01001737 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001738 ret = h1_process_output(h1c, buf, count);
1739 if (ret > 0) {
1740 h1_send(h1c);
1741
Christopher Faulet539e0292018-11-19 10:40:09 +01001742 /* We need to do that because of the infinite forwarding. <buf>
1743 * contains HTX messages so when infinite forwarding is enabled,
1744 * count is equal to the buffer size. From outside, the buffer
1745 * appears as full.
1746 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001747 if (!b_data(buf))
1748 ret = count;
1749 }
1750 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001751}
1752
Christopher Faulet1be55f92018-10-02 15:59:23 +02001753#if defined(CONFIG_HAP_LINUX_SPLICE)
1754/* Send and get, using splicing */
1755static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
1756{
1757 struct h1s *h1s = cs->ctx;
1758 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
1759 int ret = 0;
1760
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001761 if (b_data(&h1s->h1c->ibuf)) {
1762 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001763 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001764 }
1765
1766 h1s->flags &= ~H1S_F_BUF_FLUSH;
1767 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001768 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
1769 count = h1m->curr_len;
1770 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
1771 if (h1m->state == H1_MSG_DATA && ret > 0)
1772 h1m->curr_len -= ret;
1773 end:
1774 return ret;
1775
1776}
1777
1778static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
1779{
1780 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001781 int ret = 0;
1782
1783 if (b_data(&h1s->h1c->obuf))
1784 goto end;
1785
1786 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001787 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001788 if (pipe->data) {
1789 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
1790 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
1791 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001792 return ret;
1793}
1794#endif
1795
Christopher Faulet51dbc942018-09-13 09:05:15 +02001796/****************************************/
1797/* MUX initialization and instanciation */
1798/****************************************/
1799
1800/* The mux operations */
1801const struct mux_ops mux_h1_ops = {
1802 .init = h1_init,
1803 .wake = h1_wake,
1804 .attach = h1_attach,
1805 .get_first_cs = h1_get_first_cs,
1806 .detach = h1_detach,
1807 .destroy = h1_destroy,
1808 .avail_streams = h1_avail_streams,
1809 .rcv_buf = h1_rcv_buf,
1810 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02001811#if defined(CONFIG_HAP_LINUX_SPLICE)
1812 .rcv_pipe = h1_rcv_pipe,
1813 .snd_pipe = h1_snd_pipe,
1814#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02001815 .subscribe = h1_subscribe,
1816 .unsubscribe = h1_unsubscribe,
1817 .shutr = h1_shutr,
1818 .shutw = h1_shutw,
1819 .flags = MX_FL_NONE,
1820 .name = "h1",
1821};
1822
1823
1824/* this mux registers default HTX proto */
1825static struct mux_proto_list mux_proto_htx =
1826{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
1827
Willy Tarreau0108d902018-11-25 19:14:37 +01001828INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
1829
Christopher Faulet51dbc942018-09-13 09:05:15 +02001830/*
1831 * Local variables:
1832 * c-indent-level: 8
1833 * c-basic-offset: 8
1834 * End:
1835 */