blob: 09c29c2f02969dc7ab33a9814870b6c81a80af3d [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 Tarreauafba57a2018-12-11 13:44:24 +010014#include <common/h1.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010015#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010016#include <common/initcall.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020017
Christopher Faulet1be55f92018-10-02 15:59:23 +020018#include <types/pipe.h>
Christopher Fauletf2824e62018-10-01 12:12:37 +020019#include <types/proxy.h>
20#include <types/session.h>
21
Christopher Faulet51dbc942018-09-13 09:05:15 +020022#include <proto/connection.h>
Christopher Faulet9768c262018-10-22 09:34:31 +020023#include <proto/http_htx.h>
Christopher Faulet129817b2018-09-20 16:14:40 +020024#include <proto/log.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020026#include <proto/stream.h>
27#include <proto/stream_interface.h>
28
29/*
30 * H1 Connection flags (32 bits)
31 */
32#define H1C_F_NONE 0x00000000
33
34/* Flags indicating why writing output data are blocked */
35#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
36#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
37/* 0x00000004 - 0x00000008 unused */
38
39/* Flags indicating why reading input data are blocked. */
40#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
41#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletcb55f482018-12-10 11:56:47 +010042#define H1C_F_IN_BUSY 0x00000040
Christopher Faulet539e0292018-11-19 10:40:09 +010043/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020044
45#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
46#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
47#define H1C_F_CS_SHUTW 0x00004000 /* connection is already shut down */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +020048#define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */
Christopher Faulet51dbc942018-09-13 09:05:15 +020049
Christopher Fauletf2824e62018-10-01 12:12:37 +020050#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 +020051
Christopher Faulet51dbc942018-09-13 09:05:15 +020052/*
53 * H1 Stream flags (32 bits)
54 */
Christopher Faulet129817b2018-09-20 16:14:40 +020055#define H1S_F_NONE 0x00000000
56#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020057#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
58#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Christopher Faulet539e0292018-11-19 10:40:09 +010059/* 0x00000008 unused */
Christopher Fauletf2824e62018-10-01 12:12:37 +020060#define H1S_F_WANT_KAL 0x00000010
61#define H1S_F_WANT_TUN 0x00000020
62#define H1S_F_WANT_CLO 0x00000040
63#define H1S_F_WANT_MSK 0x00000070
64#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010065#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010066#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Christopher Faulet32188212018-11-20 18:21:43 +010067#define H1S_F_HAVE_EOD 0x00000400 /* Set during input/output process to know the last empty chunk was processed */
68#define H1S_F_HAVE_TLR 0x00000800 /* Set during input/output process to know the trailers were processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020069
70/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020071struct h1c {
72 struct connection *conn;
73 struct proxy *px;
74 uint32_t flags; /* Connection flags: H1C_F_* */
75
76 struct buffer ibuf; /* Input buffer to store data before parsing */
77 struct buffer obuf; /* Output buffer to store data after reformatting */
78
79 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
80 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
81
82 struct h1s *h1s; /* H1 stream descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020083};
84
85/* H1 stream descriptor */
86struct h1s {
87 struct h1c *h1c;
88 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010089 struct cs_info csinfo; /* CS info, only used for client connections */
90 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020091
Christopher Faulet51dbc942018-09-13 09:05:15 +020092 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
93 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020094
95 struct h1m req;
96 struct h1m res;
97
98 enum http_meth_t meth; /* HTTP resquest method */
99 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200100};
101
102/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100103DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
104DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200105
Christopher Faulet51dbc942018-09-13 09:05:15 +0200106static int h1_recv(struct h1c *h1c);
107static int h1_send(struct h1c *h1c);
108static int h1_process(struct h1c *h1c);
109static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
110static void h1_shutw_conn(struct connection *conn);
111
112/*****************************************************/
113/* functions below are for dynamic buffer management */
114/*****************************************************/
115/*
116 * Indicates whether or not the we may call the h1_recv() function to
117 * attempt to receive data into the buffer and/or parse pending data. The
118 * condition is a bit complex due to some API limits for now. The rules are the
119 * following :
120 * - if an error or a shutdown was detected on the connection and the buffer
121 * is empty, we must not attempt to receive
122 * - if the input buffer failed to be allocated, we must not try to receive
123 * and we know there is nothing pending
124 * - if no flag indicates a blocking condition, we may attempt to receive,
125 * regardless of whether the input buffer is full or not, so that only de
126 * receiving part decides whether or not to block. This is needed because
127 * the connection API indeed prevents us from re-enabling receipt that is
128 * already enabled in a polled state, so we must always immediately stop as
129 * soon as the mux can't proceed so as never to hit an end of read with data
130 * pending in the buffers.
131 * - otherwise must may not attempt to receive
132 */
133static inline int h1_recv_allowed(const struct h1c *h1c)
134{
Christopher Fauletcf56b992018-12-11 16:12:31 +0100135 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200136 return 0;
137
Christopher Fauletcf56b992018-12-11 16:12:31 +0100138 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
139 return 0;
140
Christopher Fauletcb55f482018-12-10 11:56:47 +0100141 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200142 return 1;
143
144 return 0;
145}
146
147/*
148 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
149 * flags are used to figure what buffer was requested. It returns 1 if the
150 * allocation succeeds, in which case the connection is woken up, or 0 if it's
151 * impossible to wake up and we prefer to be woken up later.
152 */
153static int h1_buf_available(void *target)
154{
155 struct h1c *h1c = target;
156
157 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
158 h1c->flags &= ~H1C_F_IN_ALLOC;
159 if (h1_recv_allowed(h1c))
160 tasklet_wakeup(h1c->wait_event.task);
161 return 1;
162 }
163
164 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
165 h1c->flags &= ~H1C_F_OUT_ALLOC;
166 tasklet_wakeup(h1c->wait_event.task);
167 return 1;
168 }
169
Christopher Faulet51dbc942018-09-13 09:05:15 +0200170 return 0;
171}
172
173/*
174 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
175 */
176static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
177{
178 struct buffer *buf = NULL;
179
180 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
181 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
182 h1c->buf_wait.target = h1c;
183 h1c->buf_wait.wakeup_cb = h1_buf_available;
184 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
185 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
186 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
187 __conn_xprt_stop_recv(h1c->conn);
188 }
189 return buf;
190}
191
192/*
193 * Release a buffer, if any, and try to wake up entities waiting in the buffer
194 * wait queue.
195 */
196static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
197{
198 if (bptr->size) {
199 b_free(bptr);
200 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
201 }
202}
203
204static int h1_avail_streams(struct connection *conn)
205{
206 struct h1c *h1c = conn->mux_ctx;
207
208 return h1c->h1s ? 0 : 1;
209}
210
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100211static int h1_max_streams(struct connection *conn)
212{
213 return 1;
214}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200215
216/*****************************************************************/
217/* functions below are dedicated to the mux setup and management */
218/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100219static struct conn_stream *h1s_new_cs(struct h1s *h1s)
220{
221 struct conn_stream *cs;
222
223 cs = cs_new(h1s->h1c->conn);
224 if (!cs)
225 goto err;
226 h1s->cs = cs;
227 cs->ctx = h1s;
228
229 if (h1s->flags & H1S_F_NOT_FIRST)
230 cs->flags |= CS_FL_NOT_FIRST;
231
232 if (stream_create_from_cs(cs) < 0)
233 goto err;
234 return cs;
235
236 err:
237 cs_free(cs);
238 h1s->cs = NULL;
239 return NULL;
240}
241
Christopher Fauletf2824e62018-10-01 12:12:37 +0200242static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200243{
244 struct h1s *h1s;
245
246 h1s = pool_alloc(pool_head_h1s);
247 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100248 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200249
250 h1s->h1c = h1c;
251 h1c->h1s = h1s;
252
253 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200254 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200255
256 h1s->recv_wait = NULL;
257 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200258
259 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200260 h1s->req.flags |= H1_MF_NO_PHDR;
261
Christopher Faulet129817b2018-09-20 16:14:40 +0200262 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200263 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200264
265 h1s->status = 0;
266 h1s->meth = HTTP_METH_OTHER;
267
Christopher Faulet47365272018-10-31 17:40:50 +0100268 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
269 h1s->flags |= H1S_F_NOT_FIRST;
270 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
271
Christopher Faulet129817b2018-09-20 16:14:40 +0200272 if (!conn_is_back(h1c->conn)) {
273 if (h1c->px->options2 & PR_O2_REQBUG_OK)
274 h1s->req.err_pos = -1;
275 }
276 else {
277 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
278 h1s->res.err_pos = -1;
279 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200280
Christopher Faulet539e0292018-11-19 10:40:09 +0100281 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
282 * create a new one.
283 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200284 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100285 h1s->csinfo.create_date = date;
286 h1s->csinfo.tv_create = now;
287 h1s->csinfo.t_handshake = 0;
288 h1s->csinfo.t_idle = -1;
289
Christopher Fauletf2824e62018-10-01 12:12:37 +0200290 cs->ctx = h1s;
291 h1s->cs = cs;
292 }
Christopher Faulet47365272018-10-31 17:40:50 +0100293 else {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100294 struct session *sess = h1c->conn->owner;
295
296 h1s->csinfo.create_date = sess->accept_date;
297 h1s->csinfo.tv_create = sess->tv_accept;
298 h1s->csinfo.t_handshake = sess->t_handshake;
299 h1s->csinfo.t_idle = -1;
300
Christopher Faulet47365272018-10-31 17:40:50 +0100301 cs = h1s_new_cs(h1s);
302 if (!cs)
303 goto fail;
304 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200305 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100306
307 fail:
308 pool_free(pool_head_h1s, h1s);
309 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200310}
311
312static void h1s_destroy(struct h1s *h1s)
313{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200314 if (h1s) {
315 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200316
Christopher Fauletf2824e62018-10-01 12:12:37 +0200317 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200318
Christopher Fauletf2824e62018-10-01 12:12:37 +0200319 if (h1s->recv_wait != NULL)
320 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
321 if (h1s->send_wait != NULL)
322 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
323
Christopher Fauletcb55f482018-12-10 11:56:47 +0100324 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100325 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
326 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
327 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200328
Christopher Fauletf2824e62018-10-01 12:12:37 +0200329 cs_free(h1s->cs);
330 pool_free(pool_head_h1s, h1s);
331 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200332}
333
Christopher Fauletfeb11742018-11-29 15:12:34 +0100334static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
335{
336 struct h1s *h1s = cs->ctx;
337
338 if (h1s && !conn_is_back(cs->conn))
339 return &h1s->csinfo;
340 return NULL;
341}
342
Christopher Faulet51dbc942018-09-13 09:05:15 +0200343/*
344 * Initialize the mux once it's attached. It is expected that conn->mux_ctx
345 * points to the existing conn_stream (for outgoing connections) or NULL (for
346 * incoming ones). Returns < 0 on error.
347 */
348static int h1_init(struct connection *conn, struct proxy *proxy)
349{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200350 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200351
352 h1c = pool_alloc(pool_head_h1c);
353 if (!h1c)
354 goto fail_h1c;
355 h1c->conn = conn;
356 h1c->px = proxy;
357
358 h1c->flags = H1C_F_NONE;
359 h1c->ibuf = BUF_NULL;
360 h1c->obuf = BUF_NULL;
361 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200362
Christopher Faulet51dbc942018-09-13 09:05:15 +0200363 LIST_INIT(&h1c->buf_wait.list);
364 h1c->wait_event.task = tasklet_new();
365 if (!h1c->wait_event.task)
366 goto fail;
367 h1c->wait_event.task->process = h1_io_cb;
368 h1c->wait_event.task->context = h1c;
369 h1c->wait_event.wait_reason = 0;
370
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200371 if (!(conn->flags & CO_FL_CONNECTED))
372 h1c->flags |= H1C_F_CS_WAIT_CONN;
373
Christopher Fauletf2824e62018-10-01 12:12:37 +0200374 /* Always Create a new H1S */
375 if (!h1s_create(h1c, conn->mux_ctx))
376 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200377
Christopher Faulet129817b2018-09-20 16:14:40 +0200378 conn->mux_ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200379
Christopher Faulet51dbc942018-09-13 09:05:15 +0200380 /* Try to read, if nothing is available yet we'll just subscribe */
381 if (h1_recv(h1c))
382 h1_process(h1c);
383
384 /* mux->wake will be called soon to complete the operation */
385 return 0;
386
387 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100388 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200389 tasklet_free(h1c->wait_event.task);
390 pool_free(pool_head_h1c, h1c);
391 fail_h1c:
392 return -1;
393}
394
395
396/* release function for a connection. This one should be called to free all
397 * resources allocated to the mux.
398 */
399static void h1_release(struct connection *conn)
400{
401 struct h1c *h1c = conn->mux_ctx;
402
403 LIST_DEL(&conn->list);
404
405 if (h1c) {
406 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
407 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
408 LIST_DEL(&h1c->buf_wait.list);
409 LIST_INIT(&h1c->buf_wait.list);
410 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
411 }
412
413 h1_release_buf(h1c, &h1c->ibuf);
414 h1_release_buf(h1c, &h1c->obuf);
415
Christopher Faulet51dbc942018-09-13 09:05:15 +0200416 if (h1c->wait_event.task)
417 tasklet_free(h1c->wait_event.task);
418
Christopher Fauletf2824e62018-10-01 12:12:37 +0200419 h1s_destroy(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200420 if (h1c->wait_event.wait_reason != 0)
421 conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason,
422 &h1c->wait_event);
423 pool_free(pool_head_h1c, h1c);
424 }
425
426 conn->mux = NULL;
427 conn->mux_ctx = NULL;
428
429 conn_stop_tracking(conn);
430 conn_full_close(conn);
431 if (conn->destroy_cb)
432 conn->destroy_cb(conn);
433 conn_free(conn);
434}
435
436/******************************************************/
437/* functions below are for the H1 protocol processing */
438/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200439/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
440 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200441 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100442static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200443{
Christopher Faulet570d1612018-11-26 11:13:57 +0100444 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200445
Christopher Faulet570d1612018-11-26 11:13:57 +0100446 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200447 (*(p + 5) > '1' ||
448 (*(p + 5) == '1' && *(p + 7) >= '1')))
449 h1m->flags |= H1_MF_VER_11;
450}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200451
Christopher Faulet9768c262018-10-22 09:34:31 +0200452/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
453 * greater or equal to 1.1
454 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100455static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200456{
Christopher Faulet570d1612018-11-26 11:13:57 +0100457 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200458
Christopher Faulet570d1612018-11-26 11:13:57 +0100459 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200460 (*(p + 5) > '1' ||
461 (*(p + 5) == '1' && *(p + 7) >= '1')))
462 h1m->flags |= H1_MF_VER_11;
463}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200464
Christopher Faulet9768c262018-10-22 09:34:31 +0200465/*
466 * Check the validity of the request version. If the version is valid, it
467 * returns 1. Otherwise, it returns 0.
468 */
469static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
470{
471 struct h1c *h1c = h1s->h1c;
472
473 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
474 * exactly one digit "." one digit. This check may be disabled using
475 * option accept-invalid-http-request.
476 */
477 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
478 if (sl.rq.v.len != 8)
479 return 0;
480
481 if (*(sl.rq.v.ptr + 4) != '/' ||
482 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
483 *(sl.rq.v.ptr + 6) != '.' ||
484 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
485 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200486 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200487 else if (!sl.rq.v.len) {
488 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200489
Christopher Faulet9768c262018-10-22 09:34:31 +0200490 /* RFC 1945 allows only GET for HTTP/0.9 requests */
491 if (sl.rq.meth != HTTP_METH_GET)
492 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200493
Christopher Faulet9768c262018-10-22 09:34:31 +0200494 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
495 if (!sl.rq.u.len)
496 return 0;
497
498 /* Add HTTP version */
499 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100500 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200501 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100502
503 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100504 ((*(sl.rq.v.ptr + 5) > '1') ||
505 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100506 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200507 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200508}
509
Christopher Faulet9768c262018-10-22 09:34:31 +0200510/*
511 * Check the validity of the response version. If the version is valid, it
512 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200513 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200514static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200515{
Christopher Faulet9768c262018-10-22 09:34:31 +0200516 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200517
Christopher Faulet9768c262018-10-22 09:34:31 +0200518 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
519 * exactly one digit "." one digit. This check may be disabled using
520 * option accept-invalid-http-request.
521 */
522 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
523 if (sl.st.v.len != 8)
524 return 0;
525
526 if (*(sl.st.v.ptr + 4) != '/' ||
527 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
528 *(sl.st.v.ptr + 6) != '.' ||
529 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
530 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200531 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100532
533 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100534 ((*(sl.st.v.ptr + 5) > '1') ||
535 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100536 h1m->flags |= H1_MF_VER_11;
537
Christopher Faulet9768c262018-10-22 09:34:31 +0200538 return 1;
539}
540/* Remove all "Connection:" headers from the HTX message <htx> */
541static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
542{
543 struct ist hdr = {.ptr = "Connection", .len = 10};
544 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200545
Christopher Faulet9768c262018-10-22 09:34:31 +0200546 while (http_find_header(htx, hdr, &ctx, 1))
547 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200548
Christopher Faulet9768c262018-10-22 09:34:31 +0200549 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
550}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200551
Christopher Faulet9768c262018-10-22 09:34:31 +0200552/* Add a "Connection:" header with the value <value> into the HTX message
553 * <htx>.
554 */
555static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
556{
557 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200558
Christopher Faulet9768c262018-10-22 09:34:31 +0200559 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200560}
561
562/* Deduce the connection mode of the client connection, depending on the
563 * configuration and the H1 message flags. This function is called twice, the
564 * first time when the request is parsed and the second time when the response
565 * is parsed.
566 */
567static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
568{
569 struct proxy *fe = h1s->h1c->px;
570 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
571
572 /* Tunnel mode can only by set on the frontend */
573 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
574 flag = H1S_F_WANT_TUN;
575 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
576 flag = H1S_F_WANT_CLO;
577
578 /* flags order: CLO > SCL > TUN > KAL */
579 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
580 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
581
582 if (h1m->flags & H1_MF_RESP) {
583 /* Either we've established an explicit tunnel, or we're
584 * switching the protocol. In both cases, we're very unlikely to
585 * understand the next protocols. We have to switch to tunnel
586 * mode, so that we transfer the request and responses then let
587 * this protocol pass unmodified. When we later implement
588 * specific parsers for such protocols, we'll want to check the
589 * Upgrade header which contains information about that protocol
590 * for responses with status 101 (eg: see RFC2817 about TLS).
591 */
592 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
593 h1s->status == 101)
594 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletbd44ca62018-11-29 09:55:22 +0100595 else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */
596 (h1m->flags & H1_MF_CONN_CLO && h1s->req.state != H1_MSG_DONE)) /*explicit close and unfinished request */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200597 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
598 }
599 else {
600 if (h1s->flags & H1S_F_WANT_KAL &&
601 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
602 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
603 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
604 }
605
606 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
607 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
608 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
609}
610
611/* Deduce the connection mode of the client connection, depending on the
612 * configuration and the H1 message flags. This function is called twice, the
613 * first time when the request is parsed and the second time when the response
614 * is parsed.
615 */
616static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
617{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100618 struct h1c *h1c = h1s->h1c;
619 struct session *sess = h1c->conn->owner;
620 struct proxy *fe = sess->fe;
621 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200622 int flag = H1S_F_WANT_KAL;
623
624 /* Tunnel mode can only by set on the frontend */
625 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
626 flag = H1S_F_WANT_TUN;
627
628 /* For the server connection: server-close == httpclose */
629 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
630 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
631 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
632 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
633 flag = H1S_F_WANT_CLO;
634
635 /* flags order: CLO > SCL > TUN > KAL */
636 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
637 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
638
639 if (h1m->flags & H1_MF_RESP) {
640 /* Either we've established an explicit tunnel, or we're
641 * switching the protocol. In both cases, we're very unlikely to
642 * understand the next protocols. We have to switch to tunnel
643 * mode, so that we transfer the request and responses then let
644 * this protocol pass unmodified. When we later implement
645 * specific parsers for such protocols, we'll want to check the
646 * Upgrade header which contains information about that protocol
647 * for responses with status 101 (eg: see RFC2817 about TLS).
648 */
649 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
650 h1s->status == 101)
651 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
652 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
653 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
654 else if (h1s->flags & H1S_F_WANT_KAL &&
655 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
656 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
657 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
658 }
Christopher Faulet70033782018-12-05 13:50:11 +0100659 else {
660 if (h1s->flags & H1S_F_WANT_KAL &&
661 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
662 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
663 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
664 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200665
666 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
667 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
668 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200669}
670
Christopher Faulet9768c262018-10-22 09:34:31 +0200671static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
672 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200673{
674 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200675
676 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
677 * token is found
678 */
679 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200680 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200681
682 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200683 if (h1m->flags & H1_MF_CONN_CLO) {
684 if (conn_val)
685 *conn_val = ist("");
686 if (htx)
687 h1_remove_conn_hdrs(h1m, htx);
688 }
689 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
690 if (conn_val)
691 *conn_val = ist("keep-alive");
692 if (htx)
693 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
694 }
Christopher Fauletce851492018-12-07 18:06:59 +0100695 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
696 if (conn_val)
697 *conn_val = ist("");
698 if (htx)
699 h1_remove_conn_hdrs(h1m, htx);
700 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200701 }
702 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200703 if (h1m->flags & H1_MF_CONN_KAL) {
704 if (conn_val)
705 *conn_val = ist("");
706 if (htx)
707 h1_remove_conn_hdrs(h1m, htx);
708 }
709 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
710 if (conn_val)
711 *conn_val = ist("close");
712 if (htx)
713 h1_add_conn_hdr(h1m, htx, ist("close"));
714 }
Christopher Fauletce851492018-12-07 18:06:59 +0100715 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
716 if (conn_val)
717 *conn_val = ist("");
718 if (htx)
719 h1_remove_conn_hdrs(h1m, htx);
720 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200721 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200722}
723
Christopher Faulet9768c262018-10-22 09:34:31 +0200724static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
725 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200726{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200727 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
728 * token is found
729 */
730 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200731 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200732
733 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200734 if (h1m->flags & H1_MF_CONN_CLO) {
735 if (conn_val)
736 *conn_val = ist("");
737 if (htx)
738 h1_remove_conn_hdrs(h1m, htx);
739 }
740 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
741 if (conn_val)
742 *conn_val = ist("keep-alive");
743 if (htx)
744 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
745 }
Christopher Fauletce851492018-12-07 18:06:59 +0100746 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
747 if (conn_val)
748 *conn_val = ist("");
749 if (htx)
750 h1_remove_conn_hdrs(h1m, htx);
751 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200752 }
753 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200754 if (h1m->flags & H1_MF_CONN_KAL) {
755 if (conn_val)
756 *conn_val = ist("");
757 if (htx)
758 h1_remove_conn_hdrs(h1m, htx);
759 }
760 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
761 if (conn_val)
762 *conn_val = ist("close");
763 if (htx)
764 h1_add_conn_hdr(h1m, htx, ist("close"));
765 }
Christopher Fauletce851492018-12-07 18:06:59 +0100766 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
767 if (conn_val)
768 *conn_val = ist("");
769 if (htx)
770 h1_remove_conn_hdrs(h1m, htx);
771 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200772 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200773}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200774
Christopher Faulet9768c262018-10-22 09:34:31 +0200775/* Set the right connection mode and update "Connection:" header if
776 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
777 * message is updated accordingly. When <conn_val> is not NULL, it is set with
778 * the new header value.
779 */
780static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
781 struct htx *htx, struct ist *conn_val)
782{
783 if (!conn_is_back(h1s->h1c->conn)) {
784 h1_set_cli_conn_mode(h1s, h1m);
785 if (h1m->flags & H1_MF_RESP)
786 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
787 }
788 else {
789 h1_set_srv_conn_mode(h1s, h1m);
790 if (!(h1m->flags & H1_MF_RESP))
791 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
792 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200793}
794
Christopher Faulete44769b2018-11-29 23:01:45 +0100795
796/* Append the description of what is present in error snapshot <es> into <out>.
797 * The description must be small enough to always fit in a buffer. The output
798 * buffer may be the trash so the trash must not be used inside this function.
799 */
800static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
801{
802 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100803 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
804 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
805 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
806 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
807 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
808 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100809}
810/*
811 * Capture a bad request or response and archive it in the proxy's structure.
812 * By default it tries to report the error position as h1m->err_pos. However if
813 * this one is not set, it will then report h1m->next, which is the last known
814 * parsing point. The function is able to deal with wrapping buffers. It always
815 * displays buffers as a contiguous area starting at buf->p. The direction is
816 * determined thanks to the h1m's flags.
817 */
818static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
819 struct h1m *h1m, struct buffer *buf)
820{
821 struct session *sess = h1c->conn->owner;
822 struct proxy *proxy = h1c->px;
823 struct proxy *other_end = sess->fe;
824 union error_snapshot_ctx ctx;
825
826 if (h1s->cs->data)
827 other_end = si_strm(h1s->cs->data)->be;
828
829 /* http-specific part now */
830 ctx.h1.state = h1m->state;
831 ctx.h1.c_flags = h1c->flags;
832 ctx.h1.s_flags = h1s->flags;
833 ctx.h1.m_flags = h1m->flags;
834 ctx.h1.m_clen = h1m->curr_len;
835 ctx.h1.m_blen = h1m->body_len;
836
837 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
838 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100839 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
840 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100841}
842
Christopher Fauletadb22202018-12-12 10:32:09 +0100843/* Emit the chunksize followed by a CRLF in front of data of the buffer
844 * <buf>. It goes backwards and starts with the byte before the buffer's
845 * head. The caller is responsible for ensuring there is enough room left before
846 * the buffer's head for the string.
847 */
848static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
849{
850 char *beg, *end;
851
852 beg = end = b_head(buf);
853 *--beg = '\n';
854 *--beg = '\r';
855 do {
856 *--beg = hextab[chksz & 0xF];
857 } while (chksz >>= 4);
858 buf->head -= (end - beg);
859 b_add(buf, end - beg);
860}
861
862/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
863 * ensuring there is enough room left in the buffer for the string. */
864static void h1_emit_chunk_crlf(struct buffer *buf)
865{
866 *(b_peek(buf, b_data(buf))) = '\r';
867 *(b_peek(buf, b_data(buf) + 1)) = '\n';
868 b_add(buf, 2);
869}
870
Christopher Faulet129817b2018-09-20 16:14:40 +0200871/*
872 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200873 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
874 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800875 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200876 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200877static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200878 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200879{
880 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100881 union h1_sl h1sl;
882 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200883 int ret = 0;
884
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100885 if (!max)
886 goto end;
887
Christopher Faulet129817b2018-09-20 16:14:40 +0200888 /* Realing input buffer if necessary */
889 if (b_head(buf) + b_data(buf) > b_wrap(buf))
890 b_slow_realign(buf, trash.area, 0);
891
Christopher Fauletf2824e62018-10-01 12:12:37 +0200892 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100893 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200894 if (ret <= 0) {
895 /* Incomplete or invalid message. If the buffer is full, it's an
896 * error because headers are too large to be handled by the
897 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200898 if (ret < 0 || (!ret && b_full(buf)))
899 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200900 goto end;
901 }
902
903 /* messages headers fully parsed, do some checks to prepare the body
904 * parsing.
905 */
906
907 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200908 if (ret > (b_size(buf) - global.tune.maxrewrite))
909 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200910
Christopher Faulet9768c262018-10-22 09:34:31 +0200911 /* Save the request's method or the response's status, check if the body
912 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200913 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100914 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200915
Christopher Faulet129817b2018-09-20 16:14:40 +0200916 /* Request have always a known length */
917 h1m->flags |= H1_MF_XFER_LEN;
918 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
919 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200920
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100921 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
922 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200923 h1m->err_state = h1m->state;
924 goto vsn_error;
925 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200926 }
927 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100928 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200929
930 if ((h1s->meth == HTTP_METH_HEAD) ||
931 (h1s->status >= 100 && h1s->status < 200) ||
932 (h1s->status == 204) || (h1s->status == 304) ||
933 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
934 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
935 h1m->flags |= H1_MF_XFER_LEN;
936 h1m->curr_len = h1m->body_len = 0;
937 h1m->state = H1_MSG_DONE;
938 }
939 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
940 h1m->flags |= H1_MF_XFER_LEN;
941 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
942 h1m->state = H1_MSG_DONE;
943 }
944 else
945 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200946
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100947 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
948 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200949 h1m->err_state = h1m->state;
950 goto vsn_error;
951 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200952 }
953
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100954 /* Set HTX start-line flags */
955 if (h1m->flags & H1_MF_VER_11)
956 flags |= HTX_SL_F_VER_11;
957 if (h1m->flags & H1_MF_XFER_ENC)
958 flags |= HTX_SL_F_XFER_ENC;
959 if (h1m->flags & H1_MF_XFER_LEN) {
960 flags |= HTX_SL_F_XFER_LEN;
961 if (h1m->flags & H1_MF_CHNK)
962 flags |= HTX_SL_F_CHNK;
963 else if (h1m->flags & H1_MF_CLEN)
964 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100965 if (h1m->state == H1_MSG_DONE)
966 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100967 }
968
Christopher Faulet9768c262018-10-22 09:34:31 +0200969 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100970 struct htx_sl *sl;
971
972 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
973 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200974 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100975 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200976 }
977 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100978 struct htx_sl *sl;
979
980 flags |= HTX_SL_F_IS_RESP;
981 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
982 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200983 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100984 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200985 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100986
Christopher Faulet9768c262018-10-22 09:34:31 +0200987 if (h1m->state == H1_MSG_DONE)
988 if (!htx_add_endof(htx, HTX_BLK_EOM))
989 goto error;
990
991 h1_process_conn_mode(h1s, h1m, htx, NULL);
992
993 /* If body length cannot be determined, set htx->extra to
994 * ULLONG_MAX. This value is impossible in other cases.
995 */
996 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
997
998 /* Recheck there is enough space to do headers rewritting */
999 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
1000 goto error;
1001
1002 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001003 end:
1004 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001005
1006 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001007 h1m->err_state = h1m->state;
1008 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001009 vsn_error:
1010 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +01001011 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001012 ret = 0;
1013 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001014}
1015
1016/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001017 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1018 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001019 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001020 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001021 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001022static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001023 struct buffer *buf, size_t *ofs, size_t max,
1024 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001025{
Christopher Faulet9768c262018-10-22 09:34:31 +02001026 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +02001027 size_t total = 0;
1028 int ret = 0;
1029
1030 if (h1m->flags & H1_MF_XFER_LEN) {
1031 if (h1m->flags & H1_MF_CLEN) {
1032 /* content-length: read only h2m->body_len */
1033 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001034 if (ret > data_space)
1035 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001036 if ((uint64_t)ret > h1m->curr_len)
1037 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001038 if (ret > b_contig_data(buf, *ofs))
1039 ret = b_contig_data(buf, *ofs);
1040 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001041 /* very often with large files we'll face the following
1042 * situation :
1043 * - htx is empty and points to <htxbuf>
1044 * - ret == buf->data
1045 * - buf->head == sizeof(struct htx)
1046 * => we can swap the buffers and place an htx header into
1047 * the target buffer instead
1048 */
1049 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1050 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1051 void *raw_area = buf->area;
1052 void *htx_area = htxbuf->area;
1053 struct htx_blk *blk;
1054
1055 buf->area = htx_area;
1056 htxbuf->area = raw_area;
1057 htx = (struct htx *)htxbuf->area;
1058 htx->size = htxbuf->size - sizeof(*htx);
1059 htx_reset(htx);
1060 b_set_data(htxbuf, b_size(htxbuf));
1061
1062 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1063 blk->info += ret;
1064 /* nothing else to do, the old buffer now contains an
1065 * empty pre-initialized HTX header
1066 */
1067 }
1068 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001069 goto end;
1070 h1m->curr_len -= ret;
1071 *ofs += ret;
1072 total += ret;
1073 }
1074
1075 if (!h1m->curr_len) {
1076 if (!htx_add_endof(htx, HTX_BLK_EOM))
1077 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001078 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +02001079 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001080 }
1081 else if (h1m->flags & H1_MF_CHNK) {
1082 new_chunk:
1083 /* te:chunked : parse chunks */
1084 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001085 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001086 if (ret <= 0)
1087 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001088 h1m->state = H1_MSG_CHUNK_SIZE;
1089
Christopher Faulet129817b2018-09-20 16:14:40 +02001090 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001091 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001092 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001093 }
1094
1095 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1096 unsigned int chksz;
1097
Christopher Fauletf2824e62018-10-01 12:12:37 +02001098 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001099 if (ret <= 0)
1100 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001101 if (!chksz) {
1102 if (!htx_add_endof(htx, HTX_BLK_EOD))
1103 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001104 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001105 h1m->state = H1_MSG_TRAILERS;
1106 }
1107 else
1108 h1m->state = H1_MSG_DATA;
1109
Christopher Faulet129817b2018-09-20 16:14:40 +02001110 h1m->curr_len = chksz;
1111 h1m->body_len += chksz;
1112 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001113 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001114 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001115 }
1116
1117 if (h1m->state == H1_MSG_DATA) {
1118 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001119 if (ret > data_space)
1120 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001121 if ((uint64_t)ret > h1m->curr_len)
1122 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001123 if (ret > b_contig_data(buf, *ofs))
1124 ret = b_contig_data(buf, *ofs);
1125 if (ret) {
1126 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1127 goto end;
1128 h1m->curr_len -= ret;
1129 max -= ret;
1130 *ofs += ret;
1131 total += ret;
1132 }
1133 if (!h1m->curr_len) {
1134 h1m->state = H1_MSG_CHUNK_CRLF;
1135 goto new_chunk;
1136 }
1137 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001138 }
1139
1140 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001141 /* Trailers were alread parsed, only the EOM
1142 * need to be added */
1143 if (h1s->flags & H1S_F_HAVE_TLR)
1144 goto skip_tlr_parsing;
1145
Christopher Fauletf2824e62018-10-01 12:12:37 +02001146 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001147 if (ret > data_space)
1148 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001149 if (ret <= 0)
1150 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001151
1152 /* Realing input buffer if tailers wrap. For now
1153 * this is a workaroung. Because trailers are
1154 * not split on CRLF, like headers, there is no
1155 * way to know where to split it when trailers
1156 * wrap. This is a limitation of
1157 * h1_measure_trailers.
1158 */
1159 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1160 b_slow_realign(buf, trash.area, 0);
1161
1162 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1163 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001164 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001165 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001166 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001167 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001168
Christopher Faulet17276482018-11-22 11:44:35 +01001169 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001170 if (!htx_add_endof(htx, HTX_BLK_EOM))
1171 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001172 h1m->state = H1_MSG_DONE;
1173 }
1174 }
1175 else {
1176 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1177 * is no body. Switch the message in DONE state
1178 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001179 if (!htx_add_endof(htx, HTX_BLK_EOM))
1180 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001181 h1m->state = H1_MSG_DONE;
1182 }
1183 }
1184 else {
1185 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001186 ret = max;
1187 if (ret > data_space)
1188 ret = data_space;
1189 if (ret > b_contig_data(buf, *ofs))
1190 ret = b_contig_data(buf, *ofs);
1191 if (ret) {
1192 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1193 goto end;
1194
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001195 *ofs += ret;
1196 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001197 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001198 }
1199
1200 end:
1201 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001202 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001203 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001204 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001205 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001206 return 0;
1207 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001208 /* update htx->extra, only when the body length is known */
1209 if (h1m->flags & H1_MF_XFER_LEN)
1210 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001211 return total;
1212}
1213
1214/*
1215 * Synchronize the request and the response before reseting them. Except for 1xx
1216 * responses, we wait that the request and the response are in DONE state and
1217 * that all data are forwarded for both. For 1xx responses, only the response is
1218 * reset, waiting the final one. Many 1xx messages can be sent.
1219 */
1220static void h1_sync_messages(struct h1c *h1c)
1221{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001222 struct h1s *h1s = h1c->h1s;
1223
1224 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001225 return;
1226
Christopher Fauletf2824e62018-10-01 12:12:37 +02001227 if (h1s->res.state == H1_MSG_DONE &&
1228 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001229 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001230 /* For 100-Continue response or any other informational 1xx
1231 * response which is non-final, don't reset the request, the
1232 * transaction is not finished. We take care the response was
1233 * transferred before.
1234 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001235 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001237 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet129817b2018-09-20 16:14:40 +02001238 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001239 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001240 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1241 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001242 h1m_init_req(&h1s->req);
1243 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001244 h1s->req.state = H1_MSG_TUNNEL;
1245 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001246 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001247 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001248 }
1249}
1250
1251/*
1252 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001253 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1254 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001255 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001256static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001257{
Christopher Faulet539e0292018-11-19 10:40:09 +01001258 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001259 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001260 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001261 size_t total = 0;
1262 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001263 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001264 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001265
Christopher Faulet539e0292018-11-19 10:40:09 +01001266 htx = htx_from_buf(buf);
1267 count = b_data(&h1c->ibuf);
1268 max = htx_free_space(htx);
1269 if (flags & CO_RFL_KEEP_RSV) {
1270 if (max < global.tune.maxrewrite)
1271 goto end;
1272 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001273 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001274 if (count > max)
1275 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001276
Christopher Fauletf2824e62018-10-01 12:12:37 +02001277 if (!conn_is_back(h1c->conn)) {
1278 h1m = &h1s->req;
1279 errflag = H1S_F_REQ_ERROR;
1280 }
1281 else {
1282 h1m = &h1s->res;
1283 errflag = H1S_F_RES_ERROR;
1284 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001285
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001286 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001287 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001288 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001289 if (!ret)
1290 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001291 }
1292 else if (h1m->state <= H1_MSG_TRAILERS) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001293 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1294 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 if (!ret)
1296 break;
1297 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001298 else if (h1m->state == H1_MSG_DONE) {
1299 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001300 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001301 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001302 else if (h1m->state == H1_MSG_TUNNEL) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001303 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1304 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001305 if (!ret)
1306 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001307 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001308 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001309 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001310 break;
1311 }
1312
Christopher Faulet539e0292018-11-19 10:40:09 +01001313 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001314 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001315
Christopher Faulet47365272018-10-31 17:40:50 +01001316 if (h1s->flags & errflag)
1317 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001318
Christopher Faulet539e0292018-11-19 10:40:09 +01001319 b_del(&h1c->ibuf, total);
1320
1321 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001322 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001323
Willy Tarreau45f2b892018-12-05 07:59:27 +01001324 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001325 h1c->flags &= ~H1C_F_IN_FULL;
1326 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001327 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001328
Christopher Fauletcf56b992018-12-11 16:12:31 +01001329 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1330
1331 if (!b_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001332 h1_release_buf(h1c, &h1c->ibuf);
1333 h1_sync_messages(h1c);
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001334 }
Christopher Fauletcf56b992018-12-11 16:12:31 +01001335 else if (!htx_is_empty(htx))
1336 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001337
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001338 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1339 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet539e0292018-11-19 10:40:09 +01001340 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001341
Christopher Faulet539e0292018-11-19 10:40:09 +01001342 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001343
1344 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001345 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001346 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001347 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001348 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001349 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001350}
1351
Christopher Faulet129817b2018-09-20 16:14:40 +02001352/*
1353 * Process outgoing data. It parses data and transfer them from the channel buffer into
1354 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1355 * 0 if it couldn't proceed.
1356 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001357static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1358{
Christopher Faulet129817b2018-09-20 16:14:40 +02001359 struct h1s *h1s = h1c->h1s;
1360 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001361 struct htx *chn_htx;
1362 struct htx_blk *blk;
1363 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001364 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001365 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001366 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001367
Christopher Faulet47365272018-10-31 17:40:50 +01001368 if (!count)
1369 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001370
Christopher Faulet9768c262018-10-22 09:34:31 +02001371 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001372 if (htx_is_empty(chn_htx))
1373 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001374
Christopher Faulet51dbc942018-09-13 09:05:15 +02001375 if (!h1_get_buf(h1c, &h1c->obuf)) {
1376 h1c->flags |= H1C_F_OUT_ALLOC;
1377 goto end;
1378 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001379
Christopher Fauletf2824e62018-10-01 12:12:37 +02001380 if (!conn_is_back(h1c->conn)) {
1381 h1m = &h1s->res;
1382 errflag = H1S_F_RES_ERROR;
1383 }
1384 else {
1385 h1m = &h1s->req;
1386 errflag = H1S_F_REQ_ERROR;
1387 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001388
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001389 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001390 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001391
1392 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001393
Willy Tarreau3815b222018-12-11 19:50:43 +01001394 /* Perform some optimizations to reduce the number of buffer copies.
1395 * First, if the mux's buffer is empty and the htx area contains
1396 * exactly one data block of the same size as the requested count,
1397 * then it's possible to simply swap the caller's buffer with the
1398 * mux's output buffer and adjust offsets and length to match the
1399 * entire DATA HTX block in the middle. In this case we perform a
1400 * true zero-copy operation from end-to-end. This is the situation
1401 * that happens all the time with large files. Second, if this is not
1402 * possible, but the mux's output buffer is empty, we still have an
1403 * opportunity to avoid the copy to the intermediary buffer, by making
1404 * the intermediary buffer's area point to the output buffer's area.
1405 * In this case we want to skip the HTX header to make sure that copies
1406 * remain aligned and that this operation remains possible all the
1407 * time. This goes for headers, data blocks and any data extracted from
1408 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001409 */
1410 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001411 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001412
1413 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001414 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001415 htx_get_blk_value(chn_htx, blk).len == count) {
1416 void *old_area = h1c->obuf.area;
1417
1418 h1c->obuf.area = buf->area;
1419 h1c->obuf.data = count;
1420
1421 buf->area = old_area;
1422 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001423
1424 /* The message is chunked. We need to emit the chunk
1425 * size. We have at least the size of the struct htx to
1426 * write the chunk envelope. It should be enough.
1427 */
1428 if (h1m->flags & H1_MF_CHNK) {
1429 h1_emit_chunk_size(&h1c->obuf, count);
1430 h1_emit_chunk_crlf(&h1c->obuf);
1431 }
1432
Willy Tarreau3815b222018-12-11 19:50:43 +01001433 total += count;
1434 goto out;
1435 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001436 tmp->area = h1c->obuf.area + h1c->obuf.head;
1437 }
1438
Christopher Faulet9768c262018-10-22 09:34:31 +02001439 tmp->size = b_room(&h1c->obuf);
1440
Christopher Fauletb2e84162018-12-06 11:39:49 +01001441 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001442 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001443 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001444 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001445 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001446 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001447
Christopher Fauletb2e84162018-12-06 11:39:49 +01001448 vlen = sz;
1449 if (vlen > count) {
1450 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1451 goto copy;
1452 vlen = count;
1453 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001454
Christopher Fauletb2e84162018-12-06 11:39:49 +01001455 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001456 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001457 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001458
1459 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001460 h1m_init_req(h1m);
1461 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001462 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001463 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001464 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001465 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001466 goto copy;
1467 h1m->flags |= H1_MF_XFER_LEN;
1468 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001469 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001470
Christopher Faulet9768c262018-10-22 09:34:31 +02001471 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001472 h1m_init_res(h1m);
1473 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001474 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001475 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001476 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001477 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001479 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001480 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001481 if (sl->info.res.status < 200 &&
1482 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1483 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001484 h1m->state = H1_MSG_HDR_FIRST;
1485 break;
1486
1487 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001488 h1m->state = H1_MSG_HDR_NAME;
1489 n = htx_get_blk_name(chn_htx, blk);
1490 v = htx_get_blk_value(chn_htx, blk);
1491
1492 if (isteqi(n, ist("transfer-encoding")))
1493 h1_parse_xfer_enc_header(h1m, v);
1494 else if (isteqi(n, ist("connection"))) {
1495 h1_parse_connection_header(h1m, v);
1496 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001497 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001498 if (!v.len)
1499 goto skip_hdr;
1500 }
1501
Christopher Fauletc59ff232018-12-03 13:58:44 +01001502 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001503 goto copy;
1504 skip_hdr:
1505 h1m->state = H1_MSG_HDR_L2_LWS;
1506 break;
1507
1508 case HTX_BLK_PHDR:
1509 /* not implemented yet */
1510 h1m->flags |= errflag;
1511 break;
1512
1513 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001514 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001515 /* There is no "Connection:" header and
1516 * it the conn_mode must be
1517 * processed. So do it */
1518 n = ist("Connection");
1519 v = ist("");
1520 h1_process_conn_mode(h1s, h1m, NULL, &v);
1521 process_conn_mode = 0;
1522 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001523 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001524 goto copy;
1525 }
1526 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001527 h1m->state = H1_MSG_LAST_LF;
1528 if (!chunk_memcat(tmp, "\r\n", 2))
1529 goto copy;
1530
1531 h1m->state = H1_MSG_DATA;
1532 break;
1533
1534 case HTX_BLK_DATA:
1535 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001536 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001537 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001538 goto copy;
1539 break;
1540
1541 case HTX_BLK_EOD:
1542 if (!chunk_memcat(tmp, "0\r\n", 3))
1543 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001544 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001545 h1m->state = H1_MSG_TRAILERS;
1546 break;
1547
1548 case HTX_BLK_TLR:
Christopher Faulet32188212018-11-20 18:21:43 +01001549 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1550 if (!chunk_memcat(tmp, "0\r\n", 3))
1551 goto copy;
1552 h1s->flags |= H1S_F_HAVE_EOD;
1553 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001555 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001556 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001557 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001558 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001559 break;
1560
1561 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001562 if ((h1m->flags & H1_MF_CHNK)) {
1563 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1564 if (!chunk_memcat(tmp, "0\r\n", 3))
1565 goto copy;
1566 h1s->flags |= H1S_F_HAVE_EOD;
1567 }
1568 if (!(h1s->flags & H1S_F_HAVE_TLR)) {
1569 if (!chunk_memcat(tmp, "\r\n", 2))
1570 goto copy;
1571 h1s->flags |= H1S_F_HAVE_TLR;
1572 }
1573 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001574 h1m->state = H1_MSG_DONE;
1575 break;
1576
1577 case HTX_BLK_OOB:
1578 v = htx_get_blk_value(chn_htx, blk);
1579 if (!chunk_memcat(tmp, v.ptr, v.len))
1580 goto copy;
1581 break;
1582
1583 default:
1584 h1m->flags |= errflag;
1585 break;
1586 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001587 total += vlen;
1588 count -= vlen;
1589 if (sz == vlen)
1590 blk = htx_remove_blk(chn_htx, blk);
1591 else {
1592 htx_cut_data_blk(chn_htx, blk, vlen);
1593 break;
1594 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001595 }
1596
Christopher Faulet9768c262018-10-22 09:34:31 +02001597 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001598 /* when the output buffer is empty, tmp shares the same area so that we
1599 * only have to update pointers and lengths.
1600 */
1601 if (tmp->area == h1c->obuf.area)
1602 h1c->obuf.data = tmp->data;
1603 else
1604 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001605
Willy Tarreau3815b222018-12-11 19:50:43 +01001606 htx_to_buf(chn_htx, buf);
1607 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001608 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001609 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001610 end:
1611 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001612}
1613
Christopher Faulet51dbc942018-09-13 09:05:15 +02001614/*********************************************************/
1615/* functions below are I/O callbacks from the connection */
1616/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001617static void h1_wake_stream_for_recv(struct h1s *h1s)
1618{
1619 if (h1s && h1s->recv_wait) {
1620 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1621 tasklet_wakeup(h1s->recv_wait->task);
1622 h1s->recv_wait = NULL;
1623 }
1624}
1625static void h1_wake_stream_for_send(struct h1s *h1s)
1626{
1627 if (h1s && h1s->send_wait) {
1628 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1629 tasklet_wakeup(h1s->send_wait->task);
1630 h1s->send_wait = NULL;
1631 }
1632}
1633
Christopher Faulet51dbc942018-09-13 09:05:15 +02001634/*
1635 * Attempt to read data, and subscribe if none available
1636 */
1637static int h1_recv(struct h1c *h1c)
1638{
1639 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001640 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001641 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001642 int rcvd = 0;
1643
1644 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001645 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001646
Olivier Houchard75159a92018-12-03 18:46:09 +01001647 if (!h1_recv_allowed(h1c)) {
1648 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001650 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001651
Christopher Fauletfeb11742018-11-29 15:12:34 +01001652 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001653 rcvd = 1;
1654 goto end;
1655 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001656
Christopher Faulet51dbc942018-09-13 09:05:15 +02001657 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1658 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001659 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001660 }
1661
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001662 /*
1663 * If we only have a small amount of data, realign it,
1664 * it's probably cheaper than doing 2 recv() calls.
1665 */
1666 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1667 b_slow_realign(&h1c->ibuf, trash.area, 0);
1668
Willy Tarreau45f2b892018-12-05 07:59:27 +01001669 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001670 if (max) {
1671 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001672
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001673 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001674 if (!b_data(&h1c->ibuf)) {
1675 /* try to pre-align the buffer like the rxbufs will be
1676 * to optimize memory copies.
1677 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001678 h1c->ibuf.head = sizeof(struct htx);
1679 }
Willy Tarreauc0960d12018-12-14 10:59:15 +01001680 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001681 }
Christopher Faulet47365272018-10-31 17:40:50 +01001682 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001683 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001684 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001685 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001686 if (h1s->csinfo.t_idle == -1)
1687 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1688 }
Christopher Faulet47365272018-10-31 17:40:50 +01001689 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001690
Christopher Fauletcf56b992018-12-11 16:12:31 +01001691 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001692 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001693 goto end;
1694 }
1695
1696 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001697
Christopher Faulet9768c262018-10-22 09:34:31 +02001698 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001699 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1700 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001701
Christopher Faulete6b39942018-12-07 09:42:49 +01001702 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Olivier Houchard6a2d3342018-12-06 17:41:26 +01001703 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001704 if (!b_data(&h1c->ibuf))
1705 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001706 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001707 h1c->flags |= H1C_F_IN_FULL;
1708 return rcvd;
1709}
1710
1711
1712/*
1713 * Try to send data if possible
1714 */
1715static int h1_send(struct h1c *h1c)
1716{
1717 struct connection *conn = h1c->conn;
1718 unsigned int flags = 0;
1719 size_t ret;
1720 int sent = 0;
1721
1722 if (conn->flags & CO_FL_ERROR)
1723 return 0;
1724
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001725 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1726 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1727 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1728 return 0;
1729 }
1730
Christopher Faulet51dbc942018-09-13 09:05:15 +02001731 if (!b_data(&h1c->obuf))
1732 goto end;
1733
1734 if (h1c->flags & H1C_F_OUT_FULL)
1735 flags |= CO_SFL_MSG_MORE;
1736
1737 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1738 if (ret > 0) {
1739 h1c->flags &= ~H1C_F_OUT_FULL;
1740 b_del(&h1c->obuf, ret);
1741 sent = 1;
1742 }
1743
Christopher Faulet145aa472018-12-06 10:56:20 +01001744 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1745 /* error or output closed, nothing to send, clear the buffer to release it */
1746 b_reset(&h1c->obuf);
1747 }
1748
Christopher Faulet51dbc942018-09-13 09:05:15 +02001749 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001750 if (!(h1c->flags & H1C_F_OUT_FULL))
1751 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001752
Christopher Faulet51dbc942018-09-13 09:05:15 +02001753 /* We're done, no more to send */
1754 if (!b_data(&h1c->obuf)) {
1755 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001756 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001757 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1758 h1_shutw_conn(conn);
1759 }
1760 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1761 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1762
1763 return sent;
1764}
1765
Christopher Faulet51dbc942018-09-13 09:05:15 +02001766
1767/* callback called on any event by the connection handler.
1768 * It applies changes and returns zero, or < 0 if it wants immediate
1769 * destruction of the connection.
1770 */
1771static int h1_process(struct h1c * h1c)
1772{
1773 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001774 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001775
Christopher Faulet51dbc942018-09-13 09:05:15 +02001776 if (!conn->mux_ctx)
1777 return -1;
1778
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001779 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001780 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1781 goto end;
1782 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001783 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001784 }
1785
Christopher Fauletfeb11742018-11-29 15:12:34 +01001786 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001787 if (h1c->flags & H1C_F_CS_ERROR ||
1788 conn->flags & CO_FL_ERROR ||
1789 conn_xprt_read0_pending(conn))
1790 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001791 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001792 if (!h1s_create(h1c, NULL))
1793 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001794 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001795 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001796 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001797 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001798 }
1799
Christopher Fauletfeb11742018-11-29 15:12:34 +01001800 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1801 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1802
Olivier Houchard75159a92018-12-03 18:46:09 +01001803 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1804 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
1805 conn->flags & CO_FL_ERROR)) {
1806 int flags = 0;
1807
1808 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1809 flags |= CS_FL_ERROR;
1810 if (conn_xprt_read0_pending(conn))
Christopher Faulet5f50f5e2018-12-07 11:39:55 +01001811 flags |= CS_FL_EOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001812 h1s->cs->flags |= flags;
1813 h1s->cs->data_cb->wake(h1s->cs);
1814 }
Christopher Faulet47365272018-10-31 17:40:50 +01001815 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001816 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001817
1818 release:
1819 h1_release(conn);
1820 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001821}
1822
1823static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1824{
1825 struct h1c *h1c = ctx;
1826 int ret = 0;
1827
1828 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1829 ret = h1_send(h1c);
1830 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1831 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001832 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001833 h1_process(h1c);
1834 return NULL;
1835}
1836
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001837static void h1_reset(struct connection *conn)
1838{
1839 struct h1c *h1c = conn->mux_ctx;
1840
1841 /* Reset the flags, and let the mux know we're waiting for a connection */
1842 h1c->flags = H1C_F_CS_WAIT_CONN;
1843}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001844
1845static int h1_wake(struct connection *conn)
1846{
1847 struct h1c *h1c = conn->mux_ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001848 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001849
Christopher Faulet539e0292018-11-19 10:40:09 +01001850 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001851 ret = h1_process(h1c);
1852 if (ret == 0) {
1853 struct h1s *h1s = h1c->h1s;
1854
1855 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1856 ret = h1s->cs->data_cb->wake(h1s->cs);
1857 }
1858 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001859}
1860
Christopher Faulet51dbc942018-09-13 09:05:15 +02001861/*******************************************/
1862/* functions below are used by the streams */
1863/*******************************************/
1864/*
1865 * Attach a new stream to a connection
1866 * (Used for outgoing connections)
1867 */
1868static struct conn_stream *h1_attach(struct connection *conn)
1869{
1870 struct h1c *h1c = conn->mux_ctx;
1871 struct conn_stream *cs = NULL;
1872 struct h1s *h1s;
1873
1874 if (h1c->flags & H1C_F_CS_ERROR)
1875 goto end;
1876
1877 cs = cs_new(h1c->conn);
1878 if (!cs)
1879 goto end;
1880
Christopher Fauletf2824e62018-10-01 12:12:37 +02001881 h1s = h1s_create(h1c, cs);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001882 if (h1s == NULL)
1883 goto end;
1884
1885 return cs;
1886 end:
1887 cs_free(cs);
1888 return NULL;
1889}
1890
1891/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1892 * this mux, it's easy as we can only store a single conn_stream.
1893 */
1894static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1895{
1896 struct h1c *h1c = conn->mux_ctx;
1897 struct h1s *h1s = h1c->h1s;
1898
1899 if (h1s)
1900 return h1s->cs;
1901
1902 return NULL;
1903}
1904
1905static void h1_destroy(struct connection *conn)
1906{
1907 struct h1c *h1c = conn->mux_ctx;
1908
1909 if (!h1c->h1s)
1910 h1_release(conn);
1911}
1912
1913/*
1914 * Detach the stream from the connection and possibly release the connection.
1915 */
1916static void h1_detach(struct conn_stream *cs)
1917{
1918 struct h1s *h1s = cs->ctx;
1919 struct h1c *h1c;
1920
1921 cs->ctx = NULL;
1922 if (!h1s)
1923 return;
1924
1925 h1c = h1s->h1c;
1926 h1s->cs = NULL;
1927
Olivier Houchard44d59142018-12-13 18:46:22 +01001928 if (conn_is_back(h1c->conn) && (h1s->flags & H1S_F_WANT_KAL) &&
1929 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
1930 struct stream_interface *si = cs->data;
1931 struct stream *s = si_strm(si);
1932
Christopher Faulet9400a392018-11-23 23:10:39 +01001933 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01001934 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01001935 h1c->conn->flags |= CO_FL_PRIVATE;
1936
Olivier Houchard44d59142018-12-13 18:46:22 +01001937 if (!(h1c->conn->owner)) {
1938 h1c->conn->owner = s->sess;
1939 session_add_conn(s->sess, h1c->conn, s->target);
1940 }
Christopher Faulet9400a392018-11-23 23:10:39 +01001941 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01001942 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001943 struct server *srv = objt_server(h1c->conn->target);
1944
1945 if (srv) {
1946 if (h1c->conn->flags & CO_FL_PRIVATE)
1947 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
1948 else if (h1s->flags & H1S_F_NOT_FIRST)
1949 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1950 else
1951 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1952 }
1953 }
1954 }
1955
Christopher Faulet51dbc942018-09-13 09:05:15 +02001956 h1s_destroy(h1s);
1957
1958 /* We don't want to close right now unless the connection is in error */
1959 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01001960 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001961 h1_release(h1c->conn);
1962 else
1963 tasklet_wakeup(h1c->wait_event.task);
1964}
1965
1966
1967static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1968{
1969 struct h1s *h1s = cs->ctx;
1970
1971 if (!h1s)
1972 return;
1973
Christopher Fauletf2824e62018-10-01 12:12:37 +02001974 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1975 return;
1976
Christopher Faulet51dbc942018-09-13 09:05:15 +02001977 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1978 if (cs->flags & CS_FL_SHR)
1979 return;
1980 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
1981 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
1982 if (cs->flags & CS_FL_SHW) {
1983 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
1984 conn_full_close(cs->conn);
1985 }
1986}
1987
1988static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1989{
1990 struct h1s *h1s = cs->ctx;
1991 struct h1c *h1c;
1992
1993 if (!h1s)
1994 return;
1995 h1c = h1s->h1c;
1996
Christopher Fauletf2824e62018-10-01 12:12:37 +02001997 if ((h1s->flags & H1S_F_WANT_KAL) &&
1998 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
1999 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2000 return;
2001
Christopher Faulet51dbc942018-09-13 09:05:15 +02002002 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2003 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2004 return;
2005
2006 h1_shutw_conn(cs->conn);
2007}
2008
2009static void h1_shutw_conn(struct connection *conn)
2010{
2011 struct h1c *h1c = conn->mux_ctx;
2012
2013 if (conn_xprt_ready(conn) && conn->xprt->shutw)
2014 conn->xprt->shutw(conn, 1);
2015 if (!(conn->flags & CO_FL_SOCK_RD_SH))
2016 conn_sock_shutw(conn, 1);
2017 else {
2018 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
2019 conn_full_close(conn);
2020 }
2021}
2022
2023/* Called from the upper layer, to unsubscribe to events */
2024static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2025{
2026 struct wait_event *sw;
2027 struct h1s *h1s = cs->ctx;
2028
2029 if (!h1s)
2030 return 0;
2031
2032 if (event_type & SUB_CAN_RECV) {
2033 sw = param;
2034 if (h1s->recv_wait == sw) {
2035 sw->wait_reason &= ~SUB_CAN_RECV;
2036 h1s->recv_wait = NULL;
2037 }
2038 }
2039 if (event_type & SUB_CAN_SEND) {
2040 sw = param;
2041 if (h1s->send_wait == sw) {
2042 sw->wait_reason &= ~SUB_CAN_SEND;
2043 h1s->send_wait = NULL;
2044 }
2045 }
2046 return 0;
2047}
2048
2049/* Called from the upper layer, to subscribe to events, such as being able to send */
2050static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2051{
2052 struct wait_event *sw;
2053 struct h1s *h1s = cs->ctx;
2054
2055 if (!h1s)
2056 return -1;
2057
2058 switch (event_type) {
2059 case SUB_CAN_RECV:
2060 sw = param;
2061 if (!(sw->wait_reason & SUB_CAN_RECV)) {
2062 sw->wait_reason |= SUB_CAN_RECV;
2063 sw->handle = h1s;
2064 h1s->recv_wait = sw;
2065 }
2066 return 0;
2067 case SUB_CAN_SEND:
2068 sw = param;
2069 if (!(sw->wait_reason & SUB_CAN_SEND)) {
2070 sw->wait_reason |= SUB_CAN_SEND;
2071 sw->handle = h1s;
2072 h1s->send_wait = sw;
2073 }
2074 return 0;
2075 default:
2076 break;
2077 }
2078 return -1;
2079}
2080
2081/* Called from the upper layer, to receive data */
2082static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2083{
2084 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002085 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002086 size_t ret = 0;
2087
Christopher Faulet539e0292018-11-19 10:40:09 +01002088 if (!(h1c->flags & H1C_F_IN_ALLOC))
2089 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002090
2091 if (flags & CO_RFL_BUF_FLUSH)
2092 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002093 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2094 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01002095 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
2096 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002097 }
2098 return ret;
2099}
2100
2101
2102/* Called from the upper layer, to send data */
2103static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2104{
2105 struct h1s *h1s = cs->ctx;
2106 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002107 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002108
2109 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002110 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002111
2112 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002113 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2114 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002115
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002116 while (total != count) {
2117 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002118
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002119 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2120 ret = h1_process_output(h1c, buf, count);
2121 if (!ret)
2122 break;
2123 total += ret;
2124 if (!h1_send(h1c))
2125 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002126 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002127
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002128 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002129}
2130
Christopher Faulet1be55f92018-10-02 15:59:23 +02002131#if defined(CONFIG_HAP_LINUX_SPLICE)
2132/* Send and get, using splicing */
2133static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2134{
2135 struct h1s *h1s = cs->ctx;
2136 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2137 int ret = 0;
2138
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002139 if (b_data(&h1s->h1c->ibuf)) {
2140 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002141 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002142 }
2143
2144 h1s->flags &= ~H1S_F_BUF_FLUSH;
2145 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002146 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2147 count = h1m->curr_len;
2148 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2149 if (h1m->state == H1_MSG_DATA && ret > 0)
2150 h1m->curr_len -= ret;
2151 end:
2152 return ret;
2153
2154}
2155
2156static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2157{
2158 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002159 int ret = 0;
2160
2161 if (b_data(&h1s->h1c->obuf))
2162 goto end;
2163
2164 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002165 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002166 if (pipe->data) {
2167 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
2168 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
2169 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002170 return ret;
2171}
2172#endif
2173
Christopher Faulet51dbc942018-09-13 09:05:15 +02002174/****************************************/
2175/* MUX initialization and instanciation */
2176/****************************************/
2177
2178/* The mux operations */
2179const struct mux_ops mux_h1_ops = {
2180 .init = h1_init,
2181 .wake = h1_wake,
2182 .attach = h1_attach,
2183 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002184 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002185 .detach = h1_detach,
2186 .destroy = h1_destroy,
2187 .avail_streams = h1_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01002188 .max_streams = h1_max_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002189 .rcv_buf = h1_rcv_buf,
2190 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002191#if defined(CONFIG_HAP_LINUX_SPLICE)
2192 .rcv_pipe = h1_rcv_pipe,
2193 .snd_pipe = h1_snd_pipe,
2194#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002195 .subscribe = h1_subscribe,
2196 .unsubscribe = h1_unsubscribe,
2197 .shutr = h1_shutr,
2198 .shutw = h1_shutw,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002199 .reset = h1_reset,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002200 .flags = MX_FL_NONE,
2201 .name = "h1",
2202};
2203
2204
2205/* this mux registers default HTX proto */
2206static struct mux_proto_list mux_proto_htx =
2207{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2208
Willy Tarreau0108d902018-11-25 19:14:37 +01002209INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2210
Christopher Faulet51dbc942018-09-13 09:05:15 +02002211/*
2212 * Local variables:
2213 * c-indent-level: 8
2214 * c-basic-offset: 8
2215 * End:
2216 */