blob: cbbcd90aa338de2e37c2a17a50dc40a2d236be75 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <common/cfgparse.h>
13#include <common/config.h>
14
Christopher Faulet1be55f92018-10-02 15:59:23 +020015#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020016#include <types/proxy.h>
17#include <types/session.h>
18
Christopher Faulet51dbc942018-09-13 09:05:15 +020019#include <proto/connection.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020020#include <proto/h1.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020021#include <proto/http_htx.h>
22#include <proto/htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020023#include <proto/log.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020024#include <proto/stream.h>
25#include <proto/stream_interface.h>
26
27/*
28 * H1 Connection flags (32 bits)
29 */
30#define H1C_F_NONE 0x00000000
31
32/* Flags indicating why writing output data are blocked */
33#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
34#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
35/* 0x00000004 - 0x00000008 unused */
36
37/* Flags indicating why reading input data are blocked. */
38#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
39#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Faulet539e0292018-11-19 10:40:09 +010040/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020041
42#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
43#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
44#define H1C_F_CS_SHUTW 0x00004000 /* connection is already shut down */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020045#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Fauletf2824e62018-10-01 12:12:37 +020047#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet129817b2018-09-20 16:14:40 +020048
Christopher Faulet51dbc942018-09-13 09:05:15 +020049/*
50 * H1 Stream flags (32 bits)
51 */
Christopher Faulet129817b2018-09-20 16:14:40 +020052#define H1S_F_NONE 0x00000000
53#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020054#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
55#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Christopher Faulet539e0292018-11-19 10:40:09 +010056/* 0x00000008 unused */
Christopher Fauletf2824e62018-10-01 12:12:37 +020057#define H1S_F_WANT_KAL 0x00000010
58#define H1S_F_WANT_TUN 0x00000020
59#define H1S_F_WANT_CLO 0x00000040
60#define H1S_F_WANT_MSK 0x00000070
61#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010062#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Faulet129817b2018-09-20 16:14:40 +020063
Christopher Faulet51dbc942018-09-13 09:05:15 +020064
65/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020066struct h1c {
67 struct connection *conn;
68 struct proxy *px;
69 uint32_t flags; /* Connection flags: H1C_F_* */
70
71 struct buffer ibuf; /* Input buffer to store data before parsing */
72 struct buffer obuf; /* Output buffer to store data after reformatting */
73
74 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
75 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
76
77 struct h1s *h1s; /* H1 stream descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020078};
79
80/* H1 stream descriptor */
81struct h1s {
82 struct h1c *h1c;
83 struct conn_stream *cs;
84 uint32_t flags; /* Connection flags: H1S_F_* */
85
Christopher Faulet51dbc942018-09-13 09:05:15 +020086 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
87 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020088
89 struct h1m req;
90 struct h1m res;
91
92 enum http_meth_t meth; /* HTTP resquest method */
93 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +020094};
95
96/* the h1c and h1s pools */
97static struct pool_head *pool_head_h1c;
98static struct pool_head *pool_head_h1s;
99
Christopher Faulet51dbc942018-09-13 09:05:15 +0200100static int h1_recv(struct h1c *h1c);
101static int h1_send(struct h1c *h1c);
102static int h1_process(struct h1c *h1c);
103static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
104static void h1_shutw_conn(struct connection *conn);
105
106/*****************************************************/
107/* functions below are for dynamic buffer management */
108/*****************************************************/
109/*
110 * Indicates whether or not the we may call the h1_recv() function to
111 * attempt to receive data into the buffer and/or parse pending data. The
112 * condition is a bit complex due to some API limits for now. The rules are the
113 * following :
114 * - if an error or a shutdown was detected on the connection and the buffer
115 * is empty, we must not attempt to receive
116 * - if the input buffer failed to be allocated, we must not try to receive
117 * and we know there is nothing pending
118 * - if no flag indicates a blocking condition, we may attempt to receive,
119 * regardless of whether the input buffer is full or not, so that only de
120 * receiving part decides whether or not to block. This is needed because
121 * the connection API indeed prevents us from re-enabling receipt that is
122 * already enabled in a polled state, so we must always immediately stop as
123 * soon as the mux can't proceed so as never to hit an end of read with data
124 * pending in the buffers.
125 * - otherwise must may not attempt to receive
126 */
127static inline int h1_recv_allowed(const struct h1c *h1c)
128{
129 if (b_data(&h1c->ibuf) == 0 &&
130 (h1c->flags & (H1C_F_CS_ERROR||H1C_F_CS_SHUTW) ||
131 h1c->conn->flags & CO_FL_ERROR ||
132 conn_xprt_read0_pending(h1c->conn)))
133 return 0;
134
135 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL)))
136 return 1;
137
138 return 0;
139}
140
141/*
142 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
143 * flags are used to figure what buffer was requested. It returns 1 if the
144 * allocation succeeds, in which case the connection is woken up, or 0 if it's
145 * impossible to wake up and we prefer to be woken up later.
146 */
147static int h1_buf_available(void *target)
148{
149 struct h1c *h1c = target;
150
151 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
152 h1c->flags &= ~H1C_F_IN_ALLOC;
153 if (h1_recv_allowed(h1c))
154 tasklet_wakeup(h1c->wait_event.task);
155 return 1;
156 }
157
158 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
159 h1c->flags &= ~H1C_F_OUT_ALLOC;
160 tasklet_wakeup(h1c->wait_event.task);
161 return 1;
162 }
163
Christopher Faulet51dbc942018-09-13 09:05:15 +0200164 return 0;
165}
166
167/*
168 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
169 */
170static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
171{
172 struct buffer *buf = NULL;
173
174 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
175 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
176 h1c->buf_wait.target = h1c;
177 h1c->buf_wait.wakeup_cb = h1_buf_available;
178 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
179 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
180 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
181 __conn_xprt_stop_recv(h1c->conn);
182 }
183 return buf;
184}
185
186/*
187 * Release a buffer, if any, and try to wake up entities waiting in the buffer
188 * wait queue.
189 */
190static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
191{
192 if (bptr->size) {
193 b_free(bptr);
194 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
195 }
196}
197
198static int h1_avail_streams(struct connection *conn)
199{
200 struct h1c *h1c = conn->mux_ctx;
201
202 return h1c->h1s ? 0 : 1;
203}
204
205
206/*****************************************************************/
207/* functions below are dedicated to the mux setup and management */
208/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100209static struct conn_stream *h1s_new_cs(struct h1s *h1s)
210{
211 struct conn_stream *cs;
212
213 cs = cs_new(h1s->h1c->conn);
214 if (!cs)
215 goto err;
216 h1s->cs = cs;
217 cs->ctx = h1s;
218
219 if (h1s->flags & H1S_F_NOT_FIRST)
220 cs->flags |= CS_FL_NOT_FIRST;
221
222 if (stream_create_from_cs(cs) < 0)
223 goto err;
224 return cs;
225
226 err:
227 cs_free(cs);
228 h1s->cs = NULL;
229 return NULL;
230}
231
Christopher Fauletf2824e62018-10-01 12:12:37 +0200232static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200233{
234 struct h1s *h1s;
235
236 h1s = pool_alloc(pool_head_h1s);
237 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100238 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200239
240 h1s->h1c = h1c;
241 h1c->h1s = h1s;
242
243 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200244 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200245
246 h1s->recv_wait = NULL;
247 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200248
249 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200250 h1s->req.flags |= H1_MF_NO_PHDR;
251
Christopher Faulet129817b2018-09-20 16:14:40 +0200252 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200253 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200254
255 h1s->status = 0;
256 h1s->meth = HTTP_METH_OTHER;
257
Christopher Faulet47365272018-10-31 17:40:50 +0100258 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
259 h1s->flags |= H1S_F_NOT_FIRST;
260 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
261
Christopher Faulet129817b2018-09-20 16:14:40 +0200262 if (!conn_is_back(h1c->conn)) {
263 if (h1c->px->options2 & PR_O2_REQBUG_OK)
264 h1s->req.err_pos = -1;
265 }
266 else {
267 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
268 h1s->res.err_pos = -1;
269 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200270
Christopher Faulet539e0292018-11-19 10:40:09 +0100271 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
272 * create a new one.
273 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200274 if (cs) {
275 cs->ctx = h1s;
276 h1s->cs = cs;
277 }
Christopher Faulet47365272018-10-31 17:40:50 +0100278 else {
279 cs = h1s_new_cs(h1s);
280 if (!cs)
281 goto fail;
282 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200283 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100284
285 fail:
286 pool_free(pool_head_h1s, h1s);
287 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200288}
289
290static void h1s_destroy(struct h1s *h1s)
291{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200292 if (h1s) {
293 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200294
Christopher Fauletf2824e62018-10-01 12:12:37 +0200295 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200296
Christopher Fauletf2824e62018-10-01 12:12:37 +0200297 if (h1s->recv_wait != NULL)
298 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
299 if (h1s->send_wait != NULL)
300 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
301
Christopher Faulet47365272018-10-31 17:40:50 +0100302 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
303 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
304 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200305
Christopher Fauletf2824e62018-10-01 12:12:37 +0200306 cs_free(h1s->cs);
307 pool_free(pool_head_h1s, h1s);
308 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200309}
310
311/*
312 * Initialize the mux once it's attached. It is expected that conn->mux_ctx
313 * points to the existing conn_stream (for outgoing connections) or NULL (for
314 * incoming ones). Returns < 0 on error.
315 */
316static int h1_init(struct connection *conn, struct proxy *proxy)
317{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200318 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200319
320 h1c = pool_alloc(pool_head_h1c);
321 if (!h1c)
322 goto fail_h1c;
323 h1c->conn = conn;
324 h1c->px = proxy;
325
326 h1c->flags = H1C_F_NONE;
327 h1c->ibuf = BUF_NULL;
328 h1c->obuf = BUF_NULL;
329 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200330
Christopher Faulet51dbc942018-09-13 09:05:15 +0200331 LIST_INIT(&h1c->buf_wait.list);
332 h1c->wait_event.task = tasklet_new();
333 if (!h1c->wait_event.task)
334 goto fail;
335 h1c->wait_event.task->process = h1_io_cb;
336 h1c->wait_event.task->context = h1c;
337 h1c->wait_event.wait_reason = 0;
338
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200339 if (!(conn->flags & CO_FL_CONNECTED))
340 h1c->flags |= H1C_F_CS_WAIT_CONN;
341
Christopher Fauletf2824e62018-10-01 12:12:37 +0200342 /* Always Create a new H1S */
343 if (!h1s_create(h1c, conn->mux_ctx))
344 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200345
Christopher Faulet129817b2018-09-20 16:14:40 +0200346 conn->mux_ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200347
Christopher Faulet51dbc942018-09-13 09:05:15 +0200348 /* Try to read, if nothing is available yet we'll just subscribe */
349 if (h1_recv(h1c))
350 h1_process(h1c);
351
352 /* mux->wake will be called soon to complete the operation */
353 return 0;
354
355 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100356 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200357 tasklet_free(h1c->wait_event.task);
358 pool_free(pool_head_h1c, h1c);
359 fail_h1c:
360 return -1;
361}
362
363
364/* release function for a connection. This one should be called to free all
365 * resources allocated to the mux.
366 */
367static void h1_release(struct connection *conn)
368{
369 struct h1c *h1c = conn->mux_ctx;
370
371 LIST_DEL(&conn->list);
372
373 if (h1c) {
374 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
375 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
376 LIST_DEL(&h1c->buf_wait.list);
377 LIST_INIT(&h1c->buf_wait.list);
378 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
379 }
380
381 h1_release_buf(h1c, &h1c->ibuf);
382 h1_release_buf(h1c, &h1c->obuf);
383
Christopher Faulet51dbc942018-09-13 09:05:15 +0200384 if (h1c->wait_event.task)
385 tasklet_free(h1c->wait_event.task);
386
Christopher Fauletf2824e62018-10-01 12:12:37 +0200387 h1s_destroy(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200388 if (h1c->wait_event.wait_reason != 0)
389 conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason,
390 &h1c->wait_event);
391 pool_free(pool_head_h1c, h1c);
392 }
393
394 conn->mux = NULL;
395 conn->mux_ctx = NULL;
396
397 conn_stop_tracking(conn);
398 conn_full_close(conn);
399 if (conn->destroy_cb)
400 conn->destroy_cb(conn);
401 conn_free(conn);
402}
403
404/******************************************************/
405/* functions below are for the H1 protocol processing */
406/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200407/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
408 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200409 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200410static void h1_parse_req_vsn(struct h1m *h1m, const union htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200411{
Christopher Faulet9768c262018-10-22 09:34:31 +0200412 const char *p = sl->rq.l + sl->rq.m_len + sl->rq.u_len;
413
414 if ((sl->rq.v_len == 8) &&
415 (*(p + 5) > '1' ||
416 (*(p + 5) == '1' && *(p + 7) >= '1')))
417 h1m->flags |= H1_MF_VER_11;
418}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200419
Christopher Faulet9768c262018-10-22 09:34:31 +0200420/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
421 * greater or equal to 1.1
422 */
423static void h1_parse_res_vsn(struct h1m *h1m, const union htx_sl *sl)
424{
425 const char *p = sl->rq.l;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200426
Christopher Faulet9768c262018-10-22 09:34:31 +0200427 if ((sl->st.v_len == 8) &&
428 (*(p + 5) > '1' ||
429 (*(p + 5) == '1' && *(p + 7) >= '1')))
430 h1m->flags |= H1_MF_VER_11;
431}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200432
Christopher Faulet9768c262018-10-22 09:34:31 +0200433/*
434 * Check the validity of the request version. If the version is valid, it
435 * returns 1. Otherwise, it returns 0.
436 */
437static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
438{
439 struct h1c *h1c = h1s->h1c;
440
441 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
442 * exactly one digit "." one digit. This check may be disabled using
443 * option accept-invalid-http-request.
444 */
445 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
446 if (sl.rq.v.len != 8)
447 return 0;
448
449 if (*(sl.rq.v.ptr + 4) != '/' ||
450 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
451 *(sl.rq.v.ptr + 6) != '.' ||
452 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
453 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200454 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200455 else if (!sl.rq.v.len) {
456 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200457
Christopher Faulet9768c262018-10-22 09:34:31 +0200458 /* RFC 1945 allows only GET for HTTP/0.9 requests */
459 if (sl.rq.meth != HTTP_METH_GET)
460 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200461
Christopher Faulet9768c262018-10-22 09:34:31 +0200462 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
463 if (!sl.rq.u.len)
464 return 0;
465
466 /* Add HTTP version */
467 sl.rq.v = ist("HTTP/1.0");
468 }
469 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200470}
471
Christopher Faulet9768c262018-10-22 09:34:31 +0200472/*
473 * Check the validity of the response version. If the version is valid, it
474 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200475 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200476static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200477{
Christopher Faulet9768c262018-10-22 09:34:31 +0200478 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200479
Christopher Faulet9768c262018-10-22 09:34:31 +0200480 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
481 * exactly one digit "." one digit. This check may be disabled using
482 * option accept-invalid-http-request.
483 */
484 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
485 if (sl.st.v.len != 8)
486 return 0;
487
488 if (*(sl.st.v.ptr + 4) != '/' ||
489 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
490 *(sl.st.v.ptr + 6) != '.' ||
491 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
492 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200493 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200494 return 1;
495}
496/* Remove all "Connection:" headers from the HTX message <htx> */
497static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
498{
499 struct ist hdr = {.ptr = "Connection", .len = 10};
500 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200501
Christopher Faulet9768c262018-10-22 09:34:31 +0200502 while (http_find_header(htx, hdr, &ctx, 1))
503 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200504
Christopher Faulet9768c262018-10-22 09:34:31 +0200505 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
506}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200507
Christopher Faulet9768c262018-10-22 09:34:31 +0200508/* Add a "Connection:" header with the value <value> into the HTX message
509 * <htx>.
510 */
511static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
512{
513 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200514
Christopher Faulet9768c262018-10-22 09:34:31 +0200515 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200516}
517
518/* Deduce the connection mode of the client connection, depending on the
519 * configuration and the H1 message flags. This function is called twice, the
520 * first time when the request is parsed and the second time when the response
521 * is parsed.
522 */
523static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
524{
525 struct proxy *fe = h1s->h1c->px;
526 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
527
528 /* Tunnel mode can only by set on the frontend */
529 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
530 flag = H1S_F_WANT_TUN;
531 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
532 flag = H1S_F_WANT_CLO;
533
534 /* flags order: CLO > SCL > TUN > KAL */
535 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
536 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
537
538 if (h1m->flags & H1_MF_RESP) {
539 /* Either we've established an explicit tunnel, or we're
540 * switching the protocol. In both cases, we're very unlikely to
541 * understand the next protocols. We have to switch to tunnel
542 * mode, so that we transfer the request and responses then let
543 * this protocol pass unmodified. When we later implement
544 * specific parsers for such protocols, we'll want to check the
545 * Upgrade header which contains information about that protocol
546 * for responses with status 101 (eg: see RFC2817 about TLS).
547 */
548 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
549 h1s->status == 101)
550 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
551 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
552 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
553 }
554 else {
555 if (h1s->flags & H1S_F_WANT_KAL &&
556 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
557 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
558 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
559 }
560
561 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
562 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
563 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
564}
565
566/* Deduce the connection mode of the client connection, depending on the
567 * configuration and the H1 message flags. This function is called twice, the
568 * first time when the request is parsed and the second time when the response
569 * is parsed.
570 */
571static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
572{
573 struct proxy *be = h1s->h1c->px;
574 struct proxy *fe = strm_fe(si_strm(h1s->cs->data));
575 int flag = H1S_F_WANT_KAL;
576
577 /* Tunnel mode can only by set on the frontend */
578 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
579 flag = H1S_F_WANT_TUN;
580
581 /* For the server connection: server-close == httpclose */
582 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
583 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
584 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
585 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
586 flag = H1S_F_WANT_CLO;
587
588 /* flags order: CLO > SCL > TUN > KAL */
589 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
590 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
591
592 if (h1m->flags & H1_MF_RESP) {
593 /* Either we've established an explicit tunnel, or we're
594 * switching the protocol. In both cases, we're very unlikely to
595 * understand the next protocols. We have to switch to tunnel
596 * mode, so that we transfer the request and responses then let
597 * this protocol pass unmodified. When we later implement
598 * specific parsers for such protocols, we'll want to check the
599 * Upgrade header which contains information about that protocol
600 * for responses with status 101 (eg: see RFC2817 about TLS).
601 */
602 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
603 h1s->status == 101)
604 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
605 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
606 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
607 else if (h1s->flags & H1S_F_WANT_KAL &&
608 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
609 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
610 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
611 }
612
613 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
614 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
615 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
616
617 /* TODO: For now on the server-side, we disable keep-alive */
618 if (h1s->flags & H1S_F_WANT_KAL)
619 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
620}
621
Christopher Faulet9768c262018-10-22 09:34:31 +0200622static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
623 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200624{
625 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200626
627 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
628 * token is found
629 */
630 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200631 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200632
633 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200634 if (h1m->flags & H1_MF_CONN_CLO) {
635 if (conn_val)
636 *conn_val = ist("");
637 if (htx)
638 h1_remove_conn_hdrs(h1m, htx);
639 }
640 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
641 if (conn_val)
642 *conn_val = ist("keep-alive");
643 if (htx)
644 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
645 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200646 }
647 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200648 if (h1m->flags & H1_MF_CONN_KAL) {
649 if (conn_val)
650 *conn_val = ist("");
651 if (htx)
652 h1_remove_conn_hdrs(h1m, htx);
653 }
654 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
655 if (conn_val)
656 *conn_val = ist("close");
657 if (htx)
658 h1_add_conn_hdr(h1m, htx, ist("close"));
659 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200660 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200661}
662
Christopher Faulet9768c262018-10-22 09:34:31 +0200663static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
664 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200665{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200666 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
667 * token is found
668 */
669 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200670 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200671
672 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200673 if (h1m->flags & H1_MF_CONN_CLO) {
674 if (conn_val)
675 *conn_val = ist("");
676 if (htx)
677 h1_remove_conn_hdrs(h1m, htx);
678 }
679 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
680 if (conn_val)
681 *conn_val = ist("keep-alive");
682 if (htx)
683 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
684 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200685 }
686 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200687 if (h1m->flags & H1_MF_CONN_KAL) {
688 if (conn_val)
689 *conn_val = ist("");
690 if (htx)
691 h1_remove_conn_hdrs(h1m, htx);
692 }
693 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
694 if (conn_val)
695 *conn_val = ist("close");
696 if (htx)
697 h1_add_conn_hdr(h1m, htx, ist("close"));
698 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200699 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200700}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200701
Christopher Faulet9768c262018-10-22 09:34:31 +0200702/* Set the right connection mode and update "Connection:" header if
703 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
704 * message is updated accordingly. When <conn_val> is not NULL, it is set with
705 * the new header value.
706 */
707static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
708 struct htx *htx, struct ist *conn_val)
709{
710 if (!conn_is_back(h1s->h1c->conn)) {
711 h1_set_cli_conn_mode(h1s, h1m);
712 if (h1m->flags & H1_MF_RESP)
713 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
714 }
715 else {
716 h1_set_srv_conn_mode(h1s, h1m);
717 if (!(h1m->flags & H1_MF_RESP))
718 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
719 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200720}
721
Christopher Faulet129817b2018-09-20 16:14:40 +0200722/*
723 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200724 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
725 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet129817b2018-09-20 16:14:40 +0200726 * responsibile to update the parser state <h1m>.
727 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200728static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200729 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200730{
731 struct http_hdr hdrs[MAX_HTTP_HDR];
732 union h1_sl sl;
733 int ret = 0;
734
735 /* Realing input buffer if necessary */
736 if (b_head(buf) + b_data(buf) > b_wrap(buf))
737 b_slow_realign(buf, trash.area, 0);
738
Christopher Fauletf2824e62018-10-01 12:12:37 +0200739 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Faulet129817b2018-09-20 16:14:40 +0200740 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &sl);
741 if (ret <= 0) {
742 /* Incomplete or invalid message. If the buffer is full, it's an
743 * error because headers are too large to be handled by the
744 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200745 if (ret < 0 || (!ret && b_full(buf)))
746 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200747 goto end;
748 }
749
750 /* messages headers fully parsed, do some checks to prepare the body
751 * parsing.
752 */
753
754 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200755 if (ret > (b_size(buf) - global.tune.maxrewrite))
756 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200757
Christopher Faulet9768c262018-10-22 09:34:31 +0200758 /* Save the request's method or the response's status, check if the body
759 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200760 if (!(h1m->flags & H1_MF_RESP)) {
761 h1s->meth = sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200762
Christopher Faulet129817b2018-09-20 16:14:40 +0200763 /* Request have always a known length */
764 h1m->flags |= H1_MF_XFER_LEN;
765 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
766 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200767
768 if (!h1_process_req_vsn(h1s, h1m, sl)) {
769 h1m->err_pos = sl.rq.v.ptr - b_head(buf);
770 h1m->err_state = h1m->state;
771 goto vsn_error;
772 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200773 }
774 else {
775 h1s->status = sl.st.status;
776
777 if ((h1s->meth == HTTP_METH_HEAD) ||
778 (h1s->status >= 100 && h1s->status < 200) ||
779 (h1s->status == 204) || (h1s->status == 304) ||
780 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
781 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
782 h1m->flags |= H1_MF_XFER_LEN;
783 h1m->curr_len = h1m->body_len = 0;
784 h1m->state = H1_MSG_DONE;
785 }
786 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
787 h1m->flags |= H1_MF_XFER_LEN;
788 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
789 h1m->state = H1_MSG_DONE;
790 }
791 else
792 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200793
794 if (!h1_process_res_vsn(h1s, h1m, sl)) {
795 h1m->err_pos = sl.st.v.ptr - b_head(buf);
796 h1m->err_state = h1m->state;
797 goto vsn_error;
798 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200799 }
800
Christopher Faulet9768c262018-10-22 09:34:31 +0200801 if (!(h1m->flags & H1_MF_RESP)) {
802 if (!htx_add_reqline(htx, sl) || !htx_add_all_headers(htx, hdrs))
803 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200804 }
805 else {
Christopher Faulet9768c262018-10-22 09:34:31 +0200806 if (!htx_add_resline(htx, sl) || !htx_add_all_headers(htx, hdrs))
807 goto error;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200808 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200809 if (h1m->state == H1_MSG_DONE)
810 if (!htx_add_endof(htx, HTX_BLK_EOM))
811 goto error;
812
813 h1_process_conn_mode(h1s, h1m, htx, NULL);
814
815 /* If body length cannot be determined, set htx->extra to
816 * ULLONG_MAX. This value is impossible in other cases.
817 */
818 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
819
820 /* Recheck there is enough space to do headers rewritting */
821 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
822 goto error;
823
824 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200825 end:
826 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200827
828 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +0200829 h1m->err_state = h1m->state;
830 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +0200831 vsn_error:
832 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200833 ret = 0;
834 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200835}
836
837/*
Christopher Fauletf2824e62018-10-01 12:12:37 +0200838 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
839 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +0200840 * and filling h1s->err_pos and h1s->err_state fields. This functions is
841 * responsibile to update the parser state <h1m>.
842 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200843static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200844 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200845{
Christopher Faulet9768c262018-10-22 09:34:31 +0200846 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +0200847 size_t total = 0;
848 int ret = 0;
849
850 if (h1m->flags & H1_MF_XFER_LEN) {
851 if (h1m->flags & H1_MF_CLEN) {
852 /* content-length: read only h2m->body_len */
853 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200854 if (ret > data_space)
855 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200856 if ((uint64_t)ret > h1m->curr_len)
857 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200858 if (ret > b_contig_data(buf, *ofs))
859 ret = b_contig_data(buf, *ofs);
860 if (ret) {
861 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
862 goto end;
863 h1m->curr_len -= ret;
864 *ofs += ret;
865 total += ret;
866 }
867
868 if (!h1m->curr_len) {
869 if (!htx_add_endof(htx, HTX_BLK_EOM))
870 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200871 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200872 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200873 }
874 else if (h1m->flags & H1_MF_CHNK) {
875 new_chunk:
876 /* te:chunked : parse chunks */
877 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200878 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +0200879 if (ret <= 0)
880 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200881 h1m->state = H1_MSG_CHUNK_SIZE;
882
Christopher Faulet129817b2018-09-20 16:14:40 +0200883 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200884 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200885 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200886 }
887
888 if (h1m->state == H1_MSG_CHUNK_SIZE) {
889 unsigned int chksz;
890
Christopher Fauletf2824e62018-10-01 12:12:37 +0200891 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +0200892 if (ret <= 0)
893 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200894 if (!chksz) {
895 if (!htx_add_endof(htx, HTX_BLK_EOD))
896 goto end;
897 h1m->state = H1_MSG_TRAILERS;
898 }
899 else
900 h1m->state = H1_MSG_DATA;
901
Christopher Faulet129817b2018-09-20 16:14:40 +0200902 h1m->curr_len = chksz;
903 h1m->body_len += chksz;
904 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200905 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200906 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200907 }
908
909 if (h1m->state == H1_MSG_DATA) {
910 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +0200911 if (ret > data_space)
912 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +0200913 if ((uint64_t)ret > h1m->curr_len)
914 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +0200915 if (ret > b_contig_data(buf, *ofs))
916 ret = b_contig_data(buf, *ofs);
917 if (ret) {
918 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
919 goto end;
920 h1m->curr_len -= ret;
921 max -= ret;
922 *ofs += ret;
923 total += ret;
924 }
925 if (!h1m->curr_len) {
926 h1m->state = H1_MSG_CHUNK_CRLF;
927 goto new_chunk;
928 }
929 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200930 }
931
932 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200933 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +0200934 if (ret > data_space)
935 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +0200936 if (ret <= 0)
937 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +0200938
939 /* Realing input buffer if tailers wrap. For now
940 * this is a workaroung. Because trailers are
941 * not split on CRLF, like headers, there is no
942 * way to know where to split it when trailers
943 * wrap. This is a limitation of
944 * h1_measure_trailers.
945 */
946 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
947 b_slow_realign(buf, trash.area, 0);
948
949 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
950 goto end;
951
Christopher Faulet129817b2018-09-20 16:14:40 +0200952 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200953 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200954 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +0200955
956 /* FIXME: if it fails here, this is a problem,
957 * because there is no way to return here. */
958 if (!htx_add_endof(htx, HTX_BLK_EOM))
959 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200960 h1m->state = H1_MSG_DONE;
961 }
962 }
963 else {
964 /* XFER_LEN is set but not CLEN nor CHNK, it means there
965 * is no body. Switch the message in DONE state
966 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200967 if (!htx_add_endof(htx, HTX_BLK_EOM))
968 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +0200969 h1m->state = H1_MSG_DONE;
970 }
971 }
972 else {
973 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +0200974 ret = max;
975 if (ret > data_space)
976 ret = data_space;
977 if (ret > b_contig_data(buf, *ofs))
978 ret = b_contig_data(buf, *ofs);
979 if (ret) {
980 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
981 goto end;
982
983 *ofs += max;
984 total = max;
985 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200986 }
987
988 end:
989 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200990 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200991 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200992 h1m->err_pos = *ofs + max + ret;
Christopher Faulet129817b2018-09-20 16:14:40 +0200993 return 0;
994 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200995 /* update htx->extra, only when the body length is known */
996 if (h1m->flags & H1_MF_XFER_LEN)
997 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +0200998 return total;
999}
1000
1001/*
1002 * Synchronize the request and the response before reseting them. Except for 1xx
1003 * responses, we wait that the request and the response are in DONE state and
1004 * that all data are forwarded for both. For 1xx responses, only the response is
1005 * reset, waiting the final one. Many 1xx messages can be sent.
1006 */
1007static void h1_sync_messages(struct h1c *h1c)
1008{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001009 struct h1s *h1s = h1c->h1s;
1010
1011 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001012 return;
1013
Christopher Fauletf2824e62018-10-01 12:12:37 +02001014 if (h1s->res.state == H1_MSG_DONE &&
1015 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001016 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001017 /* For 100-Continue response or any other informational 1xx
1018 * response which is non-final, don't reset the request, the
1019 * transaction is not finished. We take care the response was
1020 * transferred before.
1021 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001022 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001023 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001024 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001025 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001026 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1027 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001028 h1m_init_req(&h1s->req);
1029 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001030 h1s->req.state = H1_MSG_TUNNEL;
1031 h1s->res.state = H1_MSG_TUNNEL;
1032 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001033 }
1034}
1035
1036/*
1037 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001038 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1039 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001040 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001041static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001042{
Christopher Faulet539e0292018-11-19 10:40:09 +01001043 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001044 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001045 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001046 size_t total = 0;
1047 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001048 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001049 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001050
Christopher Faulet539e0292018-11-19 10:40:09 +01001051 htx = htx_from_buf(buf);
1052 count = b_data(&h1c->ibuf);
1053 max = htx_free_space(htx);
1054 if (flags & CO_RFL_KEEP_RSV) {
1055 if (max < global.tune.maxrewrite)
1056 goto end;
1057 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001058 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001059 if (count > max)
1060 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001061
Christopher Fauletf2824e62018-10-01 12:12:37 +02001062 if (!conn_is_back(h1c->conn)) {
1063 h1m = &h1s->req;
1064 errflag = H1S_F_REQ_ERROR;
1065 }
1066 else {
1067 h1m = &h1s->res;
1068 errflag = H1S_F_RES_ERROR;
1069 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001070
Christopher Faulet539e0292018-11-19 10:40:09 +01001071 while (!(h1s->flags & errflag) && count) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001072 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001073 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001074 if (!ret)
1075 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001076 }
1077 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001078 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001079 if (!ret)
1080 break;
1081 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001082 else if (h1m->state == H1_MSG_DONE)
1083 break;
1084 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001085 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet9768c262018-10-22 09:34:31 +02001086 if (!ret)
1087 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001088 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001089 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001090 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001091 break;
1092 }
1093
Christopher Faulet539e0292018-11-19 10:40:09 +01001094 count -= ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001095 }
1096
Christopher Faulet47365272018-10-31 17:40:50 +01001097 if (h1s->flags & errflag)
1098 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001099
Christopher Faulet539e0292018-11-19 10:40:09 +01001100 b_del(&h1c->ibuf, total);
1101
1102 end:
1103 if (htx_is_not_empty(htx))
1104 b_set_data(buf, b_size(buf));
1105 else {
1106 htx_reset(htx);
1107 b_set_data(buf, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001108 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001109
Christopher Faulet539e0292018-11-19 10:40:09 +01001110 if (h1c->flags & H1C_F_IN_FULL && b_room(&h1c->ibuf)) {
1111 h1c->flags &= ~H1C_F_IN_FULL;
1112 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001113 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001114
Christopher Faulet539e0292018-11-19 10:40:09 +01001115 if (b_data(&h1c->ibuf))
1116 h1s->cs->flags |= CS_FL_RCV_MORE;
1117 else {
1118 h1_release_buf(h1c, &h1c->ibuf);
1119 h1_sync_messages(h1c);
1120
1121 h1s->cs->flags &= ~CS_FL_RCV_MORE;
1122 if (h1s->cs->flags & CS_FL_REOS)
1123 h1s->cs->flags |= CS_FL_EOS;
1124 }
1125 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001126
1127 parsing_err:
1128 // FIXME: create an error snapshot here
1129 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001130 htx->flags |= HTX_FL_PARSING_ERROR;
1131 b_set_data(buf, b_size(buf));
1132 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001133 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001134}
1135
Christopher Faulet129817b2018-09-20 16:14:40 +02001136/*
1137 * Process outgoing data. It parses data and transfer them from the channel buffer into
1138 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1139 * 0 if it couldn't proceed.
1140 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001141static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1142{
Christopher Faulet129817b2018-09-20 16:14:40 +02001143 struct h1s *h1s = h1c->h1s;
1144 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001145 struct htx *chn_htx;
1146 struct htx_blk *blk;
1147 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001148 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001149 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001150
Christopher Faulet47365272018-10-31 17:40:50 +01001151 if (!count)
1152 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001153 chn_htx = htx_from_buf(buf);
1154
Christopher Faulet51dbc942018-09-13 09:05:15 +02001155 if (!h1_get_buf(h1c, &h1c->obuf)) {
1156 h1c->flags |= H1C_F_OUT_ALLOC;
1157 goto end;
1158 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001159
Christopher Fauletf2824e62018-10-01 12:12:37 +02001160 if (!conn_is_back(h1c->conn)) {
1161 h1m = &h1s->res;
1162 errflag = H1S_F_RES_ERROR;
1163 }
1164 else {
1165 h1m = &h1s->req;
1166 errflag = H1S_F_REQ_ERROR;
1167 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001168
1169
1170 tmp = get_trash_chunk();
1171 tmp->size = b_room(&h1c->obuf);
1172
1173 blk = htx_get_head_blk(chn_htx);
1174 while (!(h1s->flags & errflag) && blk) {
1175 union htx_sl *sl;
1176 struct ist n, v;
1177 uint32_t sz = htx_get_blksz(blk);
1178
1179 if (total + sz > count)
1180 goto copy;
1181
1182 switch (htx_get_blk_type(blk)) {
1183 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001184 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001185
1186 case HTX_BLK_REQ_SL:
1187 sl = htx_get_blk_ptr(chn_htx, blk);
1188 h1s->meth = sl->rq.meth;
1189 h1_parse_req_vsn(h1m, sl);
1190 if (!htx_reqline_to_str(sl, tmp))
1191 goto copy;
1192 h1m->flags |= H1_MF_XFER_LEN;
1193 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001194 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001195
Christopher Faulet9768c262018-10-22 09:34:31 +02001196 case HTX_BLK_RES_SL:
1197 sl = htx_get_blk_ptr(chn_htx, blk);
1198 h1s->status = sl->st.status;
1199 h1_parse_res_vsn(h1m, sl);
1200 if (!htx_stline_to_str(sl, tmp))
1201 goto copy;
1202 if (chn_htx->extra != ULLONG_MAX)
1203 h1m->flags |= H1_MF_XFER_LEN;
1204 h1m->state = H1_MSG_HDR_FIRST;
1205 break;
1206
1207 case HTX_BLK_HDR:
1208 if (h1m->state == H1_MSG_HDR_FIRST) {
1209 struct http_hdr_ctx ctx;
1210
1211 n = ist("Connection");
1212 v = ist("");
1213
1214 /* If there is no "Connection:" header,
1215 * process conn_mode now and add the
1216 * right one.
1217 */
1218 ctx.blk = blk;
1219 if (http_find_header(chn_htx, n, &ctx, 1))
1220 goto process_hdr;
1221 h1_process_conn_mode(h1s, h1m, NULL, &v);
1222 if (!v.len)
1223 goto process_hdr;
1224
1225 if (!htx_hdr_to_str(n, v, tmp))
1226 goto copy;
1227 }
1228 process_hdr:
1229 h1m->state = H1_MSG_HDR_NAME;
1230 n = htx_get_blk_name(chn_htx, blk);
1231 v = htx_get_blk_value(chn_htx, blk);
1232
1233 if (isteqi(n, ist("transfer-encoding")))
1234 h1_parse_xfer_enc_header(h1m, v);
1235 else if (isteqi(n, ist("connection"))) {
1236 h1_parse_connection_header(h1m, v);
1237 h1_process_conn_mode(h1s, h1m, NULL, &v);
1238 if (!v.len)
1239 goto skip_hdr;
1240 }
1241
1242 if (!htx_hdr_to_str(n, v, tmp))
1243 goto copy;
1244 skip_hdr:
1245 h1m->state = H1_MSG_HDR_L2_LWS;
1246 break;
1247
1248 case HTX_BLK_PHDR:
1249 /* not implemented yet */
1250 h1m->flags |= errflag;
1251 break;
1252
1253 case HTX_BLK_EOH:
1254 h1m->state = H1_MSG_LAST_LF;
1255 if (!chunk_memcat(tmp, "\r\n", 2))
1256 goto copy;
1257
1258 h1m->state = H1_MSG_DATA;
1259 break;
1260
1261 case HTX_BLK_DATA:
1262 v = htx_get_blk_value(chn_htx, blk);
1263 if (!htx_data_to_str(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
1264 goto copy;
1265 break;
1266
1267 case HTX_BLK_EOD:
1268 if (!chunk_memcat(tmp, "0\r\n", 3))
1269 goto copy;
1270 h1m->state = H1_MSG_TRAILERS;
1271 break;
1272
1273 case HTX_BLK_TLR:
1274 v = htx_get_blk_value(chn_htx, blk);
1275 if (!htx_trailer_to_str(v, tmp))
1276 goto copy;
1277 break;
1278
1279 case HTX_BLK_EOM:
1280 /* if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(tmp, "\r\n", 2)) */
1281 /* goto copy; */
1282 h1m->state = H1_MSG_DONE;
1283 break;
1284
1285 case HTX_BLK_OOB:
1286 v = htx_get_blk_value(chn_htx, blk);
1287 if (!chunk_memcat(tmp, v.ptr, v.len))
1288 goto copy;
1289 break;
1290
1291 default:
1292 h1m->flags |= errflag;
1293 break;
1294 }
1295 total += sz;
1296 blk = htx_remove_blk(chn_htx, blk);
Christopher Faulet129817b2018-09-20 16:14:40 +02001297 }
1298
Christopher Faulet9768c262018-10-22 09:34:31 +02001299 copy:
1300 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001301
1302 if (b_full(&h1c->obuf))
1303 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001304 if (htx_is_empty(chn_htx)) {
1305 htx_reset(chn_htx);
1306 b_set_data(buf, 0);
1307 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001308 end:
1309 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001310}
1311
Christopher Faulet51dbc942018-09-13 09:05:15 +02001312/*********************************************************/
1313/* functions below are I/O callbacks from the connection */
1314/*********************************************************/
1315/*
1316 * Attempt to read data, and subscribe if none available
1317 */
1318static int h1_recv(struct h1c *h1c)
1319{
1320 struct connection *conn = h1c->conn;
1321 size_t ret, max;
1322 int rcvd = 0;
1323
1324 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
1325 return 0;
1326
Christopher Faulet539e0292018-11-19 10:40:09 +01001327 if (!h1_recv_allowed(h1c))
Christopher Faulet9768c262018-10-22 09:34:31 +02001328 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001329
Christopher Faulet9768c262018-10-22 09:34:31 +02001330 if (h1c->h1s && (h1c->h1s->flags & H1S_F_BUF_FLUSH)) {
1331 rcvd = 1;
1332 goto end;
1333 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001334
Christopher Faulet51dbc942018-09-13 09:05:15 +02001335 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1336 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001337 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001338 }
1339
1340 ret = 0;
1341 max = b_room(&h1c->ibuf);
1342 if (max) {
1343 h1c->flags &= ~H1C_F_IN_FULL;
1344 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
1345 }
Christopher Faulet47365272018-10-31 17:40:50 +01001346 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001347 rcvd = 1;
Christopher Faulet47365272018-10-31 17:40:50 +01001348 if (h1c->h1s && h1c->h1s->cs)
1349 h1c->h1s->cs->flags |= CS_FL_READ_PARTIAL;
1350 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001351
1352 if (h1_recv_allowed(h1c))
1353 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
1354
Christopher Faulet9768c262018-10-22 09:34:31 +02001355 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001356 if (!b_data(&h1c->ibuf))
1357 h1_release_buf(h1c, &h1c->ibuf);
1358 else if (b_full(&h1c->ibuf))
1359 h1c->flags |= H1C_F_IN_FULL;
1360 return rcvd;
1361}
1362
1363
1364/*
1365 * Try to send data if possible
1366 */
1367static int h1_send(struct h1c *h1c)
1368{
1369 struct connection *conn = h1c->conn;
1370 unsigned int flags = 0;
1371 size_t ret;
1372 int sent = 0;
1373
1374 if (conn->flags & CO_FL_ERROR)
1375 return 0;
1376
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001377 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1378 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1379 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1380 return 0;
1381 }
1382
Christopher Faulet51dbc942018-09-13 09:05:15 +02001383 if (!b_data(&h1c->obuf))
1384 goto end;
1385
1386 if (h1c->flags & H1C_F_OUT_FULL)
1387 flags |= CO_SFL_MSG_MORE;
1388
1389 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1390 if (ret > 0) {
1391 h1c->flags &= ~H1C_F_OUT_FULL;
1392 b_del(&h1c->obuf, ret);
1393 sent = 1;
1394 }
1395
1396 end:
1397 /* We're done, no more to send */
1398 if (!b_data(&h1c->obuf)) {
1399 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001400 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001401 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1402 h1_shutw_conn(conn);
1403 }
1404 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1405 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1406
1407 return sent;
1408}
1409
1410
1411static void h1_wake_stream(struct h1c *h1c)
1412{
1413 struct connection *conn = h1c->conn;
1414 struct h1s *h1s = h1c->h1s;
1415 uint32_t flags = 0;
1416 int dont_wake = 0;
1417
1418 if (!h1s || !h1s->cs)
1419 return;
1420
1421 if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR))
1422 flags |= CS_FL_ERROR;
1423 if (conn_xprt_read0_pending(conn))
1424 flags |= CS_FL_REOS;
1425
1426 h1s->cs->flags |= flags;
1427 if (h1s->recv_wait) {
1428 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1429 tasklet_wakeup(h1s->recv_wait->task);
1430 h1s->recv_wait = NULL;
1431 dont_wake = 1;
1432 }
1433 if (h1s->send_wait) {
1434 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1435 tasklet_wakeup(h1s->send_wait->task);
1436 h1s->send_wait = NULL;
1437 dont_wake = 1;
1438 }
1439 if (!dont_wake && h1s->cs->data_cb->wake)
1440 h1s->cs->data_cb->wake(h1s->cs);
1441}
1442
1443/* callback called on any event by the connection handler.
1444 * It applies changes and returns zero, or < 0 if it wants immediate
1445 * destruction of the connection.
1446 */
1447static int h1_process(struct h1c * h1c)
1448{
1449 struct connection *conn = h1c->conn;
1450
Christopher Faulet51dbc942018-09-13 09:05:15 +02001451 if (!conn->mux_ctx)
1452 return -1;
1453
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001454 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001455 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1456 goto end;
1457 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001458 }
1459
Christopher Faulet539e0292018-11-19 10:40:09 +01001460 if (!h1c->h1s) {
1461 if (h1c->flags & H1C_F_CS_ERROR ||
1462 conn->flags & CO_FL_ERROR ||
1463 conn_xprt_read0_pending(conn))
1464 goto release;
1465 if (!(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
1466 if (!h1s_create(h1c, NULL))
1467 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001468 }
1469 }
1470
Christopher Faulet539e0292018-11-19 10:40:09 +01001471 h1_wake_stream(h1c);
Christopher Faulet47365272018-10-31 17:40:50 +01001472 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001473 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001474
1475 release:
1476 h1_release(conn);
1477 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001478}
1479
1480static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1481{
1482 struct h1c *h1c = ctx;
1483 int ret = 0;
1484
1485 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1486 ret = h1_send(h1c);
1487 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1488 ret |= h1_recv(h1c);
Christopher Faulet539e0292018-11-19 10:40:09 +01001489 if (ret/* || b_data(&h1c->ibuf)*/)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001490 h1_process(h1c);
1491 return NULL;
1492}
1493
1494
1495static int h1_wake(struct connection *conn)
1496{
1497 struct h1c *h1c = conn->mux_ctx;
1498
Christopher Faulet539e0292018-11-19 10:40:09 +01001499 h1_send(h1c);
1500 return h1_process(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001501}
1502
Christopher Faulet51dbc942018-09-13 09:05:15 +02001503/*******************************************/
1504/* functions below are used by the streams */
1505/*******************************************/
1506/*
1507 * Attach a new stream to a connection
1508 * (Used for outgoing connections)
1509 */
1510static struct conn_stream *h1_attach(struct connection *conn)
1511{
1512 struct h1c *h1c = conn->mux_ctx;
1513 struct conn_stream *cs = NULL;
1514 struct h1s *h1s;
1515
1516 if (h1c->flags & H1C_F_CS_ERROR)
1517 goto end;
1518
1519 cs = cs_new(h1c->conn);
1520 if (!cs)
1521 goto end;
1522
Christopher Fauletf2824e62018-10-01 12:12:37 +02001523 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001524 if (h1s == NULL)
1525 goto end;
1526
1527 return cs;
1528 end:
1529 cs_free(cs);
1530 return NULL;
1531}
1532
1533/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1534 * this mux, it's easy as we can only store a single conn_stream.
1535 */
1536static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1537{
1538 struct h1c *h1c = conn->mux_ctx;
1539 struct h1s *h1s = h1c->h1s;
1540
1541 if (h1s)
1542 return h1s->cs;
1543
1544 return NULL;
1545}
1546
1547static void h1_destroy(struct connection *conn)
1548{
1549 struct h1c *h1c = conn->mux_ctx;
1550
1551 if (!h1c->h1s)
1552 h1_release(conn);
1553}
1554
1555/*
1556 * Detach the stream from the connection and possibly release the connection.
1557 */
1558static void h1_detach(struct conn_stream *cs)
1559{
1560 struct h1s *h1s = cs->ctx;
1561 struct h1c *h1c;
1562
1563 cs->ctx = NULL;
1564 if (!h1s)
1565 return;
1566
1567 h1c = h1s->h1c;
1568 h1s->cs = NULL;
1569
1570 h1s_destroy(h1s);
1571
1572 /* We don't want to close right now unless the connection is in error */
1573 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
1574 (h1c->conn->flags & CO_FL_ERROR))
1575 h1_release(h1c->conn);
1576 else
1577 tasklet_wakeup(h1c->wait_event.task);
1578}
1579
1580
1581static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1582{
1583 struct h1s *h1s = cs->ctx;
1584
1585 if (!h1s)
1586 return;
1587
Christopher Fauletf2824e62018-10-01 12:12:37 +02001588 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1589 return;
1590
Christopher Faulet51dbc942018-09-13 09:05:15 +02001591 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1592 if (cs->flags & CS_FL_SHR)
1593 return;
1594 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1595 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1596 if (cs->flags & CS_FL_SHW) {
1597 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1598 conn_full_close(cs->conn);
1599 }
1600}
1601
1602static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1603{
1604 struct h1s *h1s = cs->ctx;
1605 struct h1c *h1c;
1606
1607 if (!h1s)
1608 return;
1609 h1c = h1s->h1c;
1610
Christopher Fauletf2824e62018-10-01 12:12:37 +02001611 if ((h1s->flags & H1S_F_WANT_KAL) &&
1612 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1613 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
1614 return;
1615
Christopher Faulet51dbc942018-09-13 09:05:15 +02001616 h1c->flags |= H1C_F_CS_SHUTW_NOW;
1617 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
1618 return;
1619
1620 h1_shutw_conn(cs->conn);
1621}
1622
1623static void h1_shutw_conn(struct connection *conn)
1624{
1625 struct h1c *h1c = conn->mux_ctx;
1626
1627 if (conn_xprt_ready(conn) && conn->xprt->shutw)
1628 conn->xprt->shutw(conn, 1);
1629 if (!(conn->flags & CO_FL_SOCK_RD_SH))
1630 conn_sock_shutw(conn, 1);
1631 else {
1632 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1633 conn_full_close(conn);
1634 }
1635}
1636
1637/* Called from the upper layer, to unsubscribe to events */
1638static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
1639{
1640 struct wait_event *sw;
1641 struct h1s *h1s = cs->ctx;
1642
1643 if (!h1s)
1644 return 0;
1645
1646 if (event_type & SUB_CAN_RECV) {
1647 sw = param;
1648 if (h1s->recv_wait == sw) {
1649 sw->wait_reason &= ~SUB_CAN_RECV;
1650 h1s->recv_wait = NULL;
1651 }
1652 }
1653 if (event_type & SUB_CAN_SEND) {
1654 sw = param;
1655 if (h1s->send_wait == sw) {
1656 sw->wait_reason &= ~SUB_CAN_SEND;
1657 h1s->send_wait = NULL;
1658 }
1659 }
1660 return 0;
1661}
1662
1663/* Called from the upper layer, to subscribe to events, such as being able to send */
1664static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
1665{
1666 struct wait_event *sw;
1667 struct h1s *h1s = cs->ctx;
1668
1669 if (!h1s)
1670 return -1;
1671
1672 switch (event_type) {
1673 case SUB_CAN_RECV:
1674 sw = param;
1675 if (!(sw->wait_reason & SUB_CAN_RECV)) {
1676 sw->wait_reason |= SUB_CAN_RECV;
1677 sw->handle = h1s;
1678 h1s->recv_wait = sw;
1679 }
1680 return 0;
1681 case SUB_CAN_SEND:
1682 sw = param;
1683 if (!(sw->wait_reason & SUB_CAN_SEND)) {
1684 sw->wait_reason |= SUB_CAN_SEND;
1685 sw->handle = h1s;
1686 h1s->send_wait = sw;
1687 }
1688 return 0;
1689 default:
1690 break;
1691 }
1692 return -1;
1693}
1694
1695/* Called from the upper layer, to receive data */
1696static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1697{
1698 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01001699 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001700 size_t ret = 0;
1701
Christopher Faulet539e0292018-11-19 10:40:09 +01001702 if (!(h1c->flags & H1C_F_IN_ALLOC))
1703 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02001704
1705 if (flags & CO_RFL_BUF_FLUSH)
1706 h1s->flags |= H1S_F_BUF_FLUSH;
1707 else if (ret > 0 || (h1s->flags & H1S_F_BUF_FLUSH)) {
1708 h1s->flags &= ~H1S_F_BUF_FLUSH;
Christopher Faulet539e0292018-11-19 10:40:09 +01001709 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1710 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001711 }
1712 return ret;
1713}
1714
1715
1716/* Called from the upper layer, to send data */
1717static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1718{
1719 struct h1s *h1s = cs->ctx;
1720 struct h1c *h1c;
1721 size_t ret = 0;
1722
1723 if (!h1s)
1724 return ret;
1725
1726 h1c = h1s->h1c;
1727
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001728 if (h1c->flags & H1C_F_CS_WAIT_CONN)
1729 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001730
Christopher Faulet539e0292018-11-19 10:40:09 +01001731 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001732 ret = h1_process_output(h1c, buf, count);
1733 if (ret > 0) {
1734 h1_send(h1c);
1735
Christopher Faulet539e0292018-11-19 10:40:09 +01001736 /* We need to do that because of the infinite forwarding. <buf>
1737 * contains HTX messages so when infinite forwarding is enabled,
1738 * count is equal to the buffer size. From outside, the buffer
1739 * appears as full.
1740 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001741 if (!b_data(buf))
1742 ret = count;
1743 }
1744 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001745}
1746
Christopher Faulet1be55f92018-10-02 15:59:23 +02001747#if defined(CONFIG_HAP_LINUX_SPLICE)
1748/* Send and get, using splicing */
1749static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
1750{
1751 struct h1s *h1s = cs->ctx;
1752 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
1753 int ret = 0;
1754
Christopher Faulet539e0292018-11-19 10:40:09 +01001755 if (b_data(&h1s->h1c->ibuf))
Christopher Faulet1be55f92018-10-02 15:59:23 +02001756 goto end;
1757 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
1758 count = h1m->curr_len;
1759 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
1760 if (h1m->state == H1_MSG_DATA && ret > 0)
1761 h1m->curr_len -= ret;
1762 end:
1763 return ret;
1764
1765}
1766
1767static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
1768{
1769 struct h1s *h1s = cs->ctx;
1770 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->res : &h1s->req);
1771 int ret = 0;
1772
1773 if (b_data(&h1s->h1c->obuf))
1774 goto end;
1775
1776 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
1777 if (h1m->state == H1_MSG_DATA && ret > 0)
1778 h1m->curr_len -= ret;
1779 end:
1780 return ret;
1781}
1782#endif
1783
Christopher Faulet51dbc942018-09-13 09:05:15 +02001784/****************************************/
1785/* MUX initialization and instanciation */
1786/****************************************/
1787
1788/* The mux operations */
1789const struct mux_ops mux_h1_ops = {
1790 .init = h1_init,
1791 .wake = h1_wake,
1792 .attach = h1_attach,
1793 .get_first_cs = h1_get_first_cs,
1794 .detach = h1_detach,
1795 .destroy = h1_destroy,
1796 .avail_streams = h1_avail_streams,
1797 .rcv_buf = h1_rcv_buf,
1798 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02001799#if defined(CONFIG_HAP_LINUX_SPLICE)
1800 .rcv_pipe = h1_rcv_pipe,
1801 .snd_pipe = h1_snd_pipe,
1802#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02001803 .subscribe = h1_subscribe,
1804 .unsubscribe = h1_unsubscribe,
1805 .shutr = h1_shutr,
1806 .shutw = h1_shutw,
1807 .flags = MX_FL_NONE,
1808 .name = "h1",
1809};
1810
1811
1812/* this mux registers default HTX proto */
1813static struct mux_proto_list mux_proto_htx =
1814{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
1815
1816static void __h1_deinit(void)
1817{
1818 pool_destroy(pool_head_h1c);
1819 pool_destroy(pool_head_h1s);
1820}
1821
1822__attribute__((constructor))
1823static void __h1_init(void)
1824{
1825 register_mux_proto(&mux_proto_htx);
1826 hap_register_post_deinit(__h1_deinit);
1827 pool_head_h1c = create_pool("h1c", sizeof(struct h1c), MEM_F_SHARED);
1828 pool_head_h1s = create_pool("h1s", sizeof(struct h1s), MEM_F_SHARED);
1829}
1830/*
1831 * Local variables:
1832 * c-indent-level: 8
1833 * c-basic-offset: 8
1834 * End:
1835 */