blob: bf9a723aa1f7efc09f3c55363cfb56b004a19247 [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{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100207 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200208
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)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100324 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200325 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100326 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200327
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/*
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100348 * Initialize the mux once it's attached. It is expected that conn->ctx
Christopher Faulet51dbc942018-09-13 09:05:15 +0200349 * 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;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100373 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200374
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 */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100379 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200380 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200381
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100382 conn->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{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100405 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200406
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);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100424 if (h1c->wait_event.events != 0)
425 conn->xprt->unsubscribe(conn, h1c->wait_event.events,
Christopher Faulet51dbc942018-09-13 09:05:15 +0200426 &h1c->wait_event);
427 pool_free(pool_head_h1c, h1c);
428 }
429
430 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100431 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200432
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
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100830 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100831 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
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001281 if (!count)
1282 goto end;
1283
Christopher Fauletf2824e62018-10-01 12:12:37 +02001284 if (!conn_is_back(h1c->conn)) {
1285 h1m = &h1s->req;
1286 errflag = H1S_F_REQ_ERROR;
1287 }
1288 else {
1289 h1m = &h1s->res;
1290 errflag = H1S_F_RES_ERROR;
1291 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001292
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001293 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001294 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001295 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001296 if (!ret)
1297 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001298 }
1299 else if (h1m->state <= H1_MSG_TRAILERS) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001300 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1301 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001302 if (!ret)
1303 break;
1304 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001305 else if (h1m->state == H1_MSG_DONE) {
1306 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001307 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001308 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001309 else if (h1m->state == H1_MSG_TUNNEL) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001310 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf);
1311 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001312 if (!ret)
1313 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001314 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001315 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001316 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001317 break;
1318 }
1319
Christopher Faulet539e0292018-11-19 10:40:09 +01001320 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001321 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001322
Christopher Faulet47365272018-10-31 17:40:50 +01001323 if (h1s->flags & errflag)
1324 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001325
Christopher Faulet539e0292018-11-19 10:40:09 +01001326 b_del(&h1c->ibuf, total);
1327
1328 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001329 htx_to_buf(htx, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001330
Willy Tarreau45f2b892018-12-05 07:59:27 +01001331 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001332 h1c->flags &= ~H1C_F_IN_FULL;
1333 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001334 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001335
Christopher Fauletcf56b992018-12-11 16:12:31 +01001336 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1337
1338 if (!b_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001339 h1_release_buf(h1c, &h1c->ibuf);
1340 h1_sync_messages(h1c);
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001341 }
Christopher Fauletcf56b992018-12-11 16:12:31 +01001342 else if (!htx_is_empty(htx))
1343 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001344
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001345 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1346 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet539e0292018-11-19 10:40:09 +01001347 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001348
Christopher Faulet539e0292018-11-19 10:40:09 +01001349 return total;
Christopher Faulet47365272018-10-31 17:40:50 +01001350
1351 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001352 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001353 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001354 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001355 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001356 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001357}
1358
Christopher Faulet129817b2018-09-20 16:14:40 +02001359/*
1360 * Process outgoing data. It parses data and transfer them from the channel buffer into
1361 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1362 * 0 if it couldn't proceed.
1363 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001364static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1365{
Christopher Faulet129817b2018-09-20 16:14:40 +02001366 struct h1s *h1s = h1c->h1s;
1367 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001368 struct htx *chn_htx;
1369 struct htx_blk *blk;
1370 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001371 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001372 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001373 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001374
Christopher Faulet47365272018-10-31 17:40:50 +01001375 if (!count)
1376 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001377
Christopher Faulet9768c262018-10-22 09:34:31 +02001378 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001379 if (htx_is_empty(chn_htx))
1380 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001381
Christopher Faulet51dbc942018-09-13 09:05:15 +02001382 if (!h1_get_buf(h1c, &h1c->obuf)) {
1383 h1c->flags |= H1C_F_OUT_ALLOC;
1384 goto end;
1385 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001386
Christopher Fauletf2824e62018-10-01 12:12:37 +02001387 if (!conn_is_back(h1c->conn)) {
1388 h1m = &h1s->res;
1389 errflag = H1S_F_RES_ERROR;
1390 }
1391 else {
1392 h1m = &h1s->req;
1393 errflag = H1S_F_REQ_ERROR;
1394 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001395
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001396 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001397 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001398
1399 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001400
Willy Tarreau3815b222018-12-11 19:50:43 +01001401 /* Perform some optimizations to reduce the number of buffer copies.
1402 * First, if the mux's buffer is empty and the htx area contains
1403 * exactly one data block of the same size as the requested count,
1404 * then it's possible to simply swap the caller's buffer with the
1405 * mux's output buffer and adjust offsets and length to match the
1406 * entire DATA HTX block in the middle. In this case we perform a
1407 * true zero-copy operation from end-to-end. This is the situation
1408 * that happens all the time with large files. Second, if this is not
1409 * possible, but the mux's output buffer is empty, we still have an
1410 * opportunity to avoid the copy to the intermediary buffer, by making
1411 * the intermediary buffer's area point to the output buffer's area.
1412 * In this case we want to skip the HTX header to make sure that copies
1413 * remain aligned and that this operation remains possible all the
1414 * time. This goes for headers, data blocks and any data extracted from
1415 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001416 */
1417 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001418 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001419
1420 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001421 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001422 htx_get_blk_value(chn_htx, blk).len == count) {
1423 void *old_area = h1c->obuf.area;
1424
1425 h1c->obuf.area = buf->area;
1426 h1c->obuf.data = count;
1427
1428 buf->area = old_area;
1429 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001430
1431 /* The message is chunked. We need to emit the chunk
1432 * size. We have at least the size of the struct htx to
1433 * write the chunk envelope. It should be enough.
1434 */
1435 if (h1m->flags & H1_MF_CHNK) {
1436 h1_emit_chunk_size(&h1c->obuf, count);
1437 h1_emit_chunk_crlf(&h1c->obuf);
1438 }
1439
Willy Tarreau3815b222018-12-11 19:50:43 +01001440 total += count;
1441 goto out;
1442 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001443 tmp->area = h1c->obuf.area + h1c->obuf.head;
1444 }
1445
Christopher Faulet9768c262018-10-22 09:34:31 +02001446 tmp->size = b_room(&h1c->obuf);
1447
Christopher Fauletb2e84162018-12-06 11:39:49 +01001448 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001449 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001450 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001451 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001452 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001453 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001454
Christopher Fauletb2e84162018-12-06 11:39:49 +01001455 vlen = sz;
1456 if (vlen > count) {
1457 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1458 goto copy;
1459 vlen = count;
1460 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001461
Christopher Fauletb2e84162018-12-06 11:39:49 +01001462 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001463 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001464 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001465
1466 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001467 h1m_init_req(h1m);
1468 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001469 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001470 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001471 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001472 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001473 goto copy;
1474 h1m->flags |= H1_MF_XFER_LEN;
1475 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001476 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001477
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001479 h1m_init_res(h1m);
1480 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001481 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001482 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001483 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001484 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001485 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001486 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001487 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001488 if (sl->info.res.status < 200 &&
1489 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1490 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001491 h1m->state = H1_MSG_HDR_FIRST;
1492 break;
1493
1494 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001495 h1m->state = H1_MSG_HDR_NAME;
1496 n = htx_get_blk_name(chn_htx, blk);
1497 v = htx_get_blk_value(chn_htx, blk);
1498
1499 if (isteqi(n, ist("transfer-encoding")))
1500 h1_parse_xfer_enc_header(h1m, v);
1501 else if (isteqi(n, ist("connection"))) {
1502 h1_parse_connection_header(h1m, v);
1503 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001504 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001505 if (!v.len)
1506 goto skip_hdr;
1507 }
1508
Christopher Fauletc59ff232018-12-03 13:58:44 +01001509 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001510 goto copy;
1511 skip_hdr:
1512 h1m->state = H1_MSG_HDR_L2_LWS;
1513 break;
1514
1515 case HTX_BLK_PHDR:
1516 /* not implemented yet */
1517 h1m->flags |= errflag;
1518 break;
1519
1520 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001521 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001522 /* There is no "Connection:" header and
1523 * it the conn_mode must be
1524 * processed. So do it */
1525 n = ist("Connection");
1526 v = ist("");
1527 h1_process_conn_mode(h1s, h1m, NULL, &v);
1528 process_conn_mode = 0;
1529 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001530 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001531 goto copy;
1532 }
1533 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001534 h1m->state = H1_MSG_LAST_LF;
1535 if (!chunk_memcat(tmp, "\r\n", 2))
1536 goto copy;
1537
1538 h1m->state = H1_MSG_DATA;
1539 break;
1540
1541 case HTX_BLK_DATA:
1542 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001543 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001544 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001545 goto copy;
1546 break;
1547
1548 case HTX_BLK_EOD:
1549 if (!chunk_memcat(tmp, "0\r\n", 3))
1550 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001551 h1s->flags |= H1S_F_HAVE_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001552 h1m->state = H1_MSG_TRAILERS;
1553 break;
1554
1555 case HTX_BLK_TLR:
Christopher Faulet32188212018-11-20 18:21:43 +01001556 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1557 if (!chunk_memcat(tmp, "0\r\n", 3))
1558 goto copy;
1559 h1s->flags |= H1S_F_HAVE_EOD;
1560 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001561 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001562 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001563 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001564 goto copy;
Christopher Faulet32188212018-11-20 18:21:43 +01001565 h1s->flags |= H1S_F_HAVE_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001566 break;
1567
1568 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001569 if ((h1m->flags & H1_MF_CHNK)) {
1570 if (!(h1s->flags & H1S_F_HAVE_EOD)) {
1571 if (!chunk_memcat(tmp, "0\r\n", 3))
1572 goto copy;
1573 h1s->flags |= H1S_F_HAVE_EOD;
1574 }
1575 if (!(h1s->flags & H1S_F_HAVE_TLR)) {
1576 if (!chunk_memcat(tmp, "\r\n", 2))
1577 goto copy;
1578 h1s->flags |= H1S_F_HAVE_TLR;
1579 }
1580 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001581 h1m->state = H1_MSG_DONE;
1582 break;
1583
1584 case HTX_BLK_OOB:
1585 v = htx_get_blk_value(chn_htx, blk);
1586 if (!chunk_memcat(tmp, v.ptr, v.len))
1587 goto copy;
1588 break;
1589
1590 default:
1591 h1m->flags |= errflag;
1592 break;
1593 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001594 total += vlen;
1595 count -= vlen;
1596 if (sz == vlen)
1597 blk = htx_remove_blk(chn_htx, blk);
1598 else {
1599 htx_cut_data_blk(chn_htx, blk, vlen);
1600 break;
1601 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001602 }
1603
Christopher Faulet9768c262018-10-22 09:34:31 +02001604 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001605 /* when the output buffer is empty, tmp shares the same area so that we
1606 * only have to update pointers and lengths.
1607 */
1608 if (tmp->area == h1c->obuf.area)
1609 h1c->obuf.data = tmp->data;
1610 else
1611 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001612
Willy Tarreau3815b222018-12-11 19:50:43 +01001613 htx_to_buf(chn_htx, buf);
1614 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001615 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001616 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001617 end:
1618 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001619}
1620
Christopher Faulet51dbc942018-09-13 09:05:15 +02001621/*********************************************************/
1622/* functions below are I/O callbacks from the connection */
1623/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001624static void h1_wake_stream_for_recv(struct h1s *h1s)
1625{
1626 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001627 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001628 tasklet_wakeup(h1s->recv_wait->task);
1629 h1s->recv_wait = NULL;
1630 }
1631}
1632static void h1_wake_stream_for_send(struct h1s *h1s)
1633{
1634 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001635 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001636 tasklet_wakeup(h1s->send_wait->task);
1637 h1s->send_wait = NULL;
1638 }
1639}
1640
Christopher Faulet51dbc942018-09-13 09:05:15 +02001641/*
1642 * Attempt to read data, and subscribe if none available
1643 */
1644static int h1_recv(struct h1c *h1c)
1645{
1646 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001647 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001648 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001649 int rcvd = 0;
1650
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001651 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001652 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001653
Olivier Houchard75159a92018-12-03 18:46:09 +01001654 if (!h1_recv_allowed(h1c)) {
1655 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001656 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001657 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001658
Christopher Fauletfeb11742018-11-29 15:12:34 +01001659 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001660 rcvd = 1;
1661 goto end;
1662 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001663
Christopher Faulet51dbc942018-09-13 09:05:15 +02001664 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1665 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001666 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001667 }
1668
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001669 /*
1670 * If we only have a small amount of data, realign it,
1671 * it's probably cheaper than doing 2 recv() calls.
1672 */
1673 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1674 b_slow_realign(&h1c->ibuf, trash.area, 0);
1675
Willy Tarreau45f2b892018-12-05 07:59:27 +01001676 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001677 if (max) {
1678 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001679
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001680 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001681 if (!b_data(&h1c->ibuf)) {
1682 /* try to pre-align the buffer like the rxbufs will be
1683 * to optimize memory copies.
1684 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001685 h1c->ibuf.head = sizeof(struct htx);
1686 }
Willy Tarreauc0960d12018-12-14 10:59:15 +01001687 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001688 }
Christopher Faulet47365272018-10-31 17:40:50 +01001689 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001690 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001691 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001692 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001693 if (h1s->csinfo.t_idle == -1)
1694 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1695 }
Christopher Faulet47365272018-10-31 17:40:50 +01001696 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001697
Christopher Fauletcf56b992018-12-11 16:12:31 +01001698 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001699 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001700 goto end;
1701 }
1702
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001703 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001704
Christopher Faulet9768c262018-10-22 09:34:31 +02001705 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001706 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1707 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001708
Christopher Faulete6b39942018-12-07 09:42:49 +01001709 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Olivier Houchard6a2d3342018-12-06 17:41:26 +01001710 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001711 if (!b_data(&h1c->ibuf))
1712 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001713 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001714 h1c->flags |= H1C_F_IN_FULL;
1715 return rcvd;
1716}
1717
1718
1719/*
1720 * Try to send data if possible
1721 */
1722static int h1_send(struct h1c *h1c)
1723{
1724 struct connection *conn = h1c->conn;
1725 unsigned int flags = 0;
1726 size_t ret;
1727 int sent = 0;
1728
1729 if (conn->flags & CO_FL_ERROR)
1730 return 0;
1731
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001732 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001733 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1734 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001735 return 0;
1736 }
1737
Christopher Faulet51dbc942018-09-13 09:05:15 +02001738 if (!b_data(&h1c->obuf))
1739 goto end;
1740
1741 if (h1c->flags & H1C_F_OUT_FULL)
1742 flags |= CO_SFL_MSG_MORE;
1743
1744 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1745 if (ret > 0) {
1746 h1c->flags &= ~H1C_F_OUT_FULL;
1747 b_del(&h1c->obuf, ret);
1748 sent = 1;
1749 }
1750
Christopher Faulet145aa472018-12-06 10:56:20 +01001751 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1752 /* error or output closed, nothing to send, clear the buffer to release it */
1753 b_reset(&h1c->obuf);
1754 }
1755
Christopher Faulet51dbc942018-09-13 09:05:15 +02001756 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001757 if (!(h1c->flags & H1C_F_OUT_FULL))
1758 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001759
Christopher Faulet51dbc942018-09-13 09:05:15 +02001760 /* We're done, no more to send */
1761 if (!b_data(&h1c->obuf)) {
1762 h1_release_buf(h1c, &h1c->obuf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001763 h1_sync_messages(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001764 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
1765 h1_shutw_conn(conn);
1766 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001767 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1768 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001769
1770 return sent;
1771}
1772
Christopher Faulet51dbc942018-09-13 09:05:15 +02001773
1774/* callback called on any event by the connection handler.
1775 * It applies changes and returns zero, or < 0 if it wants immediate
1776 * destruction of the connection.
1777 */
1778static int h1_process(struct h1c * h1c)
1779{
1780 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001781 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001782
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001783 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001784 return -1;
1785
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001786 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001787 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1788 goto end;
1789 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001790 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001791 }
1792
Christopher Fauletfeb11742018-11-29 15:12:34 +01001793 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001794 if (h1c->flags & H1C_F_CS_ERROR ||
1795 conn->flags & CO_FL_ERROR ||
1796 conn_xprt_read0_pending(conn))
1797 goto release;
Christopher Faulet81d48432018-11-19 21:22:43 +01001798 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTW))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001799 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001800 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001801 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001802 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001803 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001804 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001805 }
1806
Christopher Fauletfeb11742018-11-29 15:12:34 +01001807 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1808 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1809
Olivier Houchard75159a92018-12-03 18:46:09 +01001810 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1811 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
1812 conn->flags & CO_FL_ERROR)) {
1813 int flags = 0;
1814
1815 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1816 flags |= CS_FL_ERROR;
1817 if (conn_xprt_read0_pending(conn))
Christopher Faulet5f50f5e2018-12-07 11:39:55 +01001818 flags |= CS_FL_EOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001819 h1s->cs->flags |= flags;
1820 h1s->cs->data_cb->wake(h1s->cs);
1821 }
Christopher Faulet47365272018-10-31 17:40:50 +01001822 end:
Christopher Faulet51dbc942018-09-13 09:05:15 +02001823 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001824
1825 release:
1826 h1_release(conn);
1827 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001828}
1829
1830static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1831{
1832 struct h1c *h1c = ctx;
1833 int ret = 0;
1834
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001835 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001836 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001837 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001838 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001839 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001840 h1_process(h1c);
1841 return NULL;
1842}
1843
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001844static void h1_reset(struct connection *conn)
1845{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001846 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001847
1848 /* Reset the flags, and let the mux know we're waiting for a connection */
1849 h1c->flags = H1C_F_CS_WAIT_CONN;
1850}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001851
1852static int h1_wake(struct connection *conn)
1853{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001854 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001855 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001856
Christopher Faulet539e0292018-11-19 10:40:09 +01001857 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001858 ret = h1_process(h1c);
1859 if (ret == 0) {
1860 struct h1s *h1s = h1c->h1s;
1861
1862 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1863 ret = h1s->cs->data_cb->wake(h1s->cs);
1864 }
1865 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001866}
1867
Christopher Faulet51dbc942018-09-13 09:05:15 +02001868/*******************************************/
1869/* functions below are used by the streams */
1870/*******************************************/
1871/*
1872 * Attach a new stream to a connection
1873 * (Used for outgoing connections)
1874 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001875static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001876{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001877 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001878 struct conn_stream *cs = NULL;
1879 struct h1s *h1s;
1880
1881 if (h1c->flags & H1C_F_CS_ERROR)
1882 goto end;
1883
1884 cs = cs_new(h1c->conn);
1885 if (!cs)
1886 goto end;
1887
Olivier Houchardf502aca2018-12-14 19:42:40 +01001888 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001889 if (h1s == NULL)
1890 goto end;
1891
1892 return cs;
1893 end:
1894 cs_free(cs);
1895 return NULL;
1896}
1897
1898/* Retrieves a valid conn_stream from this connection, or returns NULL. For
1899 * this mux, it's easy as we can only store a single conn_stream.
1900 */
1901static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
1902{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001903 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001904 struct h1s *h1s = h1c->h1s;
1905
1906 if (h1s)
1907 return h1s->cs;
1908
1909 return NULL;
1910}
1911
1912static void h1_destroy(struct connection *conn)
1913{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001914 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001915
1916 if (!h1c->h1s)
1917 h1_release(conn);
1918}
1919
1920/*
1921 * Detach the stream from the connection and possibly release the connection.
1922 */
1923static void h1_detach(struct conn_stream *cs)
1924{
1925 struct h1s *h1s = cs->ctx;
1926 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001927 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01001928 int has_keepalive;
1929 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001930
1931 cs->ctx = NULL;
1932 if (!h1s)
1933 return;
1934
Olivier Houchardf502aca2018-12-14 19:42:40 +01001935 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001936 h1c = h1s->h1c;
1937 h1s->cs = NULL;
1938
Olivier Houchard8a786902018-12-15 16:05:40 +01001939 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
1940 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
1941 h1s_destroy(h1s);
1942
1943 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01001944 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001945 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01001946 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01001947 h1c->conn->flags |= CO_FL_PRIVATE;
1948
Olivier Houchard44d59142018-12-13 18:46:22 +01001949 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001950 h1c->conn->owner = sess;
1951 session_add_conn(sess, h1c->conn, h1c->conn->target);
Olivier Houchard44d59142018-12-13 18:46:22 +01001952 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01001953 if (h1c->conn->owner == sess) {
1954 int ret = session_check_idle_conn(sess, h1c->conn);
1955 if (ret == -1)
1956 /* The connection got destroyed, let's leave */
1957 return;
1958 else if (ret == 1) {
1959 /* The connection was added to the server list,
1960 * wake the task so we can subscribe to events
1961 */
1962 tasklet_wakeup(h1c->wait_event.task);
1963 return;
1964 }
1965 }
Christopher Faulet9400a392018-11-23 23:10:39 +01001966 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01001967 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01001968 struct server *srv = objt_server(h1c->conn->target);
1969
1970 if (srv) {
1971 if (h1c->conn->flags & CO_FL_PRIVATE)
1972 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01001973 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01001974 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
1975 else
1976 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
1977 }
1978 }
1979 }
1980
Christopher Faulet51dbc942018-09-13 09:05:15 +02001981 /* We don't want to close right now unless the connection is in error */
1982 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01001983 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001984 h1_release(h1c->conn);
1985 else
1986 tasklet_wakeup(h1c->wait_event.task);
1987}
1988
1989
1990static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1991{
1992 struct h1s *h1s = cs->ctx;
1993
1994 if (!h1s)
1995 return;
1996
Olivier Houchardd2e88c72018-12-19 15:55:23 +01001997 if ((h1s->flags & H1S_F_WANT_KAL) &&
1998 !(cs->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001999 return;
2000
Christopher Faulet51dbc942018-09-13 09:05:15 +02002001 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2002 if (cs->flags & CS_FL_SHR)
2003 return;
2004 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
2005 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
2006 if (cs->flags & CS_FL_SHW) {
2007 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
2008 conn_full_close(cs->conn);
2009 }
2010}
2011
2012static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2013{
2014 struct h1s *h1s = cs->ctx;
2015 struct h1c *h1c;
2016
2017 if (!h1s)
2018 return;
2019 h1c = h1s->h1c;
2020
Christopher Fauletf2824e62018-10-01 12:12:37 +02002021 if ((h1s->flags & H1S_F_WANT_KAL) &&
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002022 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) &&
2023
Christopher Fauletf2824e62018-10-01 12:12:37 +02002024 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2025 return;
2026
Christopher Faulet51dbc942018-09-13 09:05:15 +02002027 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2028 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2029 return;
2030
2031 h1_shutw_conn(cs->conn);
2032}
2033
2034static void h1_shutw_conn(struct connection *conn)
2035{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002036 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002037
2038 if (conn_xprt_ready(conn) && conn->xprt->shutw)
2039 conn->xprt->shutw(conn, 1);
2040 if (!(conn->flags & CO_FL_SOCK_RD_SH))
2041 conn_sock_shutw(conn, 1);
2042 else {
2043 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW;
2044 conn_full_close(conn);
2045 }
2046}
2047
2048/* Called from the upper layer, to unsubscribe to events */
2049static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2050{
2051 struct wait_event *sw;
2052 struct h1s *h1s = cs->ctx;
2053
2054 if (!h1s)
2055 return 0;
2056
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002057 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002058 sw = param;
2059 if (h1s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002060 sw->events &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002061 h1s->recv_wait = NULL;
2062 }
2063 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002064 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002065 sw = param;
2066 if (h1s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002067 sw->events &= ~SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002068 h1s->send_wait = NULL;
2069 }
2070 }
2071 return 0;
2072}
2073
2074/* Called from the upper layer, to subscribe to events, such as being able to send */
2075static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2076{
2077 struct wait_event *sw;
2078 struct h1s *h1s = cs->ctx;
2079
2080 if (!h1s)
2081 return -1;
2082
2083 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002084 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002085 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002086 if (!(sw->events & SUB_RETRY_RECV)) {
2087 sw->events |= SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002088 sw->handle = h1s;
2089 h1s->recv_wait = sw;
2090 }
2091 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002092 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002093 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002094 if (!(sw->events & SUB_RETRY_SEND)) {
2095 sw->events |= SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002096 sw->handle = h1s;
2097 h1s->send_wait = sw;
2098 }
2099 return 0;
2100 default:
2101 break;
2102 }
2103 return -1;
2104}
2105
2106/* Called from the upper layer, to receive data */
2107static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2108{
2109 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002110 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002111 size_t ret = 0;
2112
Christopher Faulet539e0292018-11-19 10:40:09 +01002113 if (!(h1c->flags & H1C_F_IN_ALLOC))
2114 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002115
2116 if (flags & CO_RFL_BUF_FLUSH)
2117 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002118 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2119 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002120 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002121 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002122 }
2123 return ret;
2124}
2125
2126
2127/* Called from the upper layer, to send data */
2128static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2129{
2130 struct h1s *h1s = cs->ctx;
2131 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002132 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002133
2134 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002135 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002136
2137 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002138 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2139 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002140
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002141 while (total != count) {
2142 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002143
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002144 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2145 ret = h1_process_output(h1c, buf, count);
2146 if (!ret)
2147 break;
2148 total += ret;
2149 if (!h1_send(h1c))
2150 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002151 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002152
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002153 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002154}
2155
Christopher Faulet1be55f92018-10-02 15:59:23 +02002156#if defined(CONFIG_HAP_LINUX_SPLICE)
2157/* Send and get, using splicing */
2158static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2159{
2160 struct h1s *h1s = cs->ctx;
2161 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2162 int ret = 0;
2163
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002164 if (b_data(&h1s->h1c->ibuf)) {
2165 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002166 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002167 }
2168
2169 h1s->flags &= ~H1S_F_BUF_FLUSH;
2170 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002171 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2172 count = h1m->curr_len;
2173 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2174 if (h1m->state == H1_MSG_DATA && ret > 0)
2175 h1m->curr_len -= ret;
2176 end:
2177 return ret;
2178
2179}
2180
2181static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2182{
2183 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002184 int ret = 0;
2185
2186 if (b_data(&h1s->h1c->obuf))
2187 goto end;
2188
2189 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002190 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002191 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002192 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
2193 cs->conn->xprt->subscribe(cs->conn, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002194 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002195 return ret;
2196}
2197#endif
2198
Christopher Faulet51dbc942018-09-13 09:05:15 +02002199/****************************************/
2200/* MUX initialization and instanciation */
2201/****************************************/
2202
2203/* The mux operations */
2204const struct mux_ops mux_h1_ops = {
2205 .init = h1_init,
2206 .wake = h1_wake,
2207 .attach = h1_attach,
2208 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002209 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002210 .detach = h1_detach,
2211 .destroy = h1_destroy,
2212 .avail_streams = h1_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01002213 .max_streams = h1_max_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002214 .rcv_buf = h1_rcv_buf,
2215 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002216#if defined(CONFIG_HAP_LINUX_SPLICE)
2217 .rcv_pipe = h1_rcv_pipe,
2218 .snd_pipe = h1_snd_pipe,
2219#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002220 .subscribe = h1_subscribe,
2221 .unsubscribe = h1_unsubscribe,
2222 .shutr = h1_shutr,
2223 .shutw = h1_shutw,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002224 .reset = h1_reset,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002225 .flags = MX_FL_NONE,
2226 .name = "h1",
2227};
2228
2229
2230/* this mux registers default HTX proto */
2231static struct mux_proto_list mux_proto_htx =
2232{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2233
Willy Tarreau0108d902018-11-25 19:14:37 +01002234INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2235
Christopher Faulet51dbc942018-09-13 09:05:15 +02002236/*
2237 * Local variables:
2238 * c-indent-level: 8
2239 * c-basic-offset: 8
2240 * End:
2241 */