blob: a2184a97077f4393ad33d53ec31a5b601ab0df20 [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{
597 struct proxy *be = h1s->h1c->px;
598 struct proxy *fe = strm_fe(si_strm(h1s->cs->data));
599 int flag = H1S_F_WANT_KAL;
600
601 /* Tunnel mode can only by set on the frontend */
602 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
603 flag = H1S_F_WANT_TUN;
604
605 /* For the server connection: server-close == httpclose */
606 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
607 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
608 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
609 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
610 flag = H1S_F_WANT_CLO;
611
612 /* flags order: CLO > SCL > TUN > KAL */
613 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
614 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
615
616 if (h1m->flags & H1_MF_RESP) {
617 /* Either we've established an explicit tunnel, or we're
618 * switching the protocol. In both cases, we're very unlikely to
619 * understand the next protocols. We have to switch to tunnel
620 * mode, so that we transfer the request and responses then let
621 * this protocol pass unmodified. When we later implement
622 * specific parsers for such protocols, we'll want to check the
623 * Upgrade header which contains information about that protocol
624 * for responses with status 101 (eg: see RFC2817 about TLS).
625 */
626 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
627 h1s->status == 101)
628 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
629 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
630 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
631 else if (h1s->flags & H1S_F_WANT_KAL &&
632 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
633 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
634 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
635 }
636
637 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
638 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
639 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200640}
641
Christopher Faulet9768c262018-10-22 09:34:31 +0200642static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
643 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200644{
645 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200646
647 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
648 * token is found
649 */
650 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200651 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200652
653 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200654 if (h1m->flags & H1_MF_CONN_CLO) {
655 if (conn_val)
656 *conn_val = ist("");
657 if (htx)
658 h1_remove_conn_hdrs(h1m, htx);
659 }
660 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
661 if (conn_val)
662 *conn_val = ist("keep-alive");
663 if (htx)
664 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
665 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200666 }
667 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200668 if (h1m->flags & H1_MF_CONN_KAL) {
669 if (conn_val)
670 *conn_val = ist("");
671 if (htx)
672 h1_remove_conn_hdrs(h1m, htx);
673 }
674 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
675 if (conn_val)
676 *conn_val = ist("close");
677 if (htx)
678 h1_add_conn_hdr(h1m, htx, ist("close"));
679 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200680 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200681}
682
Christopher Faulet9768c262018-10-22 09:34:31 +0200683static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
684 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200685{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200686 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
687 * token is found
688 */
689 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200690 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200691
692 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200693 if (h1m->flags & H1_MF_CONN_CLO) {
694 if (conn_val)
695 *conn_val = ist("");
696 if (htx)
697 h1_remove_conn_hdrs(h1m, htx);
698 }
699 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
700 if (conn_val)
701 *conn_val = ist("keep-alive");
702 if (htx)
703 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
704 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200705 }
706 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200707 if (h1m->flags & H1_MF_CONN_KAL) {
708 if (conn_val)
709 *conn_val = ist("");
710 if (htx)
711 h1_remove_conn_hdrs(h1m, htx);
712 }
713 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
714 if (conn_val)
715 *conn_val = ist("close");
716 if (htx)
717 h1_add_conn_hdr(h1m, htx, ist("close"));
718 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200719 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200720}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200721
Christopher Faulet9768c262018-10-22 09:34:31 +0200722/* Set the right connection mode and update "Connection:" header if
723 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
724 * message is updated accordingly. When <conn_val> is not NULL, it is set with
725 * the new header value.
726 */
727static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
728 struct htx *htx, struct ist *conn_val)
729{
730 if (!conn_is_back(h1s->h1c->conn)) {
731 h1_set_cli_conn_mode(h1s, h1m);
732 if (h1m->flags & H1_MF_RESP)
733 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
734 }
735 else {
736 h1_set_srv_conn_mode(h1s, h1m);
737 if (!(h1m->flags & H1_MF_RESP))
738 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
739 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200740}
741
Christopher Faulet129817b2018-09-20 16:14:40 +0200742/*
743 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200744 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
745 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet129817b2018-09-20 16:14:40 +0200746 * responsibile to update the parser state <h1m>.
747 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200748static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200749 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200750{
751 struct http_hdr hdrs[MAX_HTTP_HDR];
752 union h1_sl sl;
753 int ret = 0;
754
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100755 if (!max)
756 goto end;
757
Christopher Faulet129817b2018-09-20 16:14:40 +0200758 /* Realing input buffer if necessary */
759 if (b_head(buf) + b_data(buf) > b_wrap(buf))
760 b_slow_realign(buf, trash.area, 0);
761
Christopher Fauletf2824e62018-10-01 12:12:37 +0200762 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Faulet129817b2018-09-20 16:14:40 +0200763 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &sl);
764 if (ret <= 0) {
765 /* Incomplete or invalid message. If the buffer is full, it's an
766 * error because headers are too large to be handled by the
767 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200768 if (ret < 0 || (!ret && b_full(buf)))
769 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200770 goto end;
771 }
772
773 /* messages headers fully parsed, do some checks to prepare the body
774 * parsing.
775 */
776
777 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200778 if (ret > (b_size(buf) - global.tune.maxrewrite))
779 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200780
Christopher Faulet9768c262018-10-22 09:34:31 +0200781 /* Save the request's method or the response's status, check if the body
782 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200783 if (!(h1m->flags & H1_MF_RESP)) {
784 h1s->meth = sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200785
Christopher Faulet129817b2018-09-20 16:14:40 +0200786 /* Request have always a known length */
787 h1m->flags |= H1_MF_XFER_LEN;
788 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
789 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200790
791 if (!h1_process_req_vsn(h1s, h1m, sl)) {
792 h1m->err_pos = sl.rq.v.ptr - b_head(buf);
793 h1m->err_state = h1m->state;
794 goto vsn_error;
795 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200796 }
797 else {
798 h1s->status = sl.st.status;
799
800 if ((h1s->meth == HTTP_METH_HEAD) ||
801 (h1s->status >= 100 && h1s->status < 200) ||
802 (h1s->status == 204) || (h1s->status == 304) ||
803 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
804 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
805 h1m->flags |= H1_MF_XFER_LEN;
806 h1m->curr_len = h1m->body_len = 0;
807 h1m->state = H1_MSG_DONE;
808 }
809 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
810 h1m->flags |= H1_MF_XFER_LEN;
811 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
812 h1m->state = H1_MSG_DONE;
813 }
814 else
815 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200816
817 if (!h1_process_res_vsn(h1s, h1m, sl)) {
818 h1m->err_pos = sl.st.v.ptr - b_head(buf);
819 h1m->err_state = h1m->state;
820 goto vsn_error;
821 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200822 }
823
Christopher Faulet9768c262018-10-22 09:34:31 +0200824 if (!(h1m->flags & H1_MF_RESP)) {
825 if (!htx_add_reqline(htx, sl) || !htx_add_all_headers(htx, hdrs))
826 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200827 }
828 else {
Christopher Faulet9768c262018-10-22 09:34:31 +0200829 if (!htx_add_resline(htx, sl) || !htx_add_all_headers(htx, hdrs))
830 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200831 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200832 if (h1m->state == H1_MSG_DONE)
833 if (!htx_add_endof(htx, HTX_BLK_EOM))
834 goto error;
835
836 h1_process_conn_mode(h1s, h1m, htx, NULL);
837
838 /* If body length cannot be determined, set htx->extra to
839 * ULLONG_MAX. This value is impossible in other cases.
840 */
841 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
842
843 /* Recheck there is enough space to do headers rewritting */
844 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
845 goto error;
846
847 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200848 end:
849 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200850
851 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +0200852 h1m->err_state = h1m->state;
853 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +0200854 vsn_error:
855 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200856 ret = 0;
857 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200858}
859
860/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200861 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
862 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +0200863 * and filling h1s->err_pos and h1s->err_state fields. This functions is
864 * responsibile to update the parser state <h1m>.
865 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200866static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200867 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200868{
Christopher Faulet9768c262018-10-22 09:34:31 +0200869 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +0200870 size_t total = 0;
871 int ret = 0;
872
873 if (h1m->flags & H1_MF_XFER_LEN) {
874 if (h1m->flags & H1_MF_CLEN) {
875 /* content-length: read only h2m->body_len */
876 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200877 if (ret > data_space)
878 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200879 if ((uint64_t)ret > h1m->curr_len)
880 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200881 if (ret > b_contig_data(buf, *ofs))
882 ret = b_contig_data(buf, *ofs);
883 if (ret) {
884 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
885 goto end;
886 h1m->curr_len -= ret;
887 *ofs += ret;
888 total += ret;
889 }
890
891 if (!h1m->curr_len) {
892 if (!htx_add_endof(htx, HTX_BLK_EOM))
893 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200894 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200895 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200896 }
897 else if (h1m->flags & H1_MF_CHNK) {
898 new_chunk:
899 /* te:chunked : parse chunks */
900 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200901 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +0200902 if (ret <= 0)
903 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200904 h1m->state = H1_MSG_CHUNK_SIZE;
905
Christopher Faulet129817b2018-09-20 16:14:40 +0200906 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200907 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200908 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200909 }
910
911 if (h1m->state == H1_MSG_CHUNK_SIZE) {
912 unsigned int chksz;
913
Christopher Fauletf2824e62018-10-01 12:12:37 +0200914 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +0200915 if (ret <= 0)
916 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200917 if (!chksz) {
918 if (!htx_add_endof(htx, HTX_BLK_EOD))
919 goto end;
920 h1m->state = H1_MSG_TRAILERS;
921 }
922 else
923 h1m->state = H1_MSG_DATA;
924
Christopher Faulet129817b2018-09-20 16:14:40 +0200925 h1m->curr_len = chksz;
926 h1m->body_len += chksz;
927 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200928 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200929 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200930 }
931
932 if (h1m->state == H1_MSG_DATA) {
933 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200934 if (ret > data_space)
935 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200936 if ((uint64_t)ret > h1m->curr_len)
937 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200938 if (ret > b_contig_data(buf, *ofs))
939 ret = b_contig_data(buf, *ofs);
940 if (ret) {
941 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
942 goto end;
943 h1m->curr_len -= ret;
944 max -= ret;
945 *ofs += ret;
946 total += ret;
947 }
948 if (!h1m->curr_len) {
949 h1m->state = H1_MSG_CHUNK_CRLF;
950 goto new_chunk;
951 }
952 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200953 }
954
955 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200956 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +0200957 if (ret > data_space)
958 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +0200959 if (ret <= 0)
960 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200961
962 /* Realing input buffer if tailers wrap. For now
963 * this is a workaroung. Because trailers are
964 * not split on CRLF, like headers, there is no
965 * way to know where to split it when trailers
966 * wrap. This is a limitation of
967 * h1_measure_trailers.
968 */
969 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
970 b_slow_realign(buf, trash.area, 0);
971
972 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
973 goto end;
974
Christopher Faulet129817b2018-09-20 16:14:40 +0200975 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200976 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200977 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +0200978
979 /* FIXME: if it fails here, this is a problem,
980 * because there is no way to return here. */
981 if (!htx_add_endof(htx, HTX_BLK_EOM))
982 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200983 h1m->state = H1_MSG_DONE;
984 }
985 }
986 else {
987 /* XFER_LEN is set but not CLEN nor CHNK, it means there
988 * is no body. Switch the message in DONE state
989 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200990 if (!htx_add_endof(htx, HTX_BLK_EOM))
991 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200992 h1m->state = H1_MSG_DONE;
993 }
994 }
995 else {
996 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +0200997 ret = max;
998 if (ret > data_space)
999 ret = data_space;
1000 if (ret > b_contig_data(buf, *ofs))
1001 ret = b_contig_data(buf, *ofs);
1002 if (ret) {
1003 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1004 goto end;
1005
1006 *ofs += max;
1007 total = max;
1008 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001009 }
1010
1011 end:
1012 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001013 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001014 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001015 h1m->err_pos = *ofs + max + ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001016 return 0;
1017 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001018 /* update htx->extra, only when the body length is known */
1019 if (h1m->flags & H1_MF_XFER_LEN)
1020 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001021 return total;
1022}
1023
1024/*
1025 * Synchronize the request and the response before reseting them. Except for 1xx
1026 * responses, we wait that the request and the response are in DONE state and
1027 * that all data are forwarded for both. For 1xx responses, only the response is
1028 * reset, waiting the final one. Many 1xx messages can be sent.
1029 */
1030static void h1_sync_messages(struct h1c *h1c)
1031{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001032 struct h1s *h1s = h1c->h1s;
1033
1034 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001035 return;
1036
Christopher Fauletf2824e62018-10-01 12:12:37 +02001037 if (h1s->res.state == H1_MSG_DONE &&
1038 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001039 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001040 /* For 100-Continue response or any other informational 1xx
1041 * response which is non-final, don't reset the request, the
1042 * transaction is not finished. We take care the response was
1043 * transferred before.
1044 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001045 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001046 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001047 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001048 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001049 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1050 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001051 h1m_init_req(&h1s->req);
1052 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001053 h1s->req.state = H1_MSG_TUNNEL;
1054 h1s->res.state = H1_MSG_TUNNEL;
1055 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001056 }
1057}
1058
1059/*
1060 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001061 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1062 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001063 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001064static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001065{
Christopher Faulet539e0292018-11-19 10:40:09 +01001066 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001067 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001068 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001069 size_t total = 0;
1070 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001071 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001072 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001073
Christopher Faulet539e0292018-11-19 10:40:09 +01001074 htx = htx_from_buf(buf);
1075 count = b_data(&h1c->ibuf);
1076 max = htx_free_space(htx);
1077 if (flags & CO_RFL_KEEP_RSV) {
1078 if (max < global.tune.maxrewrite)
1079 goto end;
1080 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001081 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001082 if (count > max)
1083 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001084
Christopher Fauletf2824e62018-10-01 12:12:37 +02001085 if (!conn_is_back(h1c->conn)) {
1086 h1m = &h1s->req;
1087 errflag = H1S_F_REQ_ERROR;
1088 }
1089 else {
1090 h1m = &h1s->res;
1091 errflag = H1S_F_RES_ERROR;
1092 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001093
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001094 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001095 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001096 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001097 if (!ret)
1098 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001099 }
1100 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001101 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001102 if (!ret)
1103 break;
1104 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001105 else if (h1m->state == H1_MSG_DONE)
1106 break;
1107 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001108 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet9768c262018-10-22 09:34:31 +02001109 if (!ret)
1110 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001111 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001112 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001113 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001114 break;
1115 }
1116
Christopher Faulet539e0292018-11-19 10:40:09 +01001117 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001118 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001119
Christopher Faulet47365272018-10-31 17:40:50 +01001120 if (h1s->flags & errflag)
1121 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001122
Christopher Faulet539e0292018-11-19 10:40:09 +01001123 b_del(&h1c->ibuf, total);
1124
1125 end:
1126 if (htx_is_not_empty(htx))
1127 b_set_data(buf, b_size(buf));
1128 else {
1129 htx_reset(htx);
1130 b_set_data(buf, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001131 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001132
Christopher Faulet539e0292018-11-19 10:40:09 +01001133 if (h1c->flags & H1C_F_IN_FULL && b_room(&h1c->ibuf)) {
1134 h1c->flags &= ~H1C_F_IN_FULL;
1135 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001136 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001137
Christopher Faulet9c388402018-11-19 21:54:26 +01001138 if (b_data(&h1c->ibuf)) {
1139 if (!htx_is_empty(htx))
1140 h1s->cs->flags |= CS_FL_RCV_MORE;
1141 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001142 else {
1143 h1_release_buf(h1c, &h1c->ibuf);
1144 h1_sync_messages(h1c);
1145
1146 h1s->cs->flags &= ~CS_FL_RCV_MORE;
1147 if (h1s->cs->flags & CS_FL_REOS)
1148 h1s->cs->flags |= CS_FL_EOS;
1149 }
1150 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001151
1152 parsing_err:
1153 // FIXME: create an error snapshot here
1154 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001155 htx->flags |= HTX_FL_PARSING_ERROR;
1156 b_set_data(buf, b_size(buf));
1157 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001158 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001159}
1160
Christopher Faulet129817b2018-09-20 16:14:40 +02001161/*
1162 * Process outgoing data. It parses data and transfer them from the channel buffer into
1163 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1164 * 0 if it couldn't proceed.
1165 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001166static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1167{
Christopher Faulet129817b2018-09-20 16:14:40 +02001168 struct h1s *h1s = h1c->h1s;
1169 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001170 struct htx *chn_htx;
1171 struct htx_blk *blk;
1172 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001173 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001174 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001175
Christopher Faulet47365272018-10-31 17:40:50 +01001176 if (!count)
1177 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001178 chn_htx = htx_from_buf(buf);
1179
Christopher Faulet51dbc942018-09-13 09:05:15 +02001180 if (!h1_get_buf(h1c, &h1c->obuf)) {
1181 h1c->flags |= H1C_F_OUT_ALLOC;
1182 goto end;
1183 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001184
Christopher Fauletf2824e62018-10-01 12:12:37 +02001185 if (!conn_is_back(h1c->conn)) {
1186 h1m = &h1s->res;
1187 errflag = H1S_F_RES_ERROR;
1188 }
1189 else {
1190 h1m = &h1s->req;
1191 errflag = H1S_F_REQ_ERROR;
1192 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001193
1194
1195 tmp = get_trash_chunk();
1196 tmp->size = b_room(&h1c->obuf);
1197
1198 blk = htx_get_head_blk(chn_htx);
1199 while (!(h1s->flags & errflag) && blk) {
1200 union htx_sl *sl;
1201 struct ist n, v;
1202 uint32_t sz = htx_get_blksz(blk);
1203
1204 if (total + sz > count)
1205 goto copy;
1206
1207 switch (htx_get_blk_type(blk)) {
1208 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001210
1211 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001212 h1m_init_req(h1m);
1213 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001214 sl = htx_get_blk_ptr(chn_htx, blk);
1215 h1s->meth = sl->rq.meth;
1216 h1_parse_req_vsn(h1m, sl);
1217 if (!htx_reqline_to_str(sl, tmp))
1218 goto copy;
1219 h1m->flags |= H1_MF_XFER_LEN;
1220 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001221 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001222
Christopher Faulet9768c262018-10-22 09:34:31 +02001223 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001224 h1m_init_res(h1m);
1225 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001226 sl = htx_get_blk_ptr(chn_htx, blk);
1227 h1s->status = sl->st.status;
1228 h1_parse_res_vsn(h1m, sl);
1229 if (!htx_stline_to_str(sl, tmp))
1230 goto copy;
1231 if (chn_htx->extra != ULLONG_MAX)
1232 h1m->flags |= H1_MF_XFER_LEN;
1233 h1m->state = H1_MSG_HDR_FIRST;
1234 break;
1235
1236 case HTX_BLK_HDR:
1237 if (h1m->state == H1_MSG_HDR_FIRST) {
1238 struct http_hdr_ctx ctx;
1239
1240 n = ist("Connection");
1241 v = ist("");
1242
1243 /* If there is no "Connection:" header,
1244 * process conn_mode now and add the
1245 * right one.
1246 */
1247 ctx.blk = blk;
Christopher Faulet5999b862018-11-27 10:46:09 +01001248 ctx.value = ist(NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +02001249 if (http_find_header(chn_htx, n, &ctx, 1))
1250 goto process_hdr;
1251 h1_process_conn_mode(h1s, h1m, NULL, &v);
1252 if (!v.len)
1253 goto process_hdr;
1254
1255 if (!htx_hdr_to_str(n, v, tmp))
1256 goto copy;
1257 }
1258 process_hdr:
1259 h1m->state = H1_MSG_HDR_NAME;
1260 n = htx_get_blk_name(chn_htx, blk);
1261 v = htx_get_blk_value(chn_htx, blk);
1262
1263 if (isteqi(n, ist("transfer-encoding")))
1264 h1_parse_xfer_enc_header(h1m, v);
1265 else if (isteqi(n, ist("connection"))) {
1266 h1_parse_connection_header(h1m, v);
1267 h1_process_conn_mode(h1s, h1m, NULL, &v);
1268 if (!v.len)
1269 goto skip_hdr;
1270 }
1271
1272 if (!htx_hdr_to_str(n, v, tmp))
1273 goto copy;
1274 skip_hdr:
1275 h1m->state = H1_MSG_HDR_L2_LWS;
1276 break;
1277
1278 case HTX_BLK_PHDR:
1279 /* not implemented yet */
1280 h1m->flags |= errflag;
1281 break;
1282
1283 case HTX_BLK_EOH:
1284 h1m->state = H1_MSG_LAST_LF;
1285 if (!chunk_memcat(tmp, "\r\n", 2))
1286 goto copy;
1287
1288 h1m->state = H1_MSG_DATA;
1289 break;
1290
1291 case HTX_BLK_DATA:
1292 v = htx_get_blk_value(chn_htx, blk);
1293 if (!htx_data_to_str(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
1294 goto copy;
1295 break;
1296
1297 case HTX_BLK_EOD:
1298 if (!chunk_memcat(tmp, "0\r\n", 3))
1299 goto copy;
1300 h1m->state = H1_MSG_TRAILERS;
1301 break;
1302
1303 case HTX_BLK_TLR:
1304 v = htx_get_blk_value(chn_htx, blk);
1305 if (!htx_trailer_to_str(v, tmp))
1306 goto copy;
1307 break;
1308
1309 case HTX_BLK_EOM:
1310 /* if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(tmp, "\r\n", 2)) */
1311 /* goto copy; */
1312 h1m->state = H1_MSG_DONE;
1313 break;
1314
1315 case HTX_BLK_OOB:
1316 v = htx_get_blk_value(chn_htx, blk);
1317 if (!chunk_memcat(tmp, v.ptr, v.len))
1318 goto copy;
1319 break;
1320
1321 default:
1322 h1m->flags |= errflag;
1323 break;
1324 }
1325 total += sz;
1326 blk = htx_remove_blk(chn_htx, blk);
Christopher Faulet129817b2018-09-20 16:14:40 +02001327 }
1328
Christopher Faulet9768c262018-10-22 09:34:31 +02001329 copy:
1330 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001331
1332 if (b_full(&h1c->obuf))
1333 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 if (htx_is_empty(chn_htx)) {
1335 htx_reset(chn_htx);
1336 b_set_data(buf, 0);
1337 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001338 end:
1339 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001340}
1341
Christopher Faulet51dbc942018-09-13 09:05:15 +02001342/*********************************************************/
1343/* functions below are I/O callbacks from the connection */
1344/*********************************************************/
1345/*
1346 * Attempt to read data, and subscribe if none available
1347 */
1348static int h1_recv(struct h1c *h1c)
1349{
1350 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001351 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001352 size_t ret, max;
1353 int rcvd = 0;
1354
1355 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
1356 return 0;
1357
Christopher Faulet539e0292018-11-19 10:40:09 +01001358 if (!h1_recv_allowed(h1c))
Christopher Faulet9768c262018-10-22 09:34:31 +02001359 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001360
Christopher Fauletfeb11742018-11-29 15:12:34 +01001361 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001362 rcvd = 1;
1363 goto end;
1364 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001365
Christopher Faulet51dbc942018-09-13 09:05:15 +02001366 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1367 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001368 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001369 }
1370
1371 ret = 0;
1372 max = b_room(&h1c->ibuf);
1373 if (max) {
1374 h1c->flags &= ~H1C_F_IN_FULL;
1375 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
1376 }
Christopher Faulet47365272018-10-31 17:40:50 +01001377 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001378 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001379 if (h1s && h1s->cs) {
1380 h1s->cs->flags |= CS_FL_READ_PARTIAL;
1381 if (h1s->csinfo.t_idle == -1)
1382 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1383 }
Christopher Faulet47365272018-10-31 17:40:50 +01001384 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001385
1386 if (h1_recv_allowed(h1c))
1387 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet81d48432018-11-19 21:22:43 +01001388 else
1389 rcvd = 1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001390
Christopher Faulet9768c262018-10-22 09:34:31 +02001391 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001392 if (!b_data(&h1c->ibuf))
1393 h1_release_buf(h1c, &h1c->ibuf);
1394 else if (b_full(&h1c->ibuf))
1395 h1c->flags |= H1C_F_IN_FULL;
1396 return rcvd;
1397}
1398
1399
1400/*
1401 * Try to send data if possible
1402 */
1403static int h1_send(struct h1c *h1c)
1404{
1405 struct connection *conn = h1c->conn;
1406 unsigned int flags = 0;
1407 size_t ret;
1408 int sent = 0;
1409
1410 if (conn->flags & CO_FL_ERROR)
1411 return 0;
1412
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001413 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1414 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1415 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1416 return 0;
1417 }
1418
Christopher Faulet51dbc942018-09-13 09:05:15 +02001419 if (!b_data(&h1c->obuf))
1420 goto end;
1421
1422 if (h1c->flags & H1C_F_OUT_FULL)
1423 flags |= CO_SFL_MSG_MORE;
1424
1425 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1426 if (ret > 0) {
1427 h1c->flags &= ~H1C_F_OUT_FULL;
1428 b_del(&h1c->obuf, ret);
1429 sent = 1;
1430 }
1431
1432 end:
1433 /* We're done, no more to send */
1434 if (!b_data(&h1c->obuf)) {
1435 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001436 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001437 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1438 h1_shutw_conn(conn);
1439 }
1440 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1441 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1442
1443 return sent;
1444}
1445
1446
1447static void h1_wake_stream(struct h1c *h1c)
1448{
1449 struct connection *conn = h1c->conn;
1450 struct h1s *h1s = h1c->h1s;
1451 uint32_t flags = 0;
1452 int dont_wake = 0;
1453
1454 if (!h1s || !h1s->cs)
1455 return;
1456
1457 if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR))
1458 flags |= CS_FL_ERROR;
1459 if (conn_xprt_read0_pending(conn))
1460 flags |= CS_FL_REOS;
1461
1462 h1s->cs->flags |= flags;
1463 if (h1s->recv_wait) {
1464 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1465 tasklet_wakeup(h1s->recv_wait->task);
1466 h1s->recv_wait = NULL;
1467 dont_wake = 1;
1468 }
1469 if (h1s->send_wait) {
1470 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1471 tasklet_wakeup(h1s->send_wait->task);
1472 h1s->send_wait = NULL;
1473 dont_wake = 1;
1474 }
1475 if (!dont_wake && h1s->cs->data_cb->wake)
1476 h1s->cs->data_cb->wake(h1s->cs);
1477}
1478
1479/* callback called on any event by the connection handler.
1480 * It applies changes and returns zero, or < 0 if it wants immediate
1481 * destruction of the connection.
1482 */
1483static int h1_process(struct h1c * h1c)
1484{
1485 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001486 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001487
Christopher Faulet51dbc942018-09-13 09:05:15 +02001488 if (!conn->mux_ctx)
1489 return -1;
1490
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001491 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001492 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1493 goto end;
1494 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001495 }
1496
Christopher Fauletfeb11742018-11-29 15:12:34 +01001497 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001498 if (h1c->flags & H1C_F_CS_ERROR ||
1499 conn->flags & CO_FL_ERROR ||
1500 conn_xprt_read0_pending(conn))
1501 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001502 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001503 if (!h1s_create(h1c, NULL))
1504 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001505 }
Christopher Fauletfeb11742018-11-29 15:12:34 +01001506 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001507 }
1508
Christopher Fauletfeb11742018-11-29 15:12:34 +01001509 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1510 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1511
Christopher Faulet539e0292018-11-19 10:40:09 +01001512 h1_wake_stream(h1c);
Christopher Faulet47365272018-10-31 17:40:50 +01001513 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001514 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001515
1516 release:
1517 h1_release(conn);
1518 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001519}
1520
1521static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1522{
1523 struct h1c *h1c = ctx;
1524 int ret = 0;
1525
1526 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1527 ret = h1_send(h1c);
1528 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1529 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001530 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001531 h1_process(h1c);
1532 return NULL;
1533}
1534
1535
1536static int h1_wake(struct connection *conn)
1537{
1538 struct h1c *h1c = conn->mux_ctx;
1539
Christopher Faulet539e0292018-11-19 10:40:09 +01001540 h1_send(h1c);
1541 return h1_process(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001542}
1543
Christopher Faulet51dbc942018-09-13 09:05:15 +02001544/*******************************************/
1545/* functions below are used by the streams */
1546/*******************************************/
1547/*
1548 * Attach a new stream to a connection
1549 * (Used for outgoing connections)
1550 */
1551static struct conn_stream *h1_attach(struct connection *conn)
1552{
1553 struct h1c *h1c = conn->mux_ctx;
1554 struct conn_stream *cs = NULL;
1555 struct h1s *h1s;
1556
1557 if (h1c->flags & H1C_F_CS_ERROR)
1558 goto end;
1559
1560 cs = cs_new(h1c->conn);
1561 if (!cs)
1562 goto end;
1563
Christopher Fauletf2824e62018-10-01 12:12:37 +02001564 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001565 if (h1s == NULL)
1566 goto end;
1567
1568 return cs;
1569 end:
1570 cs_free(cs);
1571 return NULL;
1572}
1573
1574/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1575 * this mux, it's easy as we can only store a single conn_stream.
1576 */
1577static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1578{
1579 struct h1c *h1c = conn->mux_ctx;
1580 struct h1s *h1s = h1c->h1s;
1581
1582 if (h1s)
1583 return h1s->cs;
1584
1585 return NULL;
1586}
1587
1588static void h1_destroy(struct connection *conn)
1589{
1590 struct h1c *h1c = conn->mux_ctx;
1591
1592 if (!h1c->h1s)
1593 h1_release(conn);
1594}
1595
1596/*
1597 * Detach the stream from the connection and possibly release the connection.
1598 */
1599static void h1_detach(struct conn_stream *cs)
1600{
1601 struct h1s *h1s = cs->ctx;
1602 struct h1c *h1c;
1603
1604 cs->ctx = NULL;
1605 if (!h1s)
1606 return;
1607
1608 h1c = h1s->h1c;
1609 h1s->cs = NULL;
1610
1611 h1s_destroy(h1s);
1612
1613 /* We don't want to close right now unless the connection is in error */
1614 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
1615 (h1c->conn->flags & CO_FL_ERROR))
1616 h1_release(h1c->conn);
1617 else
1618 tasklet_wakeup(h1c->wait_event.task);
1619}
1620
1621
1622static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1623{
1624 struct h1s *h1s = cs->ctx;
1625
1626 if (!h1s)
1627 return;
1628
Christopher Fauletf2824e62018-10-01 12:12:37 +02001629 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1630 return;
1631
Christopher Faulet51dbc942018-09-13 09:05:15 +02001632 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1633 if (cs->flags & CS_FL_SHR)
1634 return;
1635 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1636 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1637 if (cs->flags & CS_FL_SHW) {
1638 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1639 conn_full_close(cs->conn);
1640 }
1641}
1642
1643static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1644{
1645 struct h1s *h1s = cs->ctx;
1646 struct h1c *h1c;
1647
1648 if (!h1s)
1649 return;
1650 h1c = h1s->h1c;
1651
Christopher Fauletf2824e62018-10-01 12:12:37 +02001652 if ((h1s->flags & H1S_F_WANT_KAL) &&
1653 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1654 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1655 return;
1656
Christopher Faulet51dbc942018-09-13 09:05:15 +02001657 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1658 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1659 return;
1660
1661 h1_shutw_conn(cs->conn);
1662}
1663
1664static void h1_shutw_conn(struct connection *conn)
1665{
1666 struct h1c *h1c = conn->mux_ctx;
1667
1668 if (conn_xprt_ready(conn) && conn->xprt->shutw)
1669 conn->xprt->shutw(conn, 1);
1670 if (!(conn->flags & CO_FL_SOCK_RD_SH))
1671 conn_sock_shutw(conn, 1);
1672 else {
1673 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1674 conn_full_close(conn);
1675 }
1676}
1677
1678/* Called from the upper layer, to unsubscribe to events */
1679static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1680{
1681 struct wait_event *sw;
1682 struct h1s *h1s = cs->ctx;
1683
1684 if (!h1s)
1685 return 0;
1686
1687 if (event_type & SUB_CAN_RECV) {
1688 sw = param;
1689 if (h1s->recv_wait == sw) {
1690 sw->wait_reason &= ~SUB_CAN_RECV;
1691 h1s->recv_wait = NULL;
1692 }
1693 }
1694 if (event_type & SUB_CAN_SEND) {
1695 sw = param;
1696 if (h1s->send_wait == sw) {
1697 sw->wait_reason &= ~SUB_CAN_SEND;
1698 h1s->send_wait = NULL;
1699 }
1700 }
1701 return 0;
1702}
1703
1704/* Called from the upper layer, to subscribe to events, such as being able to send */
1705static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1706{
1707 struct wait_event *sw;
1708 struct h1s *h1s = cs->ctx;
1709
1710 if (!h1s)
1711 return -1;
1712
1713 switch (event_type) {
1714 case SUB_CAN_RECV:
1715 sw = param;
1716 if (!(sw->wait_reason & SUB_CAN_RECV)) {
1717 sw->wait_reason |= SUB_CAN_RECV;
1718 sw->handle = h1s;
1719 h1s->recv_wait = sw;
1720 }
1721 return 0;
1722 case SUB_CAN_SEND:
1723 sw = param;
1724 if (!(sw->wait_reason & SUB_CAN_SEND)) {
1725 sw->wait_reason |= SUB_CAN_SEND;
1726 sw->handle = h1s;
1727 h1s->send_wait = sw;
1728 }
1729 return 0;
1730 default:
1731 break;
1732 }
1733 return -1;
1734}
1735
1736/* Called from the upper layer, to receive data */
1737static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1738{
1739 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01001740 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001741 size_t ret = 0;
1742
Christopher Faulet539e0292018-11-19 10:40:09 +01001743 if (!(h1c->flags & H1C_F_IN_ALLOC))
1744 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001745
1746 if (flags & CO_RFL_BUF_FLUSH)
1747 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001748 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
1749 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01001750 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1751 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001752 }
1753 return ret;
1754}
1755
1756
1757/* Called from the upper layer, to send data */
1758static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1759{
1760 struct h1s *h1s = cs->ctx;
1761 struct h1c *h1c;
1762 size_t ret = 0;
1763
1764 if (!h1s)
1765 return ret;
1766
1767 h1c = h1s->h1c;
1768
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001769 if (h1c->flags & H1C_F_CS_WAIT_CONN)
1770 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001771
Christopher Faulet539e0292018-11-19 10:40:09 +01001772 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001773 ret = h1_process_output(h1c, buf, count);
1774 if (ret > 0) {
1775 h1_send(h1c);
1776
Christopher Faulet539e0292018-11-19 10:40:09 +01001777 /* We need to do that because of the infinite forwarding. <buf>
1778 * contains HTX messages so when infinite forwarding is enabled,
1779 * count is equal to the buffer size. From outside, the buffer
1780 * appears as full.
1781 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001782 if (!b_data(buf))
1783 ret = count;
1784 }
1785 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001786}
1787
Christopher Faulet1be55f92018-10-02 15:59:23 +02001788#if defined(CONFIG_HAP_LINUX_SPLICE)
1789/* Send and get, using splicing */
1790static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
1791{
1792 struct h1s *h1s = cs->ctx;
1793 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
1794 int ret = 0;
1795
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001796 if (b_data(&h1s->h1c->ibuf)) {
1797 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001798 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001799 }
1800
1801 h1s->flags &= ~H1S_F_BUF_FLUSH;
1802 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001803 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
1804 count = h1m->curr_len;
1805 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
1806 if (h1m->state == H1_MSG_DATA && ret > 0)
1807 h1m->curr_len -= ret;
1808 end:
1809 return ret;
1810
1811}
1812
1813static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
1814{
1815 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02001816 int ret = 0;
1817
1818 if (b_data(&h1s->h1c->obuf))
1819 goto end;
1820
1821 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001822 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001823 if (pipe->data) {
1824 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
1825 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
1826 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001827 return ret;
1828}
1829#endif
1830
Christopher Faulet51dbc942018-09-13 09:05:15 +02001831/****************************************/
1832/* MUX initialization and instanciation */
1833/****************************************/
1834
1835/* The mux operations */
1836const struct mux_ops mux_h1_ops = {
1837 .init = h1_init,
1838 .wake = h1_wake,
1839 .attach = h1_attach,
1840 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01001841 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02001842 .detach = h1_detach,
1843 .destroy = h1_destroy,
1844 .avail_streams = h1_avail_streams,
1845 .rcv_buf = h1_rcv_buf,
1846 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02001847#if defined(CONFIG_HAP_LINUX_SPLICE)
1848 .rcv_pipe = h1_rcv_pipe,
1849 .snd_pipe = h1_snd_pipe,
1850#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02001851 .subscribe = h1_subscribe,
1852 .unsubscribe = h1_unsubscribe,
1853 .shutr = h1_shutr,
1854 .shutw = h1_shutw,
1855 .flags = MX_FL_NONE,
1856 .name = "h1",
1857};
1858
1859
1860/* this mux registers default HTX proto */
1861static struct mux_proto_list mux_proto_htx =
1862{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
1863
Willy Tarreau0108d902018-11-25 19:14:37 +01001864INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
1865
Christopher Faulet51dbc942018-09-13 09:05:15 +02001866/*
1867 * Local variables:
1868 * c-indent-level: 8
1869 * c-basic-offset: 8
1870 * End:
1871 */