blob: 45d24c1836b9eee88641c1d731cb83eb9db6ba87 [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
Olivier Houchardf502aca2018-12-14 19:42:40 +010095 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +020096 struct h1m req;
97 struct h1m res;
98
99 enum http_meth_t meth; /* HTTP resquest method */
100 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200101};
102
103/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100104DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
105DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200106
Christopher Faulet51dbc942018-09-13 09:05:15 +0200107static int h1_recv(struct h1c *h1c);
108static int h1_send(struct h1c *h1c);
109static int h1_process(struct h1c *h1c);
110static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
111static void h1_shutw_conn(struct connection *conn);
112
113/*****************************************************/
114/* functions below are for dynamic buffer management */
115/*****************************************************/
116/*
117 * Indicates whether or not the we may call the h1_recv() function to
118 * attempt to receive data into the buffer and/or parse pending data. The
119 * condition is a bit complex due to some API limits for now. The rules are the
120 * following :
121 * - if an error or a shutdown was detected on the connection and the buffer
122 * is empty, we must not attempt to receive
123 * - if the input buffer failed to be allocated, we must not try to receive
124 * and we know there is nothing pending
125 * - if no flag indicates a blocking condition, we may attempt to receive,
126 * regardless of whether the input buffer is full or not, so that only de
127 * receiving part decides whether or not to block. This is needed because
128 * the connection API indeed prevents us from re-enabling receipt that is
129 * already enabled in a polled state, so we must always immediately stop as
130 * soon as the mux can't proceed so as never to hit an end of read with data
131 * pending in the buffers.
132 * - otherwise must may not attempt to receive
133 */
134static inline int h1_recv_allowed(const struct h1c *h1c)
135{
Christopher Fauletcf56b992018-12-11 16:12:31 +0100136 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200137 return 0;
138
Christopher Fauletcf56b992018-12-11 16:12:31 +0100139 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
140 return 0;
141
Christopher Fauletcb55f482018-12-10 11:56:47 +0100142 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200143 return 1;
144
145 return 0;
146}
147
148/*
149 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
150 * flags are used to figure what buffer was requested. It returns 1 if the
151 * allocation succeeds, in which case the connection is woken up, or 0 if it's
152 * impossible to wake up and we prefer to be woken up later.
153 */
154static int h1_buf_available(void *target)
155{
156 struct h1c *h1c = target;
157
158 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
159 h1c->flags &= ~H1C_F_IN_ALLOC;
160 if (h1_recv_allowed(h1c))
161 tasklet_wakeup(h1c->wait_event.task);
162 return 1;
163 }
164
165 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
166 h1c->flags &= ~H1C_F_OUT_ALLOC;
167 tasklet_wakeup(h1c->wait_event.task);
168 return 1;
169 }
170
Christopher Faulet51dbc942018-09-13 09:05:15 +0200171 return 0;
172}
173
174/*
175 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
176 */
177static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
178{
179 struct buffer *buf = NULL;
180
181 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
182 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
183 h1c->buf_wait.target = h1c;
184 h1c->buf_wait.wakeup_cb = h1_buf_available;
185 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
186 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
187 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
188 __conn_xprt_stop_recv(h1c->conn);
189 }
190 return buf;
191}
192
193/*
194 * Release a buffer, if any, and try to wake up entities waiting in the buffer
195 * wait queue.
196 */
197static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
198{
199 if (bptr->size) {
200 b_free(bptr);
201 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
202 }
203}
204
205static int h1_avail_streams(struct connection *conn)
206{
207 struct h1c *h1c = conn->mux_ctx;
208
209 return h1c->h1s ? 0 : 1;
210}
211
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100212static int h1_max_streams(struct connection *conn)
213{
214 return 1;
215}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200216
217/*****************************************************************/
218/* functions below are dedicated to the mux setup and management */
219/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100220static struct conn_stream *h1s_new_cs(struct h1s *h1s)
221{
222 struct conn_stream *cs;
223
224 cs = cs_new(h1s->h1c->conn);
225 if (!cs)
226 goto err;
227 h1s->cs = cs;
228 cs->ctx = h1s;
229
230 if (h1s->flags & H1S_F_NOT_FIRST)
231 cs->flags |= CS_FL_NOT_FIRST;
232
233 if (stream_create_from_cs(cs) < 0)
234 goto err;
235 return cs;
236
237 err:
238 cs_free(cs);
239 h1s->cs = NULL;
240 return NULL;
241}
242
Olivier Houchardf502aca2018-12-14 19:42:40 +0100243static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200244{
245 struct h1s *h1s;
246
247 h1s = pool_alloc(pool_head_h1s);
248 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100249 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200250
251 h1s->h1c = h1c;
252 h1c->h1s = h1s;
253
Olivier Houchardf502aca2018-12-14 19:42:40 +0100254 h1s->sess = sess;
255
Christopher Faulet51dbc942018-09-13 09:05:15 +0200256 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200257 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200258
259 h1s->recv_wait = NULL;
260 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200261
262 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200263 h1s->req.flags |= H1_MF_NO_PHDR;
264
Christopher Faulet129817b2018-09-20 16:14:40 +0200265 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200266 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200267
268 h1s->status = 0;
269 h1s->meth = HTTP_METH_OTHER;
270
Christopher Faulet47365272018-10-31 17:40:50 +0100271 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
272 h1s->flags |= H1S_F_NOT_FIRST;
273 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
274
Christopher Faulet129817b2018-09-20 16:14:40 +0200275 if (!conn_is_back(h1c->conn)) {
276 if (h1c->px->options2 & PR_O2_REQBUG_OK)
277 h1s->req.err_pos = -1;
278 }
279 else {
280 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
281 h1s->res.err_pos = -1;
282 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200283
Christopher Faulet539e0292018-11-19 10:40:09 +0100284 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
285 * create a new one.
286 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200287 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100288 h1s->csinfo.create_date = date;
289 h1s->csinfo.tv_create = now;
290 h1s->csinfo.t_handshake = 0;
291 h1s->csinfo.t_idle = -1;
292
Christopher Fauletf2824e62018-10-01 12:12:37 +0200293 cs->ctx = h1s;
294 h1s->cs = cs;
295 }
Christopher Faulet47365272018-10-31 17:40:50 +0100296 else {
Olivier Houchardf502aca2018-12-14 19:42:40 +0100297 /* For frontend connections we should always have a session */
298 sess = h1c->conn->owner;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100299
300 h1s->csinfo.create_date = sess->accept_date;
301 h1s->csinfo.tv_create = sess->tv_accept;
302 h1s->csinfo.t_handshake = sess->t_handshake;
303 h1s->csinfo.t_idle = -1;
304
Christopher Faulet47365272018-10-31 17:40:50 +0100305 cs = h1s_new_cs(h1s);
306 if (!cs)
307 goto fail;
308 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200309 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100310
311 fail:
312 pool_free(pool_head_h1s, h1s);
313 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200314}
315
316static void h1s_destroy(struct h1s *h1s)
317{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200318 if (h1s) {
319 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200320
Christopher Fauletf2824e62018-10-01 12:12:37 +0200321 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200322
Christopher Fauletf2824e62018-10-01 12:12:37 +0200323 if (h1s->recv_wait != NULL)
324 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
325 if (h1s->send_wait != NULL)
326 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
327
Christopher Fauletcb55f482018-12-10 11:56:47 +0100328 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100329 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
330 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
331 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200332
Christopher Fauletf2824e62018-10-01 12:12:37 +0200333 cs_free(h1s->cs);
334 pool_free(pool_head_h1s, h1s);
335 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200336}
337
Christopher Fauletfeb11742018-11-29 15:12:34 +0100338static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
339{
340 struct h1s *h1s = cs->ctx;
341
342 if (h1s && !conn_is_back(cs->conn))
343 return &h1s->csinfo;
344 return NULL;
345}
346
Christopher Faulet51dbc942018-09-13 09:05:15 +0200347/*
348 * Initialize the mux once it's attached. It is expected that conn->mux_ctx
349 * points to the existing conn_stream (for outgoing connections) or NULL (for
350 * incoming ones). Returns < 0 on error.
351 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100352static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200353{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200354 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200355
356 h1c = pool_alloc(pool_head_h1c);
357 if (!h1c)
358 goto fail_h1c;
359 h1c->conn = conn;
360 h1c->px = proxy;
361
362 h1c->flags = H1C_F_NONE;
363 h1c->ibuf = BUF_NULL;
364 h1c->obuf = BUF_NULL;
365 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200366
Christopher Faulet51dbc942018-09-13 09:05:15 +0200367 LIST_INIT(&h1c->buf_wait.list);
368 h1c->wait_event.task = tasklet_new();
369 if (!h1c->wait_event.task)
370 goto fail;
371 h1c->wait_event.task->process = h1_io_cb;
372 h1c->wait_event.task->context = h1c;
373 h1c->wait_event.wait_reason = 0;
374
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200375 if (!(conn->flags & CO_FL_CONNECTED))
376 h1c->flags |= H1C_F_CS_WAIT_CONN;
377
Christopher Fauletf2824e62018-10-01 12:12:37 +0200378 /* Always Create a new H1S */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100379 if (!h1s_create(h1c, conn->mux_ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200380 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200381
Christopher Faulet129817b2018-09-20 16:14:40 +0200382 conn->mux_ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200383
Christopher Faulet51dbc942018-09-13 09:05:15 +0200384 /* Try to read, if nothing is available yet we'll just subscribe */
385 if (h1_recv(h1c))
386 h1_process(h1c);
387
388 /* mux->wake will be called soon to complete the operation */
389 return 0;
390
391 fail:
Christopher Faulet47365272018-10-31 17:40:50 +0100392 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200393 tasklet_free(h1c->wait_event.task);
394 pool_free(pool_head_h1c, h1c);
395 fail_h1c:
396 return -1;
397}
398
399
400/* release function for a connection. This one should be called to free all
401 * resources allocated to the mux.
402 */
403static void h1_release(struct connection *conn)
404{
405 struct h1c *h1c = conn->mux_ctx;
406
407 LIST_DEL(&conn->list);
408
409 if (h1c) {
410 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
411 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
412 LIST_DEL(&h1c->buf_wait.list);
413 LIST_INIT(&h1c->buf_wait.list);
414 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
415 }
416
417 h1_release_buf(h1c, &h1c->ibuf);
418 h1_release_buf(h1c, &h1c->obuf);
419
Christopher Faulet51dbc942018-09-13 09:05:15 +0200420 if (h1c->wait_event.task)
421 tasklet_free(h1c->wait_event.task);
422
Christopher Fauletf2824e62018-10-01 12:12:37 +0200423 h1s_destroy(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200424 if (h1c->wait_event.wait_reason != 0)
425 conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason,
426 &h1c->wait_event);
427 pool_free(pool_head_h1c, h1c);
428 }
429
430 conn->mux = NULL;
431 conn->mux_ctx = NULL;
432
433 conn_stop_tracking(conn);
434 conn_full_close(conn);
435 if (conn->destroy_cb)
436 conn->destroy_cb(conn);
437 conn_free(conn);
438}
439
440/******************************************************/
441/* functions below are for the H1 protocol processing */
442/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200443/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
444 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200445 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100446static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200447{
Christopher Faulet570d1612018-11-26 11:13:57 +0100448 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200449
Christopher Faulet570d1612018-11-26 11:13:57 +0100450 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200451 (*(p + 5) > '1' ||
452 (*(p + 5) == '1' && *(p + 7) >= '1')))
453 h1m->flags |= H1_MF_VER_11;
454}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200455
Christopher Faulet9768c262018-10-22 09:34:31 +0200456/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
457 * greater or equal to 1.1
458 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100459static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200460{
Christopher Faulet570d1612018-11-26 11:13:57 +0100461 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200462
Christopher Faulet570d1612018-11-26 11:13:57 +0100463 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200464 (*(p + 5) > '1' ||
465 (*(p + 5) == '1' && *(p + 7) >= '1')))
466 h1m->flags |= H1_MF_VER_11;
467}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200468
Christopher Faulet9768c262018-10-22 09:34:31 +0200469/*
470 * Check the validity of the request version. If the version is valid, it
471 * returns 1. Otherwise, it returns 0.
472 */
473static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
474{
475 struct h1c *h1c = h1s->h1c;
476
477 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
478 * exactly one digit "." one digit. This check may be disabled using
479 * option accept-invalid-http-request.
480 */
481 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
482 if (sl.rq.v.len != 8)
483 return 0;
484
485 if (*(sl.rq.v.ptr + 4) != '/' ||
486 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
487 *(sl.rq.v.ptr + 6) != '.' ||
488 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
489 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200490 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200491 else if (!sl.rq.v.len) {
492 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200493
Christopher Faulet9768c262018-10-22 09:34:31 +0200494 /* RFC 1945 allows only GET for HTTP/0.9 requests */
495 if (sl.rq.meth != HTTP_METH_GET)
496 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200497
Christopher Faulet9768c262018-10-22 09:34:31 +0200498 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
499 if (!sl.rq.u.len)
500 return 0;
501
502 /* Add HTTP version */
503 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100504 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200505 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100506
507 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100508 ((*(sl.rq.v.ptr + 5) > '1') ||
509 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100510 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200511 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200512}
513
Christopher Faulet9768c262018-10-22 09:34:31 +0200514/*
515 * Check the validity of the response version. If the version is valid, it
516 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200517 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200518static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200519{
Christopher Faulet9768c262018-10-22 09:34:31 +0200520 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200521
Christopher Faulet9768c262018-10-22 09:34:31 +0200522 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
523 * exactly one digit "." one digit. This check may be disabled using
524 * option accept-invalid-http-request.
525 */
526 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
527 if (sl.st.v.len != 8)
528 return 0;
529
530 if (*(sl.st.v.ptr + 4) != '/' ||
531 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
532 *(sl.st.v.ptr + 6) != '.' ||
533 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
534 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200535 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100536
537 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100538 ((*(sl.st.v.ptr + 5) > '1') ||
539 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100540 h1m->flags |= H1_MF_VER_11;
541
Christopher Faulet9768c262018-10-22 09:34:31 +0200542 return 1;
543}
544/* Remove all "Connection:" headers from the HTX message <htx> */
545static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
546{
547 struct ist hdr = {.ptr = "Connection", .len = 10};
548 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200549
Christopher Faulet9768c262018-10-22 09:34:31 +0200550 while (http_find_header(htx, hdr, &ctx, 1))
551 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200552
Christopher Faulet9768c262018-10-22 09:34:31 +0200553 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
554}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200555
Christopher Faulet9768c262018-10-22 09:34:31 +0200556/* Add a "Connection:" header with the value <value> into the HTX message
557 * <htx>.
558 */
559static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
560{
561 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200562
Christopher Faulet9768c262018-10-22 09:34:31 +0200563 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200564}
565
566/* Deduce the connection mode of the client connection, depending on the
567 * configuration and the H1 message flags. This function is called twice, the
568 * first time when the request is parsed and the second time when the response
569 * is parsed.
570 */
571static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
572{
573 struct proxy *fe = h1s->h1c->px;
574 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
575
576 /* Tunnel mode can only by set on the frontend */
577 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
578 flag = H1S_F_WANT_TUN;
579 else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
580 flag = H1S_F_WANT_CLO;
581
582 /* flags order: CLO > SCL > TUN > KAL */
583 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
584 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
585
586 if (h1m->flags & H1_MF_RESP) {
587 /* Either we've established an explicit tunnel, or we're
588 * switching the protocol. In both cases, we're very unlikely to
589 * understand the next protocols. We have to switch to tunnel
590 * mode, so that we transfer the request and responses then let
591 * this protocol pass unmodified. When we later implement
592 * specific parsers for such protocols, we'll want to check the
593 * Upgrade header which contains information about that protocol
594 * for responses with status 101 (eg: see RFC2817 about TLS).
595 */
596 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
597 h1s->status == 101)
598 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletbd44ca62018-11-29 09:55:22 +0100599 else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */
600 (h1m->flags & H1_MF_CONN_CLO && h1s->req.state != H1_MSG_DONE)) /*explicit close and unfinished request */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200601 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
602 }
603 else {
604 if (h1s->flags & H1S_F_WANT_KAL &&
605 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
606 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
607 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
608 }
609
610 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
611 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
612 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
613}
614
615/* Deduce the connection mode of the client connection, depending on the
616 * configuration and the H1 message flags. This function is called twice, the
617 * first time when the request is parsed and the second time when the response
618 * is parsed.
619 */
620static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
621{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100622 struct h1c *h1c = h1s->h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100623 struct session *sess = h1s->sess;
Christopher Fauleta1692f52018-11-22 10:19:50 +0100624 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200625 int flag = H1S_F_WANT_KAL;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100626 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200627
628 /* Tunnel mode can only by set on the frontend */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100629 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200630 flag = H1S_F_WANT_TUN;
631
632 /* For the server connection: server-close == httpclose */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100633 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200634 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Olivier Houchardf502aca2018-12-14 19:42:40 +0100635 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200636 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
637 flag = H1S_F_WANT_CLO;
638
639 /* flags order: CLO > SCL > TUN > KAL */
640 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
641 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
642
643 if (h1m->flags & H1_MF_RESP) {
644 /* Either we've established an explicit tunnel, or we're
645 * switching the protocol. In both cases, we're very unlikely to
646 * understand the next protocols. We have to switch to tunnel
647 * mode, so that we transfer the request and responses then let
648 * this protocol pass unmodified. When we later implement
649 * specific parsers for such protocols, we'll want to check the
650 * Upgrade header which contains information about that protocol
651 * for responses with status 101 (eg: see RFC2817 about TLS).
652 */
653 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
654 h1s->status == 101)
655 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
656 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
657 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
658 else if (h1s->flags & H1S_F_WANT_KAL &&
659 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
660 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
661 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
662 }
Christopher Faulet70033782018-12-05 13:50:11 +0100663 else {
664 if (h1s->flags & H1S_F_WANT_KAL &&
665 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
666 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
667 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
668 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200669
670 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
671 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
672 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200673}
674
Christopher Faulet9768c262018-10-22 09:34:31 +0200675static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
676 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200677{
678 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200679
680 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
681 * token is found
682 */
683 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200684 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200685
686 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200687 if (h1m->flags & H1_MF_CONN_CLO) {
688 if (conn_val)
689 *conn_val = ist("");
690 if (htx)
691 h1_remove_conn_hdrs(h1m, htx);
692 }
693 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
694 if (conn_val)
695 *conn_val = ist("keep-alive");
696 if (htx)
697 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
698 }
Christopher Fauletce851492018-12-07 18:06:59 +0100699 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
700 if (conn_val)
701 *conn_val = ist("");
702 if (htx)
703 h1_remove_conn_hdrs(h1m, htx);
704 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200705 }
706 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200707 if (h1m->flags & H1_MF_CONN_KAL) {
708 if (conn_val)
709 *conn_val = ist("");
710 if (htx)
711 h1_remove_conn_hdrs(h1m, htx);
712 }
713 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
714 if (conn_val)
715 *conn_val = ist("close");
716 if (htx)
717 h1_add_conn_hdr(h1m, htx, ist("close"));
718 }
Christopher Fauletce851492018-12-07 18:06:59 +0100719 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
720 if (conn_val)
721 *conn_val = ist("");
722 if (htx)
723 h1_remove_conn_hdrs(h1m, htx);
724 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200725 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200726}
727
Christopher Faulet9768c262018-10-22 09:34:31 +0200728static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
729 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200730{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200731 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
732 * token is found
733 */
734 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200735 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200736
737 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200738 if (h1m->flags & H1_MF_CONN_CLO) {
739 if (conn_val)
740 *conn_val = ist("");
741 if (htx)
742 h1_remove_conn_hdrs(h1m, htx);
743 }
744 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
745 if (conn_val)
746 *conn_val = ist("keep-alive");
747 if (htx)
748 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
749 }
Christopher Fauletce851492018-12-07 18:06:59 +0100750 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
751 if (conn_val)
752 *conn_val = ist("");
753 if (htx)
754 h1_remove_conn_hdrs(h1m, htx);
755 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200756 }
757 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200758 if (h1m->flags & H1_MF_CONN_KAL) {
759 if (conn_val)
760 *conn_val = ist("");
761 if (htx)
762 h1_remove_conn_hdrs(h1m, htx);
763 }
764 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
765 if (conn_val)
766 *conn_val = ist("close");
767 if (htx)
768 h1_add_conn_hdr(h1m, htx, ist("close"));
769 }
Christopher Fauletce851492018-12-07 18:06:59 +0100770 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
771 if (conn_val)
772 *conn_val = ist("");
773 if (htx)
774 h1_remove_conn_hdrs(h1m, htx);
775 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200776 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200777}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200778
Christopher Faulet9768c262018-10-22 09:34:31 +0200779/* Set the right connection mode and update "Connection:" header if
780 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
781 * message is updated accordingly. When <conn_val> is not NULL, it is set with
782 * the new header value.
783 */
784static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
785 struct htx *htx, struct ist *conn_val)
786{
787 if (!conn_is_back(h1s->h1c->conn)) {
788 h1_set_cli_conn_mode(h1s, h1m);
789 if (h1m->flags & H1_MF_RESP)
790 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
791 }
792 else {
793 h1_set_srv_conn_mode(h1s, h1m);
794 if (!(h1m->flags & H1_MF_RESP))
795 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
796 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200797}
798
Christopher Faulete44769b2018-11-29 23:01:45 +0100799
800/* Append the description of what is present in error snapshot <es> into <out>.
801 * The description must be small enough to always fit in a buffer. The output
802 * buffer may be the trash so the trash must not be used inside this function.
803 */
804static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
805{
806 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100807 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
808 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
809 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
810 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
811 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
812 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100813}
814/*
815 * Capture a bad request or response and archive it in the proxy's structure.
816 * By default it tries to report the error position as h1m->err_pos. However if
817 * this one is not set, it will then report h1m->next, which is the last known
818 * parsing point. The function is able to deal with wrapping buffers. It always
819 * displays buffers as a contiguous area starting at buf->p. The direction is
820 * determined thanks to the h1m's flags.
821 */
822static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
823 struct h1m *h1m, struct buffer *buf)
824{
825 struct session *sess = h1c->conn->owner;
826 struct proxy *proxy = h1c->px;
827 struct proxy *other_end = sess->fe;
828 union error_snapshot_ctx ctx;
829
830 if (h1s->cs->data)
831 other_end = si_strm(h1s->cs->data)->be;
832
833 /* http-specific part now */
834 ctx.h1.state = h1m->state;
835 ctx.h1.c_flags = h1c->flags;
836 ctx.h1.s_flags = h1s->flags;
837 ctx.h1.m_flags = h1m->flags;
838 ctx.h1.m_clen = h1m->curr_len;
839 ctx.h1.m_blen = h1m->body_len;
840
841 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
842 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100843 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
844 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100845}
846
Christopher Fauletadb22202018-12-12 10:32:09 +0100847/* Emit the chunksize followed by a CRLF in front of data of the buffer
848 * <buf>. It goes backwards and starts with the byte before the buffer's
849 * head. The caller is responsible for ensuring there is enough room left before
850 * the buffer's head for the string.
851 */
852static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
853{
854 char *beg, *end;
855
856 beg = end = b_head(buf);
857 *--beg = '\n';
858 *--beg = '\r';
859 do {
860 *--beg = hextab[chksz & 0xF];
861 } while (chksz >>= 4);
862 buf->head -= (end - beg);
863 b_add(buf, end - beg);
864}
865
866/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
867 * ensuring there is enough room left in the buffer for the string. */
868static void h1_emit_chunk_crlf(struct buffer *buf)
869{
870 *(b_peek(buf, b_data(buf))) = '\r';
871 *(b_peek(buf, b_data(buf) + 1)) = '\n';
872 b_add(buf, 2);
873}
874
Christopher Faulet129817b2018-09-20 16:14:40 +0200875/*
876 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200877 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
878 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800879 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200880 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200881static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200882 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200883{
884 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100885 union h1_sl h1sl;
886 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200887 int ret = 0;
888
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100889 if (!max)
890 goto end;
891
Christopher Faulet129817b2018-09-20 16:14:40 +0200892 /* Realing input buffer if necessary */
893 if (b_head(buf) + b_data(buf) > b_wrap(buf))
894 b_slow_realign(buf, trash.area, 0);
895
Christopher Fauletf2824e62018-10-01 12:12:37 +0200896 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100897 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200898 if (ret <= 0) {
899 /* Incomplete or invalid message. If the buffer is full, it's an
900 * error because headers are too large to be handled by the
901 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200902 if (ret < 0 || (!ret && b_full(buf)))
903 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200904 goto end;
905 }
906
907 /* messages headers fully parsed, do some checks to prepare the body
908 * parsing.
909 */
910
911 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200912 if (ret > (b_size(buf) - global.tune.maxrewrite))
913 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200914
Christopher Faulet9768c262018-10-22 09:34:31 +0200915 /* Save the request's method or the response's status, check if the body
916 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +0200917 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100918 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +0200919
Christopher Faulet129817b2018-09-20 16:14:40 +0200920 /* Request have always a known length */
921 h1m->flags |= H1_MF_XFER_LEN;
922 if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len)
923 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +0200924
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100925 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
926 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200927 h1m->err_state = h1m->state;
928 goto vsn_error;
929 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200930 }
931 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100932 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +0200933
934 if ((h1s->meth == HTTP_METH_HEAD) ||
935 (h1s->status >= 100 && h1s->status < 200) ||
936 (h1s->status == 204) || (h1s->status == 304) ||
937 (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) {
938 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
939 h1m->flags |= H1_MF_XFER_LEN;
940 h1m->curr_len = h1m->body_len = 0;
941 h1m->state = H1_MSG_DONE;
942 }
943 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
944 h1m->flags |= H1_MF_XFER_LEN;
945 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
946 h1m->state = H1_MSG_DONE;
947 }
948 else
949 h1m->state = H1_MSG_TUNNEL;
Christopher Faulet9768c262018-10-22 09:34:31 +0200950
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100951 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
952 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +0200953 h1m->err_state = h1m->state;
954 goto vsn_error;
955 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200956 }
957
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100958 /* Set HTX start-line flags */
959 if (h1m->flags & H1_MF_VER_11)
960 flags |= HTX_SL_F_VER_11;
961 if (h1m->flags & H1_MF_XFER_ENC)
962 flags |= HTX_SL_F_XFER_ENC;
963 if (h1m->flags & H1_MF_XFER_LEN) {
964 flags |= HTX_SL_F_XFER_LEN;
965 if (h1m->flags & H1_MF_CHNK)
966 flags |= HTX_SL_F_CHNK;
967 else if (h1m->flags & H1_MF_CLEN)
968 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100969 if (h1m->state == H1_MSG_DONE)
970 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100971 }
972
Christopher Faulet9768c262018-10-22 09:34:31 +0200973 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100974 struct htx_sl *sl;
975
976 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
977 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200978 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100979 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200980 }
981 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100982 struct htx_sl *sl;
983
984 flags |= HTX_SL_F_IS_RESP;
985 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
986 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +0200987 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100988 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200989 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100990
Christopher Faulet9768c262018-10-22 09:34:31 +0200991 if (h1m->state == H1_MSG_DONE)
992 if (!htx_add_endof(htx, HTX_BLK_EOM))
993 goto error;
994
995 h1_process_conn_mode(h1s, h1m, htx, NULL);
996
997 /* If body length cannot be determined, set htx->extra to
998 * ULLONG_MAX. This value is impossible in other cases.
999 */
1000 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
1001
1002 /* Recheck there is enough space to do headers rewritting */
1003 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
1004 goto error;
1005
1006 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001007 end:
1008 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001009
1010 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001011 h1m->err_state = h1m->state;
1012 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001013 vsn_error:
1014 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +01001015 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001016 ret = 0;
1017 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001018}
1019
1020/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001021 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1022 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001023 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001024 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001026static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001027 struct buffer *buf, size_t *ofs, size_t max,
1028 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001029{
Christopher Faulet9768c262018-10-22 09:34:31 +02001030 uint32_t data_space = htx_free_data_space(htx);
Christopher Faulet129817b2018-09-20 16:14:40 +02001031 size_t total = 0;
1032 int ret = 0;
1033
1034 if (h1m->flags & H1_MF_XFER_LEN) {
1035 if (h1m->flags & H1_MF_CLEN) {
1036 /* content-length: read only h2m->body_len */
1037 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001038 if (ret > data_space)
1039 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001040 if ((uint64_t)ret > h1m->curr_len)
1041 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001042 if (ret > b_contig_data(buf, *ofs))
1043 ret = b_contig_data(buf, *ofs);
1044 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001045 /* very often with large files we'll face the following
1046 * situation :
1047 * - htx is empty and points to <htxbuf>
1048 * - ret == buf->data
1049 * - buf->head == sizeof(struct htx)
1050 * => we can swap the buffers and place an htx header into
1051 * the target buffer instead
1052 */
1053 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1054 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1055 void *raw_area = buf->area;
1056 void *htx_area = htxbuf->area;
1057 struct htx_blk *blk;
1058
1059 buf->area = htx_area;
1060 htxbuf->area = raw_area;
1061 htx = (struct htx *)htxbuf->area;
1062 htx->size = htxbuf->size - sizeof(*htx);
1063 htx_reset(htx);
1064 b_set_data(htxbuf, b_size(htxbuf));
1065
1066 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1067 blk->info += ret;
1068 /* nothing else to do, the old buffer now contains an
1069 * empty pre-initialized HTX header
1070 */
1071 }
1072 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001073 goto end;
1074 h1m->curr_len -= ret;
1075 *ofs += ret;
1076 total += ret;
1077 }
1078
1079 if (!h1m->curr_len) {
1080 if (!htx_add_endof(htx, HTX_BLK_EOM))
1081 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001082 h1m->state = H1_MSG_DONE;
Christopher Faulet9768c262018-10-22 09:34:31 +02001083 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001084 }
1085 else if (h1m->flags & H1_MF_CHNK) {
1086 new_chunk:
1087 /* te:chunked : parse chunks */
1088 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001089 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001090 if (ret <= 0)
1091 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001092 h1m->state = H1_MSG_CHUNK_SIZE;
1093
Christopher Faulet129817b2018-09-20 16:14:40 +02001094 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001095 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001096 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001097 }
1098
1099 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1100 unsigned int chksz;
1101
Christopher Fauletf2824e62018-10-01 12:12:37 +02001102 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001103 if (ret <= 0)
1104 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001105 if (!chksz) {
1106 if (!htx_add_endof(htx, HTX_BLK_EOD))
1107 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001108 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001109 h1m->state = H1_MSG_TRAILERS;
1110 }
1111 else
1112 h1m->state = H1_MSG_DATA;
1113
Christopher Faulet129817b2018-09-20 16:14:40 +02001114 h1m->curr_len = chksz;
1115 h1m->body_len += chksz;
1116 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001117 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001118 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001119 }
1120
1121 if (h1m->state == H1_MSG_DATA) {
1122 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001123 if (ret > data_space)
1124 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001125 if ((uint64_t)ret > h1m->curr_len)
1126 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001127 if (ret > b_contig_data(buf, *ofs))
1128 ret = b_contig_data(buf, *ofs);
1129 if (ret) {
1130 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1131 goto end;
1132 h1m->curr_len -= ret;
1133 max -= ret;
1134 *ofs += ret;
1135 total += ret;
1136 }
1137 if (!h1m->curr_len) {
1138 h1m->state = H1_MSG_CHUNK_CRLF;
1139 goto new_chunk;
1140 }
1141 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001142 }
1143
1144 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001145 /* Trailers were alread parsed, only the EOM
1146 * need to be added */
1147 if (h1s->flags & H1S_F_HAVE_TLR)
1148 goto skip_tlr_parsing;
1149
Christopher Fauletf2824e62018-10-01 12:12:37 +02001150 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001151 if (ret > data_space)
1152 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001153 if (ret <= 0)
1154 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001155
1156 /* Realing input buffer if tailers wrap. For now
1157 * this is a workaroung. Because trailers are
1158 * not split on CRLF, like headers, there is no
1159 * way to know where to split it when trailers
1160 * wrap. This is a limitation of
1161 * h1_measure_trailers.
1162 */
1163 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1164 b_slow_realign(buf, trash.area, 0);
1165
1166 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1167 goto end;
Christopher Faulet17276482018-11-22 11:44:35 +01001168 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001169 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001170 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001171 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001172
Christopher Faulet17276482018-11-22 11:44:35 +01001173 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001174 if (!htx_add_endof(htx, HTX_BLK_EOM))
1175 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001176 h1m->state = H1_MSG_DONE;
1177 }
1178 }
1179 else {
1180 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1181 * is no body. Switch the message in DONE state
1182 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001183 if (!htx_add_endof(htx, HTX_BLK_EOM))
1184 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001185 h1m->state = H1_MSG_DONE;
1186 }
1187 }
1188 else {
1189 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001190 ret = max;
1191 if (ret > data_space)
1192 ret = data_space;
1193 if (ret > b_contig_data(buf, *ofs))
1194 ret = b_contig_data(buf, *ofs);
1195 if (ret) {
1196 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1197 goto end;
1198
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001199 *ofs += ret;
1200 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001201 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001202 }
1203
1204 end:
1205 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001206 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001207 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001208 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001209 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001210 return 0;
1211 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001212 /* update htx->extra, only when the body length is known */
1213 if (h1m->flags & H1_MF_XFER_LEN)
1214 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001215 return total;
1216}
1217
1218/*
1219 * Synchronize the request and the response before reseting them. Except for 1xx
1220 * responses, we wait that the request and the response are in DONE state and
1221 * that all data are forwarded for both. For 1xx responses, only the response is
1222 * reset, waiting the final one. Many 1xx messages can be sent.
1223 */
1224static void h1_sync_messages(struct h1c *h1c)
1225{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001226 struct h1s *h1s = h1c->h1s;
1227
1228 if (!h1s)
Christopher Faulet129817b2018-09-20 16:14:40 +02001229 return;
1230
Christopher Fauletf2824e62018-10-01 12:12:37 +02001231 if (h1s->res.state == H1_MSG_DONE &&
1232 (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) &&
Christopher Faulet539e0292018-11-19 10:40:09 +01001233 (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) {
Christopher Faulet129817b2018-09-20 16:14:40 +02001234 /* For 100-Continue response or any other informational 1xx
1235 * response which is non-final, don't reset the request, the
1236 * transaction is not finished. We take care the response was
1237 * transferred before.
1238 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001239 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001241 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet129817b2018-09-20 16:14:40 +02001242 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001243 else if (!b_data(&h1c->obuf) &&
Christopher Fauletf2824e62018-10-01 12:12:37 +02001244 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
1245 if (h1s->flags & H1S_F_WANT_TUN) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001246 h1m_init_req(&h1s->req);
1247 h1m_init_res(&h1s->res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001248 h1s->req.state = H1_MSG_TUNNEL;
1249 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001250 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001251 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001252 }
1253}
1254
1255/*
1256 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001257 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1258 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001259 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001260static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001261{
Christopher Faulet539e0292018-11-19 10:40:09 +01001262 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001263 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001264 struct htx *htx;
Christopher Faulet129817b2018-09-20 16:14:40 +02001265 size_t total = 0;
1266 size_t ret = 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001267 size_t count, max;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001268 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001269
Christopher Faulet539e0292018-11-19 10:40:09 +01001270 htx = htx_from_buf(buf);
1271 count = b_data(&h1c->ibuf);
1272 max = htx_free_space(htx);
1273 if (flags & CO_RFL_KEEP_RSV) {
1274 if (max < global.tune.maxrewrite)
1275 goto end;
1276 max -= global.tune.maxrewrite;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001277 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001278 if (count > max)
1279 count = max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001280
Christopher Fauletf2824e62018-10-01 12:12:37 +02001281 if (!conn_is_back(h1c->conn)) {
1282 h1m = &h1s->req;
1283 errflag = H1S_F_REQ_ERROR;
1284 }
1285 else {
1286 h1m = &h1s->res;
1287 errflag = H1S_F_RES_ERROR;
1288 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001289
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001290 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001291 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001292 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001293 if (!ret)
1294 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 }
1296 else if (h1m->state <= H1_MSG_TRAILERS) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001297 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1298 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001299 if (!ret)
1300 break;
1301 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001302 else if (h1m->state == H1_MSG_DONE) {
1303 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001304 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001305 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001306 else if (h1m->state == H1_MSG_TUNNEL) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001307 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1308 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001309 if (!ret)
1310 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001311 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001312 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001313 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001314 break;
1315 }
1316
Christopher Faulet539e0292018-11-19 10:40:09 +01001317 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001318 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001319
Christopher Faulet47365272018-10-31 17:40:50 +01001320 if (h1s->flags & errflag)
1321 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001322
Christopher Faulet539e0292018-11-19 10:40:09 +01001323 b_del(&h1c->ibuf, total);
1324
1325 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001326 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001327
Willy Tarreau45f2b892018-12-05 07:59:27 +01001328 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001329 h1c->flags &= ~H1C_F_IN_FULL;
1330 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001331 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001332
Christopher Fauletcf56b992018-12-11 16:12:31 +01001333 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1334
1335 if (!b_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001336 h1_release_buf(h1c, &h1c->ibuf);
1337 h1_sync_messages(h1c);
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001338 }
Christopher Fauletcf56b992018-12-11 16:12:31 +01001339 else if (!htx_is_empty(htx))
1340 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001341
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001342 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1343 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet539e0292018-11-19 10:40:09 +01001344 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001345
Christopher Faulet539e0292018-11-19 10:40:09 +01001346 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001347
1348 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001349 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001350 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001351 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001352 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001353 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001354}
1355
Christopher Faulet129817b2018-09-20 16:14:40 +02001356/*
1357 * Process outgoing data. It parses data and transfer them from the channel buffer into
1358 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1359 * 0 if it couldn't proceed.
1360 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001361static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1362{
Christopher Faulet129817b2018-09-20 16:14:40 +02001363 struct h1s *h1s = h1c->h1s;
1364 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001365 struct htx *chn_htx;
1366 struct htx_blk *blk;
1367 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001368 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001369 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001370 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001371
Christopher Faulet47365272018-10-31 17:40:50 +01001372 if (!count)
1373 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001374
Christopher Faulet9768c262018-10-22 09:34:31 +02001375 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001376 if (htx_is_empty(chn_htx))
1377 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001378
Christopher Faulet51dbc942018-09-13 09:05:15 +02001379 if (!h1_get_buf(h1c, &h1c->obuf)) {
1380 h1c->flags |= H1C_F_OUT_ALLOC;
1381 goto end;
1382 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001383
Christopher Fauletf2824e62018-10-01 12:12:37 +02001384 if (!conn_is_back(h1c->conn)) {
1385 h1m = &h1s->res;
1386 errflag = H1S_F_RES_ERROR;
1387 }
1388 else {
1389 h1m = &h1s->req;
1390 errflag = H1S_F_REQ_ERROR;
1391 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001392
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001393 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001394 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001395
1396 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001397
Willy Tarreau3815b222018-12-11 19:50:43 +01001398 /* Perform some optimizations to reduce the number of buffer copies.
1399 * First, if the mux's buffer is empty and the htx area contains
1400 * exactly one data block of the same size as the requested count,
1401 * then it's possible to simply swap the caller's buffer with the
1402 * mux's output buffer and adjust offsets and length to match the
1403 * entire DATA HTX block in the middle. In this case we perform a
1404 * true zero-copy operation from end-to-end. This is the situation
1405 * that happens all the time with large files. Second, if this is not
1406 * possible, but the mux's output buffer is empty, we still have an
1407 * opportunity to avoid the copy to the intermediary buffer, by making
1408 * the intermediary buffer's area point to the output buffer's area.
1409 * In this case we want to skip the HTX header to make sure that copies
1410 * remain aligned and that this operation remains possible all the
1411 * time. This goes for headers, data blocks and any data extracted from
1412 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001413 */
1414 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001415 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001416
1417 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001418 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001419 htx_get_blk_value(chn_htx, blk).len == count) {
1420 void *old_area = h1c->obuf.area;
1421
1422 h1c->obuf.area = buf->area;
1423 h1c->obuf.data = count;
1424
1425 buf->area = old_area;
1426 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001427
1428 /* The message is chunked. We need to emit the chunk
1429 * size. We have at least the size of the struct htx to
1430 * write the chunk envelope. It should be enough.
1431 */
1432 if (h1m->flags & H1_MF_CHNK) {
1433 h1_emit_chunk_size(&h1c->obuf, count);
1434 h1_emit_chunk_crlf(&h1c->obuf);
1435 }
1436
Willy Tarreau3815b222018-12-11 19:50:43 +01001437 total += count;
1438 goto out;
1439 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001440 tmp->area = h1c->obuf.area + h1c->obuf.head;
1441 }
1442
Christopher Faulet9768c262018-10-22 09:34:31 +02001443 tmp->size = b_room(&h1c->obuf);
1444
Christopher Fauletb2e84162018-12-06 11:39:49 +01001445 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001446 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001447 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001448 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001449 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001450 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001451
Christopher Fauletb2e84162018-12-06 11:39:49 +01001452 vlen = sz;
1453 if (vlen > count) {
1454 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1455 goto copy;
1456 vlen = count;
1457 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001458
Christopher Fauletb2e84162018-12-06 11:39:49 +01001459 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001460 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001461 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001462
1463 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001464 h1m_init_req(h1m);
1465 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001466 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001467 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001468 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001469 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001470 goto copy;
1471 h1m->flags |= H1_MF_XFER_LEN;
1472 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001473 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001474
Christopher Faulet9768c262018-10-22 09:34:31 +02001475 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001476 h1m_init_res(h1m);
1477 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001479 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001480 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001481 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001482 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001483 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001484 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001485 if (sl->info.res.status < 200 &&
1486 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1487 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001488 h1m->state = H1_MSG_HDR_FIRST;
1489 break;
1490
1491 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001492 h1m->state = H1_MSG_HDR_NAME;
1493 n = htx_get_blk_name(chn_htx, blk);
1494 v = htx_get_blk_value(chn_htx, blk);
1495
1496 if (isteqi(n, ist("transfer-encoding")))
1497 h1_parse_xfer_enc_header(h1m, v);
1498 else if (isteqi(n, ist("connection"))) {
1499 h1_parse_connection_header(h1m, v);
1500 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001501 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001502 if (!v.len)
1503 goto skip_hdr;
1504 }
1505
Christopher Fauletc59ff232018-12-03 13:58:44 +01001506 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001507 goto copy;
1508 skip_hdr:
1509 h1m->state = H1_MSG_HDR_L2_LWS;
1510 break;
1511
1512 case HTX_BLK_PHDR:
1513 /* not implemented yet */
1514 h1m->flags |= errflag;
1515 break;
1516
1517 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001518 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001519 /* There is no "Connection:" header and
1520 * it the conn_mode must be
1521 * processed. So do it */
1522 n = ist("Connection");
1523 v = ist("");
1524 h1_process_conn_mode(h1s, h1m, NULL, &v);
1525 process_conn_mode = 0;
1526 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001527 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001528 goto copy;
1529 }
1530 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001531 h1m->state = H1_MSG_LAST_LF;
1532 if (!chunk_memcat(tmp, "\r\n", 2))
1533 goto copy;
1534
1535 h1m->state = H1_MSG_DATA;
1536 break;
1537
1538 case HTX_BLK_DATA:
1539 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001540 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001541 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001542 goto copy;
1543 break;
1544
1545 case HTX_BLK_EOD:
1546 if (!chunk_memcat(tmp, "0\r\n", 3))
1547 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001548 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001549 h1m->state = H1_MSG_TRAILERS;
1550 break;
1551
1552 case HTX_BLK_TLR:
Christopher Faulet32188212018-11-20 18:21:43 +01001553 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1554 if (!chunk_memcat(tmp, "0\r\n", 3))
1555 goto copy;
1556 h1s->flags |= H1S_F_HAVE_EOD;
1557 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001558 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001559 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001560 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001561 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001562 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001563 break;
1564
1565 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001566 if ((h1m->flags & H1_MF_CHNK)) {
1567 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1568 if (!chunk_memcat(tmp, "0\r\n", 3))
1569 goto copy;
1570 h1s->flags |= H1S_F_HAVE_EOD;
1571 }
1572 if (!(h1s->flags & H1S_F_HAVE_TLR)) {
1573 if (!chunk_memcat(tmp, "\r\n", 2))
1574 goto copy;
1575 h1s->flags |= H1S_F_HAVE_TLR;
1576 }
1577 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001578 h1m->state = H1_MSG_DONE;
1579 break;
1580
1581 case HTX_BLK_OOB:
1582 v = htx_get_blk_value(chn_htx, blk);
1583 if (!chunk_memcat(tmp, v.ptr, v.len))
1584 goto copy;
1585 break;
1586
1587 default:
1588 h1m->flags |= errflag;
1589 break;
1590 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001591 total += vlen;
1592 count -= vlen;
1593 if (sz == vlen)
1594 blk = htx_remove_blk(chn_htx, blk);
1595 else {
1596 htx_cut_data_blk(chn_htx, blk, vlen);
1597 break;
1598 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001599 }
1600
Christopher Faulet9768c262018-10-22 09:34:31 +02001601 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001602 /* when the output buffer is empty, tmp shares the same area so that we
1603 * only have to update pointers and lengths.
1604 */
1605 if (tmp->area == h1c->obuf.area)
1606 h1c->obuf.data = tmp->data;
1607 else
1608 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001609
Willy Tarreau3815b222018-12-11 19:50:43 +01001610 htx_to_buf(chn_htx, buf);
1611 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001612 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001613 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001614 end:
1615 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001616}
1617
Christopher Faulet51dbc942018-09-13 09:05:15 +02001618/*********************************************************/
1619/* functions below are I/O callbacks from the connection */
1620/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001621static void h1_wake_stream_for_recv(struct h1s *h1s)
1622{
1623 if (h1s && h1s->recv_wait) {
1624 h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1625 tasklet_wakeup(h1s->recv_wait->task);
1626 h1s->recv_wait = NULL;
1627 }
1628}
1629static void h1_wake_stream_for_send(struct h1s *h1s)
1630{
1631 if (h1s && h1s->send_wait) {
1632 h1s->send_wait->wait_reason &= ~SUB_CAN_SEND;
1633 tasklet_wakeup(h1s->send_wait->task);
1634 h1s->send_wait = NULL;
1635 }
1636}
1637
Christopher Faulet51dbc942018-09-13 09:05:15 +02001638/*
1639 * Attempt to read data, and subscribe if none available
1640 */
1641static int h1_recv(struct h1c *h1c)
1642{
1643 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001644 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001645 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001646 int rcvd = 0;
1647
1648 if (h1c->wait_event.wait_reason & SUB_CAN_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001649 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001650
Olivier Houchard75159a92018-12-03 18:46:09 +01001651 if (!h1_recv_allowed(h1c)) {
1652 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001653 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001654 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001655
Christopher Fauletfeb11742018-11-29 15:12:34 +01001656 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001657 rcvd = 1;
1658 goto end;
1659 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001660
Christopher Faulet51dbc942018-09-13 09:05:15 +02001661 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1662 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001663 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001664 }
1665
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001666 /*
1667 * If we only have a small amount of data, realign it,
1668 * it's probably cheaper than doing 2 recv() calls.
1669 */
1670 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1671 b_slow_realign(&h1c->ibuf, trash.area, 0);
1672
Willy Tarreau45f2b892018-12-05 07:59:27 +01001673 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001674 if (max) {
1675 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001676
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001677 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001678 if (!b_data(&h1c->ibuf)) {
1679 /* try to pre-align the buffer like the rxbufs will be
1680 * to optimize memory copies.
1681 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001682 h1c->ibuf.head = sizeof(struct htx);
1683 }
Willy Tarreauc0960d12018-12-14 10:59:15 +01001684 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001685 }
Christopher Faulet47365272018-10-31 17:40:50 +01001686 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001687 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001688 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001689 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001690 if (h1s->csinfo.t_idle == -1)
1691 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1692 }
Christopher Faulet47365272018-10-31 17:40:50 +01001693 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001694
Christopher Fauletcf56b992018-12-11 16:12:31 +01001695 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001696 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001697 goto end;
1698 }
1699
1700 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001701
Christopher Faulet9768c262018-10-22 09:34:31 +02001702 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001703 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1704 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001705
Christopher Faulete6b39942018-12-07 09:42:49 +01001706 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Olivier Houchard6a2d3342018-12-06 17:41:26 +01001707 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001708 if (!b_data(&h1c->ibuf))
1709 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001710 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001711 h1c->flags |= H1C_F_IN_FULL;
1712 return rcvd;
1713}
1714
1715
1716/*
1717 * Try to send data if possible
1718 */
1719static int h1_send(struct h1c *h1c)
1720{
1721 struct connection *conn = h1c->conn;
1722 unsigned int flags = 0;
1723 size_t ret;
1724 int sent = 0;
1725
1726 if (conn->flags & CO_FL_ERROR)
1727 return 0;
1728
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001729 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
1730 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1731 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1732 return 0;
1733 }
1734
Christopher Faulet51dbc942018-09-13 09:05:15 +02001735 if (!b_data(&h1c->obuf))
1736 goto end;
1737
1738 if (h1c->flags & H1C_F_OUT_FULL)
1739 flags |= CO_SFL_MSG_MORE;
1740
1741 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1742 if (ret > 0) {
1743 h1c->flags &= ~H1C_F_OUT_FULL;
1744 b_del(&h1c->obuf, ret);
1745 sent = 1;
1746 }
1747
Christopher Faulet145aa472018-12-06 10:56:20 +01001748 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1749 /* error or output closed, nothing to send, clear the buffer to release it */
1750 b_reset(&h1c->obuf);
1751 }
1752
Christopher Faulet51dbc942018-09-13 09:05:15 +02001753 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001754 if (!(h1c->flags & H1C_F_OUT_FULL))
1755 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001756
Christopher Faulet51dbc942018-09-13 09:05:15 +02001757 /* We're done, no more to send */
1758 if (!b_data(&h1c->obuf)) {
1759 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001760 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001761 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1762 h1_shutw_conn(conn);
1763 }
1764 else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1765 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event);
1766
1767 return sent;
1768}
1769
Christopher Faulet51dbc942018-09-13 09:05:15 +02001770
1771/* callback called on any event by the connection handler.
1772 * It applies changes and returns zero, or < 0 if it wants immediate
1773 * destruction of the connection.
1774 */
1775static int h1_process(struct h1c * h1c)
1776{
1777 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001778 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001779
Christopher Faulet51dbc942018-09-13 09:05:15 +02001780 if (!conn->mux_ctx)
1781 return -1;
1782
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001783 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001784 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1785 goto end;
1786 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001787 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001788 }
1789
Christopher Fauletfeb11742018-11-29 15:12:34 +01001790 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001791 if (h1c->flags & H1C_F_CS_ERROR ||
1792 conn->flags & CO_FL_ERROR ||
1793 conn_xprt_read0_pending(conn))
1794 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001795 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001796 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001797 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001798 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001799 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001800 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001801 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001802 }
1803
Christopher Fauletfeb11742018-11-29 15:12:34 +01001804 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1805 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1806
Olivier Houchard75159a92018-12-03 18:46:09 +01001807 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1808 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
1809 conn->flags & CO_FL_ERROR)) {
1810 int flags = 0;
1811
1812 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1813 flags |= CS_FL_ERROR;
1814 if (conn_xprt_read0_pending(conn))
Christopher Faulet5f50f5e2018-12-07 11:39:55 +01001815 flags |= CS_FL_EOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001816 h1s->cs->flags |= flags;
1817 h1s->cs->data_cb->wake(h1s->cs);
1818 }
Christopher Faulet47365272018-10-31 17:40:50 +01001819 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001820 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001821
1822 release:
1823 h1_release(conn);
1824 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001825}
1826
1827static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1828{
1829 struct h1c *h1c = ctx;
1830 int ret = 0;
1831
1832 if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND))
1833 ret = h1_send(h1c);
1834 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
1835 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001836 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001837 h1_process(h1c);
1838 return NULL;
1839}
1840
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001841static void h1_reset(struct connection *conn)
1842{
1843 struct h1c *h1c = conn->mux_ctx;
1844
1845 /* Reset the flags, and let the mux know we're waiting for a connection */
1846 h1c->flags = H1C_F_CS_WAIT_CONN;
1847}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001848
1849static int h1_wake(struct connection *conn)
1850{
1851 struct h1c *h1c = conn->mux_ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001852 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001853
Christopher Faulet539e0292018-11-19 10:40:09 +01001854 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001855 ret = h1_process(h1c);
1856 if (ret == 0) {
1857 struct h1s *h1s = h1c->h1s;
1858
1859 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1860 ret = h1s->cs->data_cb->wake(h1s->cs);
1861 }
1862 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001863}
1864
Christopher Faulet51dbc942018-09-13 09:05:15 +02001865/*******************************************/
1866/* functions below are used by the streams */
1867/*******************************************/
1868/*
1869 * Attach a new stream to a connection
1870 * (Used for outgoing connections)
1871 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001872static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001873{
1874 struct h1c *h1c = conn->mux_ctx;
1875 struct conn_stream *cs = NULL;
1876 struct h1s *h1s;
1877
1878 if (h1c->flags & H1C_F_CS_ERROR)
1879 goto end;
1880
1881 cs = cs_new(h1c->conn);
1882 if (!cs)
1883 goto end;
1884
Olivier Houchardf502aca2018-12-14 19:42:40 +01001885 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001886 if (h1s == NULL)
1887 goto end;
1888
1889 return cs;
1890 end:
1891 cs_free(cs);
1892 return NULL;
1893}
1894
1895/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1896 * this mux, it's easy as we can only store a single conn_stream.
1897 */
1898static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1899{
1900 struct h1c *h1c = conn->mux_ctx;
1901 struct h1s *h1s = h1c->h1s;
1902
1903 if (h1s)
1904 return h1s->cs;
1905
1906 return NULL;
1907}
1908
1909static void h1_destroy(struct connection *conn)
1910{
1911 struct h1c *h1c = conn->mux_ctx;
1912
1913 if (!h1c->h1s)
1914 h1_release(conn);
1915}
1916
1917/*
1918 * Detach the stream from the connection and possibly release the connection.
1919 */
1920static void h1_detach(struct conn_stream *cs)
1921{
1922 struct h1s *h1s = cs->ctx;
1923 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001924 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01001925 int has_keepalive;
1926 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001927
1928 cs->ctx = NULL;
1929 if (!h1s)
1930 return;
1931
Olivier Houchardf502aca2018-12-14 19:42:40 +01001932 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001933 h1c = h1s->h1c;
1934 h1s->cs = NULL;
1935
Olivier Houchard8a786902018-12-15 16:05:40 +01001936 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
1937 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
1938 h1s_destroy(h1s);
1939
1940 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01001941 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001942 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01001943 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01001944 h1c->conn->flags |= CO_FL_PRIVATE;
1945
Olivier Houchard44d59142018-12-13 18:46:22 +01001946 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001947 h1c->conn->owner = sess;
1948 session_add_conn(sess, h1c->conn, h1c->conn->target);
Olivier Houchard44d59142018-12-13 18:46:22 +01001949 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01001950 if (h1c->conn->owner == sess) {
1951 int ret = session_check_idle_conn(sess, h1c->conn);
1952 if (ret == -1)
1953 /* The connection got destroyed, let's leave */
1954 return;
1955 else if (ret == 1) {
1956 /* The connection was added to the server list,
1957 * wake the task so we can subscribe to events
1958 */
1959 tasklet_wakeup(h1c->wait_event.task);
1960 return;
1961 }
1962 }
Christopher Faulet9400a392018-11-23 23:10:39 +01001963 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01001964 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001965 struct server *srv = objt_server(h1c->conn->target);
1966
1967 if (srv) {
1968 if (h1c->conn->flags & CO_FL_PRIVATE)
1969 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01001970 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01001971 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1972 else
1973 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1974 }
1975 }
1976 }
1977
Christopher Faulet51dbc942018-09-13 09:05:15 +02001978 /* We don't want to close right now unless the connection is in error */
1979 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01001980 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001981 h1_release(h1c->conn);
1982 else
1983 tasklet_wakeup(h1c->wait_event.task);
1984}
1985
1986
1987static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1988{
1989 struct h1s *h1s = cs->ctx;
1990
1991 if (!h1s)
1992 return;
1993
Christopher Fauletf2824e62018-10-01 12:12:37 +02001994 if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS)))
1995 return;
1996
Christopher Faulet51dbc942018-09-13 09:05:15 +02001997 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
1998 if (cs->flags & CS_FL_SHR)
1999 return;
2000 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
2001 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
2002 if (cs->flags & CS_FL_SHW) {
2003 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
2004 conn_full_close(cs->conn);
2005 }
2006}
2007
2008static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2009{
2010 struct h1s *h1s = cs->ctx;
2011 struct h1c *h1c;
2012
2013 if (!h1s)
2014 return;
2015 h1c = h1s->h1c;
2016
Christopher Fauletf2824e62018-10-01 12:12:37 +02002017 if ((h1s->flags & H1S_F_WANT_KAL) &&
2018 !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) &&
2019 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2020 return;
2021
Christopher Faulet51dbc942018-09-13 09:05:15 +02002022 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2023 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2024 return;
2025
2026 h1_shutw_conn(cs->conn);
2027}
2028
2029static void h1_shutw_conn(struct connection *conn)
2030{
2031 struct h1c *h1c = conn->mux_ctx;
2032
2033 if (conn_xprt_ready(conn) && conn->xprt->shutw)
2034 conn->xprt->shutw(conn, 1);
2035 if (!(conn->flags & CO_FL_SOCK_RD_SH))
2036 conn_sock_shutw(conn, 1);
2037 else {
2038 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
2039 conn_full_close(conn);
2040 }
2041}
2042
2043/* Called from the upper layer, to unsubscribe to events */
2044static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2045{
2046 struct wait_event *sw;
2047 struct h1s *h1s = cs->ctx;
2048
2049 if (!h1s)
2050 return 0;
2051
2052 if (event_type & SUB_CAN_RECV) {
2053 sw = param;
2054 if (h1s->recv_wait == sw) {
2055 sw->wait_reason &= ~SUB_CAN_RECV;
2056 h1s->recv_wait = NULL;
2057 }
2058 }
2059 if (event_type & SUB_CAN_SEND) {
2060 sw = param;
2061 if (h1s->send_wait == sw) {
2062 sw->wait_reason &= ~SUB_CAN_SEND;
2063 h1s->send_wait = NULL;
2064 }
2065 }
2066 return 0;
2067}
2068
2069/* Called from the upper layer, to subscribe to events, such as being able to send */
2070static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2071{
2072 struct wait_event *sw;
2073 struct h1s *h1s = cs->ctx;
2074
2075 if (!h1s)
2076 return -1;
2077
2078 switch (event_type) {
2079 case SUB_CAN_RECV:
2080 sw = param;
2081 if (!(sw->wait_reason & SUB_CAN_RECV)) {
2082 sw->wait_reason |= SUB_CAN_RECV;
2083 sw->handle = h1s;
2084 h1s->recv_wait = sw;
2085 }
2086 return 0;
2087 case SUB_CAN_SEND:
2088 sw = param;
2089 if (!(sw->wait_reason & SUB_CAN_SEND)) {
2090 sw->wait_reason |= SUB_CAN_SEND;
2091 sw->handle = h1s;
2092 h1s->send_wait = sw;
2093 }
2094 return 0;
2095 default:
2096 break;
2097 }
2098 return -1;
2099}
2100
2101/* Called from the upper layer, to receive data */
2102static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2103{
2104 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002105 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002106 size_t ret = 0;
2107
Christopher Faulet539e0292018-11-19 10:40:09 +01002108 if (!(h1c->flags & H1C_F_IN_ALLOC))
2109 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002110
2111 if (flags & CO_RFL_BUF_FLUSH)
2112 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002113 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2114 h1s->flags &= ~H1S_F_SPLICED_DATA;
Christopher Faulet539e0292018-11-19 10:40:09 +01002115 if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV))
2116 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002117 }
2118 return ret;
2119}
2120
2121
2122/* Called from the upper layer, to send data */
2123static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2124{
2125 struct h1s *h1s = cs->ctx;
2126 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002127 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002128
2129 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002130 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002131
2132 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002133 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2134 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002135
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002136 while (total != count) {
2137 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002138
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002139 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2140 ret = h1_process_output(h1c, buf, count);
2141 if (!ret)
2142 break;
2143 total += ret;
2144 if (!h1_send(h1c))
2145 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002146 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002147
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002148 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002149}
2150
Christopher Faulet1be55f92018-10-02 15:59:23 +02002151#if defined(CONFIG_HAP_LINUX_SPLICE)
2152/* Send and get, using splicing */
2153static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2154{
2155 struct h1s *h1s = cs->ctx;
2156 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2157 int ret = 0;
2158
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002159 if (b_data(&h1s->h1c->ibuf)) {
2160 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002161 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002162 }
2163
2164 h1s->flags &= ~H1S_F_BUF_FLUSH;
2165 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002166 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2167 count = h1m->curr_len;
2168 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2169 if (h1m->state == H1_MSG_DATA && ret > 0)
2170 h1m->curr_len -= ret;
2171 end:
2172 return ret;
2173
2174}
2175
2176static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2177{
2178 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002179 int ret = 0;
2180
2181 if (b_data(&h1s->h1c->obuf))
2182 goto end;
2183
2184 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002185 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002186 if (pipe->data) {
2187 if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND))
2188 cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event);
2189 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002190 return ret;
2191}
2192#endif
2193
Christopher Faulet51dbc942018-09-13 09:05:15 +02002194/****************************************/
2195/* MUX initialization and instanciation */
2196/****************************************/
2197
2198/* The mux operations */
2199const struct mux_ops mux_h1_ops = {
2200 .init = h1_init,
2201 .wake = h1_wake,
2202 .attach = h1_attach,
2203 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002204 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002205 .detach = h1_detach,
2206 .destroy = h1_destroy,
2207 .avail_streams = h1_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01002208 .max_streams = h1_max_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002209 .rcv_buf = h1_rcv_buf,
2210 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002211#if defined(CONFIG_HAP_LINUX_SPLICE)
2212 .rcv_pipe = h1_rcv_pipe,
2213 .snd_pipe = h1_snd_pipe,
2214#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002215 .subscribe = h1_subscribe,
2216 .unsubscribe = h1_unsubscribe,
2217 .shutr = h1_shutr,
2218 .shutw = h1_shutw,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002219 .reset = h1_reset,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002220 .flags = MX_FL_NONE,
2221 .name = "h1",
2222};
2223
2224
2225/* this mux registers default HTX proto */
2226static struct mux_proto_list mux_proto_htx =
2227{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2228
Willy Tarreau0108d902018-11-25 19:14:37 +01002229INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2230
Christopher Faulet51dbc942018-09-13 09:05:15 +02002231/*
2232 * Local variables:
2233 * c-indent-level: 8
2234 * c-basic-offset: 8
2235 * End:
2236 */