blob: c95cd90e7b8452adc3c26feb7299f8ef0dc0393d [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;
Christopher Fauletfeb11742018-11-29 15:12:34 +010086 struct cs_info csinfo; /* CS info, only used for client connections */
87 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020088
Christopher Faulet51dbc942018-09-13 09:05:15 +020089 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
90 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020091
92 struct h1m req;
93 struct h1m res;
94
95 enum http_meth_t meth; /* HTTP resquest method */
96 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +020097};
98
99/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100100DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
101DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200102
Christopher Faulet51dbc942018-09-13 09:05:15 +0200103static int h1_recv(struct h1c *h1c);
104static int h1_send(struct h1c *h1c);
105static int h1_process(struct h1c *h1c);
106static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
107static void h1_shutw_conn(struct connection *conn);
108
109/*****************************************************/
110/* functions below are for dynamic buffer management */
111/*****************************************************/
112/*
113 * Indicates whether or not the we may call the h1_recv() function to
114 * attempt to receive data into the buffer and/or parse pending data. The
115 * condition is a bit complex due to some API limits for now. The rules are the
116 * following :
117 * - if an error or a shutdown was detected on the connection and the buffer
118 * is empty, we must not attempt to receive
119 * - if the input buffer failed to be allocated, we must not try to receive
120 * and we know there is nothing pending
121 * - if no flag indicates a blocking condition, we may attempt to receive,
122 * regardless of whether the input buffer is full or not, so that only de
123 * receiving part decides whether or not to block. This is needed because
124 * the connection API indeed prevents us from re-enabling receipt that is
125 * already enabled in a polled state, so we must always immediately stop as
126 * soon as the mux can't proceed so as never to hit an end of read with data
127 * pending in the buffers.
128 * - otherwise must may not attempt to receive
129 */
130static inline int h1_recv_allowed(const struct h1c *h1c)
131{
132 if (b_data(&h1c->ibuf) == 0 &&
Christopher Faulet7e346f32018-11-20 17:13:52 +0100133 (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW) ||
Christopher Faulet51dbc942018-09-13 09:05:15 +0200134 h1c->conn->flags & CO_FL_ERROR ||
135 conn_xprt_read0_pending(h1c->conn)))
136 return 0;
137
138 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL)))
139 return 1;
140
141 return 0;
142}
143
144/*
145 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
146 * flags are used to figure what buffer was requested. It returns 1 if the
147 * allocation succeeds, in which case the connection is woken up, or 0 if it's
148 * impossible to wake up and we prefer to be woken up later.
149 */
150static int h1_buf_available(void *target)
151{
152 struct h1c *h1c = target;
153
154 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
155 h1c->flags &= ~H1C_F_IN_ALLOC;
156 if (h1_recv_allowed(h1c))
157 tasklet_wakeup(h1c->wait_event.task);
158 return 1;
159 }
160
161 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
162 h1c->flags &= ~H1C_F_OUT_ALLOC;
163 tasklet_wakeup(h1c->wait_event.task);
164 return 1;
165 }
166
Christopher Faulet51dbc942018-09-13 09:05:15 +0200167 return 0;
168}
169
170/*
171 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
172 */
173static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
174{
175 struct buffer *buf = NULL;
176
177 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
178 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
179 h1c->buf_wait.target = h1c;
180 h1c->buf_wait.wakeup_cb = h1_buf_available;
181 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
182 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
183 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
184 __conn_xprt_stop_recv(h1c->conn);
185 }
186 return buf;
187}
188
189/*
190 * Release a buffer, if any, and try to wake up entities waiting in the buffer
191 * wait queue.
192 */
193static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
194{
195 if (bptr->size) {
196 b_free(bptr);
197 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
198 }
199}
200
201static int h1_avail_streams(struct connection *conn)
202{
203 struct h1c *h1c = conn->mux_ctx;
204
205 return h1c->h1s ? 0 : 1;
206}
207
208
209/*****************************************************************/
210/* functions below are dedicated to the mux setup and management */
211/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100212static struct conn_stream *h1s_new_cs(struct h1s *h1s)
213{
214 struct conn_stream *cs;
215
216 cs = cs_new(h1s->h1c->conn);
217 if (!cs)
218 goto err;
219 h1s->cs = cs;
220 cs->ctx = h1s;
221
222 if (h1s->flags & H1S_F_NOT_FIRST)
223 cs->flags |= CS_FL_NOT_FIRST;
224
225 if (stream_create_from_cs(cs) < 0)
226 goto err;
227 return cs;
228
229 err:
230 cs_free(cs);
231 h1s->cs = NULL;
232 return NULL;
233}
234
Christopher Fauletf2824e62018-10-01 12:12:37 +0200235static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200236{
237 struct h1s *h1s;
238
239 h1s = pool_alloc(pool_head_h1s);
240 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100241 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200242
243 h1s->h1c = h1c;
244 h1c->h1s = h1s;
245
246 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200247 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200248
249 h1s->recv_wait = NULL;
250 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200251
252 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200253 h1s->req.flags |= H1_MF_NO_PHDR;
254
Christopher Faulet129817b2018-09-20 16:14:40 +0200255 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200256 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200257
258 h1s->status = 0;
259 h1s->meth = HTTP_METH_OTHER;
260
Christopher Faulet47365272018-10-31 17:40:50 +0100261 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
262 h1s->flags |= H1S_F_NOT_FIRST;
263 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
264
Christopher Faulet129817b2018-09-20 16:14:40 +0200265 if (!conn_is_back(h1c->conn)) {
266 if (h1c->px->options2 & PR_O2_REQBUG_OK)
267 h1s->req.err_pos = -1;
268 }
269 else {
270 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
271 h1s->res.err_pos = -1;
272 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200273
Christopher Faulet539e0292018-11-19 10:40:09 +0100274 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
275 * create a new one.
276 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200277 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100278 h1s->csinfo.create_date = date;
279 h1s->csinfo.tv_create = now;
280 h1s->csinfo.t_handshake = 0;
281 h1s->csinfo.t_idle = -1;
282
Christopher Fauletf2824e62018-10-01 12:12:37 +0200283 cs->ctx = h1s;
284 h1s->cs = cs;
285 }
Christopher Faulet47365272018-10-31 17:40:50 +0100286 else {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100287 struct session *sess = h1c->conn->owner;
288
289 h1s->csinfo.create_date = sess->accept_date;
290 h1s->csinfo.tv_create = sess->tv_accept;
291 h1s->csinfo.t_handshake = sess->t_handshake;
292 h1s->csinfo.t_idle = -1;
293
Christopher Faulet47365272018-10-31 17:40:50 +0100294 cs = h1s_new_cs(h1s);
295 if (!cs)
296 goto fail;
297 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200298 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100299
300 fail:
301 pool_free(pool_head_h1s, h1s);
302 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200303}
304
305static void h1s_destroy(struct h1s *h1s)
306{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200307 if (h1s) {
308 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200309
Christopher Fauletf2824e62018-10-01 12:12:37 +0200310 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200311
Christopher Fauletf2824e62018-10-01 12:12:37 +0200312 if (h1s->recv_wait != NULL)
313 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
314 if (h1s->send_wait != NULL)
315 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
316
Christopher Faulet47365272018-10-31 17:40:50 +0100317 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
318 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
319 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200320
Christopher Fauletf2824e62018-10-01 12:12:37 +0200321 cs_free(h1s->cs);
322 pool_free(pool_head_h1s, h1s);
323 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200324}
325
Christopher Fauletfeb11742018-11-29 15:12:34 +0100326static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
327{
328 struct h1s *h1s = cs->ctx;
329
330 if (h1s && !conn_is_back(cs->conn))
331 return &h1s->csinfo;
332 return NULL;
333}
334
Christopher Faulet51dbc942018-09-13 09:05:15 +0200335/*
336 * Initialize the mux once it's attached. It is expected that conn->mux_ctx
337 * points to the existing conn_stream (for outgoing connections) or NULL (for
338 * incoming ones). Returns < 0 on error.
339 */
340static int h1_init(struct connection *conn, struct proxy *proxy)
341{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200342 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200343
344 h1c = pool_alloc(pool_head_h1c);
345 if (!h1c)
346 goto fail_h1c;
347 h1c->conn = conn;
348 h1c->px = proxy;
349
350 h1c->flags = H1C_F_NONE;
351 h1c->ibuf = BUF_NULL;
352 h1c->obuf = BUF_NULL;
353 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200354
Christopher Faulet51dbc942018-09-13 09:05:15 +0200355 LIST_INIT(&h1c->buf_wait.list);
356 h1c->wait_event.task = tasklet_new();
357 if (!h1c->wait_event.task)
358 goto fail;
359 h1c->wait_event.task->process = h1_io_cb;
360 h1c->wait_event.task->context = h1c;
361 h1c->wait_event.wait_reason = 0;
362
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200363 if (!(conn->flags & CO_FL_CONNECTED))
364 h1c->flags |= H1C_F_CS_WAIT_CONN;
365
Christopher Fauletf2824e62018-10-01 12:12:37 +0200366 /* Always Create a new H1S */
367 if (!h1s_create(h1c, conn->mux_ctx))
368 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200369
Christopher Faulet129817b2018-09-20 16:14:40 +0200370 conn->mux_ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200371
Christopher Faulet51dbc942018-09-13 09:05:15 +0200372 /* Try to read, if nothing is available yet we'll just subscribe */
373 if (h1_recv(h1c))
374 h1_process(h1c);
375
376 /* mux->wake will be called soon to complete the operation */
377 return 0;
378
379 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100380 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200381 tasklet_free(h1c->wait_event.task);
382 pool_free(pool_head_h1c, h1c);
383 fail_h1c:
384 return -1;
385}
386
387
388/* release function for a connection. This one should be called to free all
389 * resources allocated to the mux.
390 */
391static void h1_release(struct connection *conn)
392{
393 struct h1c *h1c = conn->mux_ctx;
394
395 LIST_DEL(&conn->list);
396
397 if (h1c) {
398 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
399 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
400 LIST_DEL(&h1c->buf_wait.list);
401 LIST_INIT(&h1c->buf_wait.list);
402 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
403 }
404
405 h1_release_buf(h1c, &h1c->ibuf);
406 h1_release_buf(h1c, &h1c->obuf);
407
Christopher Faulet51dbc942018-09-13 09:05:15 +0200408 if (h1c->wait_event.task)
409 tasklet_free(h1c->wait_event.task);
410
Christopher Fauletf2824e62018-10-01 12:12:37 +0200411 h1s_destroy(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200412 if (h1c->wait_event.wait_reason != 0)
413 conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason,
414 &h1c->wait_event);
415 pool_free(pool_head_h1c, h1c);
416 }
417
418 conn->mux = NULL;
419 conn->mux_ctx = NULL;
420
421 conn_stop_tracking(conn);
422 conn_full_close(conn);
423 if (conn->destroy_cb)
424 conn->destroy_cb(conn);
425 conn_free(conn);
426}
427
428/******************************************************/
429/* functions below are for the H1 protocol processing */
430/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200431/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
432 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200433 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200434static void h1_parse_req_vsn(struct h1m *h1m, const union htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200435{
Christopher Faulet9768c262018-10-22 09:34:31 +0200436 const char *p = sl->rq.l + sl->rq.m_len + sl->rq.u_len;
437
438 if ((sl->rq.v_len == 8) &&
439 (*(p + 5) > '1' ||
440 (*(p + 5) == '1' && *(p + 7) >= '1')))
441 h1m->flags |= H1_MF_VER_11;
442}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200443
Christopher Faulet9768c262018-10-22 09:34:31 +0200444/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
445 * greater or equal to 1.1
446 */
447static void h1_parse_res_vsn(struct h1m *h1m, const union htx_sl *sl)
448{
449 const char *p = sl->rq.l;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200450
Christopher Faulet9768c262018-10-22 09:34:31 +0200451 if ((sl->st.v_len == 8) &&
452 (*(p + 5) > '1' ||
453 (*(p + 5) == '1' && *(p + 7) >= '1')))
454 h1m->flags |= H1_MF_VER_11;
455}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200456
Christopher Faulet9768c262018-10-22 09:34:31 +0200457/*
458 * Check the validity of the request version. If the version is valid, it
459 * returns 1. Otherwise, it returns 0.
460 */
461static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
462{
463 struct h1c *h1c = h1s->h1c;
464
465 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
466 * exactly one digit "." one digit. This check may be disabled using
467 * option accept-invalid-http-request.
468 */
469 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
470 if (sl.rq.v.len != 8)
471 return 0;
472
473 if (*(sl.rq.v.ptr + 4) != '/' ||
474 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
475 *(sl.rq.v.ptr + 6) != '.' ||
476 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
477 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200478 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200479 else if (!sl.rq.v.len) {
480 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200481
Christopher Faulet9768c262018-10-22 09:34:31 +0200482 /* RFC 1945 allows only GET for HTTP/0.9 requests */
483 if (sl.rq.meth != HTTP_METH_GET)
484 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200485
Christopher Faulet9768c262018-10-22 09:34:31 +0200486 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
487 if (!sl.rq.u.len)
488 return 0;
489
490 /* Add HTTP version */
491 sl.rq.v = ist("HTTP/1.0");
492 }
493 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200494}
495
Christopher Faulet9768c262018-10-22 09:34:31 +0200496/*
497 * Check the validity of the response version. If the version is valid, it
498 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200499 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200500static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200501{
Christopher Faulet9768c262018-10-22 09:34:31 +0200502 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200503
Christopher Faulet9768c262018-10-22 09:34:31 +0200504 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
505 * exactly one digit "." one digit. This check may be disabled using
506 * option accept-invalid-http-request.
507 */
508 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
509 if (sl.st.v.len != 8)
510 return 0;
511
512 if (*(sl.st.v.ptr + 4) != '/' ||
513 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
514 *(sl.st.v.ptr + 6) != '.' ||
515 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
516 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200517 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200518 return 1;
519}
520/* Remove all "Connection:" headers from the HTX message <htx> */
521static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
522{
523 struct ist hdr = {.ptr = "Connection", .len = 10};
524 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200525
Christopher Faulet9768c262018-10-22 09:34:31 +0200526 while (http_find_header(htx, hdr, &ctx, 1))
527 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200528
Christopher Faulet9768c262018-10-22 09:34:31 +0200529 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
530}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200531
Christopher Faulet9768c262018-10-22 09:34:31 +0200532/* Add a "Connection:" header with the value <value> into the HTX message
533 * <htx>.
534 */
535static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
536{
537 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200538
Christopher Faulet9768c262018-10-22 09:34:31 +0200539 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200540}
541
542/* Deduce the connection mode of the client connection, depending on the
543 * configuration and the H1 message flags. This function is called twice, the
544 * first time when the request is parsed and the second time when the response
545 * is parsed.
546 */
547static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
548{
549 struct proxy *fe = h1s->h1c->px;
550 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
551
552 /* Tunnel mode can only by set on the frontend */
553 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
554 flag = H1S_F_WANT_TUN;
555 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
556 flag = H1S_F_WANT_CLO;
557
558 /* flags order: CLO > SCL > TUN > KAL */
559 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
560 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
561
562 if (h1m->flags & H1_MF_RESP) {
563 /* Either we've established an explicit tunnel, or we're
564 * switching the protocol. In both cases, we're very unlikely to
565 * understand the next protocols. We have to switch to tunnel
566 * mode, so that we transfer the request and responses then let
567 * this protocol pass unmodified. When we later implement
568 * specific parsers for such protocols, we'll want to check the
569 * Upgrade header which contains information about that protocol
570 * for responses with status 101 (eg: see RFC2817 about TLS).
571 */
572 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
573 h1s->status == 101)
574 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
575 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
576 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
577 }
578 else {
579 if (h1s->flags & H1S_F_WANT_KAL &&
580 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
581 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
582 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
583 }
584
585 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
586 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
587 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
588}
589
590/* Deduce the connection mode of the client connection, depending on the
591 * configuration and the H1 message flags. This function is called twice, the
592 * first time when the request is parsed and the second time when the response
593 * is parsed.
594 */
595static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
596{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100597 struct h1c *h1c = h1s->h1c;
598 struct session *sess = h1c->conn->owner;
599 struct proxy *fe = sess->fe;
600 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200601 int flag = H1S_F_WANT_KAL;
602
603 /* Tunnel mode can only by set on the frontend */
604 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
605 flag = H1S_F_WANT_TUN;
606
607 /* For the server connection: server-close == httpclose */
608 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
609 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
610 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
611 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
612 flag = H1S_F_WANT_CLO;
613
614 /* flags order: CLO > SCL > TUN > KAL */
615 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
616 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
617
618 if (h1m->flags & H1_MF_RESP) {
619 /* Either we've established an explicit tunnel, or we're
620 * switching the protocol. In both cases, we're very unlikely to
621 * understand the next protocols. We have to switch to tunnel
622 * mode, so that we transfer the request and responses then let
623 * this protocol pass unmodified. When we later implement
624 * specific parsers for such protocols, we'll want to check the
625 * Upgrade header which contains information about that protocol
626 * for responses with status 101 (eg: see RFC2817 about TLS).
627 */
628 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
629 h1s->status == 101)
630 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
631 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
632 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
633 else if (h1s->flags & H1S_F_WANT_KAL &&
634 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
635 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
636 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
637 }
638
639 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
640 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
641 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200642}
643
Christopher Faulet9768c262018-10-22 09:34:31 +0200644static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
645 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200646{
647 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200648
649 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
650 * token is found
651 */
652 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200653 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200654
655 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200656 if (h1m->flags & H1_MF_CONN_CLO) {
657 if (conn_val)
658 *conn_val = ist("");
659 if (htx)
660 h1_remove_conn_hdrs(h1m, htx);
661 }
662 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
663 if (conn_val)
664 *conn_val = ist("keep-alive");
665 if (htx)
666 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
667 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200668 }
669 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200670 if (h1m->flags & H1_MF_CONN_KAL) {
671 if (conn_val)
672 *conn_val = ist("");
673 if (htx)
674 h1_remove_conn_hdrs(h1m, htx);
675 }
676 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
677 if (conn_val)
678 *conn_val = ist("close");
679 if (htx)
680 h1_add_conn_hdr(h1m, htx, ist("close"));
681 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200682 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200683}
684
Christopher Faulet9768c262018-10-22 09:34:31 +0200685static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
686 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200687{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200688 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
689 * token is found
690 */
691 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200692 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200693
694 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200695 if (h1m->flags & H1_MF_CONN_CLO) {
696 if (conn_val)
697 *conn_val = ist("");
698 if (htx)
699 h1_remove_conn_hdrs(h1m, htx);
700 }
701 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
702 if (conn_val)
703 *conn_val = ist("keep-alive");
704 if (htx)
705 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
706 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200707 }
708 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200709 if (h1m->flags & H1_MF_CONN_KAL) {
710 if (conn_val)
711 *conn_val = ist("");
712 if (htx)
713 h1_remove_conn_hdrs(h1m, htx);
714 }
715 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
716 if (conn_val)
717 *conn_val = ist("close");
718 if (htx)
719 h1_add_conn_hdr(h1m, htx, ist("close"));
720 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200721 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200722}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200723
Christopher Faulet9768c262018-10-22 09:34:31 +0200724/* Set the right connection mode and update "Connection:" header if
725 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
726 * message is updated accordingly. When <conn_val> is not NULL, it is set with
727 * the new header value.
728 */
729static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
730 struct htx *htx, struct ist *conn_val)
731{
732 if (!conn_is_back(h1s->h1c->conn)) {
733 h1_set_cli_conn_mode(h1s, h1m);
734 if (h1m->flags & H1_MF_RESP)
735 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
736 }
737 else {
738 h1_set_srv_conn_mode(h1s, h1m);
739 if (!(h1m->flags & H1_MF_RESP))
740 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
741 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200742}
743
Christopher Faulet129817b2018-09-20 16:14:40 +0200744/*
745 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200746 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
747 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet129817b2018-09-20 16:14:40 +0200748 * responsibile to update the parser state <h1m>.
749 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200750static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200751 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200752{
753 struct http_hdr hdrs[MAX_HTTP_HDR];
754 union h1_sl sl;
755 int ret = 0;
756
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100757 if (!max)
758 goto end;
759
Christopher Faulet129817b2018-09-20 16:14:40 +0200760 /* Realing input buffer if necessary */
761 if (b_head(buf) + b_data(buf) > b_wrap(buf))
762 b_slow_realign(buf, trash.area, 0);
763
Christopher Fauletf2824e62018-10-01 12:12:37 +0200764 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Faulet129817b2018-09-20 16:14:40 +0200765 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &sl);
766 if (ret <= 0) {
767 /* Incomplete or invalid message. If the buffer is full, it's an
768 * error because headers are too large to be handled by the
769 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200770 if (ret < 0 || (!ret && b_full(buf)))
771 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200772 goto end;
773 }
774
775 /* messages headers fully parsed, do some checks to prepare the body
776 * parsing.
777 */
778
779 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200780 if (ret > (b_size(buf) - global.tune.maxrewrite))
781 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200782
Christopher Faulet9768c262018-10-22 09:34:31 +0200783 /* Save the request's method or the response's status, check if the body
784 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200785 if (!(h1m->flags & H1_MF_RESP)) {
786 h1s->meth = sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200787
Christopher Faulet129817b2018-09-20 16:14:40 +0200788 /* Request have always a known length */
789 h1m->flags |= H1_MF_XFER_LEN;
790 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
791 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200792
793 if (!h1_process_req_vsn(h1s, h1m, sl)) {
794 h1m->err_pos = sl.rq.v.ptr - b_head(buf);
795 h1m->err_state = h1m->state;
796 goto vsn_error;
797 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200798 }
799 else {
800 h1s->status = sl.st.status;
801
802 if ((h1s->meth == HTTP_METH_HEAD) ||
803 (h1s->status >= 100 && h1s->status < 200) ||
804 (h1s->status == 204) || (h1s->status == 304) ||
805 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
806 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
807 h1m->flags |= H1_MF_XFER_LEN;
808 h1m->curr_len = h1m->body_len = 0;
809 h1m->state = H1_MSG_DONE;
810 }
811 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
812 h1m->flags |= H1_MF_XFER_LEN;
813 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
814 h1m->state = H1_MSG_DONE;
815 }
816 else
817 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200818
819 if (!h1_process_res_vsn(h1s, h1m, sl)) {
820 h1m->err_pos = sl.st.v.ptr - b_head(buf);
821 h1m->err_state = h1m->state;
822 goto vsn_error;
823 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200824 }
825
Christopher Faulet9768c262018-10-22 09:34:31 +0200826 if (!(h1m->flags & H1_MF_RESP)) {
827 if (!htx_add_reqline(htx, sl) || !htx_add_all_headers(htx, hdrs))
828 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200829 }
830 else {
Christopher Faulet9768c262018-10-22 09:34:31 +0200831 if (!htx_add_resline(htx, sl) || !htx_add_all_headers(htx, hdrs))
832 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200833 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200834 if (h1m->state == H1_MSG_DONE)
835 if (!htx_add_endof(htx, HTX_BLK_EOM))
836 goto error;
837
838 h1_process_conn_mode(h1s, h1m, htx, NULL);
839
840 /* If body length cannot be determined, set htx->extra to
841 * ULLONG_MAX. This value is impossible in other cases.
842 */
843 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
844
845 /* Recheck there is enough space to do headers rewritting */
846 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
847 goto error;
848
849 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200850 end:
851 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200852
853 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +0200854 h1m->err_state = h1m->state;
855 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +0200856 vsn_error:
857 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200858 ret = 0;
859 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200860}
861
862/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200863 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
864 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +0200865 * and filling h1s->err_pos and h1s->err_state fields. This functions is
866 * responsibile to update the parser state <h1m>.
867 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200868static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200869 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200870{
Christopher Faulet9768c262018-10-22 09:34:31 +0200871 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +0200872 size_t total = 0;
873 int ret = 0;
874
875 if (h1m->flags & H1_MF_XFER_LEN) {
876 if (h1m->flags & H1_MF_CLEN) {
877 /* content-length: read only h2m->body_len */
878 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200879 if (ret > data_space)
880 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200881 if ((uint64_t)ret > h1m->curr_len)
882 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200883 if (ret > b_contig_data(buf, *ofs))
884 ret = b_contig_data(buf, *ofs);
885 if (ret) {
886 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
887 goto end;
888 h1m->curr_len -= ret;
889 *ofs += ret;
890 total += ret;
891 }
892
893 if (!h1m->curr_len) {
894 if (!htx_add_endof(htx, HTX_BLK_EOM))
895 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200896 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200897 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200898 }
899 else if (h1m->flags & H1_MF_CHNK) {
900 new_chunk:
901 /* te:chunked : parse chunks */
902 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200903 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +0200904 if (ret <= 0)
905 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200906 h1m->state = H1_MSG_CHUNK_SIZE;
907
Christopher Faulet129817b2018-09-20 16:14:40 +0200908 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200909 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200910 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200911 }
912
913 if (h1m->state == H1_MSG_CHUNK_SIZE) {
914 unsigned int chksz;
915
Christopher Fauletf2824e62018-10-01 12:12:37 +0200916 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +0200917 if (ret <= 0)
918 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200919 if (!chksz) {
920 if (!htx_add_endof(htx, HTX_BLK_EOD))
921 goto end;
922 h1m->state = H1_MSG_TRAILERS;
923 }
924 else
925 h1m->state = H1_MSG_DATA;
926
Christopher Faulet129817b2018-09-20 16:14:40 +0200927 h1m->curr_len = chksz;
928 h1m->body_len += chksz;
929 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200930 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200931 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200932 }
933
934 if (h1m->state == H1_MSG_DATA) {
935 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200936 if (ret > data_space)
937 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200938 if ((uint64_t)ret > h1m->curr_len)
939 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200940 if (ret > b_contig_data(buf, *ofs))
941 ret = b_contig_data(buf, *ofs);
942 if (ret) {
943 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
944 goto end;
945 h1m->curr_len -= ret;
946 max -= ret;
947 *ofs += ret;
948 total += ret;
949 }
950 if (!h1m->curr_len) {
951 h1m->state = H1_MSG_CHUNK_CRLF;
952 goto new_chunk;
953 }
954 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200955 }
956
957 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200958 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +0200959 if (ret > data_space)
960 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +0200961 if (ret <= 0)
962 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200963
964 /* Realing input buffer if tailers wrap. For now
965 * this is a workaroung. Because trailers are
966 * not split on CRLF, like headers, there is no
967 * way to know where to split it when trailers
968 * wrap. This is a limitation of
969 * h1_measure_trailers.
970 */
971 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
972 b_slow_realign(buf, trash.area, 0);
973
974 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
975 goto end;
976
Christopher Faulet129817b2018-09-20 16:14:40 +0200977 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200978 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200979 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +0200980
981 /* FIXME: if it fails here, this is a problem,
982 * because there is no way to return here. */
983 if (!htx_add_endof(htx, HTX_BLK_EOM))
984 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200985 h1m->state = H1_MSG_DONE;
986 }
987 }
988 else {
989 /* XFER_LEN is set but not CLEN nor CHNK, it means there
990 * is no body. Switch the message in DONE state
991 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200992 if (!htx_add_endof(htx, HTX_BLK_EOM))
993 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200994 h1m->state = H1_MSG_DONE;
995 }
996 }
997 else {
998 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +0200999 ret = max;
1000 if (ret > data_space)
1001 ret = data_space;
1002 if (ret > b_contig_data(buf, *ofs))
1003 ret = b_contig_data(buf, *ofs);
1004 if (ret) {
1005 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1006 goto end;
1007
1008 *ofs += max;
1009 total = max;
1010 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001011 }
1012
1013 end:
1014 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001015 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001016 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001017 h1m->err_pos = *ofs + max + ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001018 return 0;
1019 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001020 /* update htx->extra, only when the body length is known */
1021 if (h1m->flags & H1_MF_XFER_LEN)
1022 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001023 return total;
1024}
1025
1026/*
1027 * Synchronize the request and the response before reseting them. Except for 1xx
1028 * responses, we wait that the request and the response are in DONE state and
1029 * that all data are forwarded for both. For 1xx responses, only the response is
1030 * reset, waiting the final one. Many 1xx messages can be sent.
1031 */
1032static void h1_sync_messages(struct h1c *h1c)
1033{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001034 struct h1s *h1s = h1c->h1s;
1035
1036 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001037 return;
1038
Christopher Fauletf2824e62018-10-01 12:12:37 +02001039 if (h1s->res.state == H1_MSG_DONE &&
1040 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001041 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001042 /* For 100-Continue response or any other informational 1xx
1043 * response which is non-final, don't reset the request, the
1044 * transaction is not finished. We take care the response was
1045 * transferred before.
1046 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001047 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001048 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001049 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001050 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001051 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1052 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001053 h1m_init_req(&h1s->req);
1054 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001055 h1s->req.state = H1_MSG_TUNNEL;
1056 h1s->res.state = H1_MSG_TUNNEL;
1057 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001058 }
1059}
1060
1061/*
1062 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001063 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1064 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001065 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001066static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001067{
Christopher Faulet539e0292018-11-19 10:40:09 +01001068 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001069 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001070 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001071 size_t total = 0;
1072 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001073 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001074 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001075
Christopher Faulet539e0292018-11-19 10:40:09 +01001076 htx = htx_from_buf(buf);
1077 count = b_data(&h1c->ibuf);
1078 max = htx_free_space(htx);
1079 if (flags & CO_RFL_KEEP_RSV) {
1080 if (max < global.tune.maxrewrite)
1081 goto end;
1082 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001083 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001084 if (count > max)
1085 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001086
Christopher Fauletf2824e62018-10-01 12:12:37 +02001087 if (!conn_is_back(h1c->conn)) {
1088 h1m = &h1s->req;
1089 errflag = H1S_F_REQ_ERROR;
1090 }
1091 else {
1092 h1m = &h1s->res;
1093 errflag = H1S_F_RES_ERROR;
1094 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001095
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001096 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001097 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001098 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001099 if (!ret)
1100 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001101 }
1102 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001103 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001104 if (!ret)
1105 break;
1106 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001107 else if (h1m->state == H1_MSG_DONE)
1108 break;
1109 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001110 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet9768c262018-10-22 09:34:31 +02001111 if (!ret)
1112 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001113 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001114 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001115 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001116 break;
1117 }
1118
Christopher Faulet539e0292018-11-19 10:40:09 +01001119 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001120 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001121
Christopher Faulet47365272018-10-31 17:40:50 +01001122 if (h1s->flags & errflag)
1123 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001124
Christopher Faulet539e0292018-11-19 10:40:09 +01001125 b_del(&h1c->ibuf, total);
1126
1127 end:
1128 if (htx_is_not_empty(htx))
1129 b_set_data(buf, b_size(buf));
1130 else {
1131 htx_reset(htx);
1132 b_set_data(buf, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001133 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001134
Christopher Faulet539e0292018-11-19 10:40:09 +01001135 if (h1c->flags & H1C_F_IN_FULL && b_room(&h1c->ibuf)) {
1136 h1c->flags &= ~H1C_F_IN_FULL;
1137 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001138 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001139
Christopher Faulet9c388402018-11-19 21:54:26 +01001140 if (b_data(&h1c->ibuf)) {
1141 if (!htx_is_empty(htx))
1142 h1s->cs->flags |= CS_FL_RCV_MORE;
1143 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001144 else {
1145 h1_release_buf(h1c, &h1c->ibuf);
1146 h1_sync_messages(h1c);
1147
1148 h1s->cs->flags &= ~CS_FL_RCV_MORE;
1149 if (h1s->cs->flags & CS_FL_REOS)
1150 h1s->cs->flags |= CS_FL_EOS;
1151 }
1152 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001153
1154 parsing_err:
1155 // FIXME: create an error snapshot here
1156 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001157 htx->flags |= HTX_FL_PARSING_ERROR;
1158 b_set_data(buf, b_size(buf));
1159 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001160 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001161}
1162
Christopher Faulet129817b2018-09-20 16:14:40 +02001163/*
1164 * Process outgoing data. It parses data and transfer them from the channel buffer into
1165 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1166 * 0 if it couldn't proceed.
1167 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001168static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1169{
Christopher Faulet129817b2018-09-20 16:14:40 +02001170 struct h1s *h1s = h1c->h1s;
1171 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001172 struct htx *chn_htx;
1173 struct htx_blk *blk;
1174 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001175 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001176 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001177
Christopher Faulet47365272018-10-31 17:40:50 +01001178 if (!count)
1179 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001180 chn_htx = htx_from_buf(buf);
1181
Christopher Faulet51dbc942018-09-13 09:05:15 +02001182 if (!h1_get_buf(h1c, &h1c->obuf)) {
1183 h1c->flags |= H1C_F_OUT_ALLOC;
1184 goto end;
1185 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001186
Christopher Fauletf2824e62018-10-01 12:12:37 +02001187 if (!conn_is_back(h1c->conn)) {
1188 h1m = &h1s->res;
1189 errflag = H1S_F_RES_ERROR;
1190 }
1191 else {
1192 h1m = &h1s->req;
1193 errflag = H1S_F_REQ_ERROR;
1194 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001195
1196
1197 tmp = get_trash_chunk();
1198 tmp->size = b_room(&h1c->obuf);
1199
1200 blk = htx_get_head_blk(chn_htx);
1201 while (!(h1s->flags & errflag) && blk) {
1202 union htx_sl *sl;
1203 struct ist n, v;
1204 uint32_t sz = htx_get_blksz(blk);
1205
1206 if (total + sz > count)
1207 goto copy;
1208
1209 switch (htx_get_blk_type(blk)) {
1210 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001211 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001212
1213 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001214 h1m_init_req(h1m);
1215 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001216 sl = htx_get_blk_ptr(chn_htx, blk);
1217 h1s->meth = sl->rq.meth;
1218 h1_parse_req_vsn(h1m, sl);
1219 if (!htx_reqline_to_str(sl, tmp))
1220 goto copy;
1221 h1m->flags |= H1_MF_XFER_LEN;
1222 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001223 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001224
Christopher Faulet9768c262018-10-22 09:34:31 +02001225 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001226 h1m_init_res(h1m);
1227 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001228 sl = htx_get_blk_ptr(chn_htx, blk);
1229 h1s->status = sl->st.status;
1230 h1_parse_res_vsn(h1m, sl);
1231 if (!htx_stline_to_str(sl, tmp))
1232 goto copy;
1233 if (chn_htx->extra != ULLONG_MAX)
1234 h1m->flags |= H1_MF_XFER_LEN;
1235 h1m->state = H1_MSG_HDR_FIRST;
1236 break;
1237
1238 case HTX_BLK_HDR:
1239 if (h1m->state == H1_MSG_HDR_FIRST) {
1240 struct http_hdr_ctx ctx;
1241
1242 n = ist("Connection");
1243 v = ist("");
1244
1245 /* If there is no "Connection:" header,
1246 * process conn_mode now and add the
1247 * right one.
1248 */
1249 ctx.blk = blk;
Christopher Faulet5999b862018-11-27 10:46:09 +01001250 ctx.value = ist(NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +02001251 if (http_find_header(chn_htx, n, &ctx, 1))
1252 goto process_hdr;
1253 h1_process_conn_mode(h1s, h1m, NULL, &v);
1254 if (!v.len)
1255 goto process_hdr;
1256
1257 if (!htx_hdr_to_str(n, v, tmp))
1258 goto copy;
1259 }
1260 process_hdr:
1261 h1m->state = H1_MSG_HDR_NAME;
1262 n = htx_get_blk_name(chn_htx, blk);
1263 v = htx_get_blk_value(chn_htx, blk);
1264
1265 if (isteqi(n, ist("transfer-encoding")))
1266 h1_parse_xfer_enc_header(h1m, v);
1267 else if (isteqi(n, ist("connection"))) {
1268 h1_parse_connection_header(h1m, v);
1269 h1_process_conn_mode(h1s, h1m, NULL, &v);
1270 if (!v.len)
1271 goto skip_hdr;
1272 }
1273
1274 if (!htx_hdr_to_str(n, v, tmp))
1275 goto copy;
1276 skip_hdr:
1277 h1m->state = H1_MSG_HDR_L2_LWS;
1278 break;
1279
1280 case HTX_BLK_PHDR:
1281 /* not implemented yet */
1282 h1m->flags |= errflag;
1283 break;
1284
1285 case HTX_BLK_EOH:
1286 h1m->state = H1_MSG_LAST_LF;
1287 if (!chunk_memcat(tmp, "\r\n", 2))
1288 goto copy;
1289
1290 h1m->state = H1_MSG_DATA;
1291 break;
1292
1293 case HTX_BLK_DATA:
1294 v = htx_get_blk_value(chn_htx, blk);
1295 if (!htx_data_to_str(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
1296 goto copy;
1297 break;
1298
1299 case HTX_BLK_EOD:
1300 if (!chunk_memcat(tmp, "0\r\n", 3))
1301 goto copy;
1302 h1m->state = H1_MSG_TRAILERS;
1303 break;
1304
1305 case HTX_BLK_TLR:
1306 v = htx_get_blk_value(chn_htx, blk);
1307 if (!htx_trailer_to_str(v, tmp))
1308 goto copy;
1309 break;
1310
1311 case HTX_BLK_EOM:
1312 /* if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(tmp, "\r\n", 2)) */
1313 /* goto copy; */
1314 h1m->state = H1_MSG_DONE;
1315 break;
1316
1317 case HTX_BLK_OOB:
1318 v = htx_get_blk_value(chn_htx, blk);
1319 if (!chunk_memcat(tmp, v.ptr, v.len))
1320 goto copy;
1321 break;
1322
1323 default:
1324 h1m->flags |= errflag;
1325 break;
1326 }
1327 total += sz;
1328 blk = htx_remove_blk(chn_htx, blk);
Christopher Faulet129817b2018-09-20 16:14:40 +02001329 }
1330
Christopher Faulet9768c262018-10-22 09:34:31 +02001331 copy:
1332 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001333
1334 if (b_full(&h1c->obuf))
1335 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001336 if (htx_is_empty(chn_htx)) {
1337 htx_reset(chn_htx);
1338 b_set_data(buf, 0);
1339 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001340 end:
1341 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001342}
1343
Christopher Faulet51dbc942018-09-13 09:05:15 +02001344/*********************************************************/
1345/* functions below are I/O callbacks from the connection */
1346/*********************************************************/
1347/*
1348 * Attempt to read data, and subscribe if none available
1349 */
1350static int h1_recv(struct h1c *h1c)
1351{
1352 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001353 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001354 size_t ret, max;
1355 int rcvd = 0;
1356
1357 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
1358 return 0;
1359
Christopher Faulet539e0292018-11-19 10:40:09 +01001360 if (!h1_recv_allowed(h1c))
Christopher Faulet9768c262018-10-22 09:34:31 +02001361 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001362
Christopher Fauletfeb11742018-11-29 15:12:34 +01001363 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001364 rcvd = 1;
1365 goto end;
1366 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001367
Christopher Faulet51dbc942018-09-13 09:05:15 +02001368 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1369 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001370 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001371 }
1372
1373 ret = 0;
1374 max = b_room(&h1c->ibuf);
1375 if (max) {
1376 h1c->flags &= ~H1C_F_IN_FULL;
1377 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
1378 }
Christopher Faulet47365272018-10-31 17:40:50 +01001379 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001380 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001381 if (h1s && h1s->cs) {
1382 h1s->cs->flags |= CS_FL_READ_PARTIAL;
1383 if (h1s->csinfo.t_idle == -1)
1384 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1385 }
Christopher Faulet47365272018-10-31 17:40:50 +01001386 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001387
1388 if (h1_recv_allowed(h1c))
1389 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet81d48432018-11-19 21:22:43 +01001390 else
1391 rcvd = 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001392
Christopher Faulet9768c262018-10-22 09:34:31 +02001393 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001394 if (!b_data(&h1c->ibuf))
1395 h1_release_buf(h1c, &h1c->ibuf);
1396 else if (b_full(&h1c->ibuf))
1397 h1c->flags |= H1C_F_IN_FULL;
1398 return rcvd;
1399}
1400
1401
1402/*
1403 * Try to send data if possible
1404 */
1405static int h1_send(struct h1c *h1c)
1406{
1407 struct connection *conn = h1c->conn;
1408 unsigned int flags = 0;
1409 size_t ret;
1410 int sent = 0;
1411
1412 if (conn->flags & CO_FL_ERROR)
1413 return 0;
1414
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001415 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1416 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1417 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1418 return 0;
1419 }
1420
Christopher Faulet51dbc942018-09-13 09:05:15 +02001421 if (!b_data(&h1c->obuf))
1422 goto end;
1423
1424 if (h1c->flags & H1C_F_OUT_FULL)
1425 flags |= CO_SFL_MSG_MORE;
1426
1427 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1428 if (ret > 0) {
1429 h1c->flags &= ~H1C_F_OUT_FULL;
1430 b_del(&h1c->obuf, ret);
1431 sent = 1;
1432 }
1433
1434 end:
1435 /* We're done, no more to send */
1436 if (!b_data(&h1c->obuf)) {
1437 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001438 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001439 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1440 h1_shutw_conn(conn);
1441 }
1442 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1443 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1444
1445 return sent;
1446}
1447
1448
1449static void h1_wake_stream(struct h1c *h1c)
1450{
1451 struct connection *conn = h1c->conn;
1452 struct h1s *h1s = h1c->h1s;
1453 uint32_t flags = 0;
1454 int dont_wake = 0;
1455
1456 if (!h1s || !h1s->cs)
1457 return;
1458
1459 if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR))
1460 flags |= CS_FL_ERROR;
1461 if (conn_xprt_read0_pending(conn))
1462 flags |= CS_FL_REOS;
1463
1464 h1s->cs->flags |= flags;
1465 if (h1s->recv_wait) {
1466 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1467 tasklet_wakeup(h1s->recv_wait->task);
1468 h1s->recv_wait = NULL;
1469 dont_wake = 1;
1470 }
1471 if (h1s->send_wait) {
1472 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1473 tasklet_wakeup(h1s->send_wait->task);
1474 h1s->send_wait = NULL;
1475 dont_wake = 1;
1476 }
1477 if (!dont_wake && h1s->cs->data_cb->wake)
1478 h1s->cs->data_cb->wake(h1s->cs);
1479}
1480
1481/* callback called on any event by the connection handler.
1482 * It applies changes and returns zero, or < 0 if it wants immediate
1483 * destruction of the connection.
1484 */
1485static int h1_process(struct h1c * h1c)
1486{
1487 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001488 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001489
Christopher Faulet51dbc942018-09-13 09:05:15 +02001490 if (!conn->mux_ctx)
1491 return -1;
1492
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001493 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001494 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1495 goto end;
1496 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001497 }
1498
Christopher Fauletfeb11742018-11-29 15:12:34 +01001499 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001500 if (h1c->flags & H1C_F_CS_ERROR ||
1501 conn->flags & CO_FL_ERROR ||
1502 conn_xprt_read0_pending(conn))
1503 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001504 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001505 if (!h1s_create(h1c, NULL))
1506 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001507 }
Christopher Fauletfeb11742018-11-29 15:12:34 +01001508 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001509 }
1510
Christopher Fauletfeb11742018-11-29 15:12:34 +01001511 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1512 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1513
Christopher Faulet539e0292018-11-19 10:40:09 +01001514 h1_wake_stream(h1c);
Christopher Faulet47365272018-10-31 17:40:50 +01001515 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001516 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001517
1518 release:
1519 h1_release(conn);
1520 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001521}
1522
1523static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1524{
1525 struct h1c *h1c = ctx;
1526 int ret = 0;
1527
1528 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1529 ret = h1_send(h1c);
1530 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1531 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001532 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001533 h1_process(h1c);
1534 return NULL;
1535}
1536
1537
1538static int h1_wake(struct connection *conn)
1539{
1540 struct h1c *h1c = conn->mux_ctx;
1541
Christopher Faulet539e0292018-11-19 10:40:09 +01001542 h1_send(h1c);
1543 return h1_process(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001544}
1545
Christopher Faulet51dbc942018-09-13 09:05:15 +02001546/*******************************************/
1547/* functions below are used by the streams */
1548/*******************************************/
1549/*
1550 * Attach a new stream to a connection
1551 * (Used for outgoing connections)
1552 */
1553static struct conn_stream *h1_attach(struct connection *conn)
1554{
1555 struct h1c *h1c = conn->mux_ctx;
1556 struct conn_stream *cs = NULL;
1557 struct h1s *h1s;
1558
1559 if (h1c->flags & H1C_F_CS_ERROR)
1560 goto end;
1561
1562 cs = cs_new(h1c->conn);
1563 if (!cs)
1564 goto end;
1565
Christopher Fauletf2824e62018-10-01 12:12:37 +02001566 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001567 if (h1s == NULL)
1568 goto end;
1569
1570 return cs;
1571 end:
1572 cs_free(cs);
1573 return NULL;
1574}
1575
1576/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1577 * this mux, it's easy as we can only store a single conn_stream.
1578 */
1579static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1580{
1581 struct h1c *h1c = conn->mux_ctx;
1582 struct h1s *h1s = h1c->h1s;
1583
1584 if (h1s)
1585 return h1s->cs;
1586
1587 return NULL;
1588}
1589
1590static void h1_destroy(struct connection *conn)
1591{
1592 struct h1c *h1c = conn->mux_ctx;
1593
1594 if (!h1c->h1s)
1595 h1_release(conn);
1596}
1597
1598/*
1599 * Detach the stream from the connection and possibly release the connection.
1600 */
1601static void h1_detach(struct conn_stream *cs)
1602{
1603 struct h1s *h1s = cs->ctx;
1604 struct h1c *h1c;
1605
1606 cs->ctx = NULL;
1607 if (!h1s)
1608 return;
1609
1610 h1c = h1s->h1c;
1611 h1s->cs = NULL;
1612
1613 h1s_destroy(h1s);
1614
1615 /* We don't want to close right now unless the connection is in error */
1616 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
1617 (h1c->conn->flags & CO_FL_ERROR))
1618 h1_release(h1c->conn);
1619 else
1620 tasklet_wakeup(h1c->wait_event.task);
1621}
1622
1623
1624static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1625{
1626 struct h1s *h1s = cs->ctx;
1627
1628 if (!h1s)
1629 return;
1630
Christopher Fauletf2824e62018-10-01 12:12:37 +02001631 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1632 return;
1633
Christopher Faulet51dbc942018-09-13 09:05:15 +02001634 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1635 if (cs->flags & CS_FL_SHR)
1636 return;
1637 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1638 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1639 if (cs->flags & CS_FL_SHW) {
1640 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1641 conn_full_close(cs->conn);
1642 }
1643}
1644
1645static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1646{
1647 struct h1s *h1s = cs->ctx;
1648 struct h1c *h1c;
1649
1650 if (!h1s)
1651 return;
1652 h1c = h1s->h1c;
1653
Christopher Fauletf2824e62018-10-01 12:12:37 +02001654 if ((h1s->flags & H1S_F_WANT_KAL) &&
1655 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1656 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1657 return;
1658
Christopher Faulet51dbc942018-09-13 09:05:15 +02001659 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1660 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1661 return;
1662
1663 h1_shutw_conn(cs->conn);
1664}
1665
1666static void h1_shutw_conn(struct connection *conn)
1667{
1668 struct h1c *h1c = conn->mux_ctx;
1669
1670 if (conn_xprt_ready(conn) && conn->xprt->shutw)
1671 conn->xprt->shutw(conn, 1);
1672 if (!(conn->flags & CO_FL_SOCK_RD_SH))
1673 conn_sock_shutw(conn, 1);
1674 else {
1675 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1676 conn_full_close(conn);
1677 }
1678}
1679
1680/* Called from the upper layer, to unsubscribe to events */
1681static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1682{
1683 struct wait_event *sw;
1684 struct h1s *h1s = cs->ctx;
1685
1686 if (!h1s)
1687 return 0;
1688
1689 if (event_type & SUB_CAN_RECV) {
1690 sw = param;
1691 if (h1s->recv_wait == sw) {
1692 sw->wait_reason &= ~SUB_CAN_RECV;
1693 h1s->recv_wait = NULL;
1694 }
1695 }
1696 if (event_type & SUB_CAN_SEND) {
1697 sw = param;
1698 if (h1s->send_wait == sw) {
1699 sw->wait_reason &= ~SUB_CAN_SEND;
1700 h1s->send_wait = NULL;
1701 }
1702 }
1703 return 0;
1704}
1705
1706/* Called from the upper layer, to subscribe to events, such as being able to send */
1707static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1708{
1709 struct wait_event *sw;
1710 struct h1s *h1s = cs->ctx;
1711
1712 if (!h1s)
1713 return -1;
1714
1715 switch (event_type) {
1716 case SUB_CAN_RECV:
1717 sw = param;
1718 if (!(sw->wait_reason & SUB_CAN_RECV)) {
1719 sw->wait_reason |= SUB_CAN_RECV;
1720 sw->handle = h1s;
1721 h1s->recv_wait = sw;
1722 }
1723 return 0;
1724 case SUB_CAN_SEND:
1725 sw = param;
1726 if (!(sw->wait_reason & SUB_CAN_SEND)) {
1727 sw->wait_reason |= SUB_CAN_SEND;
1728 sw->handle = h1s;
1729 h1s->send_wait = sw;
1730 }
1731 return 0;
1732 default:
1733 break;
1734 }
1735 return -1;
1736}
1737
1738/* Called from the upper layer, to receive data */
1739static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1740{
1741 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01001742 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001743 size_t ret = 0;
1744
Christopher Faulet539e0292018-11-19 10:40:09 +01001745 if (!(h1c->flags & H1C_F_IN_ALLOC))
1746 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001747
1748 if (flags & CO_RFL_BUF_FLUSH)
1749 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001750 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
1751 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01001752 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1753 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001754 }
1755 return ret;
1756}
1757
1758
1759/* Called from the upper layer, to send data */
1760static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1761{
1762 struct h1s *h1s = cs->ctx;
1763 struct h1c *h1c;
1764 size_t ret = 0;
1765
1766 if (!h1s)
1767 return ret;
1768
1769 h1c = h1s->h1c;
1770
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001771 if (h1c->flags & H1C_F_CS_WAIT_CONN)
1772 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001773
Christopher Faulet539e0292018-11-19 10:40:09 +01001774 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001775 ret = h1_process_output(h1c, buf, count);
1776 if (ret > 0) {
1777 h1_send(h1c);
1778
Christopher Faulet539e0292018-11-19 10:40:09 +01001779 /* We need to do that because of the infinite forwarding. <buf>
1780 * contains HTX messages so when infinite forwarding is enabled,
1781 * count is equal to the buffer size. From outside, the buffer
1782 * appears as full.
1783 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001784 if (!b_data(buf))
1785 ret = count;
1786 }
1787 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001788}
1789
Christopher Faulet1be55f92018-10-02 15:59:23 +02001790#if defined(CONFIG_HAP_LINUX_SPLICE)
1791/* Send and get, using splicing */
1792static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
1793{
1794 struct h1s *h1s = cs->ctx;
1795 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
1796 int ret = 0;
1797
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001798 if (b_data(&h1s->h1c->ibuf)) {
1799 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001800 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001801 }
1802
1803 h1s->flags &= ~H1S_F_BUF_FLUSH;
1804 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001805 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
1806 count = h1m->curr_len;
1807 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
1808 if (h1m->state == H1_MSG_DATA && ret > 0)
1809 h1m->curr_len -= ret;
1810 end:
1811 return ret;
1812
1813}
1814
1815static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
1816{
1817 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001818 int ret = 0;
1819
1820 if (b_data(&h1s->h1c->obuf))
1821 goto end;
1822
1823 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001824 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001825 if (pipe->data) {
1826 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
1827 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
1828 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001829 return ret;
1830}
1831#endif
1832
Christopher Faulet51dbc942018-09-13 09:05:15 +02001833/****************************************/
1834/* MUX initialization and instanciation */
1835/****************************************/
1836
1837/* The mux operations */
1838const struct mux_ops mux_h1_ops = {
1839 .init = h1_init,
1840 .wake = h1_wake,
1841 .attach = h1_attach,
1842 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01001843 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02001844 .detach = h1_detach,
1845 .destroy = h1_destroy,
1846 .avail_streams = h1_avail_streams,
1847 .rcv_buf = h1_rcv_buf,
1848 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02001849#if defined(CONFIG_HAP_LINUX_SPLICE)
1850 .rcv_pipe = h1_rcv_pipe,
1851 .snd_pipe = h1_snd_pipe,
1852#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02001853 .subscribe = h1_subscribe,
1854 .unsubscribe = h1_unsubscribe,
1855 .shutr = h1_shutr,
1856 .shutw = h1_shutw,
1857 .flags = MX_FL_NONE,
1858 .name = "h1",
1859};
1860
1861
1862/* this mux registers default HTX proto */
1863static struct mux_proto_list mux_proto_htx =
1864{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
1865
Willy Tarreau0108d902018-11-25 19:14:37 +01001866INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
1867
Christopher Faulet51dbc942018-09-13 09:05:15 +02001868/*
1869 * Local variables:
1870 * c-indent-level: 8
1871 * c-basic-offset: 8
1872 * End:
1873 */