blob: 606e1749c5f54bb0ca0b23974fbc67271a41f6a0 [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 */
Christopher Faulet666a0c42019-01-08 11:12:04 +010047#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down for read and writes */
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 */
Willy Tarreau34d23482019-01-03 17:46:56 +010067#define H1S_F_HAVE_I_EOD 0x00000400 /* Set during input process to know the last empty chunk was processed */
68#define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */
69#define H1S_F_HAVE_O_EOD 0x00001000 /* Set during output process to know the last empty chunk was processed */
70#define H1S_F_HAVE_O_TLR 0x00002000 /* Set during output process to know the trailers were processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020071
72/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020073struct h1c {
74 struct connection *conn;
75 struct proxy *px;
76 uint32_t flags; /* Connection flags: H1C_F_* */
77
78 struct buffer ibuf; /* Input buffer to store data before parsing */
79 struct buffer obuf; /* Output buffer to store data after reformatting */
80
81 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
82 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
83
84 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010085 struct task *task; /* timeout management task */
86 int timeout; /* idle timeout duration in ticks */
87 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020088};
89
90/* H1 stream descriptor */
91struct h1s {
92 struct h1c *h1c;
93 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +010094 struct cs_info csinfo; /* CS info, only used for client connections */
95 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +020096
Christopher Faulet51dbc942018-09-13 09:05:15 +020097 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
98 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +020099
Olivier Houchardf502aca2018-12-14 19:42:40 +0100100 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200101 struct h1m req;
102 struct h1m res;
103
104 enum http_meth_t meth; /* HTTP resquest method */
105 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200106};
107
108/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100109DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
110DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200111
Christopher Faulet51dbc942018-09-13 09:05:15 +0200112static int h1_recv(struct h1c *h1c);
113static int h1_send(struct h1c *h1c);
114static int h1_process(struct h1c *h1c);
115static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100116static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100117static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200118
119/*****************************************************/
120/* functions below are for dynamic buffer management */
121/*****************************************************/
122/*
123 * Indicates whether or not the we may call the h1_recv() function to
124 * attempt to receive data into the buffer and/or parse pending data. The
125 * condition is a bit complex due to some API limits for now. The rules are the
126 * following :
127 * - if an error or a shutdown was detected on the connection and the buffer
128 * is empty, we must not attempt to receive
129 * - if the input buffer failed to be allocated, we must not try to receive
130 * and we know there is nothing pending
131 * - if no flag indicates a blocking condition, we may attempt to receive,
132 * regardless of whether the input buffer is full or not, so that only de
133 * receiving part decides whether or not to block. This is needed because
134 * the connection API indeed prevents us from re-enabling receipt that is
135 * already enabled in a polled state, so we must always immediately stop as
136 * soon as the mux can't proceed so as never to hit an end of read with data
137 * pending in the buffers.
138 * - otherwise must may not attempt to receive
139 */
140static inline int h1_recv_allowed(const struct h1c *h1c)
141{
Christopher Faulet666a0c42019-01-08 11:12:04 +0100142 if (b_data(&h1c->ibuf) == 0 && (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200143 return 0;
144
Christopher Fauletcf56b992018-12-11 16:12:31 +0100145 if (h1c->conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(h1c->conn))
146 return 0;
147
Christopher Fauletcb55f482018-12-10 11:56:47 +0100148 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200149 return 1;
150
151 return 0;
152}
153
154/*
155 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
156 * flags are used to figure what buffer was requested. It returns 1 if the
157 * allocation succeeds, in which case the connection is woken up, or 0 if it's
158 * impossible to wake up and we prefer to be woken up later.
159 */
160static int h1_buf_available(void *target)
161{
162 struct h1c *h1c = target;
163
164 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
165 h1c->flags &= ~H1C_F_IN_ALLOC;
166 if (h1_recv_allowed(h1c))
167 tasklet_wakeup(h1c->wait_event.task);
168 return 1;
169 }
170
171 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
172 h1c->flags &= ~H1C_F_OUT_ALLOC;
173 tasklet_wakeup(h1c->wait_event.task);
174 return 1;
175 }
176
Christopher Faulet51dbc942018-09-13 09:05:15 +0200177 return 0;
178}
179
180/*
181 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
182 */
183static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
184{
185 struct buffer *buf = NULL;
186
187 if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) &&
188 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
189 h1c->buf_wait.target = h1c;
190 h1c->buf_wait.wakeup_cb = h1_buf_available;
191 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
192 LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
193 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
194 __conn_xprt_stop_recv(h1c->conn);
195 }
196 return buf;
197}
198
199/*
200 * Release a buffer, if any, and try to wake up entities waiting in the buffer
201 * wait queue.
202 */
203static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
204{
205 if (bptr->size) {
206 b_free(bptr);
207 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
208 }
209}
210
Willy Tarreau00f18a32019-01-26 12:19:01 +0100211/* returns the number of streams in use on a connection to figure if it's
212 * idle or not. We can't have an h1s without a CS so checking h1s is fine,
213 * as the caller will want to know if it was the last one after a detach().
214 */
215static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200216{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100217 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200218
Willy Tarreau00f18a32019-01-26 12:19:01 +0100219 return h1c->h1s ? 1 : 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200220}
221
Willy Tarreau00f18a32019-01-26 12:19:01 +0100222/* returns the number of streams still available on a connection */
223static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100224{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100225 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100226}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200227
Willy Tarreau00f18a32019-01-26 12:19:01 +0100228
Christopher Faulet51dbc942018-09-13 09:05:15 +0200229/*****************************************************************/
230/* functions below are dedicated to the mux setup and management */
231/*****************************************************************/
Christopher Faulet47365272018-10-31 17:40:50 +0100232static struct conn_stream *h1s_new_cs(struct h1s *h1s)
233{
234 struct conn_stream *cs;
235
236 cs = cs_new(h1s->h1c->conn);
237 if (!cs)
238 goto err;
239 h1s->cs = cs;
240 cs->ctx = h1s;
241
242 if (h1s->flags & H1S_F_NOT_FIRST)
243 cs->flags |= CS_FL_NOT_FIRST;
244
245 if (stream_create_from_cs(cs) < 0)
246 goto err;
247 return cs;
248
249 err:
250 cs_free(cs);
251 h1s->cs = NULL;
252 return NULL;
253}
254
Olivier Houchardf502aca2018-12-14 19:42:40 +0100255static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200256{
257 struct h1s *h1s;
258
259 h1s = pool_alloc(pool_head_h1s);
260 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100261 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200262
263 h1s->h1c = h1c;
264 h1c->h1s = h1s;
265
Olivier Houchardf502aca2018-12-14 19:42:40 +0100266 h1s->sess = sess;
267
Christopher Faulet51dbc942018-09-13 09:05:15 +0200268 h1s->cs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200269 h1s->flags = H1S_F_NONE;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200270
271 h1s->recv_wait = NULL;
272 h1s->send_wait = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200273
274 h1m_init_req(&h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +0200275 h1s->req.flags |= H1_MF_NO_PHDR;
276
Christopher Faulet129817b2018-09-20 16:14:40 +0200277 h1m_init_res(&h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +0200278 h1s->res.flags |= H1_MF_NO_PHDR;
Christopher Faulet129817b2018-09-20 16:14:40 +0200279
280 h1s->status = 0;
281 h1s->meth = HTTP_METH_OTHER;
282
Christopher Faulet47365272018-10-31 17:40:50 +0100283 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
284 h1s->flags |= H1S_F_NOT_FIRST;
285 h1c->flags &= ~H1C_F_WAIT_NEXT_REQ;
286
Christopher Faulet129817b2018-09-20 16:14:40 +0200287 if (!conn_is_back(h1c->conn)) {
288 if (h1c->px->options2 & PR_O2_REQBUG_OK)
289 h1s->req.err_pos = -1;
290 }
291 else {
292 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
293 h1s->res.err_pos = -1;
294 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200295
Christopher Faulet539e0292018-11-19 10:40:09 +0100296 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
297 * create a new one.
298 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200299 if (cs) {
Christopher Fauletfeb11742018-11-29 15:12:34 +0100300 h1s->csinfo.create_date = date;
301 h1s->csinfo.tv_create = now;
302 h1s->csinfo.t_handshake = 0;
303 h1s->csinfo.t_idle = -1;
304
Christopher Fauletf2824e62018-10-01 12:12:37 +0200305 cs->ctx = h1s;
306 h1s->cs = cs;
307 }
Christopher Faulet47365272018-10-31 17:40:50 +0100308 else {
Olivier Houchardf502aca2018-12-14 19:42:40 +0100309 /* For frontend connections we should always have a session */
310 sess = h1c->conn->owner;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100311
312 h1s->csinfo.create_date = sess->accept_date;
313 h1s->csinfo.tv_create = sess->tv_accept;
314 h1s->csinfo.t_handshake = sess->t_handshake;
315 h1s->csinfo.t_idle = -1;
316
Christopher Faulet47365272018-10-31 17:40:50 +0100317 cs = h1s_new_cs(h1s);
318 if (!cs)
319 goto fail;
320 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200321 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100322
323 fail:
324 pool_free(pool_head_h1s, h1s);
325 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200326}
327
328static void h1s_destroy(struct h1s *h1s)
329{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200330 if (h1s) {
331 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200332
Christopher Fauletf2824e62018-10-01 12:12:37 +0200333 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200334
Christopher Fauletf2824e62018-10-01 12:12:37 +0200335 if (h1s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100336 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200337 if (h1s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100338 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200339
Christopher Fauletcb55f482018-12-10 11:56:47 +0100340 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet47365272018-10-31 17:40:50 +0100341 h1c->flags |= H1C_F_WAIT_NEXT_REQ;
342 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR))
343 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200344
Christopher Fauletf2824e62018-10-01 12:12:37 +0200345 cs_free(h1s->cs);
346 pool_free(pool_head_h1s, h1s);
347 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200348}
349
Christopher Fauletfeb11742018-11-29 15:12:34 +0100350static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
351{
352 struct h1s *h1s = cs->ctx;
353
354 if (h1s && !conn_is_back(cs->conn))
355 return &h1s->csinfo;
356 return NULL;
357}
358
Christopher Faulet51dbc942018-09-13 09:05:15 +0200359/*
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100360 * Initialize the mux once it's attached. It is expected that conn->ctx
Christopher Faulet51dbc942018-09-13 09:05:15 +0200361 * points to the existing conn_stream (for outgoing connections) or NULL (for
362 * incoming ones). Returns < 0 on error.
363 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100364static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200365{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200366 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100367 struct task *t = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200368
369 h1c = pool_alloc(pool_head_h1c);
370 if (!h1c)
371 goto fail_h1c;
372 h1c->conn = conn;
373 h1c->px = proxy;
374
375 h1c->flags = H1C_F_NONE;
376 h1c->ibuf = BUF_NULL;
377 h1c->obuf = BUF_NULL;
378 h1c->h1s = NULL;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100379 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200380
Christopher Faulet51dbc942018-09-13 09:05:15 +0200381 LIST_INIT(&h1c->buf_wait.list);
382 h1c->wait_event.task = tasklet_new();
383 if (!h1c->wait_event.task)
384 goto fail;
385 h1c->wait_event.task->process = h1_io_cb;
386 h1c->wait_event.task->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100387 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200388
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100389 if (conn->ctx) {
390 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
391 if (tick_isset(proxy->timeout.serverfin))
392 h1c->shut_timeout = proxy->timeout.serverfin;
393 } else {
394 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
395 if (tick_isset(proxy->timeout.clientfin))
396 h1c->shut_timeout = proxy->timeout.clientfin;
397 }
398 if (tick_isset(h1c->timeout)) {
399 t = task_new(tid_bit);
400 if (!t)
401 goto fail;
402
403 h1c->task = t;
404 t->process = h1_timeout_task;
405 t->context = h1c;
406 t->expire = tick_add(now_ms, h1c->timeout);
407 }
408
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200409 if (!(conn->flags & CO_FL_CONNECTED))
410 h1c->flags |= H1C_F_CS_WAIT_CONN;
411
Christopher Fauletf2824e62018-10-01 12:12:37 +0200412 /* Always Create a new H1S */
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100413 if (!h1s_create(h1c, conn->ctx, sess))
Christopher Fauletf2824e62018-10-01 12:12:37 +0200414 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200415
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100416 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200417
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100418
419 if (t)
420 task_queue(t);
421
Christopher Faulet51dbc942018-09-13 09:05:15 +0200422 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard9b960a82019-01-04 16:51:40 +0100423 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200424
425 /* mux->wake will be called soon to complete the operation */
426 return 0;
427
428 fail:
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100429 if (t)
430 task_free(t);
Christopher Faulet47365272018-10-31 17:40:50 +0100431 if (h1c->wait_event.task)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200432 tasklet_free(h1c->wait_event.task);
433 pool_free(pool_head_h1c, h1c);
434 fail_h1c:
435 return -1;
436}
437
438
439/* release function for a connection. This one should be called to free all
440 * resources allocated to the mux.
441 */
442static void h1_release(struct connection *conn)
443{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100444 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200445
Christopher Faulet51dbc942018-09-13 09:05:15 +0200446 if (h1c) {
447 if (!LIST_ISEMPTY(&h1c->buf_wait.list)) {
448 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
449 LIST_DEL(&h1c->buf_wait.list);
450 LIST_INIT(&h1c->buf_wait.list);
451 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
452 }
453
454 h1_release_buf(h1c, &h1c->ibuf);
455 h1_release_buf(h1c, &h1c->obuf);
456
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100457 if (h1c->task) {
458 h1c->task->context = NULL;
459 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
460 h1c->task = NULL;
461 }
462
Christopher Faulet51dbc942018-09-13 09:05:15 +0200463 if (h1c->wait_event.task)
464 tasklet_free(h1c->wait_event.task);
465
Christopher Fauletf2824e62018-10-01 12:12:37 +0200466 h1s_destroy(h1c->h1s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100467 if (h1c->wait_event.events != 0)
468 conn->xprt->unsubscribe(conn, h1c->wait_event.events,
Christopher Faulet51dbc942018-09-13 09:05:15 +0200469 &h1c->wait_event);
470 pool_free(pool_head_h1c, h1c);
471 }
472
473 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100474 conn->ctx = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200475
476 conn_stop_tracking(conn);
477 conn_full_close(conn);
478 if (conn->destroy_cb)
479 conn->destroy_cb(conn);
480 conn_free(conn);
481}
482
483/******************************************************/
484/* functions below are for the H1 protocol processing */
485/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200486/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
487 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200488 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100489static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200490{
Christopher Faulet570d1612018-11-26 11:13:57 +0100491 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200492
Christopher Faulet570d1612018-11-26 11:13:57 +0100493 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200494 (*(p + 5) > '1' ||
495 (*(p + 5) == '1' && *(p + 7) >= '1')))
496 h1m->flags |= H1_MF_VER_11;
497}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200498
Christopher Faulet9768c262018-10-22 09:34:31 +0200499/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
500 * greater or equal to 1.1
501 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100502static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200503{
Christopher Faulet570d1612018-11-26 11:13:57 +0100504 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200505
Christopher Faulet570d1612018-11-26 11:13:57 +0100506 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200507 (*(p + 5) > '1' ||
508 (*(p + 5) == '1' && *(p + 7) >= '1')))
509 h1m->flags |= H1_MF_VER_11;
510}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200511
Christopher Faulet9768c262018-10-22 09:34:31 +0200512/*
513 * Check the validity of the request version. If the version is valid, it
514 * returns 1. Otherwise, it returns 0.
515 */
516static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
517{
518 struct h1c *h1c = h1s->h1c;
519
520 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
521 * exactly one digit "." one digit. This check may be disabled using
522 * option accept-invalid-http-request.
523 */
524 if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) {
525 if (sl.rq.v.len != 8)
526 return 0;
527
528 if (*(sl.rq.v.ptr + 4) != '/' ||
529 !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) ||
530 *(sl.rq.v.ptr + 6) != '.' ||
531 !isdigit((unsigned char)*(sl.rq.v.ptr + 7)))
532 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200533 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200534 else if (!sl.rq.v.len) {
535 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200536
Christopher Faulet9768c262018-10-22 09:34:31 +0200537 /* RFC 1945 allows only GET for HTTP/0.9 requests */
538 if (sl.rq.meth != HTTP_METH_GET)
539 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200540
Christopher Faulet9768c262018-10-22 09:34:31 +0200541 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
542 if (!sl.rq.u.len)
543 return 0;
544
545 /* Add HTTP version */
546 sl.rq.v = ist("HTTP/1.0");
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100547 return 1;
Christopher Faulet9768c262018-10-22 09:34:31 +0200548 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100549
550 if ((sl.rq.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100551 ((*(sl.rq.v.ptr + 5) > '1') ||
552 ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100553 h1m->flags |= H1_MF_VER_11;
Christopher Faulet9768c262018-10-22 09:34:31 +0200554 return 1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200555}
556
Christopher Faulet9768c262018-10-22 09:34:31 +0200557/*
558 * Check the validity of the response version. If the version is valid, it
559 * returns 1. Otherwise, it returns 0.
Christopher Fauletf2824e62018-10-01 12:12:37 +0200560 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200561static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200562{
Christopher Faulet9768c262018-10-22 09:34:31 +0200563 struct h1c *h1c = h1s->h1c;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200564
Christopher Faulet9768c262018-10-22 09:34:31 +0200565 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
566 * exactly one digit "." one digit. This check may be disabled using
567 * option accept-invalid-http-request.
568 */
569 if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) {
570 if (sl.st.v.len != 8)
571 return 0;
572
573 if (*(sl.st.v.ptr + 4) != '/' ||
574 !isdigit((unsigned char)*(sl.st.v.ptr + 5)) ||
575 *(sl.st.v.ptr + 6) != '.' ||
576 !isdigit((unsigned char)*(sl.st.v.ptr + 7)))
577 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200578 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100579
580 if ((sl.st.v.len == 8) &&
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100581 ((*(sl.st.v.ptr + 5) > '1') ||
582 ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1'))))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100583 h1m->flags |= H1_MF_VER_11;
584
Christopher Faulet9768c262018-10-22 09:34:31 +0200585 return 1;
586}
587/* Remove all "Connection:" headers from the HTX message <htx> */
588static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx)
589{
590 struct ist hdr = {.ptr = "Connection", .len = 10};
591 struct http_hdr_ctx ctx;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200592
Christopher Faulet9768c262018-10-22 09:34:31 +0200593 while (http_find_header(htx, hdr, &ctx, 1))
594 http_remove_header(htx, &ctx);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200595
Christopher Faulet9768c262018-10-22 09:34:31 +0200596 h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO);
597}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200598
Christopher Faulet9768c262018-10-22 09:34:31 +0200599/* Add a "Connection:" header with the value <value> into the HTX message
600 * <htx>.
601 */
602static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value)
603{
604 struct ist hdr = {.ptr = "Connection", .len = 10};
Christopher Fauletf2824e62018-10-01 12:12:37 +0200605
Christopher Faulet9768c262018-10-22 09:34:31 +0200606 http_add_header(htx, hdr, value);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200607}
608
609/* Deduce the connection mode of the client connection, depending on the
610 * configuration and the H1 message flags. This function is called twice, the
611 * first time when the request is parsed and the second time when the response
612 * is parsed.
613 */
614static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
615{
616 struct proxy *fe = h1s->h1c->px;
617 int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */
618
Christopher Faulet6c9bbb22019-03-26 21:37:23 +0100619 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200620 flag = H1S_F_WANT_CLO;
621
622 /* flags order: CLO > SCL > TUN > KAL */
623 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
624 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
625
626 if (h1m->flags & H1_MF_RESP) {
627 /* Either we've established an explicit tunnel, or we're
628 * switching the protocol. In both cases, we're very unlikely to
629 * understand the next protocols. We have to switch to tunnel
630 * mode, so that we transfer the request and responses then let
631 * this protocol pass unmodified. When we later implement
632 * specific parsers for such protocols, we'll want to check the
633 * Upgrade header which contains information about that protocol
634 * for responses with status 101 (eg: see RFC2817 about TLS).
635 */
636 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
637 h1s->status == 101)
638 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Fauletbd44ca62018-11-29 09:55:22 +0100639 else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */
640 (h1m->flags & H1_MF_CONN_CLO && h1s->req.state != H1_MSG_DONE)) /*explicit close and unfinished request */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200641 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
642 }
643 else {
644 if (h1s->flags & H1S_F_WANT_KAL &&
645 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
646 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
647 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
648 }
649
650 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
651 if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED)
652 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
653}
654
655/* Deduce the connection mode of the client connection, depending on the
656 * configuration and the H1 message flags. This function is called twice, the
657 * first time when the request is parsed and the second time when the response
658 * is parsed.
659 */
660static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
661{
Christopher Fauleta1692f52018-11-22 10:19:50 +0100662 struct h1c *h1c = h1s->h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100663 struct session *sess = h1s->sess;
Christopher Fauleta1692f52018-11-22 10:19:50 +0100664 struct proxy *be = h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200665 int flag = H1S_F_WANT_KAL;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100666 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200667
Christopher Fauletf2824e62018-10-01 12:12:37 +0200668 /* For the server connection: server-close == httpclose */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100669 if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200670 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Olivier Houchardf502aca2018-12-14 19:42:40 +0100671 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
Christopher Fauletf2824e62018-10-01 12:12:37 +0200672 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
673 flag = H1S_F_WANT_CLO;
674
675 /* flags order: CLO > SCL > TUN > KAL */
676 if ((h1s->flags & H1S_F_WANT_MSK) < flag)
677 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag;
678
679 if (h1m->flags & H1_MF_RESP) {
680 /* Either we've established an explicit tunnel, or we're
681 * switching the protocol. In both cases, we're very unlikely to
682 * understand the next protocols. We have to switch to tunnel
683 * mode, so that we transfer the request and responses then let
684 * this protocol pass unmodified. When we later implement
685 * specific parsers for such protocols, we'll want to check the
686 * Upgrade header which contains information about that protocol
687 * for responses with status 101 (eg: see RFC2817 about TLS).
688 */
689 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
690 h1s->status == 101)
691 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
692 else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */
693 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
694 else if (h1s->flags & H1S_F_WANT_KAL &&
695 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
696 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
697 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
698 }
Christopher Faulet70033782018-12-05 13:50:11 +0100699 else {
700 if (h1s->flags & H1S_F_WANT_KAL &&
701 (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */
702 h1m->flags & H1_MF_CONN_CLO)) /* explicit close */
703 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
704 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200705
706 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
707 if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED)
708 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200709}
710
Christopher Faulet9768c262018-10-22 09:34:31 +0200711static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m,
712 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200713{
714 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200715
716 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
717 * token is found
718 */
719 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200720 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200721
722 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200723 if (h1m->flags & H1_MF_CONN_CLO) {
724 if (conn_val)
725 *conn_val = ist("");
726 if (htx)
727 h1_remove_conn_hdrs(h1m, htx);
728 }
729 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) {
730 if (conn_val)
731 *conn_val = ist("keep-alive");
732 if (htx)
733 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
734 }
Christopher Fauletce851492018-12-07 18:06:59 +0100735 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) {
736 if (conn_val)
737 *conn_val = ist("");
738 if (htx)
739 h1_remove_conn_hdrs(h1m, htx);
740 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200741 }
742 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet9768c262018-10-22 09:34:31 +0200743 if (h1m->flags & H1_MF_CONN_KAL) {
744 if (conn_val)
745 *conn_val = ist("");
746 if (htx)
747 h1_remove_conn_hdrs(h1m, htx);
748 }
749 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
750 if (conn_val)
751 *conn_val = ist("close");
752 if (htx)
753 h1_add_conn_hdr(h1m, htx, ist("close"));
754 }
Christopher Fauletce851492018-12-07 18:06:59 +0100755 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
756 if (conn_val)
757 *conn_val = ist("");
758 if (htx)
759 h1_remove_conn_hdrs(h1m, htx);
760 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200761 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200762}
763
Christopher Faulet9768c262018-10-22 09:34:31 +0200764static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m,
765 struct htx *htx, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200766{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200767 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
768 * token is found
769 */
770 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200771 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200772
773 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200774 if (h1m->flags & H1_MF_CONN_CLO) {
775 if (conn_val)
776 *conn_val = ist("");
777 if (htx)
778 h1_remove_conn_hdrs(h1m, htx);
779 }
Willy Tarreau7701cad2019-02-08 15:35:38 +0100780 if (!(h1m->flags & H1_MF_CONN_KAL) &&
781 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200782 if (conn_val)
783 *conn_val = ist("keep-alive");
784 if (htx)
785 h1_add_conn_hdr(h1m, htx, ist("keep-alive"));
786 }
Willy Tarreau7701cad2019-02-08 15:35:38 +0100787 else if ((h1m->flags & H1_MF_CONN_KAL) &&
788 ((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
Christopher Fauletce851492018-12-07 18:06:59 +0100789 if (conn_val)
790 *conn_val = ist("");
791 if (htx)
792 h1_remove_conn_hdrs(h1m, htx);
793 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200794 }
795 else { /* H1S_F_WANT_CLO */
Christopher Faulet9768c262018-10-22 09:34:31 +0200796 if (h1m->flags & H1_MF_CONN_KAL) {
797 if (conn_val)
798 *conn_val = ist("");
799 if (htx)
800 h1_remove_conn_hdrs(h1m, htx);
801 }
802 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) {
803 if (conn_val)
804 *conn_val = ist("close");
805 if (htx)
806 h1_add_conn_hdr(h1m, htx, ist("close"));
807 }
Christopher Fauletce851492018-12-07 18:06:59 +0100808 if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) {
809 if (conn_val)
810 *conn_val = ist("");
811 if (htx)
812 h1_remove_conn_hdrs(h1m, htx);
813 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200814 }
Christopher Faulet9768c262018-10-22 09:34:31 +0200815}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200816
Christopher Faulet9768c262018-10-22 09:34:31 +0200817/* Set the right connection mode and update "Connection:" header if
818 * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX
819 * message is updated accordingly. When <conn_val> is not NULL, it is set with
820 * the new header value.
821 */
822static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m,
823 struct htx *htx, struct ist *conn_val)
824{
825 if (!conn_is_back(h1s->h1c->conn)) {
826 h1_set_cli_conn_mode(h1s, h1m);
827 if (h1m->flags & H1_MF_RESP)
828 h1_update_res_conn_hdr(h1s, h1m, htx, conn_val);
829 }
830 else {
831 h1_set_srv_conn_mode(h1s, h1m);
832 if (!(h1m->flags & H1_MF_RESP))
833 h1_update_req_conn_hdr(h1s, h1m, htx, conn_val);
834 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200835}
836
Christopher Faulete44769b2018-11-29 23:01:45 +0100837
838/* Append the description of what is present in error snapshot <es> into <out>.
839 * The description must be small enough to always fit in a buffer. The output
840 * buffer may be the trash so the trash must not be used inside this function.
841 */
842static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
843{
844 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100845 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
846 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
847 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
848 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
849 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
850 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +0100851}
852/*
853 * Capture a bad request or response and archive it in the proxy's structure.
854 * By default it tries to report the error position as h1m->err_pos. However if
855 * this one is not set, it will then report h1m->next, which is the last known
856 * parsing point. The function is able to deal with wrapping buffers. It always
857 * displays buffers as a contiguous area starting at buf->p. The direction is
858 * determined thanks to the h1m's flags.
859 */
860static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
861 struct h1m *h1m, struct buffer *buf)
862{
863 struct session *sess = h1c->conn->owner;
864 struct proxy *proxy = h1c->px;
865 struct proxy *other_end = sess->fe;
866 union error_snapshot_ctx ctx;
867
Willy Tarreau598d7fc2018-12-18 18:10:38 +0100868 if (h1s->cs->data && !(h1m->flags & H1_MF_RESP))
Christopher Faulete44769b2018-11-29 23:01:45 +0100869 other_end = si_strm(h1s->cs->data)->be;
870
871 /* http-specific part now */
872 ctx.h1.state = h1m->state;
873 ctx.h1.c_flags = h1c->flags;
874 ctx.h1.s_flags = h1s->flags;
875 ctx.h1.m_flags = h1m->flags;
876 ctx.h1.m_clen = h1m->curr_len;
877 ctx.h1.m_blen = h1m->body_len;
878
879 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
880 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100881 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
882 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +0100883}
884
Christopher Fauletadb22202018-12-12 10:32:09 +0100885/* Emit the chunksize followed by a CRLF in front of data of the buffer
886 * <buf>. It goes backwards and starts with the byte before the buffer's
887 * head. The caller is responsible for ensuring there is enough room left before
888 * the buffer's head for the string.
889 */
890static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
891{
892 char *beg, *end;
893
894 beg = end = b_head(buf);
895 *--beg = '\n';
896 *--beg = '\r';
897 do {
898 *--beg = hextab[chksz & 0xF];
899 } while (chksz >>= 4);
900 buf->head -= (end - beg);
901 b_add(buf, end - beg);
902}
903
904/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
905 * ensuring there is enough room left in the buffer for the string. */
906static void h1_emit_chunk_crlf(struct buffer *buf)
907{
908 *(b_peek(buf, b_data(buf))) = '\r';
909 *(b_peek(buf, b_data(buf) + 1)) = '\n';
910 b_add(buf, 2);
911}
912
Christopher Faulet129817b2018-09-20 16:14:40 +0200913/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +0100914 * Switch the request to tunnel mode. This function must only be called for
915 * CONNECT requests. On the client side, the mux is mark as busy on input,
916 * waiting the response.
917 */
918static void h1_set_req_tunnel_mode(struct h1s *h1s)
919{
920 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
921 h1s->req.state = H1_MSG_TUNNEL;
922 if (!conn_is_back(h1s->h1c->conn))
923 h1s->h1c->flags |= H1C_F_IN_BUSY;
924}
925
926/*
927 * Switch the response to tunnel mode. This function must only be called on
928 * successfull replies to CONNECT requests or on protocol switching. On the
929 * server side, if the request is not finished, the mux is mark as busy on
930 * input. Otherwise the request is also switch to tunnel mode.
931 */
932static void h1_set_res_tunnel_mode(struct h1s *h1s)
933{
934 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
935 h1s->res.state = H1_MSG_TUNNEL;
936 if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE)
937 h1s->h1c->flags |= H1C_F_IN_BUSY;
938 else {
939 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
940 h1s->req.state = H1_MSG_TUNNEL;
941 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
942 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
943 tasklet_wakeup(h1s->h1c->wait_event.task);
944 }
945 }
946}
947
948/*
Christopher Fauletcdc90e92019-03-28 13:28:46 +0100949 * Handle 100-Continue responses or any other informational 1xx responses which
950 * is non-final. In such case, this function reset the response parser. It is
951 * the caller responsibility to call this function when appropriate.
952 */
953static void h1_handle_1xx_response(struct h1s *h1s, struct h1m *h1m)
954{
955 if ((h1m->flags & H1_MF_RESP) && h1m->state == H1_MSG_DONE &&
956 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
957 h1m_init_res(&h1s->res);
958 h1m->flags |= H1_MF_NO_PHDR;
959 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
960 }
961}
962
963/*
Christopher Faulet129817b2018-09-20 16:14:40 +0200964 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +0200965 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
966 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -0800967 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +0200968 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200969static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +0200970 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +0200971{
972 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100973 union h1_sl h1sl;
974 unsigned int flags = HTX_SL_F_NONE;
Christopher Faulet129817b2018-09-20 16:14:40 +0200975 int ret = 0;
976
Christopher Fauletd44ad5b2018-11-19 21:52:12 +0100977 if (!max)
978 goto end;
979
Christopher Faulet129817b2018-09-20 16:14:40 +0200980 /* Realing input buffer if necessary */
981 if (b_head(buf) + b_data(buf) > b_wrap(buf))
982 b_slow_realign(buf, trash.area, 0);
983
Christopher Fauletf2824e62018-10-01 12:12:37 +0200984 ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max,
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100985 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl);
Christopher Faulet129817b2018-09-20 16:14:40 +0200986 if (ret <= 0) {
987 /* Incomplete or invalid message. If the buffer is full, it's an
988 * error because headers are too large to be handled by the
989 * parser. */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200990 if (ret < 0 || (!ret && b_full(buf)))
991 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +0200992 goto end;
993 }
994
995 /* messages headers fully parsed, do some checks to prepare the body
996 * parsing.
997 */
998
999 /* Be sure to keep some space to do headers rewritting */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001000 if (ret > (b_size(buf) - global.tune.maxrewrite))
1001 goto error;
Christopher Faulet129817b2018-09-20 16:14:40 +02001002
Christopher Faulet9768c262018-10-22 09:34:31 +02001003 /* Save the request's method or the response's status, check if the body
1004 * length is known and check the VSN validity */
Christopher Faulet129817b2018-09-20 16:14:40 +02001005 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001006 h1s->meth = h1sl.rq.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001007
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001008 /* By default, request have always a known length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001009 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001010
1011 if (h1s->meth == HTTP_METH_CONNECT) {
1012 /* Switch CONNECT requests to tunnel mode */
1013 h1_set_req_tunnel_mode(h1s);
1014 }
1015 else if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len) {
1016 /* Switch requests with no body to done. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001017 h1m->state = H1_MSG_DONE;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001018 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001019
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001020 if (!h1_process_req_vsn(h1s, h1m, h1sl)) {
1021 h1m->err_pos = h1sl.rq.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001022 h1m->err_state = h1m->state;
1023 goto vsn_error;
1024 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001025 }
1026 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001027 h1s->status = h1sl.st.status;
Christopher Faulet129817b2018-09-20 16:14:40 +02001028
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001029 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
1030 h1s->status == 101) {
1031 /* Switch successfull replies to CONNECT requests and
1032 * protocol switching to tunnel mode. */
1033 h1_set_res_tunnel_mode(h1s);
1034 }
1035 else if ((h1s->meth == HTTP_METH_HEAD) ||
1036 (h1s->status >= 100 && h1s->status < 200) ||
1037 (h1s->status == 204) || (h1s->status == 304)) {
1038 /* Switch responses without body to done. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001039 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
1040 h1m->flags |= H1_MF_XFER_LEN;
1041 h1m->curr_len = h1m->body_len = 0;
1042 h1m->state = H1_MSG_DONE;
1043 }
1044 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001045 /* Responses with a known body length. Switch requests
1046 * with no body to done. */
Christopher Faulet129817b2018-09-20 16:14:40 +02001047 h1m->flags |= H1_MF_XFER_LEN;
1048 if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len)
1049 h1m->state = H1_MSG_DONE;
1050 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001051 else {
1052 /* Responses with an unknown body length */
Christopher Faulet129817b2018-09-20 16:14:40 +02001053 h1m->state = H1_MSG_TUNNEL;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001054 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001055
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001056 if (!h1_process_res_vsn(h1s, h1m, h1sl)) {
1057 h1m->err_pos = h1sl.st.v.ptr - b_head(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001058 h1m->err_state = h1m->state;
1059 goto vsn_error;
1060 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001061 }
1062
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001063 /* Set HTX start-line flags */
1064 if (h1m->flags & H1_MF_VER_11)
1065 flags |= HTX_SL_F_VER_11;
1066 if (h1m->flags & H1_MF_XFER_ENC)
1067 flags |= HTX_SL_F_XFER_ENC;
1068 if (h1m->flags & H1_MF_XFER_LEN) {
1069 flags |= HTX_SL_F_XFER_LEN;
1070 if (h1m->flags & H1_MF_CHNK)
1071 flags |= HTX_SL_F_CHNK;
1072 else if (h1m->flags & H1_MF_CLEN)
1073 flags |= HTX_SL_F_CLEN;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001074 if (h1m->state == H1_MSG_DONE)
1075 flags |= HTX_SL_F_BODYLESS;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001076 }
1077
Christopher Faulet9768c262018-10-22 09:34:31 +02001078 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001079 struct htx_sl *sl;
1080
1081 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
1082 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001083 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001084 sl->info.req.meth = h1s->meth;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001085 }
1086 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001087 struct htx_sl *sl;
1088
1089 flags |= HTX_SL_F_IS_RESP;
1090 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
1091 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Faulet9768c262018-10-22 09:34:31 +02001092 goto error;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001093 sl->info.res.status = h1s->status;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001094 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001095
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001096 if (h1m->state == H1_MSG_DONE) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001097 if (!htx_add_endof(htx, HTX_BLK_EOM))
1098 goto error;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001099 h1s->cs->flags |= CS_FL_EOI;
1100 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001101
1102 h1_process_conn_mode(h1s, h1m, htx, NULL);
1103
1104 /* If body length cannot be determined, set htx->extra to
1105 * ULLONG_MAX. This value is impossible in other cases.
1106 */
1107 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
1108
1109 /* Recheck there is enough space to do headers rewritting */
1110 if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite)
1111 goto error;
1112
1113 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001114 end:
1115 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001116
1117 error:
Christopher Fauletf2824e62018-10-01 12:12:37 +02001118 h1m->err_state = h1m->state;
1119 h1m->err_pos = h1m->next;
Christopher Faulet9768c262018-10-22 09:34:31 +02001120 vsn_error:
1121 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulete44769b2018-11-29 23:01:45 +01001122 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001123 ret = 0;
1124 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001125}
1126
1127/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001128 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
1129 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag
Christopher Faulet129817b2018-09-20 16:14:40 +02001130 * and filling h1s->err_pos and h1s->err_state fields. This functions is
Joseph Herlant30bc5092018-11-25 10:52:20 -08001131 * responsible to update the parser state <h1m>.
Christopher Faulet129817b2018-09-20 16:14:40 +02001132 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001133static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001134 struct buffer *buf, size_t *ofs, size_t max,
Christopher Fauletafe57842019-01-21 11:55:02 +01001135 struct buffer *htxbuf, size_t reserve)
Christopher Faulet129817b2018-09-20 16:14:40 +02001136{
Christopher Fauletafe57842019-01-21 11:55:02 +01001137 uint32_t data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001138 size_t total = 0;
1139 int ret = 0;
1140
Christopher Fauletafe57842019-01-21 11:55:02 +01001141 data_space = htx_free_data_space(htx);
1142 if (data_space <= reserve)
1143 goto end;
1144 data_space -= reserve;
1145
Christopher Faulet129817b2018-09-20 16:14:40 +02001146 if (h1m->flags & H1_MF_XFER_LEN) {
1147 if (h1m->flags & H1_MF_CLEN) {
1148 /* content-length: read only h2m->body_len */
1149 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001150 if (ret > data_space)
1151 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001152 if ((uint64_t)ret > h1m->curr_len)
1153 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001154 if (ret > b_contig_data(buf, *ofs))
1155 ret = b_contig_data(buf, *ofs);
1156 if (ret) {
Willy Tarreau78f548f2018-12-05 10:02:39 +01001157 /* very often with large files we'll face the following
1158 * situation :
1159 * - htx is empty and points to <htxbuf>
1160 * - ret == buf->data
1161 * - buf->head == sizeof(struct htx)
1162 * => we can swap the buffers and place an htx header into
1163 * the target buffer instead
1164 */
1165 if (unlikely(htx_is_empty(htx) && ret == b_data(buf) &&
1166 !*ofs && b_head_ofs(buf) == sizeof(struct htx))) {
1167 void *raw_area = buf->area;
1168 void *htx_area = htxbuf->area;
1169 struct htx_blk *blk;
1170
1171 buf->area = htx_area;
1172 htxbuf->area = raw_area;
1173 htx = (struct htx *)htxbuf->area;
1174 htx->size = htxbuf->size - sizeof(*htx);
1175 htx_reset(htx);
1176 b_set_data(htxbuf, b_size(htxbuf));
1177
1178 blk = htx_add_blk(htx, HTX_BLK_DATA, ret);
1179 blk->info += ret;
1180 /* nothing else to do, the old buffer now contains an
1181 * empty pre-initialized HTX header
1182 */
1183 }
1184 else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001185 goto end;
1186 h1m->curr_len -= ret;
1187 *ofs += ret;
1188 total += ret;
1189 }
1190
1191 if (!h1m->curr_len) {
1192 if (!htx_add_endof(htx, HTX_BLK_EOM))
1193 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001194 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001195 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet9768c262018-10-22 09:34:31 +02001196 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001197 }
1198 else if (h1m->flags & H1_MF_CHNK) {
1199 new_chunk:
1200 /* te:chunked : parse chunks */
1201 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001202 ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max);
Christopher Faulet129817b2018-09-20 16:14:40 +02001203 if (ret <= 0)
1204 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001205 h1m->state = H1_MSG_CHUNK_SIZE;
1206
Christopher Faulet129817b2018-09-20 16:14:40 +02001207 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001208 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001209 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001210 }
1211
1212 if (h1m->state == H1_MSG_CHUNK_SIZE) {
1213 unsigned int chksz;
1214
Christopher Fauletf2824e62018-10-01 12:12:37 +02001215 ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz);
Christopher Faulet129817b2018-09-20 16:14:40 +02001216 if (ret <= 0)
1217 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001218 if (!chksz) {
1219 if (!htx_add_endof(htx, HTX_BLK_EOD))
1220 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001221 h1s->flags |= H1S_F_HAVE_I_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001222 h1m->state = H1_MSG_TRAILERS;
1223 }
1224 else
1225 h1m->state = H1_MSG_DATA;
1226
Christopher Faulet129817b2018-09-20 16:14:40 +02001227 h1m->curr_len = chksz;
1228 h1m->body_len += chksz;
1229 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001230 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001231 total += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001232 }
1233
1234 if (h1m->state == H1_MSG_DATA) {
1235 ret = max;
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 if (ret > data_space)
1237 ret = data_space;
Christopher Faulet129817b2018-09-20 16:14:40 +02001238 if ((uint64_t)ret > h1m->curr_len)
1239 ret = h1m->curr_len;
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 if (ret > b_contig_data(buf, *ofs))
1241 ret = b_contig_data(buf, *ofs);
1242 if (ret) {
1243 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1244 goto end;
1245 h1m->curr_len -= ret;
1246 max -= ret;
1247 *ofs += ret;
1248 total += ret;
1249 }
1250 if (!h1m->curr_len) {
1251 h1m->state = H1_MSG_CHUNK_CRLF;
Christopher Fauletafe57842019-01-21 11:55:02 +01001252 data_space = htx_free_data_space(htx);
1253 if (data_space <= reserve)
1254 goto end;
1255 data_space -= reserve;
Christopher Faulet9768c262018-10-22 09:34:31 +02001256 goto new_chunk;
1257 }
1258 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001259 }
1260
1261 if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet17276482018-11-22 11:44:35 +01001262 /* Trailers were alread parsed, only the EOM
1263 * need to be added */
Willy Tarreau34d23482019-01-03 17:46:56 +01001264 if (h1s->flags & H1S_F_HAVE_I_TLR)
Christopher Faulet17276482018-11-22 11:44:35 +01001265 goto skip_tlr_parsing;
1266
Christopher Fauletf2824e62018-10-01 12:12:37 +02001267 ret = h1_measure_trailers(buf, *ofs, *ofs + max);
Christopher Faulet9768c262018-10-22 09:34:31 +02001268 if (ret > data_space)
1269 ret = (htx_is_empty(htx) ? -1 : 0);
Christopher Faulet129817b2018-09-20 16:14:40 +02001270 if (ret <= 0)
1271 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001272
1273 /* Realing input buffer if tailers wrap. For now
1274 * this is a workaroung. Because trailers are
1275 * not split on CRLF, like headers, there is no
1276 * way to know where to split it when trailers
1277 * wrap. This is a limitation of
1278 * h1_measure_trailers.
1279 */
1280 if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret))
1281 b_slow_realign(buf, trash.area, 0);
1282
1283 if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret)))
1284 goto end;
Willy Tarreau34d23482019-01-03 17:46:56 +01001285 h1s->flags |= H1S_F_HAVE_I_TLR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001286 max -= ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001287 *ofs += ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001288 total += ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001289
Christopher Faulet17276482018-11-22 11:44:35 +01001290 skip_tlr_parsing:
Christopher Faulet9768c262018-10-22 09:34:31 +02001291 if (!htx_add_endof(htx, HTX_BLK_EOM))
1292 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001293 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001294 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001295 }
1296 }
1297 else {
1298 /* XFER_LEN is set but not CLEN nor CHNK, it means there
1299 * is no body. Switch the message in DONE state
1300 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001301 if (!htx_add_endof(htx, HTX_BLK_EOM))
1302 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001303 h1m->state = H1_MSG_DONE;
Christopher Fauletdbe2cb42019-03-22 14:09:41 +01001304 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet129817b2018-09-20 16:14:40 +02001305 }
1306 }
1307 else {
1308 /* no content length, read till SHUTW */
Christopher Faulet9768c262018-10-22 09:34:31 +02001309 ret = max;
1310 if (ret > data_space)
1311 ret = data_space;
1312 if (ret > b_contig_data(buf, *ofs))
1313 ret = b_contig_data(buf, *ofs);
1314 if (ret) {
1315 if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret)))
1316 goto end;
1317
Olivier Houchardcf42d5a2018-12-04 17:41:58 +01001318 *ofs += ret;
1319 total = ret;
Christopher Faulet9768c262018-10-22 09:34:31 +02001320 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001321 }
1322
1323 end:
1324 if (ret < 0) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001325 h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR);
Christopher Faulet129817b2018-09-20 16:14:40 +02001326 h1m->err_state = h1m->state;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001327 h1m->err_pos = *ofs + max + ret;
Christopher Faulete44769b2018-11-29 23:01:45 +01001328 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001329 return 0;
1330 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001331 /* update htx->extra, only when the body length is known */
1332 if (h1m->flags & H1_MF_XFER_LEN)
1333 htx->extra = h1m->curr_len;
Christopher Faulet129817b2018-09-20 16:14:40 +02001334 return total;
1335}
1336
1337/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001338 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001339 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1340 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001341 */
Christopher Faulet539e0292018-11-19 10:40:09 +01001342static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, int flags)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001343{
Christopher Faulet539e0292018-11-19 10:40:09 +01001344 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001345 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001346 struct htx *htx;
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001347 size_t data = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001348 size_t total = 0;
1349 size_t ret = 0;
Christopher Fauletafe57842019-01-21 11:55:02 +01001350 size_t count, rsv;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001351 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001352
Christopher Faulet539e0292018-11-19 10:40:09 +01001353 htx = htx_from_buf(buf);
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001354
Christopher Fauletf2824e62018-10-01 12:12:37 +02001355 if (!conn_is_back(h1c->conn)) {
1356 h1m = &h1s->req;
1357 errflag = H1S_F_REQ_ERROR;
1358 }
1359 else {
1360 h1m = &h1s->res;
1361 errflag = H1S_F_RES_ERROR;
1362 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001363
Christopher Faulet74027762019-02-26 14:45:05 +01001364 data = htx->data;
1365 count = b_data(&h1c->ibuf);
1366 if (!count)
1367 goto end;
1368 rsv = ((flags & CO_RFL_KEEP_RSV) ? global.tune.maxrewrite : 0);
1369
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001370 if (htx_is_empty(htx))
1371 h1_handle_1xx_response(h1s, h1m);
1372
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001373 do {
Christopher Faulet129817b2018-09-20 16:14:40 +02001374 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001375 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001376 if (!ret)
1377 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001378 }
1379 else if (h1m->state <= H1_MSG_TRAILERS) {
Christopher Fauletafe57842019-01-21 11:55:02 +01001380 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001381 htx = htx_from_buf(buf);
Christopher Faulet129817b2018-09-20 16:14:40 +02001382 if (!ret)
1383 break;
1384 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001385 else if (h1m->state == H1_MSG_DONE) {
1386 h1c->flags |= H1C_F_IN_BUSY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001387 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001388 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001389 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Fauletafe57842019-01-21 11:55:02 +01001390 ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001391 htx = htx_from_buf(buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001392 if (!ret)
1393 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001394 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001395 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001396 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001397 break;
1398 }
1399
Christopher Faulet539e0292018-11-19 10:40:09 +01001400 count -= ret;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001401 } while (!(h1s->flags & errflag) && count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001402
Christopher Faulet47365272018-10-31 17:40:50 +01001403 if (h1s->flags & errflag)
1404 goto parsing_err;
Christopher Faulet129817b2018-09-20 16:14:40 +02001405
Christopher Faulet539e0292018-11-19 10:40:09 +01001406 b_del(&h1c->ibuf, total);
1407
1408 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001409 htx_to_buf(htx, buf);
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001410 data = (htx->data - data);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001411 if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001412 h1c->flags &= ~H1C_F_IN_FULL;
1413 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet47365272018-10-31 17:40:50 +01001414 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001415
Christopher Fauletcf56b992018-12-11 16:12:31 +01001416 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1417
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001418 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001419 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletcf56b992018-12-11 16:12:31 +01001420 else if (!htx_is_empty(htx))
1421 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet539e0292018-11-19 10:40:09 +01001422
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001423 if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) {
1424 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet26922382019-03-08 15:13:41 +01001425 if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001426 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001427 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001428
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001429 return data;
Christopher Faulet47365272018-10-31 17:40:50 +01001430
1431 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001432 b_reset(&h1c->ibuf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001433 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001434 htx_to_buf(htx, buf);
Christopher Faulet539e0292018-11-19 10:40:09 +01001435 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet9768c262018-10-22 09:34:31 +02001436 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001437}
1438
Christopher Faulet129817b2018-09-20 16:14:40 +02001439/*
1440 * Process outgoing data. It parses data and transfer them from the channel buffer into
1441 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1442 * 0 if it couldn't proceed.
1443 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001444static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1445{
Christopher Faulet129817b2018-09-20 16:14:40 +02001446 struct h1s *h1s = h1c->h1s;
1447 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001448 struct htx *chn_htx;
1449 struct htx_blk *blk;
1450 struct buffer *tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001451 size_t total = 0;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001452 int process_conn_mode = 1; /* If still 1 on EOH, process the connection mode */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001453 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001454
Christopher Faulet47365272018-10-31 17:40:50 +01001455 if (!count)
1456 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001457
Christopher Faulet9768c262018-10-22 09:34:31 +02001458 chn_htx = htx_from_buf(buf);
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001459 if (htx_is_empty(chn_htx))
1460 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001461
Christopher Faulet51dbc942018-09-13 09:05:15 +02001462 if (!h1_get_buf(h1c, &h1c->obuf)) {
1463 h1c->flags |= H1C_F_OUT_ALLOC;
1464 goto end;
1465 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001466
Christopher Fauletf2824e62018-10-01 12:12:37 +02001467 if (!conn_is_back(h1c->conn)) {
1468 h1m = &h1s->res;
1469 errflag = H1S_F_RES_ERROR;
1470 }
1471 else {
1472 h1m = &h1s->req;
1473 errflag = H1S_F_REQ_ERROR;
1474 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001475
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001476 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001477 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001478
1479 tmp = get_trash_chunk();
Willy Tarreauc5efa332018-12-05 11:19:27 +01001480
Willy Tarreau3815b222018-12-11 19:50:43 +01001481 /* Perform some optimizations to reduce the number of buffer copies.
1482 * First, if the mux's buffer is empty and the htx area contains
1483 * exactly one data block of the same size as the requested count,
1484 * then it's possible to simply swap the caller's buffer with the
1485 * mux's output buffer and adjust offsets and length to match the
1486 * entire DATA HTX block in the middle. In this case we perform a
1487 * true zero-copy operation from end-to-end. This is the situation
1488 * that happens all the time with large files. Second, if this is not
1489 * possible, but the mux's output buffer is empty, we still have an
1490 * opportunity to avoid the copy to the intermediary buffer, by making
1491 * the intermediary buffer's area point to the output buffer's area.
1492 * In this case we want to skip the HTX header to make sure that copies
1493 * remain aligned and that this operation remains possible all the
1494 * time. This goes for headers, data blocks and any data extracted from
1495 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001496 */
1497 if (!b_data(&h1c->obuf)) {
Olivier Houchard84cca662018-12-14 16:28:08 +01001498 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001499
1500 if (chn_htx->used == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001501 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001502 htx_get_blk_value(chn_htx, blk).len == count) {
1503 void *old_area = h1c->obuf.area;
1504
1505 h1c->obuf.area = buf->area;
1506 h1c->obuf.data = count;
1507
1508 buf->area = old_area;
1509 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001510
1511 /* The message is chunked. We need to emit the chunk
1512 * size. We have at least the size of the struct htx to
1513 * write the chunk envelope. It should be enough.
1514 */
1515 if (h1m->flags & H1_MF_CHNK) {
1516 h1_emit_chunk_size(&h1c->obuf, count);
1517 h1_emit_chunk_crlf(&h1c->obuf);
1518 }
1519
Willy Tarreau3815b222018-12-11 19:50:43 +01001520 total += count;
1521 goto out;
1522 }
Willy Tarreauc5efa332018-12-05 11:19:27 +01001523 tmp->area = h1c->obuf.area + h1c->obuf.head;
1524 }
1525
Christopher Faulet9768c262018-10-22 09:34:31 +02001526 tmp->size = b_room(&h1c->obuf);
1527
Christopher Fauletb2e84162018-12-06 11:39:49 +01001528 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001529 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001530 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001531 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001532 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001533 uint32_t vlen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001534
Christopher Fauletb2e84162018-12-06 11:39:49 +01001535 vlen = sz;
1536 if (vlen > count) {
1537 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
1538 goto copy;
1539 vlen = count;
1540 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001541
Christopher Fauletb2e84162018-12-06 11:39:49 +01001542 switch (type) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001543 case HTX_BLK_UNUSED:
Christopher Faulet129817b2018-09-20 16:14:40 +02001544 break;
Christopher Faulet9768c262018-10-22 09:34:31 +02001545
1546 case HTX_BLK_REQ_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001547 h1m_init_req(h1m);
1548 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001549 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001550 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001551 h1_parse_req_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001552 if (!htx_reqline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001553 goto copy;
1554 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001555 if (sl->flags & HTX_SL_F_BODYLESS)
1556 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001557 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001558 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001559
Christopher Faulet9768c262018-10-22 09:34:31 +02001560 case HTX_BLK_RES_SL:
Christopher Faulet66229af2018-11-28 16:06:57 +01001561 h1m_init_res(h1m);
1562 h1m->flags |= H1_MF_NO_PHDR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001563 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001564 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001565 h1_parse_res_vsn(h1m, sl);
Christopher Fauletc59ff232018-12-03 13:58:44 +01001566 if (!htx_stline_to_h1(sl, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001567 goto copy;
Christopher Faulet03599112018-11-27 11:21:21 +01001568 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001569 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001570 if (sl->info.res.status < 200 &&
1571 (sl->info.res.status == 100 || sl->info.res.status >= 102))
1572 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001573 h1m->state = H1_MSG_HDR_FIRST;
1574 break;
1575
1576 case HTX_BLK_HDR:
Christopher Faulet9768c262018-10-22 09:34:31 +02001577 h1m->state = H1_MSG_HDR_NAME;
1578 n = htx_get_blk_name(chn_htx, blk);
1579 v = htx_get_blk_value(chn_htx, blk);
1580
1581 if (isteqi(n, ist("transfer-encoding")))
1582 h1_parse_xfer_enc_header(h1m, v);
Willy Tarreau27cd2232019-01-03 21:52:42 +01001583 else if (isteqi(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001584 /* Only skip C-L header with invalid value. */
1585 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001586 goto skip_hdr;
1587 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001588 else if (isteqi(n, ist("connection"))) {
1589 h1_parse_connection_header(h1m, v);
1590 h1_process_conn_mode(h1s, h1m, NULL, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001591 process_conn_mode = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001592 if (!v.len)
1593 goto skip_hdr;
1594 }
1595
Christopher Fauletc59ff232018-12-03 13:58:44 +01001596 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001597 goto copy;
1598 skip_hdr:
1599 h1m->state = H1_MSG_HDR_L2_LWS;
1600 break;
1601
1602 case HTX_BLK_PHDR:
1603 /* not implemented yet */
1604 h1m->flags |= errflag;
1605 break;
1606
1607 case HTX_BLK_EOH:
Christopher Fauletde68b132018-12-10 11:21:47 +01001608 if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) {
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001609 /* There is no "Connection:" header and
1610 * it the conn_mode must be
1611 * processed. So do it */
1612 n = ist("Connection");
1613 v = ist("");
1614 h1_process_conn_mode(h1s, h1m, NULL, &v);
1615 process_conn_mode = 0;
1616 if (v.len) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01001617 if (!htx_hdr_to_h1(n, v, tmp))
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001618 goto copy;
1619 }
1620 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001621
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001622 if ((h1s->meth != HTTP_METH_CONNECT &&
1623 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001624 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1625 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1626 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1627 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1628 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001629 /* chunking needed but header not seen */
1630 if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28))
1631 goto copy;
1632 h1m->flags |= H1_MF_CHNK;
1633 }
1634
Christopher Faulet9768c262018-10-22 09:34:31 +02001635 h1m->state = H1_MSG_LAST_LF;
1636 if (!chunk_memcat(tmp, "\r\n", 2))
1637 goto copy;
1638
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001639 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1640 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1641 h1_set_req_tunnel_mode(h1s);
1642 }
1643 else if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101) {
1644 /* a successfull reply to a CONNECT or a protocol switching is sent
1645 * to the client . Switch the response to tunnel mode. */
1646 h1_set_res_tunnel_mode(h1s);
1647 }
1648 else
1649 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001650 break;
1651
1652 case HTX_BLK_DATA:
1653 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001654 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001655 if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Faulet9768c262018-10-22 09:34:31 +02001656 goto copy;
1657 break;
1658
1659 case HTX_BLK_EOD:
1660 if (!chunk_memcat(tmp, "0\r\n", 3))
1661 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001662 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet9768c262018-10-22 09:34:31 +02001663 h1m->state = H1_MSG_TRAILERS;
1664 break;
1665
1666 case HTX_BLK_TLR:
Willy Tarreau34d23482019-01-03 17:46:56 +01001667 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001668 if (!chunk_memcat(tmp, "0\r\n", 3))
1669 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001670 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001671 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001672 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001673 v.len = vlen;
Christopher Fauletc59ff232018-12-03 13:58:44 +01001674 if (!htx_trailer_to_h1(v, tmp))
Christopher Faulet9768c262018-10-22 09:34:31 +02001675 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001676 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001677 break;
1678
1679 case HTX_BLK_EOM:
Christopher Faulet32188212018-11-20 18:21:43 +01001680 if ((h1m->flags & H1_MF_CHNK)) {
Willy Tarreau34d23482019-01-03 17:46:56 +01001681 if (!(h1s->flags & H1S_F_HAVE_O_EOD)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001682 if (!chunk_memcat(tmp, "0\r\n", 3))
1683 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001684 h1s->flags |= H1S_F_HAVE_O_EOD;
Christopher Faulet32188212018-11-20 18:21:43 +01001685 }
Willy Tarreau34d23482019-01-03 17:46:56 +01001686 if (!(h1s->flags & H1S_F_HAVE_O_TLR)) {
Christopher Faulet32188212018-11-20 18:21:43 +01001687 if (!chunk_memcat(tmp, "\r\n", 2))
1688 goto copy;
Willy Tarreau34d23482019-01-03 17:46:56 +01001689 h1s->flags |= H1S_F_HAVE_O_TLR;
Christopher Faulet32188212018-11-20 18:21:43 +01001690 }
1691 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001692 h1m->state = H1_MSG_DONE;
1693 break;
1694
1695 case HTX_BLK_OOB:
1696 v = htx_get_blk_value(chn_htx, blk);
1697 if (!chunk_memcat(tmp, v.ptr, v.len))
1698 goto copy;
1699 break;
1700
1701 default:
1702 h1m->flags |= errflag;
1703 break;
1704 }
Christopher Fauletb2e84162018-12-06 11:39:49 +01001705 total += vlen;
1706 count -= vlen;
1707 if (sz == vlen)
1708 blk = htx_remove_blk(chn_htx, blk);
1709 else {
1710 htx_cut_data_blk(chn_htx, blk, vlen);
1711 break;
1712 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001713 }
1714
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001715 if (htx_is_empty(chn_htx))
1716 h1_handle_1xx_response(h1s, h1m);
1717
Christopher Faulet9768c262018-10-22 09:34:31 +02001718 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001719 /* when the output buffer is empty, tmp shares the same area so that we
1720 * only have to update pointers and lengths.
1721 */
Willy Tarreaub57af612019-01-23 20:43:53 +01001722 if (tmp->area == h1c->obuf.area + h1c->obuf.head)
Willy Tarreauc5efa332018-12-05 11:19:27 +01001723 h1c->obuf.data = tmp->data;
1724 else
1725 b_putblk(&h1c->obuf, tmp->area, tmp->data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001726
Willy Tarreau3815b222018-12-11 19:50:43 +01001727 htx_to_buf(chn_htx, buf);
1728 out:
Willy Tarreau45f2b892018-12-05 07:59:27 +01001729 if (!buf_room_for_htx_data(&h1c->obuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001730 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001731 end:
1732 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001733}
1734
Christopher Faulet51dbc942018-09-13 09:05:15 +02001735/*********************************************************/
1736/* functions below are I/O callbacks from the connection */
1737/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001738static void h1_wake_stream_for_recv(struct h1s *h1s)
1739{
1740 if (h1s && h1s->recv_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001741 h1s->recv_wait->events &= ~SUB_RETRY_RECV;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001742 tasklet_wakeup(h1s->recv_wait->task);
1743 h1s->recv_wait = NULL;
1744 }
1745}
1746static void h1_wake_stream_for_send(struct h1s *h1s)
1747{
1748 if (h1s && h1s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001749 h1s->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001750 tasklet_wakeup(h1s->send_wait->task);
1751 h1s->send_wait = NULL;
1752 }
1753}
1754
Christopher Faulet51dbc942018-09-13 09:05:15 +02001755/*
1756 * Attempt to read data, and subscribe if none available
1757 */
1758static int h1_recv(struct h1c *h1c)
1759{
1760 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001761 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01001762 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001763 int rcvd = 0;
1764
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001765 if (h1c->wait_event.events & SUB_RETRY_RECV)
Christopher Fauletc386a882018-12-04 16:06:28 +01001766 return (b_data(&h1c->ibuf));
Christopher Faulet51dbc942018-09-13 09:05:15 +02001767
Olivier Houchard75159a92018-12-03 18:46:09 +01001768 if (!h1_recv_allowed(h1c)) {
1769 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02001770 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01001771 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001772
Christopher Fauletfeb11742018-11-29 15:12:34 +01001773 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001774 rcvd = 1;
1775 goto end;
1776 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02001777
Christopher Faulet51dbc942018-09-13 09:05:15 +02001778 if (!h1_get_buf(h1c, &h1c->ibuf)) {
1779 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet9768c262018-10-22 09:34:31 +02001780 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001781 }
1782
Olivier Houchard29a22bc2018-12-04 18:16:45 +01001783 /*
1784 * If we only have a small amount of data, realign it,
1785 * it's probably cheaper than doing 2 recv() calls.
1786 */
1787 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
1788 b_slow_realign(&h1c->ibuf, trash.area, 0);
1789
Willy Tarreau45f2b892018-12-05 07:59:27 +01001790 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001791 if (max) {
1792 h1c->flags &= ~H1C_F_IN_FULL;
Willy Tarreau78f548f2018-12-05 10:02:39 +01001793
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01001794 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01001795 if (!b_data(&h1c->ibuf)) {
1796 /* try to pre-align the buffer like the rxbufs will be
1797 * to optimize memory copies.
1798 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01001799 h1c->ibuf.head = sizeof(struct htx);
1800 }
Willy Tarreauc0960d12018-12-14 10:59:15 +01001801 ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001802 }
Christopher Faulet47365272018-10-31 17:40:50 +01001803 if (ret > 0) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02001804 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001805 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01001806 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01001807 if (h1s->csinfo.t_idle == -1)
1808 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1809 }
Christopher Faulet47365272018-10-31 17:40:50 +01001810 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001811
Christopher Fauletcf56b992018-12-11 16:12:31 +01001812 if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01001813 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01001814 goto end;
1815 }
1816
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001817 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001818
Christopher Faulet9768c262018-10-22 09:34:31 +02001819 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001820 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
1821 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001822
Christopher Faulete6b39942018-12-07 09:42:49 +01001823 if (conn_xprt_read0_pending(conn) && h1s && h1s->cs)
Christopher Faulet87a8f352019-03-22 14:51:36 +01001824 h1s->cs->flags |= CS_FL_REOS;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001825 if (!b_data(&h1c->ibuf))
1826 h1_release_buf(h1c, &h1c->ibuf);
Willy Tarreau45f2b892018-12-05 07:59:27 +01001827 else if (!buf_room_for_htx_data(&h1c->ibuf))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001828 h1c->flags |= H1C_F_IN_FULL;
1829 return rcvd;
1830}
1831
1832
1833/*
1834 * Try to send data if possible
1835 */
1836static int h1_send(struct h1c *h1c)
1837{
1838 struct connection *conn = h1c->conn;
1839 unsigned int flags = 0;
1840 size_t ret;
1841 int sent = 0;
1842
1843 if (conn->flags & CO_FL_ERROR)
1844 return 0;
1845
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001846 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001847 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1848 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001849 return 0;
1850 }
1851
Christopher Faulet51dbc942018-09-13 09:05:15 +02001852 if (!b_data(&h1c->obuf))
1853 goto end;
1854
1855 if (h1c->flags & H1C_F_OUT_FULL)
1856 flags |= CO_SFL_MSG_MORE;
1857
1858 ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags);
1859 if (ret > 0) {
1860 h1c->flags &= ~H1C_F_OUT_FULL;
1861 b_del(&h1c->obuf, ret);
1862 sent = 1;
1863 }
1864
Christopher Faulet145aa472018-12-06 10:56:20 +01001865 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
1866 /* error or output closed, nothing to send, clear the buffer to release it */
1867 b_reset(&h1c->obuf);
1868 }
1869
Christopher Faulet51dbc942018-09-13 09:05:15 +02001870 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001871 if (!(h1c->flags & H1C_F_OUT_FULL))
1872 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01001873
Christopher Faulet51dbc942018-09-13 09:05:15 +02001874 /* We're done, no more to send */
1875 if (!b_data(&h1c->obuf)) {
1876 h1_release_buf(h1c, &h1c->obuf);
1877 if (h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet666a0c42019-01-08 11:12:04 +01001878 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001879 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001880 else if (!(h1c->wait_event.events & SUB_RETRY_SEND))
1881 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001882
1883 return sent;
1884}
1885
Christopher Faulet51dbc942018-09-13 09:05:15 +02001886
1887/* callback called on any event by the connection handler.
1888 * It applies changes and returns zero, or < 0 if it wants immediate
1889 * destruction of the connection.
1890 */
1891static int h1_process(struct h1c * h1c)
1892{
1893 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001894 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001895
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001896 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001897 return -1;
1898
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001899 if (h1c->flags & H1C_F_CS_WAIT_CONN) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001900 if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)))
1901 goto end;
1902 h1c->flags &= ~H1C_F_CS_WAIT_CONN;
Christopher Fauleta0883e62018-12-11 16:26:50 +01001903 h1_wake_stream_for_send(h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02001904 }
1905
Christopher Fauletfeb11742018-11-29 15:12:34 +01001906 if (!h1s) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001907 if (h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001908 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) ||
Christopher Faulet539e0292018-11-19 10:40:09 +01001909 conn_xprt_read0_pending(conn))
1910 goto release;
Christopher Faulet666a0c42019-01-08 11:12:04 +01001911 if (!conn_is_back(conn) && !(h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01001912 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01001913 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001914 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01001915 else
Olivier Houcharde7284782018-12-06 18:54:54 +01001916 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01001917 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001918 }
1919
Christopher Fauletfeb11742018-11-29 15:12:34 +01001920 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
1921 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
1922
Olivier Houchard75159a92018-12-03 18:46:09 +01001923 if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
1924 (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01001925 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01001926 int flags = 0;
1927
1928 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
1929 flags |= CS_FL_ERROR;
1930 if (conn_xprt_read0_pending(conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001931 flags |= CS_FL_REOS;
Olivier Houchard75159a92018-12-03 18:46:09 +01001932 h1s->cs->flags |= flags;
1933 h1s->cs->data_cb->wake(h1s->cs);
1934 }
Christopher Faulet47365272018-10-31 17:40:50 +01001935 end:
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001936 if (h1c->task) {
1937 h1c->task->expire = TICK_ETERNITY;
1938 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01001939 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001940 ? h1c->shut_timeout
1941 : h1c->timeout));
1942 task_queue(h1c->task);
1943 }
1944 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001945 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01001946
1947 release:
1948 h1_release(conn);
1949 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001950}
1951
1952static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
1953{
1954 struct h1c *h1c = ctx;
1955 int ret = 0;
1956
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001957 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001958 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001959 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02001960 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01001961 if (ret || !h1c->h1s)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001962 h1_process(h1c);
1963 return NULL;
1964}
1965
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001966static void h1_reset(struct connection *conn)
1967{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001968 struct h1c *h1c = conn->ctx;
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01001969
1970 /* Reset the flags, and let the mux know we're waiting for a connection */
1971 h1c->flags = H1C_F_CS_WAIT_CONN;
1972}
Christopher Faulet51dbc942018-09-13 09:05:15 +02001973
1974static int h1_wake(struct connection *conn)
1975{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001976 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01001977 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001978
Christopher Faulet539e0292018-11-19 10:40:09 +01001979 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01001980 ret = h1_process(h1c);
1981 if (ret == 0) {
1982 struct h1s *h1s = h1c->h1s;
1983
1984 if (h1s && h1s->cs && h1s->cs->data_cb->wake)
1985 ret = h1s->cs->data_cb->wake(h1s->cs);
1986 }
1987 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001988}
1989
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001990/* Connection timeout management. The principle is that if there's no receipt
1991 * nor sending for a certain amount of time, the connection is closed.
1992 */
1993static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
1994{
1995 struct h1c *h1c = context;
1996 int expired = tick_is_expired(t->expire, now_ms);
1997
1998 if (!expired && h1c)
1999 return t;
2000
2001 task_delete(t);
2002 task_free(t);
2003
2004 if (!h1c) {
2005 /* resources were already deleted */
2006 return NULL;
2007 }
2008
2009 h1c->task = NULL;
2010 /* If a stream is still attached to the mux, just set an error and wait
2011 * for the stream's timeout. Otherwise, release the mux. This is only ok
2012 * because same timeouts are used.
2013 */
2014 if (h1c->h1s && h1c->h1s->cs)
2015 h1c->flags |= H1C_F_CS_ERROR;
2016 else
2017 h1_release(h1c->conn);
2018 return NULL;
2019}
2020
Christopher Faulet51dbc942018-09-13 09:05:15 +02002021/*******************************************/
2022/* functions below are used by the streams */
2023/*******************************************/
2024/*
2025 * Attach a new stream to a connection
2026 * (Used for outgoing connections)
2027 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002028static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002029{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002030 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002031 struct conn_stream *cs = NULL;
2032 struct h1s *h1s;
2033
2034 if (h1c->flags & H1C_F_CS_ERROR)
2035 goto end;
2036
2037 cs = cs_new(h1c->conn);
2038 if (!cs)
2039 goto end;
2040
Olivier Houchardf502aca2018-12-14 19:42:40 +01002041 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002042 if (h1s == NULL)
2043 goto end;
2044
2045 return cs;
2046 end:
2047 cs_free(cs);
2048 return NULL;
2049}
2050
2051/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2052 * this mux, it's easy as we can only store a single conn_stream.
2053 */
2054static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2055{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002056 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002057 struct h1s *h1s = h1c->h1s;
2058
2059 if (h1s)
2060 return h1s->cs;
2061
2062 return NULL;
2063}
2064
2065static void h1_destroy(struct connection *conn)
2066{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002067 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002068
2069 if (!h1c->h1s)
2070 h1_release(conn);
2071}
2072
2073/*
2074 * Detach the stream from the connection and possibly release the connection.
2075 */
2076static void h1_detach(struct conn_stream *cs)
2077{
2078 struct h1s *h1s = cs->ctx;
2079 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002080 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002081 int has_keepalive;
2082 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002083
2084 cs->ctx = NULL;
2085 if (!h1s)
2086 return;
2087
Olivier Houchardf502aca2018-12-14 19:42:40 +01002088 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002089 h1c = h1s->h1c;
2090 h1s->cs = NULL;
2091
Olivier Houchard8a786902018-12-15 16:05:40 +01002092 has_keepalive = h1s->flags & H1S_F_WANT_KAL;
2093 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2094 h1s_destroy(h1s);
2095
2096 if (conn_is_back(h1c->conn) && has_keepalive &&
Olivier Houchard44d59142018-12-13 18:46:22 +01002097 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002098 /* Never ever allow to reuse a connection from a non-reuse backend */
Olivier Houchard44d59142018-12-13 18:46:22 +01002099 if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
Christopher Faulet9400a392018-11-23 23:10:39 +01002100 h1c->conn->flags |= CO_FL_PRIVATE;
2101
Olivier Houchard44d59142018-12-13 18:46:22 +01002102 if (!(h1c->conn->owner)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002103 h1c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002104 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2105 h1c->conn->owner = NULL;
2106 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn))
2107 /* The server doesn't want it, let's kill the connection right away */
2108 h1c->conn->mux->destroy(h1c->conn);
2109 else
2110 tasklet_wakeup(h1c->wait_event.task);
2111 return;
2112
2113 }
Olivier Houchard44d59142018-12-13 18:46:22 +01002114 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002115 if (h1c->conn->owner == sess) {
2116 int ret = session_check_idle_conn(sess, h1c->conn);
2117 if (ret == -1)
2118 /* The connection got destroyed, let's leave */
2119 return;
2120 else if (ret == 1) {
2121 /* The connection was added to the server list,
2122 * wake the task so we can subscribe to events
2123 */
2124 tasklet_wakeup(h1c->wait_event.task);
2125 return;
2126 }
2127 }
Christopher Faulet9400a392018-11-23 23:10:39 +01002128 /* we're in keep-alive with an idle connection, monitor it if not already done */
Olivier Houchard44d59142018-12-13 18:46:22 +01002129 if (LIST_ISEMPTY(&h1c->conn->list)) {
Christopher Faulet9400a392018-11-23 23:10:39 +01002130 struct server *srv = objt_server(h1c->conn->target);
2131
2132 if (srv) {
2133 if (h1c->conn->flags & CO_FL_PRIVATE)
2134 LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list);
Olivier Houchard8a786902018-12-15 16:05:40 +01002135 else if (is_not_first)
Christopher Faulet9400a392018-11-23 23:10:39 +01002136 LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list);
2137 else
2138 LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list);
2139 }
2140 }
2141 }
2142
Christopher Faulet51dbc942018-09-13 09:05:15 +02002143 /* We don't want to close right now unless the connection is in error */
Christopher Faulet666a0c42019-01-08 11:12:04 +01002144 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)) ||
Olivier Houchard7ccff1a2018-12-03 16:33:19 +01002145 (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002146 h1_release(h1c->conn);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002147 else {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002148 tasklet_wakeup(h1c->wait_event.task);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002149 if (h1c->task) {
2150 h1c->task->expire = TICK_ETERNITY;
2151 if (b_data(&h1c->obuf)) {
Christopher Faulet666a0c42019-01-08 11:12:04 +01002152 h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN))
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002153 ? h1c->shut_timeout
2154 : h1c->timeout));
2155 task_queue(h1c->task);
2156 }
2157 }
2158 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002159}
2160
2161
2162static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2163{
2164 struct h1s *h1s = cs->ctx;
2165
2166 if (!h1s)
2167 return;
2168
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002169 if ((h1s->flags & H1S_F_WANT_KAL) &&
2170 !(cs->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002171 return;
2172
Christopher Faulet51dbc942018-09-13 09:05:15 +02002173 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2174 if (cs->flags & CS_FL_SHR)
2175 return;
2176 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
2177 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Christopher Faulet666a0c42019-01-08 11:12:04 +01002178 if ((cs->conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2179 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002180}
2181
2182static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2183{
2184 struct h1s *h1s = cs->ctx;
2185 struct h1c *h1c;
2186
2187 if (!h1s)
2188 return;
2189 h1c = h1s->h1c;
2190
Christopher Fauletf2824e62018-10-01 12:12:37 +02002191 if ((h1s->flags & H1S_F_WANT_KAL) &&
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002192 !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) &&
2193
Christopher Fauletf2824e62018-10-01 12:12:37 +02002194 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2195 return;
2196
Christopher Faulet51dbc942018-09-13 09:05:15 +02002197 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2198 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
2199 return;
2200
Christopher Faulet666a0c42019-01-08 11:12:04 +01002201 h1_shutw_conn(cs->conn, mode);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002202}
2203
Christopher Faulet666a0c42019-01-08 11:12:04 +01002204static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002205{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002206 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002207
Christopher Faulet666a0c42019-01-08 11:12:04 +01002208 conn_xprt_shutw(conn);
2209 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
2210 if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
2211 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002212}
2213
2214/* Called from the upper layer, to unsubscribe to events */
2215static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param)
2216{
2217 struct wait_event *sw;
2218 struct h1s *h1s = cs->ctx;
2219
2220 if (!h1s)
2221 return 0;
2222
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002223 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002224 sw = param;
2225 if (h1s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002226 sw->events &= ~SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002227 h1s->recv_wait = NULL;
2228 }
2229 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002230 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002231 sw = param;
2232 if (h1s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002233 sw->events &= ~SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002234 h1s->send_wait = NULL;
2235 }
2236 }
2237 return 0;
2238}
2239
2240/* Called from the upper layer, to subscribe to events, such as being able to send */
2241static int h1_subscribe(struct conn_stream *cs, int event_type, void *param)
2242{
2243 struct wait_event *sw;
2244 struct h1s *h1s = cs->ctx;
2245
2246 if (!h1s)
2247 return -1;
2248
2249 switch (event_type) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002250 case SUB_RETRY_RECV:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002251 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002252 if (!(sw->events & SUB_RETRY_RECV)) {
2253 sw->events |= SUB_RETRY_RECV;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002254 sw->handle = h1s;
2255 h1s->recv_wait = sw;
2256 }
2257 return 0;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002258 case SUB_RETRY_SEND:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002259 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002260 if (!(sw->events & SUB_RETRY_SEND)) {
2261 sw->events |= SUB_RETRY_SEND;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002262 sw->handle = h1s;
2263 h1s->send_wait = sw;
2264 }
2265 return 0;
2266 default:
2267 break;
2268 }
2269 return -1;
2270}
2271
2272/* Called from the upper layer, to receive data */
2273static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2274{
2275 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002276 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002277 size_t ret = 0;
2278
Christopher Faulet539e0292018-11-19 10:40:09 +01002279 if (!(h1c->flags & H1C_F_IN_ALLOC))
2280 ret = h1_process_input(h1c, buf, flags);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002281
2282 if (flags & CO_RFL_BUF_FLUSH)
2283 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002284 else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) {
2285 h1s->flags &= ~H1S_F_SPLICED_DATA;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002286 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet539e0292018-11-19 10:40:09 +01002287 tasklet_wakeup(h1c->wait_event.task);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002288 }
2289 return ret;
2290}
2291
2292
2293/* Called from the upper layer, to send data */
2294static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2295{
2296 struct h1s *h1s = cs->ctx;
2297 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002298 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002299
2300 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002301 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002302
2303 h1c = h1s->h1c;
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002304 if (h1c->flags & H1C_F_CS_WAIT_CONN)
2305 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002306
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002307 while (total != count) {
2308 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002309
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002310 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2311 ret = h1_process_output(h1c, buf, count);
2312 if (!ret)
2313 break;
2314 total += ret;
2315 if (!h1_send(h1c))
2316 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002317 }
Christopher Fauletf96c3222018-11-20 18:38:01 +01002318
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002319 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002320}
2321
Christopher Faulet1be55f92018-10-02 15:59:23 +02002322#if defined(CONFIG_HAP_LINUX_SPLICE)
2323/* Send and get, using splicing */
2324static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2325{
2326 struct h1s *h1s = cs->ctx;
2327 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2328 int ret = 0;
2329
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002330 if (b_data(&h1s->h1c->ibuf)) {
2331 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002332 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002333 }
2334
2335 h1s->flags &= ~H1S_F_BUF_FLUSH;
2336 h1s->flags |= H1S_F_SPLICED_DATA;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002337 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2338 count = h1m->curr_len;
2339 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
2340 if (h1m->state == H1_MSG_DATA && ret > 0)
2341 h1m->curr_len -= ret;
2342 end:
2343 return ret;
2344
2345}
2346
2347static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2348{
2349 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002350 int ret = 0;
2351
2352 if (b_data(&h1s->h1c->obuf))
2353 goto end;
2354
2355 ret = cs->conn->xprt->snd_pipe(cs->conn, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002356 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002357 if (pipe->data) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002358 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND))
2359 cs->conn->xprt->subscribe(cs->conn, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002360 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002361 return ret;
2362}
2363#endif
2364
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002365/* for debugging with CLI's "show fd" command */
2366static void h1_show_fd(struct buffer *msg, struct connection *conn)
2367{
2368 struct h1c *h1c = conn->ctx;
2369 struct h1s *h1s = h1c->h1s;
2370
Christopher Fauletf376a312019-01-04 15:16:06 +01002371 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2372 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002373 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2374 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2375 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2376 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2377
2378 if (h1s) {
2379 char *method;
2380
2381 if (h1s->meth < HTTP_METH_OTHER)
2382 method = http_known_methods[h1s->meth].ptr;
2383 else
2384 method = "UNKNOWN";
2385 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2386 " .meth=%s status=%d",
2387 h1s, h1s->flags,
2388 h1m_state_str(h1s->req.state),
2389 h1m_state_str(h1s->res.state), method, h1s->status);
2390 if (h1s->cs)
2391 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2392 h1s->cs->flags, h1s->cs->data);
2393 }
2394}
2395
Christopher Faulet51dbc942018-09-13 09:05:15 +02002396/****************************************/
2397/* MUX initialization and instanciation */
2398/****************************************/
2399
2400/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01002401static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002402 .init = h1_init,
2403 .wake = h1_wake,
2404 .attach = h1_attach,
2405 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01002406 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002407 .detach = h1_detach,
2408 .destroy = h1_destroy,
2409 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01002410 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002411 .rcv_buf = h1_rcv_buf,
2412 .snd_buf = h1_snd_buf,
Christopher Faulet1be55f92018-10-02 15:59:23 +02002413#if defined(CONFIG_HAP_LINUX_SPLICE)
2414 .rcv_pipe = h1_rcv_pipe,
2415 .snd_pipe = h1_snd_pipe,
2416#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02002417 .subscribe = h1_subscribe,
2418 .unsubscribe = h1_unsubscribe,
2419 .shutr = h1_shutr,
2420 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002421 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002422 .reset = h1_reset,
Christopher Faulet51dbc942018-09-13 09:05:15 +02002423 .flags = MX_FL_NONE,
2424 .name = "h1",
2425};
2426
2427
2428/* this mux registers default HTX proto */
2429static struct mux_proto_list mux_proto_htx =
2430{ .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
2431
Willy Tarreau0108d902018-11-25 19:14:37 +01002432INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
2433
Christopher Faulet51dbc942018-09-13 09:05:15 +02002434/*
2435 * Local variables:
2436 * c-indent-level: 8
2437 * c-basic-offset: 8
2438 * End:
2439 */