blob: 21796c35639d0c8c1aa21f5dbc132e6caf3e8c5d [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
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
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
90 int32_t max_id; /* highest ID known on this connection, <0 before preface */
91 uint32_t rcvd_c; /* newly received data to ACK for the connection */
92 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
93
94 /* states for the demux direction */
95 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020096 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020097
98 int32_t dsi; /* demux stream ID (<0 = idle) */
99 int32_t dfl; /* demux frame length (if dsi >= 0) */
100 int8_t dft; /* demux frame type (if dsi >= 0) */
101 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100102 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
103 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200104 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
105
106 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200107 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200108 int32_t msi; /* mux stream ID (<0 = idle) */
109 int32_t mfl; /* mux frame length (if dsi >= 0) */
110 int8_t mft; /* mux frame type (if dsi >= 0) */
111 int8_t mff; /* mux frame flags (if dsi >= 0) */
112 /* 16 bit hole here */
113 int32_t miw; /* mux initial window size for all new streams */
114 int32_t mws; /* mux window size. Can be negative. */
115 int32_t mfs; /* mux's max frame size */
116
Willy Tarreauea392822017-10-31 10:02:25 +0100117 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100118 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100119 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200120 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100121 unsigned int nb_reserved; /* number of reserved streams */
122 /* 32 bit hole here */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200123 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100124 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200125 struct eb_root streams_by_id; /* all active streams by their ID */
126 struct list send_list; /* list of blocked streams requesting to send */
127 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200128 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100129 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200130 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200131};
132
Willy Tarreau18312642017-10-11 07:57:07 +0200133/* H2 stream state, in h2s->st */
134enum h2_ss {
135 H2_SS_IDLE = 0, // idle
136 H2_SS_RLOC, // reserved(local)
137 H2_SS_RREM, // reserved(remote)
138 H2_SS_OPEN, // open
139 H2_SS_HREM, // half-closed(remote)
140 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200141 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200142 H2_SS_CLOSED, // closed
143 H2_SS_ENTRIES // must be last
144} __attribute__((packed));
145
146/* HTTP/2 stream flags (32 bit), in h2s->flags */
147#define H2_SF_NONE 0x00000000
148#define H2_SF_ES_RCVD 0x00000001
149#define H2_SF_ES_SENT 0x00000002
150
151#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
152#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
153
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200154/* stream flags indicating the reason the stream is blocked */
155#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
156#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
157#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
158#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
159#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
160
Willy Tarreau454f9052017-10-26 19:40:35 +0200161/* stream flags indicating how data is supposed to be sent */
162#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
163#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
164
165/* step we're currently in when sending chunks. This is needed because we may
166 * have to transfer chunks as large as a full buffer so there's no room left
167 * for size nor crlf around.
168 */
169#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
170#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
171#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
172
173#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
174
Willy Tarreau67434202017-11-06 20:20:51 +0100175#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100176#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100177
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100178#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
179
Willy Tarreau18312642017-10-11 07:57:07 +0200180/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
181 * it is being processed in the internal HTTP representation (H1 for now).
182 */
183struct h2s {
184 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100185 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200186 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200187 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200188 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 int32_t id; /* stream ID */
190 uint32_t flags; /* H2_SF_* */
191 int mws; /* mux window size for this stream */
192 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
193 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200194 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200195 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200196 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
197 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
198 struct wait_event *send_wait; /* The streeam is waiting for flow control */
199 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200200};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200201
Willy Tarreauc6405142017-09-21 20:23:50 +0200202/* descriptor for an h2 frame header */
203struct h2_fh {
204 uint32_t len; /* length, host order, 24 bits */
205 uint32_t sid; /* stream id, host order, 31 bits */
206 uint8_t ft; /* frame type */
207 uint8_t ff; /* frame flags */
208};
209
Willy Tarreau8ceae722018-11-26 11:58:30 +0100210/* the h2c connection pool */
211DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
212
213/* the h2s stream pool */
214DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
215
Willy Tarreaudc572362018-12-12 08:08:05 +0100216/* The default connection window size is 65535, it may only be enlarged using
217 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
218 * we'll pretend we already received the difference between the two to send
219 * an equivalent window update to enlarge it to 2G-1.
220 */
221#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
222
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200223/* a few settings from the global section */
224static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200225static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200226static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200227
Willy Tarreau2a856182017-05-16 15:20:39 +0200228/* a dmumy closed stream */
229static const struct h2s *h2_closed_stream = &(const struct h2s){
230 .cs = NULL,
231 .h2c = NULL,
232 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100233 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100234 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200235 .id = 0,
236};
237
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100238/* a dmumy closed stream returning a PROTOCOL_ERROR error */
239static const struct h2s *h2_error_stream = &(const struct h2s){
240 .cs = NULL,
241 .h2c = NULL,
242 .st = H2_SS_CLOSED,
243 .errcode = H2_ERR_PROTOCOL_ERROR,
244 .flags = 0,
245 .id = 0,
246};
247
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100248/* a dmumy closed stream returning a REFUSED_STREAM error */
249static const struct h2s *h2_refused_stream = &(const struct h2s){
250 .cs = NULL,
251 .h2c = NULL,
252 .st = H2_SS_CLOSED,
253 .errcode = H2_ERR_REFUSED_STREAM,
254 .flags = 0,
255 .id = 0,
256};
257
Willy Tarreau2a856182017-05-16 15:20:39 +0200258/* and a dummy idle stream for use with any unannounced stream */
259static const struct h2s *h2_idle_stream = &(const struct h2s){
260 .cs = NULL,
261 .h2c = NULL,
262 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100263 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200264 .id = 0,
265};
266
Olivier Houchard9f6af332018-05-25 14:04:04 +0200267static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200268static int h2_send(struct h2c *h2c);
269static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200270static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200271static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100272static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +0100273static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100274static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200275static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100276static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100277static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200278
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200279/*****************************************************/
280/* functions below are for dynamic buffer management */
281/*****************************************************/
282
Willy Tarreau315d8072017-12-10 22:17:57 +0100283/* indicates whether or not the we may call the h2_recv() function to attempt
284 * to receive data into the buffer and/or demux pending data. The condition is
285 * a bit complex due to some API limits for now. The rules are the following :
286 * - if an error or a shutdown was detected on the connection and the buffer
287 * is empty, we must not attempt to receive
288 * - if the demux buf failed to be allocated, we must not try to receive and
289 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100290 * - if no flag indicates a blocking condition, we may attempt to receive,
291 * regardless of whether the demux buffer is full or not, so that only
292 * de demux part decides whether or not to block. This is needed because
293 * the connection API indeed prevents us from re-enabling receipt that is
294 * already enabled in a polled state, so we must always immediately stop
295 * as soon as the demux can't proceed so as never to hit an end of read
296 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100297 * - otherwise must may not attempt
298 */
299static inline int h2_recv_allowed(const struct h2c *h2c)
300{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200301 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100302 (h2c->st0 >= H2_CS_ERROR ||
303 h2c->conn->flags & CO_FL_ERROR ||
304 conn_xprt_read0_pending(h2c->conn)))
305 return 0;
306
307 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100308 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100309 return 1;
310
311 return 0;
312}
313
Willy Tarreau47b515a2018-12-21 16:09:41 +0100314/* restarts reading on the connection if it was not enabled */
315static inline void h2c_restart_reading(const struct h2c *h2c)
316{
317 if (!h2_recv_allowed(h2c))
318 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100319 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100320 return;
321 tasklet_wakeup(h2c->wait_event.task);
322}
323
324
Willy Tarreauf2101912018-07-19 10:11:38 +0200325/* returns true if the connection has too many conn_streams attached */
326static inline int h2_has_too_many_cs(const struct h2c *h2c)
327{
Willy Tarreaua8754662018-12-23 20:43:58 +0100328 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200329}
330
Willy Tarreau44e973f2018-03-01 17:49:30 +0100331/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
332 * flags are used to figure what buffer was requested. It returns 1 if the
333 * allocation succeeds, in which case the connection is woken up, or 0 if it's
334 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200335 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100336static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200337{
338 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100339 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200340
Willy Tarreau44e973f2018-03-01 17:49:30 +0100341 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200342 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100343 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200344 return 1;
345 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200346
Willy Tarreau44e973f2018-03-01 17:49:30 +0100347 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
348 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200349
350 if (h2c->flags & H2_CF_DEM_MROOM) {
351 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100352 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200353 }
Willy Tarreau14398122017-09-22 14:26:04 +0200354 return 1;
355 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100356
357 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
358 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200359 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100360 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100361 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100362 return 1;
363 }
364
Willy Tarreau14398122017-09-22 14:26:04 +0200365 return 0;
366}
367
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200368static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200369{
370 struct buffer *buf = NULL;
371
Willy Tarreau44e973f2018-03-01 17:49:30 +0100372 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
373 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
374 h2c->buf_wait.target = h2c;
375 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100376 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100377 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100378 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200379 __conn_xprt_stop_recv(h2c->conn);
380 }
381 return buf;
382}
383
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200384static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200385{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200386 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100387 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200388 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200389 }
390}
391
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100392/* returns the number of allocatable outgoing streams for the connection taking
393 * the last_sid and the reserved ones into account.
394 */
395static inline int h2_streams_left(const struct h2c *h2c)
396{
397 int ret;
398
399 /* consider the number of outgoing streams we're allowed to create before
400 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
401 * nb_reserved is the number of streams which don't yet have an ID.
402 */
403 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
404 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
405 if (ret < 0)
406 ret = 0;
407 return ret;
408}
409
410/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100411static int h2_avail_streams(struct connection *conn)
412{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100413 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100414 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100415
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100416 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100417 ret1 = h2_settings_max_concurrent_streams - h2c->nb_streams;
418
419 /* we must also consider the limit imposed by stream IDs */
420 ret2 = h2_streams_left(h2c);
421 return MIN(ret1, ret2);
Olivier Houchardd540b362018-11-05 18:37:53 +0100422}
423
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100424static int h2_max_streams(struct connection *conn)
425{
426 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
427 return h2_settings_max_concurrent_streams;
428}
429
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200430
Willy Tarreau62f52692017-10-08 23:01:42 +0200431/*****************************************************************/
432/* functions below are dedicated to the mux setup and management */
433/*****************************************************************/
434
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200435/* Initialize the mux once it's attached. For outgoing connections, the context
436 * is already initialized before installing the mux, so we detect incoming
437 * connections from the fact that the context is still NULL. Returns < 0 on
438 * error.
439 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100440static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200441{
442 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100443 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200444
Willy Tarreaubafbe012017-11-24 17:34:44 +0100445 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200446 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200447 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200448
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100449 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200450 h2c->flags = H2_CF_IS_BACK;
451 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
452 if (tick_isset(prx->timeout.serverfin))
453 h2c->shut_timeout = prx->timeout.serverfin;
454 } else {
455 h2c->flags = H2_CF_NONE;
456 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
457 if (tick_isset(prx->timeout.clientfin))
458 h2c->shut_timeout = prx->timeout.clientfin;
459 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100460
Willy Tarreau0b37d652018-10-03 10:33:02 +0200461 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100462 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100463 if (tick_isset(h2c->timeout)) {
464 t = task_new(tid_bit);
465 if (!t)
466 goto fail;
467
468 h2c->task = t;
469 t->process = h2_timeout_task;
470 t->context = h2c;
471 t->expire = tick_add(now_ms, h2c->timeout);
472 }
Willy Tarreauea392822017-10-31 10:02:25 +0100473
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200474 h2c->wait_event.task = tasklet_new();
475 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200476 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200477 h2c->wait_event.task->process = h2_io_cb;
478 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100479 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200480
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
482 if (!h2c->ddht)
483 goto fail;
484
485 /* Initialise the context. */
486 h2c->st0 = H2_CS_PREFACE;
487 h2c->conn = conn;
488 h2c->max_id = -1;
489 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100490 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200491 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100492 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200493 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100494 h2c->nb_reserved = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200495
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200496 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200497 h2c->dsi = -1;
498 h2c->msi = -1;
499 h2c->last_sid = -1;
500
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200501 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200502 h2c->miw = 65535; /* mux initial window size */
503 h2c->mws = 65535; /* mux window size */
504 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200505 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200506 LIST_INIT(&h2c->send_list);
507 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200508 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100509 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200510
Willy Tarreau3f133572017-10-31 19:21:06 +0100511 if (t)
512 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100513
Willy Tarreau01b44822018-10-03 14:26:37 +0200514 if (h2c->flags & H2_CF_IS_BACK) {
515 /* FIXME: this is temporary, for outgoing connections we need
516 * to immediately allocate a stream until the code is modified
517 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100518 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200519 */
520 struct h2s *h2s;
521
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100522 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200523 if (!h2s)
524 goto fail_stream;
525 }
526
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100527 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200528
Willy Tarreau0f383582018-10-03 14:22:21 +0200529 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100530 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200531 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200532 fail_stream:
533 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200534 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100535 if (t)
536 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200537 if (h2c->wait_event.task)
538 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100539 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200540 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200541 return -1;
542}
543
Willy Tarreau751f2d02018-10-05 09:35:00 +0200544/* returns the next allocatable outgoing stream ID for the H2 connection, or
545 * -1 if no more is allocatable.
546 */
547static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
548{
549 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100550
551 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200552 id = -1;
553 return id;
554}
555
Willy Tarreau2373acc2017-10-12 17:35:14 +0200556/* returns the stream associated with id <id> or NULL if not found */
557static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
558{
559 struct eb32_node *node;
560
Willy Tarreau751f2d02018-10-05 09:35:00 +0200561 if (id == 0)
562 return (struct h2s *)h2_closed_stream;
563
Willy Tarreau2a856182017-05-16 15:20:39 +0200564 if (id > h2c->max_id)
565 return (struct h2s *)h2_idle_stream;
566
Willy Tarreau2373acc2017-10-12 17:35:14 +0200567 node = eb32_lookup(&h2c->streams_by_id, id);
568 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200569 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200570
571 return container_of(node, struct h2s, by_id);
572}
573
Willy Tarreau62f52692017-10-08 23:01:42 +0200574/* release function for a connection. This one should be called to free all
575 * resources allocated to the mux.
576 */
577static void h2_release(struct connection *conn)
578{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100579 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200580
581 LIST_DEL(&conn->list);
582
583 if (h2c) {
584 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200585
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100586 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100587 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100588 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200589
Willy Tarreau44e973f2018-03-01 17:49:30 +0100590 h2_release_buf(h2c, &h2c->dbuf);
591 h2_release_buf(h2c, &h2c->mbuf);
592
Willy Tarreauea392822017-10-31 10:02:25 +0100593 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200594 h2c->task->context = NULL;
595 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100596 h2c->task = NULL;
597 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200598 if (h2c->wait_event.task)
599 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100600 if (h2c->wait_event.events != 0)
601 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200602 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100603
Willy Tarreaubafbe012017-11-24 17:34:44 +0100604 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200605 }
606
607 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100608 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200609
610 conn_stop_tracking(conn);
611 conn_full_close(conn);
612 if (conn->destroy_cb)
613 conn->destroy_cb(conn);
614 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200615}
616
617
Willy Tarreau71681172017-10-23 14:39:06 +0200618/******************************************************/
619/* functions below are for the H2 protocol processing */
620/******************************************************/
621
622/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100623static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200624{
625 return h2s ? h2s->id : 0;
626}
627
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200628/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100629static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200630{
631 if (h2c->msi < 0)
632 return 0;
633
634 if (h2c->msi == h2s_id(h2s))
635 return 0;
636
637 return 1;
638}
639
Willy Tarreau741d6df2017-10-17 08:00:59 +0200640/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100641static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200642{
643 h2c->errcode = err;
644 h2c->st0 = H2_CS_ERROR;
645}
646
Willy Tarreau175cebb2019-01-24 10:02:24 +0100647/* marks an error on the stream. It may also update an already closed stream
648 * (e.g. to report an error after an RST was received).
649 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100650static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200651{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100652 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200653 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100654 if (h2s->st < H2_SS_ERROR)
655 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100656 if (h2s->cs)
657 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200658 }
659}
660
Willy Tarreau7e094452018-12-19 18:08:52 +0100661/* attempt to notify the data layer of recv availability */
662static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
663{
664 struct wait_event *sw;
665
666 if (h2s->recv_wait) {
667 sw = h2s->recv_wait;
668 sw->events &= ~SUB_RETRY_RECV;
669 tasklet_wakeup(sw->task);
670 h2s->recv_wait = NULL;
671 }
672}
673
674/* attempt to notify the data layer of send availability */
675static void __maybe_unused h2s_notify_send(struct h2s *h2s)
676{
677 struct wait_event *sw;
678
679 if (h2s->send_wait) {
680 sw = h2s->send_wait;
681 sw->events &= ~SUB_RETRY_SEND;
682 tasklet_wakeup(sw->task);
683 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100684 LIST_DEL(&h2s->list);
685 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100686 }
687}
688
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100689/* alerts the data layer, trying to wake it up by all means, following
690 * this sequence :
691 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
692 * - if its subscribed to send, then it's woken up for send
693 * - if it was subscribed to neither, its ->wake() callback is called
694 * It is safe to call this function with a closed stream which doesn't have a
695 * conn_stream anymore.
696 */
697static void __maybe_unused h2s_alert(struct h2s *h2s)
698{
699 if (h2s->recv_wait || h2s->send_wait) {
700 h2s_notify_recv(h2s);
701 h2s_notify_send(h2s);
702 }
703 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
704 h2s->cs->data_cb->wake(h2s->cs);
705}
706
Willy Tarreaue4820742017-07-27 13:37:23 +0200707/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100708static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200709{
710 uint8_t *out = frame;
711
712 *out = len >> 16;
713 write_n16(out + 1, len);
714}
715
Willy Tarreau54c15062017-10-10 17:10:03 +0200716/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
717 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
718 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200719 * available in the buffer's input prior to calling this function. The buffer
720 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200721 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100722static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200723 const struct buffer *b, int o)
724{
Willy Tarreau591d4452018-06-15 17:21:00 +0200725 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200726}
727
Willy Tarreau1f094672017-11-20 21:27:45 +0100728static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200729{
Willy Tarreau591d4452018-06-15 17:21:00 +0200730 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200731}
732
Willy Tarreau1f094672017-11-20 21:27:45 +0100733static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200734{
Willy Tarreau591d4452018-06-15 17:21:00 +0200735 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200736}
737
Willy Tarreau1f094672017-11-20 21:27:45 +0100738static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200739{
Willy Tarreau591d4452018-06-15 17:21:00 +0200740 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200741}
742
743
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100744/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
745 * The algorithm is not obvious. It turns out that H2 headers are neither
746 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
747 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200748 *
749 * b0 b1 b2 b3 b4 b5..b8
750 * +----------+---------+--------+----+----+----------------------+
751 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
752 * +----------+---------+--------+----+----+----------------------+
753 *
754 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
755 * we get the sid properly aligned and ordered, and 16 bits of len properly
756 * ordered as well. The type and flags can be extracted using bit shifts from
757 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200758 * Returns zero if some bytes are missing, otherwise non-zero on success. The
759 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200760 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100761static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200762{
763 uint64_t w;
764
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100765 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200766 return 0;
767
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100768 w = h2_get_n64(b, o + 1);
769 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200770 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
771 h->ff = w >> 32;
772 h->ft = w >> 40;
773 h->len += w >> 48;
774 return 1;
775}
776
777/* skip the next 9 bytes corresponding to the frame header possibly parsed by
778 * h2_peek_frame_hdr() above.
779 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100780static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200781{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200782 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200783}
784
785/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100786static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200787{
788 int ret;
789
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100790 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200791 if (ret > 0)
792 h2_skip_frame_hdr(b);
793 return ret;
794}
795
Willy Tarreau00dd0782018-03-01 16:31:34 +0100796/* marks stream <h2s> as CLOSED and decrement the number of active streams for
797 * its connection if the stream was not yet closed. Please use this exclusively
798 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100799 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100800static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100801{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100802 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100803 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100804 if (!h2s->id)
805 h2s->h2c->nb_reserved--;
806 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100807 h2s->st = H2_SS_CLOSED;
808}
809
Willy Tarreau71049cc2018-03-28 13:56:39 +0200810/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
811static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100812{
813 h2s_close(h2s);
814 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200815 if (b_size(&h2s->rxbuf)) {
816 b_free(&h2s->rxbuf);
817 offer_buffers(NULL, tasks_run_queue);
818 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200819 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100820 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200821 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100822 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800823 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200824 * reference left would be in the h2c send_list/fctl_list, and if
825 * we're in it, we're getting out anyway
826 */
827 LIST_DEL(&h2s->list);
828 LIST_INIT(&h2s->list);
829 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100830 pool_free(pool_head_h2s, h2s);
831}
832
Willy Tarreaua8e49542018-10-03 18:53:55 +0200833/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
834 * stream tree. In case of error, nothing is added and NULL is returned. The
835 * causes of errors can be any failed memory allocation. The caller is
836 * responsible for checking if the connection may support an extra stream
837 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200838 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200839static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200840{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200841 struct h2s *h2s;
842
Willy Tarreaubafbe012017-11-24 17:34:44 +0100843 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200844 if (!h2s)
845 goto out;
846
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200847 h2s->wait_event.task = tasklet_new();
848 if (!h2s->wait_event.task) {
849 pool_free(pool_head_h2s, h2s);
850 goto out;
851 }
852 h2s->send_wait = NULL;
853 h2s->recv_wait = NULL;
854 h2s->wait_event.task->process = h2_deferred_shut;
855 h2s->wait_event.task->context = h2s;
856 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100857 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200858 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200859 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200860 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200861 h2s->mws = h2c->miw;
862 h2s->flags = H2_SF_NONE;
863 h2s->errcode = H2_ERR_NO_ERROR;
864 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200865 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200866 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200867
868 if (h2c->flags & H2_CF_IS_BACK) {
869 h1m_init_req(&h2s->h1m);
870 h2s->h1m.err_pos = -1; // don't care about errors on the request path
871 h2s->h1m.flags |= H1_MF_TOLOWER;
872 } else {
873 h1m_init_res(&h2s->h1m);
874 h2s->h1m.err_pos = -1; // don't care about errors on the response path
875 h2s->h1m.flags |= H1_MF_TOLOWER;
876 }
877
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200878 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200879 if (id > 0)
880 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100881 else
882 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200883
884 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100885 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200886
887 return h2s;
888
889 out_free_h2s:
890 pool_free(pool_head_h2s, h2s);
891 out:
892 return NULL;
893}
894
895/* creates a new stream <id> on the h2c connection and returns it, or NULL in
896 * case of memory allocation error.
897 */
898static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
899{
900 struct session *sess = h2c->conn->owner;
901 struct conn_stream *cs;
902 struct h2s *h2s;
903
904 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
905 goto out;
906
907 h2s = h2s_new(h2c, id);
908 if (!h2s)
909 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200910
911 cs = cs_new(h2c->conn);
912 if (!cs)
913 goto out_close;
914
Olivier Houchard746fb772018-12-15 19:42:00 +0100915 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200916 h2s->cs = cs;
917 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200918 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200919
920 if (stream_create_from_cs(cs) < 0)
921 goto out_free_cs;
922
Willy Tarreau590a0512018-09-05 11:56:48 +0200923 /* We want the accept date presented to the next stream to be the one
924 * we have now, the handshake time to be null (since the next stream
925 * is not delayed by a handshake), and the idle time to count since
926 * right now.
927 */
928 sess->accept_date = date;
929 sess->tv_accept = now;
930 sess->t_handshake = 0;
931
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200932 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200933 if (h2_has_too_many_cs(h2c))
934 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200935 return h2s;
936
937 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200938 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200939 cs_free(cs);
940 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200941 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200942 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200943 sess_log(sess);
944 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200945}
946
Willy Tarreau751f2d02018-10-05 09:35:00 +0200947/* allocates a new stream associated to conn_stream <cs> on the h2c connection
948 * and returns it, or NULL in case of memory allocation error or if the highest
949 * possible stream ID was reached.
950 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100951static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200952{
953 struct h2s *h2s = NULL;
954
955 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
956 goto out;
957
Willy Tarreaua80dca82019-01-24 17:08:28 +0100958 if (h2_streams_left(h2c) < 1)
959 goto out;
960
Willy Tarreau751f2d02018-10-05 09:35:00 +0200961 /* Defer choosing the ID until we send the first message to create the stream */
962 h2s = h2s_new(h2c, 0);
963 if (!h2s)
964 goto out;
965
966 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100967 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200968 cs->ctx = h2s;
969 h2c->nb_cs++;
970
Willy Tarreau751f2d02018-10-05 09:35:00 +0200971 out:
972 return h2s;
973}
974
Willy Tarreaube5b7152017-09-25 16:25:39 +0200975/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
976 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
977 * the various settings codes.
978 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200979static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200980{
981 struct buffer *res;
982 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200983 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200984 int ret;
985
986 if (h2c_mux_busy(h2c, NULL)) {
987 h2c->flags |= H2_CF_DEM_MBUSY;
988 return 0;
989 }
990
Willy Tarreau44e973f2018-03-01 17:49:30 +0100991 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200992 if (!res) {
993 h2c->flags |= H2_CF_MUX_MALLOC;
994 h2c->flags |= H2_CF_DEM_MROOM;
995 return 0;
996 }
997
998 chunk_init(&buf, buf_data, sizeof(buf_data));
999 chunk_memcpy(&buf,
1000 "\x00\x00\x00" /* length : 0 for now */
1001 "\x04\x00" /* type : 4 (settings), flags : 0 */
1002 "\x00\x00\x00\x00", /* stream ID : 0 */
1003 9);
1004
1005 if (h2_settings_header_table_size != 4096) {
1006 char str[6] = "\x00\x01"; /* header_table_size */
1007
1008 write_n32(str + 2, h2_settings_header_table_size);
1009 chunk_memcat(&buf, str, 6);
1010 }
1011
1012 if (h2_settings_initial_window_size != 65535) {
1013 char str[6] = "\x00\x04"; /* initial_window_size */
1014
1015 write_n32(str + 2, h2_settings_initial_window_size);
1016 chunk_memcat(&buf, str, 6);
1017 }
1018
1019 if (h2_settings_max_concurrent_streams != 0) {
1020 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1021
1022 /* Note: 0 means "unlimited" for haproxy's config but not for
1023 * the protocol, so never send this value!
1024 */
1025 write_n32(str + 2, h2_settings_max_concurrent_streams);
1026 chunk_memcat(&buf, str, 6);
1027 }
1028
1029 if (global.tune.bufsize != 16384) {
1030 char str[6] = "\x00\x05"; /* max_frame_size */
1031
1032 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1033 * match bufsize - rewrite size, but at the moment it seems
1034 * that clients don't take care of it.
1035 */
1036 write_n32(str + 2, global.tune.bufsize);
1037 chunk_memcat(&buf, str, 6);
1038 }
1039
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001040 h2_set_frame_size(buf.area, buf.data - 9);
1041 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001042 if (unlikely(ret <= 0)) {
1043 if (!ret) {
1044 h2c->flags |= H2_CF_MUX_MFULL;
1045 h2c->flags |= H2_CF_DEM_MROOM;
1046 return 0;
1047 }
1048 else {
1049 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1050 return 0;
1051 }
1052 }
1053 return ret;
1054}
1055
Willy Tarreau52eed752017-09-22 15:05:09 +02001056/* Try to receive a connection preface, then upon success try to send our
1057 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1058 * missing data. It may return an error in h2c.
1059 */
1060static int h2c_frt_recv_preface(struct h2c *h2c)
1061{
1062 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001063 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001064
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001065 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001066
1067 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001068 if (ret1 < 0)
1069 sess_log(h2c->conn->owner);
1070
Willy Tarreau52eed752017-09-22 15:05:09 +02001071 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1072 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1073 return 0;
1074 }
1075
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001076 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001077 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001078 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001079
Willy Tarreaube5b7152017-09-25 16:25:39 +02001080 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001081}
1082
Willy Tarreau01b44822018-10-03 14:26:37 +02001083/* Try to send a connection preface, then upon success try to send our
1084 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1085 * missing data. It may return an error in h2c.
1086 */
1087static int h2c_bck_send_preface(struct h2c *h2c)
1088{
1089 struct buffer *res;
1090
1091 if (h2c_mux_busy(h2c, NULL)) {
1092 h2c->flags |= H2_CF_DEM_MBUSY;
1093 return 0;
1094 }
1095
1096 res = h2_get_buf(h2c, &h2c->mbuf);
1097 if (!res) {
1098 h2c->flags |= H2_CF_MUX_MALLOC;
1099 h2c->flags |= H2_CF_DEM_MROOM;
1100 return 0;
1101 }
1102
1103 if (!b_data(res)) {
1104 /* preface not yet sent */
1105 b_istput(res, ist(H2_CONN_PREFACE));
1106 }
1107
1108 return h2c_send_settings(h2c);
1109}
1110
Willy Tarreau081d4722017-05-16 21:51:05 +02001111/* try to send a GOAWAY frame on the connection to report an error or a graceful
1112 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1113 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1114 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1115 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1116 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1117 * on unrecoverable failure. It will not attempt to send one again in this last
1118 * case so that it is safe to use h2c_error() to report such errors.
1119 */
1120static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1121{
1122 struct buffer *res;
1123 char str[17];
1124 int ret;
1125
1126 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1127 return 1; // claim that it worked
1128
1129 if (h2c_mux_busy(h2c, h2s)) {
1130 if (h2s)
1131 h2s->flags |= H2_SF_BLK_MBUSY;
1132 else
1133 h2c->flags |= H2_CF_DEM_MBUSY;
1134 return 0;
1135 }
1136
Willy Tarreau44e973f2018-03-01 17:49:30 +01001137 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001138 if (!res) {
1139 h2c->flags |= H2_CF_MUX_MALLOC;
1140 if (h2s)
1141 h2s->flags |= H2_SF_BLK_MROOM;
1142 else
1143 h2c->flags |= H2_CF_DEM_MROOM;
1144 return 0;
1145 }
1146
1147 /* len: 8, type: 7, flags: none, sid: 0 */
1148 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1149
1150 if (h2c->last_sid < 0)
1151 h2c->last_sid = h2c->max_id;
1152
1153 write_n32(str + 9, h2c->last_sid);
1154 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001155 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001156 if (unlikely(ret <= 0)) {
1157 if (!ret) {
1158 h2c->flags |= H2_CF_MUX_MFULL;
1159 if (h2s)
1160 h2s->flags |= H2_SF_BLK_MROOM;
1161 else
1162 h2c->flags |= H2_CF_DEM_MROOM;
1163 return 0;
1164 }
1165 else {
1166 /* we cannot report this error using GOAWAY, so we mark
1167 * it and claim a success.
1168 */
1169 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1170 h2c->flags |= H2_CF_GOAWAY_FAILED;
1171 return 1;
1172 }
1173 }
1174 h2c->flags |= H2_CF_GOAWAY_SENT;
1175 return ret;
1176}
1177
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001178/* Try to send an RST_STREAM frame on the connection for the indicated stream
1179 * during mux operations. This stream must be valid and cannot be closed
1180 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1181 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1182 * not yet.
1183 *
1184 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1185 * to write the message, it subscribes the stream to future notifications.
1186 */
1187static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1188{
1189 struct buffer *res;
1190 char str[13];
1191 int ret;
1192
1193 if (!h2s || h2s->st == H2_SS_CLOSED)
1194 return 1;
1195
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001196 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1197 * RST_STREAM in response to a RST_STREAM frame.
1198 */
1199 if (h2c->dft == H2_FT_RST_STREAM) {
1200 ret = 1;
1201 goto ignore;
1202 }
1203
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001204 if (h2c_mux_busy(h2c, h2s)) {
1205 h2s->flags |= H2_SF_BLK_MBUSY;
1206 return 0;
1207 }
1208
Willy Tarreau44e973f2018-03-01 17:49:30 +01001209 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001210 if (!res) {
1211 h2c->flags |= H2_CF_MUX_MALLOC;
1212 h2s->flags |= H2_SF_BLK_MROOM;
1213 return 0;
1214 }
1215
1216 /* len: 4, type: 3, flags: none */
1217 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1218 write_n32(str + 5, h2s->id);
1219 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001220 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001221
1222 if (unlikely(ret <= 0)) {
1223 if (!ret) {
1224 h2c->flags |= H2_CF_MUX_MFULL;
1225 h2s->flags |= H2_SF_BLK_MROOM;
1226 return 0;
1227 }
1228 else {
1229 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1230 return 0;
1231 }
1232 }
1233
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001234 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001235 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001236 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001237 return ret;
1238}
1239
1240/* Try to send an RST_STREAM frame on the connection for the stream being
1241 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001242 * error code, even if the stream is one of the dummy ones, and will update
1243 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001244 *
1245 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1246 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001247 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001248 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001249 */
1250static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1251{
1252 struct buffer *res;
1253 char str[13];
1254 int ret;
1255
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001256 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1257 * RST_STREAM in response to a RST_STREAM frame.
1258 */
1259 if (h2c->dft == H2_FT_RST_STREAM) {
1260 ret = 1;
1261 goto ignore;
1262 }
1263
Willy Tarreau27a84c92017-10-17 08:10:17 +02001264 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001265 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001266 return 0;
1267 }
1268
Willy Tarreau44e973f2018-03-01 17:49:30 +01001269 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001270 if (!res) {
1271 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001272 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001273 return 0;
1274 }
1275
1276 /* len: 4, type: 3, flags: none */
1277 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001278
Willy Tarreau27a84c92017-10-17 08:10:17 +02001279 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001280 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001281 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001282
Willy Tarreau27a84c92017-10-17 08:10:17 +02001283 if (unlikely(ret <= 0)) {
1284 if (!ret) {
1285 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001286 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001287 return 0;
1288 }
1289 else {
1290 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1291 return 0;
1292 }
1293 }
1294
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001295 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001296 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001297 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001298 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001299 }
1300
Willy Tarreau27a84c92017-10-17 08:10:17 +02001301 return ret;
1302}
1303
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001304/* try to send an empty DATA frame with the ES flag set to notify about the
1305 * end of stream and match a shutdown(write). If an ES was already sent as
1306 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1307 * on success or zero if nothing was done. In case of lack of room to write the
1308 * message, it subscribes the requesting stream to future notifications.
1309 */
1310static int h2_send_empty_data_es(struct h2s *h2s)
1311{
1312 struct h2c *h2c = h2s->h2c;
1313 struct buffer *res;
1314 char str[9];
1315 int ret;
1316
Willy Tarreau721c9742017-11-07 11:05:42 +01001317 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001318 return 1;
1319
1320 if (h2c_mux_busy(h2c, h2s)) {
1321 h2s->flags |= H2_SF_BLK_MBUSY;
1322 return 0;
1323 }
1324
Willy Tarreau44e973f2018-03-01 17:49:30 +01001325 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001326 if (!res) {
1327 h2c->flags |= H2_CF_MUX_MALLOC;
1328 h2s->flags |= H2_SF_BLK_MROOM;
1329 return 0;
1330 }
1331
1332 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1333 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1334 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001335 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001336 if (likely(ret > 0)) {
1337 h2s->flags |= H2_SF_ES_SENT;
1338 }
1339 else if (!ret) {
1340 h2c->flags |= H2_CF_MUX_MFULL;
1341 h2s->flags |= H2_SF_BLK_MROOM;
1342 return 0;
1343 }
1344 else {
1345 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1346 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001347 }
1348 return ret;
1349}
1350
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001351/* wake the streams attached to the connection, whose id is greater than <last>,
1352 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001353 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1354 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001355 */
1356static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1357{
1358 struct eb32_node *node;
1359 struct h2s *h2s;
1360
1361 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001362 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001363
1364 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001365 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001366
1367 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1368 while (node) {
1369 h2s = container_of(node, struct h2s, by_id);
1370 if (h2s->id <= last)
1371 break;
1372 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001373
1374 if (!h2s->cs) {
1375 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001376 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001377 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001378 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001379
1380 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001381 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1382 h2s->cs->flags |= CS_FL_ERROR;
1383
Willy Tarreauf830f012018-12-19 17:44:55 +01001384 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001385
Willy Tarreaua8519352018-12-18 16:44:28 +01001386 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001387 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001388 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001389 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001390 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001391 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001392 }
1393}
1394
Willy Tarreau3421aba2017-07-27 15:41:03 +02001395/* Increase all streams' outgoing window size by the difference passed in
1396 * argument. This is needed upon receipt of the settings frame if the initial
1397 * window size is different. The difference may be negative and the resulting
1398 * window size as well, for the time it takes to receive some window updates.
1399 */
1400static void h2c_update_all_ws(struct h2c *h2c, int diff)
1401{
1402 struct h2s *h2s;
1403 struct eb32_node *node;
1404
1405 if (!diff)
1406 return;
1407
1408 node = eb32_first(&h2c->streams_by_id);
1409 while (node) {
1410 h2s = container_of(node, struct h2s, by_id);
1411 h2s->mws += diff;
1412 node = eb32_next(node);
1413 }
1414}
1415
1416/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1417 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1418 * return an error in h2c. Described in RFC7540#6.5.
1419 */
1420static int h2c_handle_settings(struct h2c *h2c)
1421{
1422 unsigned int offset;
1423 int error;
1424
1425 if (h2c->dff & H2_F_SETTINGS_ACK) {
1426 if (h2c->dfl) {
1427 error = H2_ERR_FRAME_SIZE_ERROR;
1428 goto fail;
1429 }
1430 return 1;
1431 }
1432
1433 if (h2c->dsi != 0) {
1434 error = H2_ERR_PROTOCOL_ERROR;
1435 goto fail;
1436 }
1437
1438 if (h2c->dfl % 6) {
1439 error = H2_ERR_FRAME_SIZE_ERROR;
1440 goto fail;
1441 }
1442
1443 /* that's the limit we can process */
1444 if (h2c->dfl > global.tune.bufsize) {
1445 error = H2_ERR_FRAME_SIZE_ERROR;
1446 goto fail;
1447 }
1448
1449 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001450 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001451 return 0;
1452
1453 /* parse the frame */
1454 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001455 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1456 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001457
1458 switch (type) {
1459 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1460 /* we need to update all existing streams with the
1461 * difference from the previous iws.
1462 */
1463 if (arg < 0) { // RFC7540#6.5.2
1464 error = H2_ERR_FLOW_CONTROL_ERROR;
1465 goto fail;
1466 }
1467 h2c_update_all_ws(h2c, arg - h2c->miw);
1468 h2c->miw = arg;
1469 break;
1470 case H2_SETTINGS_MAX_FRAME_SIZE:
1471 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1472 error = H2_ERR_PROTOCOL_ERROR;
1473 goto fail;
1474 }
1475 h2c->mfs = arg;
1476 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001477 case H2_SETTINGS_ENABLE_PUSH:
1478 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1479 error = H2_ERR_PROTOCOL_ERROR;
1480 goto fail;
1481 }
1482 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001483 }
1484 }
1485
1486 /* need to ACK this frame now */
1487 h2c->st0 = H2_CS_FRAME_A;
1488 return 1;
1489 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001490 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001491 h2c_error(h2c, error);
1492 return 0;
1493}
1494
1495/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1496 * success or one of the h2_status values.
1497 */
1498static int h2c_ack_settings(struct h2c *h2c)
1499{
1500 struct buffer *res;
1501 char str[9];
1502 int ret = -1;
1503
1504 if (h2c_mux_busy(h2c, NULL)) {
1505 h2c->flags |= H2_CF_DEM_MBUSY;
1506 return 0;
1507 }
1508
Willy Tarreau44e973f2018-03-01 17:49:30 +01001509 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001510 if (!res) {
1511 h2c->flags |= H2_CF_MUX_MALLOC;
1512 h2c->flags |= H2_CF_DEM_MROOM;
1513 return 0;
1514 }
1515
1516 memcpy(str,
1517 "\x00\x00\x00" /* length : 0 (no data) */
1518 "\x04" "\x01" /* type : 4, flags : ACK */
1519 "\x00\x00\x00\x00" /* stream ID */, 9);
1520
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001521 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001522 if (unlikely(ret <= 0)) {
1523 if (!ret) {
1524 h2c->flags |= H2_CF_MUX_MFULL;
1525 h2c->flags |= H2_CF_DEM_MROOM;
1526 return 0;
1527 }
1528 else {
1529 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1530 return 0;
1531 }
1532 }
1533 return ret;
1534}
1535
Willy Tarreaucf68c782017-10-10 17:11:41 +02001536/* processes a PING frame and schedules an ACK if needed. The caller must pass
1537 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1538 * missing data. It may return an error in h2c.
1539 */
1540static int h2c_handle_ping(struct h2c *h2c)
1541{
1542 /* frame length must be exactly 8 */
1543 if (h2c->dfl != 8) {
1544 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1545 return 0;
1546 }
1547
1548 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001549 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001550 h2c->st0 = H2_CS_FRAME_A;
1551 return 1;
1552}
1553
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001554/* Try to send a window update for stream id <sid> and value <increment>.
1555 * Returns > 0 on success or zero on missing room or failure. It may return an
1556 * error in h2c.
1557 */
1558static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1559{
1560 struct buffer *res;
1561 char str[13];
1562 int ret = -1;
1563
1564 if (h2c_mux_busy(h2c, NULL)) {
1565 h2c->flags |= H2_CF_DEM_MBUSY;
1566 return 0;
1567 }
1568
Willy Tarreau44e973f2018-03-01 17:49:30 +01001569 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001570 if (!res) {
1571 h2c->flags |= H2_CF_MUX_MALLOC;
1572 h2c->flags |= H2_CF_DEM_MROOM;
1573 return 0;
1574 }
1575
1576 /* length: 4, type: 8, flags: none */
1577 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1578 write_n32(str + 5, sid);
1579 write_n32(str + 9, increment);
1580
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001581 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001582
1583 if (unlikely(ret <= 0)) {
1584 if (!ret) {
1585 h2c->flags |= H2_CF_MUX_MFULL;
1586 h2c->flags |= H2_CF_DEM_MROOM;
1587 return 0;
1588 }
1589 else {
1590 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1591 return 0;
1592 }
1593 }
1594 return ret;
1595}
1596
1597/* try to send pending window update for the connection. It's safe to call it
1598 * with no pending updates. Returns > 0 on success or zero on missing room or
1599 * failure. It may return an error in h2c.
1600 */
1601static int h2c_send_conn_wu(struct h2c *h2c)
1602{
1603 int ret = 1;
1604
1605 if (h2c->rcvd_c <= 0)
1606 return 1;
1607
Willy Tarreau97aaa672018-12-23 09:49:04 +01001608 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1609 /* increase the advertised connection window to 2G on
1610 * first update.
1611 */
1612 h2c->flags |= H2_CF_WINDOW_OPENED;
1613 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1614 }
1615
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001616 /* send WU for the connection */
1617 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1618 if (ret > 0)
1619 h2c->rcvd_c = 0;
1620
1621 return ret;
1622}
1623
1624/* try to send pending window update for the current dmux stream. It's safe to
1625 * call it with no pending updates. Returns > 0 on success or zero on missing
1626 * room or failure. It may return an error in h2c.
1627 */
1628static int h2c_send_strm_wu(struct h2c *h2c)
1629{
1630 int ret = 1;
1631
1632 if (h2c->rcvd_s <= 0)
1633 return 1;
1634
1635 /* send WU for the stream */
1636 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1637 if (ret > 0)
1638 h2c->rcvd_s = 0;
1639
1640 return ret;
1641}
1642
Willy Tarreaucf68c782017-10-10 17:11:41 +02001643/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1644 * success, 0 on missing data or one of the h2_status values.
1645 */
1646static int h2c_ack_ping(struct h2c *h2c)
1647{
1648 struct buffer *res;
1649 char str[17];
1650 int ret = -1;
1651
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001652 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001653 return 0;
1654
1655 if (h2c_mux_busy(h2c, NULL)) {
1656 h2c->flags |= H2_CF_DEM_MBUSY;
1657 return 0;
1658 }
1659
Willy Tarreau44e973f2018-03-01 17:49:30 +01001660 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001661 if (!res) {
1662 h2c->flags |= H2_CF_MUX_MALLOC;
1663 h2c->flags |= H2_CF_DEM_MROOM;
1664 return 0;
1665 }
1666
1667 memcpy(str,
1668 "\x00\x00\x08" /* length : 8 (same payload) */
1669 "\x06" "\x01" /* type : 6, flags : ACK */
1670 "\x00\x00\x00\x00" /* stream ID */, 9);
1671
1672 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001673 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001674
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001675 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001676 if (unlikely(ret <= 0)) {
1677 if (!ret) {
1678 h2c->flags |= H2_CF_MUX_MFULL;
1679 h2c->flags |= H2_CF_DEM_MROOM;
1680 return 0;
1681 }
1682 else {
1683 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1684 return 0;
1685 }
1686 }
1687 return ret;
1688}
1689
Willy Tarreau26f95952017-07-27 17:18:30 +02001690/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1691 * Returns > 0 on success or zero on missing data. It may return an error in
1692 * h2c or h2s. Described in RFC7540#6.9.
1693 */
1694static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1695{
1696 int32_t inc;
1697 int error;
1698
1699 if (h2c->dfl != 4) {
1700 error = H2_ERR_FRAME_SIZE_ERROR;
1701 goto conn_err;
1702 }
1703
1704 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001705 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001706 return 0;
1707
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001708 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001709
1710 if (h2c->dsi != 0) {
1711 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001712
1713 /* it's not an error to receive WU on a closed stream */
1714 if (h2s->st == H2_SS_CLOSED)
1715 return 1;
1716
1717 if (!inc) {
1718 error = H2_ERR_PROTOCOL_ERROR;
1719 goto strm_err;
1720 }
1721
1722 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1723 error = H2_ERR_FLOW_CONTROL_ERROR;
1724 goto strm_err;
1725 }
1726
1727 h2s->mws += inc;
1728 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1729 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001730 if (h2s->send_wait)
1731 LIST_ADDQ(&h2c->send_list, &h2s->list);
1732
Willy Tarreau26f95952017-07-27 17:18:30 +02001733 }
1734 }
1735 else {
1736 /* connection window update */
1737 if (!inc) {
1738 error = H2_ERR_PROTOCOL_ERROR;
1739 goto conn_err;
1740 }
1741
1742 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1743 error = H2_ERR_FLOW_CONTROL_ERROR;
1744 goto conn_err;
1745 }
1746
1747 h2c->mws += inc;
1748 }
1749
1750 return 1;
1751
1752 conn_err:
1753 h2c_error(h2c, error);
1754 return 0;
1755
1756 strm_err:
1757 if (h2s) {
1758 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001759 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001760 }
1761 else
1762 h2c_error(h2c, error);
1763 return 0;
1764}
1765
Willy Tarreaue96b0922017-10-30 00:28:29 +01001766/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1767 * the last ID. Returns > 0 on success or zero on missing data. It may return
1768 * an error in h2c. Described in RFC7540#6.8.
1769 */
1770static int h2c_handle_goaway(struct h2c *h2c)
1771{
1772 int error;
1773 int last;
1774
1775 if (h2c->dsi != 0) {
1776 error = H2_ERR_PROTOCOL_ERROR;
1777 goto conn_err;
1778 }
1779
1780 if (h2c->dfl < 8) {
1781 error = H2_ERR_FRAME_SIZE_ERROR;
1782 goto conn_err;
1783 }
1784
1785 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001786 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001787 return 0;
1788
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001789 last = h2_get_n32(&h2c->dbuf, 0);
1790 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001791 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001792 if (h2c->last_sid < 0)
1793 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001794 return 1;
1795
1796 conn_err:
1797 h2c_error(h2c, error);
1798 return 0;
1799}
1800
Willy Tarreau92153fc2017-12-03 19:46:19 +01001801/* processes a PRIORITY frame, and either skips it or rejects if it is
1802 * invalid. Returns > 0 on success or zero on missing data. It may return
1803 * an error in h2c. Described in RFC7540#6.3.
1804 */
1805static int h2c_handle_priority(struct h2c *h2c)
1806{
1807 int error;
1808
1809 if (h2c->dsi == 0) {
1810 error = H2_ERR_PROTOCOL_ERROR;
1811 goto conn_err;
1812 }
1813
1814 if (h2c->dfl != 5) {
1815 error = H2_ERR_FRAME_SIZE_ERROR;
1816 goto conn_err;
1817 }
1818
1819 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001820 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001821 return 0;
1822
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001823 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001824 /* 7540#5.3 : can't depend on itself */
1825 error = H2_ERR_PROTOCOL_ERROR;
1826 goto conn_err;
1827 }
1828 return 1;
1829
1830 conn_err:
1831 h2c_error(h2c, error);
1832 return 0;
1833}
1834
Willy Tarreaucd234e92017-08-18 10:59:39 +02001835/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1836 * Returns > 0 on success or zero on missing data. It may return an error in
1837 * h2c. Described in RFC7540#6.4.
1838 */
1839static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1840{
1841 int error;
1842
1843 if (h2c->dsi == 0) {
1844 error = H2_ERR_PROTOCOL_ERROR;
1845 goto conn_err;
1846 }
1847
Willy Tarreaucd234e92017-08-18 10:59:39 +02001848 if (h2c->dfl != 4) {
1849 error = H2_ERR_FRAME_SIZE_ERROR;
1850 goto conn_err;
1851 }
1852
1853 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001854 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001855 return 0;
1856
1857 /* late RST, already handled */
1858 if (h2s->st == H2_SS_CLOSED)
1859 return 1;
1860
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001861 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001862 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001863
1864 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001865 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001866 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001867 }
1868
1869 h2s->flags |= H2_SF_RST_RCVD;
1870 return 1;
1871
1872 conn_err:
1873 h2c_error(h2c, error);
1874 return 0;
1875}
1876
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001877/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1878 * It may return an error in h2c or h2s. The caller must consider that the
1879 * return value is the new h2s in case one was allocated (most common case).
1880 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001881 * errors here are reported as connection errors since it's impossible to
1882 * recover from such errors after the compression context has been altered.
1883 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001884static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001885{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001886 struct buffer rxbuf = BUF_NULL;
1887 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001888 int error;
1889
1890 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001891 /* RFC7540#4.2 */
1892 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001893 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001894 goto conn_err;
Willy Tarreau13278b42017-10-13 19:23:14 +02001895 }
1896
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001897 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001898 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001899
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001900 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001901 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001902
1903 /* now either the frame is complete or the buffer is complete */
1904 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001905 /* The stream exists/existed, this must be a trailers frame */
1906 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau31e846a2019-01-24 10:26:47 +01001907 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001908 goto out;
1909 goto done;
1910 }
Willy Tarreau13278b42017-10-13 19:23:14 +02001911 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001912 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001913 goto conn_err;
1914 }
1915 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1916 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1917 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001918 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001919 goto conn_err;
1920 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001921 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1922 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001923
Willy Tarreau25919232019-01-03 14:48:18 +01001924 error = h2c_decode_headers(h2c, &rxbuf, &flags);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001925
Willy Tarreau25919232019-01-03 14:48:18 +01001926 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001927 if (h2c->st0 >= H2_CS_ERROR)
1928 goto out;
1929
Willy Tarreau25919232019-01-03 14:48:18 +01001930 if (error <= 0) {
1931 if (error == 0)
1932 goto out; // missing data
1933
1934 /* Failed to decode this stream (e.g. too large request)
1935 * but the HPACK decompressor is still synchronized.
1936 */
1937 h2s = (struct h2s*)h2_error_stream;
1938 goto send_rst;
1939 }
1940
Willy Tarreau22de8d32018-09-05 19:55:58 +02001941 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001942 * positively from h2c_frt_stream_new(), the stream will report the error,
1943 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001944 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001945 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001946 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001947 h2s = (struct h2s*)h2_refused_stream;
1948 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001949 }
1950
1951 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001952 h2s->rxbuf = rxbuf;
1953 h2s->flags |= flags;
1954
Willy Tarreau88d138e2019-01-02 19:38:14 +01001955 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001956 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001957 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001958
1959 if (h2s->flags & H2_SF_ES_RCVD) {
1960 h2s->st = H2_SS_HREM;
Willy Tarreau39d68502018-03-02 12:26:37 +01001961 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001962 }
1963
Willy Tarreau3a429f02019-01-03 11:41:50 +01001964 /* update the max stream ID if the request is being processed */
1965 if (h2s->id > h2c->max_id)
1966 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001967
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001968 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001969
1970 conn_err:
1971 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001972 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001973
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001974 out:
1975 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001976 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001977
1978 send_rst:
1979 /* make the demux send an RST for the current stream. We may only
1980 * do this if we're certain that the HEADERS frame was properly
1981 * decompressed so that the HPACK decoder is still kept up to date.
1982 */
1983 h2_release_buf(h2c, &rxbuf);
1984 h2c->st0 = H2_CS_FRAME_E;
1985 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001986}
1987
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001988/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1989 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1990 * errors here are reported as connection errors since it's impossible to
1991 * recover from such errors after the compression context has been altered.
1992 */
1993static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1994{
1995 int error;
1996
1997 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001998 /* RFC7540#4.2 */
1999 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002000 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01002001 goto conn_err;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002002 }
2003
2004 if (!b_size(&h2c->dbuf))
2005 return NULL; // empty buffer
2006
2007 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2008 return NULL; // incomplete frame
2009
Willy Tarreau25919232019-01-03 14:48:18 +01002010 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002011
Willy Tarreau25919232019-01-03 14:48:18 +01002012 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002013 if (h2c->st0 >= H2_CS_ERROR)
2014 return NULL;
2015
Willy Tarreau25919232019-01-03 14:48:18 +01002016 if (error <= 0) {
2017 if (error == 0)
2018 return NULL; // missing data
2019
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002020 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002021 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002022 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002023 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002024 }
2025
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002026 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2027 h2s->flags |= H2_SF_ES_RCVD;
2028 h2s->cs->flags |= CS_FL_REOS;
2029 }
2030
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002031 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
2032 h2s->st = H2_SS_ERROR;
2033 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
2034 h2s->st = H2_SS_HREM;
2035 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
2036 h2s_close(h2s);
2037
2038 return h2s;
2039
2040 conn_err:
2041 h2c_error(h2c, error);
2042 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002043}
2044
Willy Tarreau454f9052017-10-26 19:40:35 +02002045/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2046 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2047 */
2048static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2049{
2050 int error;
2051
2052 /* note that empty DATA frames are perfectly valid and sometimes used
2053 * to signal an end of stream (with the ES flag).
2054 */
2055
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002056 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002057 return 0; // empty buffer
2058
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002059 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002060 return 0; // incomplete frame
2061
2062 /* now either the frame is complete or the buffer is complete */
2063
2064 if (!h2c->dsi) {
2065 /* RFC7540#6.1 */
2066 error = H2_ERR_PROTOCOL_ERROR;
2067 goto conn_err;
2068 }
2069
2070 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2071 /* RFC7540#6.1 */
2072 error = H2_ERR_STREAM_CLOSED;
2073 goto strm_err;
2074 }
2075
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002076 if (!h2_frt_transfer_data(h2s))
2077 return 0;
2078
Willy Tarreau454f9052017-10-26 19:40:35 +02002079 /* call the upper layers to process the frame, then let the upper layer
2080 * notify the stream about any change.
2081 */
2082 if (!h2s->cs) {
2083 error = H2_ERR_STREAM_CLOSED;
2084 goto strm_err;
2085 }
2086
Willy Tarreau8f650c32017-11-21 19:36:21 +01002087 if (h2c->st0 >= H2_CS_ERROR)
2088 return 0;
2089
Willy Tarreau721c9742017-11-07 11:05:42 +01002090 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002091 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002092 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002093 }
2094
2095 /* check for completion : the callee will change this to FRAME_A or
2096 * FRAME_H once done.
2097 */
2098 if (h2c->st0 == H2_CS_FRAME_P)
2099 return 0;
2100
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002101
2102 /* last frame */
2103 if (h2c->dff & H2_F_DATA_END_STREAM) {
2104 h2s->st = H2_SS_HREM;
2105 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002106 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002107 }
2108
Willy Tarreau454f9052017-10-26 19:40:35 +02002109 return 1;
2110
2111 conn_err:
2112 h2c_error(h2c, error);
2113 return 0;
2114
2115 strm_err:
2116 if (h2s) {
2117 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002118 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002119 }
2120 else
2121 h2c_error(h2c, error);
2122 return 0;
2123}
2124
Willy Tarreaubc933932017-10-09 16:21:43 +02002125/* process Rx frames to be demultiplexed */
2126static void h2_process_demux(struct h2c *h2c)
2127{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002128 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002129
Willy Tarreau081d4722017-05-16 21:51:05 +02002130 if (h2c->st0 >= H2_CS_ERROR)
2131 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002132
2133 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2134 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002135 if (h2c->flags & H2_CF_IS_BACK)
2136 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002137 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2138 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002139 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002140 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002141 sess_log(h2c->conn->owner);
2142 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002143 goto fail;
2144 }
2145
2146 h2c->max_id = 0;
2147 h2c->st0 = H2_CS_SETTINGS1;
2148 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002149
2150 if (h2c->st0 == H2_CS_SETTINGS1) {
2151 struct h2_fh hdr;
2152
2153 /* ensure that what is pending is a valid SETTINGS frame
2154 * without an ACK.
2155 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002156 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002157 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002158 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002159 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002160 sess_log(h2c->conn->owner);
2161 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002162 goto fail;
2163 }
2164
2165 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2166 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2167 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2168 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002169 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002170 goto fail;
2171 }
2172
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002173 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002174 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2175 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2176 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002177 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002178 goto fail;
2179 }
2180
Willy Tarreau3bf69182018-12-21 15:34:50 +01002181 /* that's OK, switch to FRAME_P to process it. This is
2182 * a SETTINGS frame whose header has already been
2183 * deleted above.
2184 */
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002185 h2c->dfl = hdr.len;
2186 h2c->dsi = hdr.sid;
2187 h2c->dft = hdr.ft;
2188 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002189 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002190 h2c->st0 = H2_CS_FRAME_P;
2191 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002192 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002193
2194 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002195 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002196 int ret = 0;
2197
2198 if (h2c->st0 >= H2_CS_ERROR)
2199 break;
2200
2201 if (h2c->st0 == H2_CS_FRAME_H) {
2202 struct h2_fh hdr;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002203 unsigned int padlen = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002204
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002205 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002206 break;
2207
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002208 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002209 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2210 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002211 if (!h2c->nb_streams) {
2212 /* only log if no other stream can report the error */
2213 sess_log(h2c->conn->owner);
2214 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002215 break;
2216 }
2217
Willy Tarreau3bf69182018-12-21 15:34:50 +01002218 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2219 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2220 * we read the pad length and drop it from the remaining
2221 * payload (one byte + the 9 remaining ones = 10 total
2222 * removed), so we have a frame payload starting after the
2223 * pad len. Flow controlled frames (DATA) also count the
2224 * padlen in the flow control, so it must be adjusted.
2225 */
2226 if (hdr.len < 1) {
2227 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2228 sess_log(h2c->conn->owner);
2229 goto fail;
2230 }
2231 hdr.len--;
2232
2233 if (b_data(&h2c->dbuf) < 10)
2234 break; // missing padlen
2235
2236 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2237
2238 if (padlen > hdr.len) {
2239 /* RFC7540#6.1 : pad length = length of
2240 * frame payload or greater => error.
2241 */
2242 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2243 sess_log(h2c->conn->owner);
2244 goto fail;
2245 }
2246
2247 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2248 h2c->rcvd_c++;
2249 h2c->rcvd_s++;
2250 }
2251 b_del(&h2c->dbuf, 1);
2252 }
2253 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002254 h2c->dfl = hdr.len;
2255 h2c->dsi = hdr.sid;
2256 h2c->dft = hdr.ft;
2257 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002258 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002259 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002260 }
2261
2262 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002263 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2264
Willy Tarreau567beb82018-12-18 16:52:44 +01002265 if (tmp_h2s != h2s && h2s && h2s->cs &&
2266 (b_data(&h2s->rxbuf) ||
2267 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002268 /* we may have to signal the upper layers */
2269 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002270 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002271 }
2272 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002273
Willy Tarreaud7901432017-12-29 11:34:40 +01002274 if (h2c->st0 == H2_CS_FRAME_E)
2275 goto strm_err;
2276
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002277 if (h2s->st == H2_SS_IDLE &&
2278 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2279 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2280 * this state MUST be treated as a connection error
2281 */
2282 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2283 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002284 if (!h2c->nb_streams) {
2285 /* only log if no other stream can report the error */
2286 sess_log(h2c->conn->owner);
2287 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002288 break;
2289 }
2290
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002291 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2292 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2293 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002294 * this state MUST be treated as a stream error.
2295 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2296 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002297 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002298 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2299 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2300 else
2301 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002302 goto strm_err;
2303 }
2304
Willy Tarreauab837502017-12-27 15:07:30 +01002305 /* Below the management of frames received in closed state is a
2306 * bit hackish because the spec makes strong differences between
2307 * streams closed by receiving RST, sending RST, and seeing ES
2308 * in both directions. In addition to this, the creation of a
2309 * new stream reusing the identifier of a closed one will be
2310 * detected here. Given that we cannot keep track of all closed
2311 * streams forever, we consider that unknown closed streams were
2312 * closed on RST received, which allows us to respond with an
2313 * RST without breaking the connection (eg: to abort a transfer).
2314 * Some frames have to be silently ignored as well.
2315 */
2316 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau113c7a22019-01-24 09:36:53 +01002317 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002318 /* #5.1.1: The identifier of a newly
2319 * established stream MUST be numerically
2320 * greater than all streams that the initiating
2321 * endpoint has opened or reserved. This
2322 * governs streams that are opened using a
2323 * HEADERS frame and streams that are reserved
2324 * using PUSH_PROMISE. An endpoint that
2325 * receives an unexpected stream identifier
2326 * MUST respond with a connection error.
2327 */
2328 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2329 goto strm_err;
2330 }
2331
2332 if (h2s->flags & H2_SF_RST_RCVD) {
2333 /* RFC7540#5.1:closed: an endpoint that
2334 * receives any frame other than PRIORITY after
2335 * receiving a RST_STREAM MUST treat that as a
2336 * stream error of type STREAM_CLOSED.
2337 *
2338 * Note that old streams fall into this category
2339 * and will lead to an RST being sent.
2340 */
2341 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2342 h2c->st0 = H2_CS_FRAME_E;
2343 goto strm_err;
2344 }
2345
2346 /* RFC7540#5.1:closed: if this state is reached as a
2347 * result of sending a RST_STREAM frame, the peer that
2348 * receives the RST_STREAM might have already sent
2349 * frames on the stream that cannot be withdrawn. An
2350 * endpoint MUST ignore frames that it receives on
2351 * closed streams after it has sent a RST_STREAM
2352 * frame. An endpoint MAY choose to limit the period
2353 * over which it ignores frames and treat frames that
2354 * arrive after this time as being in error.
2355 */
2356 if (!(h2s->flags & H2_SF_RST_SENT)) {
2357 /* RFC7540#5.1:closed: any frame other than
2358 * PRIO/WU/RST in this state MUST be treated as
2359 * a connection error
2360 */
2361 if (h2c->dft != H2_FT_RST_STREAM &&
2362 h2c->dft != H2_FT_PRIORITY &&
2363 h2c->dft != H2_FT_WINDOW_UPDATE) {
2364 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2365 goto strm_err;
2366 }
2367 }
2368 }
2369
Willy Tarreauc0da1962017-10-30 18:38:00 +01002370#if 0
2371 // problem below: it is not possible to completely ignore such
2372 // streams as we need to maintain the compression state as well
2373 // and for this we need to completely process these frames (eg:
2374 // HEADERS frames) as well as counting DATA frames to emit
2375 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2376 // This is a typical case of layer violation where the
2377 // transported contents are critical to the connection's
2378 // validity and must be ignored at the same time :-(
2379
2380 /* graceful shutdown, ignore streams whose ID is higher than
2381 * the one advertised in GOAWAY. RFC7540#6.8.
2382 */
2383 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002384 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2385 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002386 h2c->dfl -= ret;
2387 ret = h2c->dfl == 0;
2388 goto strm_err;
2389 }
2390#endif
2391
Willy Tarreau7e98c052017-10-10 15:56:59 +02002392 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002393 case H2_FT_SETTINGS:
2394 if (h2c->st0 == H2_CS_FRAME_P)
2395 ret = h2c_handle_settings(h2c);
2396
2397 if (h2c->st0 == H2_CS_FRAME_A)
2398 ret = h2c_ack_settings(h2c);
2399 break;
2400
Willy Tarreaucf68c782017-10-10 17:11:41 +02002401 case H2_FT_PING:
2402 if (h2c->st0 == H2_CS_FRAME_P)
2403 ret = h2c_handle_ping(h2c);
2404
2405 if (h2c->st0 == H2_CS_FRAME_A)
2406 ret = h2c_ack_ping(h2c);
2407 break;
2408
Willy Tarreau26f95952017-07-27 17:18:30 +02002409 case H2_FT_WINDOW_UPDATE:
2410 if (h2c->st0 == H2_CS_FRAME_P)
2411 ret = h2c_handle_window_update(h2c, h2s);
2412 break;
2413
Willy Tarreau61290ec2017-10-17 08:19:21 +02002414 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002415 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2416 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2417 * frames' parsers consume all following CONTINUATION
2418 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002419 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002420 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2421 sess_log(h2c->conn->owner);
2422 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002423
Willy Tarreau13278b42017-10-13 19:23:14 +02002424 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002425 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002426 if (h2c->flags & H2_CF_IS_BACK)
2427 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2428 else
2429 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002430 if (tmp_h2s) {
2431 h2s = tmp_h2s;
2432 ret = 1;
2433 }
2434 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002435 break;
2436
Willy Tarreau454f9052017-10-26 19:40:35 +02002437 case H2_FT_DATA:
2438 if (h2c->st0 == H2_CS_FRAME_P)
2439 ret = h2c_frt_handle_data(h2c, h2s);
2440
2441 if (h2c->st0 == H2_CS_FRAME_A)
2442 ret = h2c_send_strm_wu(h2c);
2443 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002444
Willy Tarreau92153fc2017-12-03 19:46:19 +01002445 case H2_FT_PRIORITY:
2446 if (h2c->st0 == H2_CS_FRAME_P)
2447 ret = h2c_handle_priority(h2c);
2448 break;
2449
Willy Tarreaucd234e92017-08-18 10:59:39 +02002450 case H2_FT_RST_STREAM:
2451 if (h2c->st0 == H2_CS_FRAME_P)
2452 ret = h2c_handle_rst_stream(h2c, h2s);
2453 break;
2454
Willy Tarreaue96b0922017-10-30 00:28:29 +01002455 case H2_FT_GOAWAY:
2456 if (h2c->st0 == H2_CS_FRAME_P)
2457 ret = h2c_handle_goaway(h2c);
2458 break;
2459
Willy Tarreau1c661982017-10-30 13:52:01 +01002460 case H2_FT_PUSH_PROMISE:
2461 /* not permitted here, RFC7540#5.1 */
2462 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002463 if (!h2c->nb_streams) {
2464 /* only log if no other stream can report the error */
2465 sess_log(h2c->conn->owner);
2466 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002467 break;
2468
2469 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002470 default:
2471 /* drop frames that we ignore. They may be larger than
2472 * the buffer so we drain all of their contents until
2473 * we reach the end.
2474 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002475 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2476 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002477 h2c->dfl -= ret;
2478 ret = h2c->dfl == 0;
2479 }
2480
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002481 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002482 /* We may have to send an RST if not done yet */
2483 if (h2s->st == H2_SS_ERROR)
2484 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002485
Willy Tarreaua20a5192017-12-27 11:02:06 +01002486 if (h2c->st0 == H2_CS_FRAME_E)
2487 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002488
Willy Tarreau7e98c052017-10-10 15:56:59 +02002489 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002490 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002491 break;
2492
2493 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002494 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002495 h2c->st0 = H2_CS_FRAME_H;
2496 }
2497 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002498
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002499 if (h2c->rcvd_c > 0 &&
2500 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2501 h2c_send_conn_wu(h2c);
2502
Willy Tarreau52eed752017-09-22 15:05:09 +02002503 fail:
2504 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002505 if (h2s && h2s->cs &&
2506 (b_data(&h2s->rxbuf) ||
2507 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002508 /* we may have to signal the upper layers */
2509 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002510 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002511 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002512
Willy Tarreau47b515a2018-12-21 16:09:41 +01002513 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002514}
2515
2516/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2517 * the end.
2518 */
2519static int h2_process_mux(struct h2c *h2c)
2520{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002521 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002522
Willy Tarreau01b44822018-10-03 14:26:37 +02002523 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2524 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2525 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2526 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2527 if (h2c->st0 == H2_CS_ERROR) {
2528 h2c->st0 = H2_CS_ERROR2;
2529 sess_log(h2c->conn->owner);
2530 }
2531 goto fail;
2532 }
2533 h2c->st0 = H2_CS_SETTINGS1;
2534 }
2535 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002536 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002537 return 1;
2538 }
2539
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002540 /* start by sending possibly pending window updates */
2541 if (h2c->rcvd_c > 0 &&
2542 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2543 h2c_send_conn_wu(h2c) < 0)
2544 goto fail;
2545
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002546 /* First we always process the flow control list because the streams
2547 * waiting there were already elected for immediate emission but were
2548 * blocked just on this.
2549 */
2550
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002551 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002552 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2553 h2c->st0 >= H2_CS_ERROR)
2554 break;
2555
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002556 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002557 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2558 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002559 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002560 LIST_DEL(&h2s->list);
2561 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002562 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002563 }
2564
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002565 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002566 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2567 break;
2568
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002569 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002570 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2571 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002572 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002573 LIST_DEL(&h2s->list);
2574 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002575 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002576 }
2577
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002578 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002579 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002580 if (h2c->st0 == H2_CS_ERROR) {
2581 if (h2c->max_id >= 0) {
2582 h2c_send_goaway_error(h2c, NULL);
2583 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2584 return 0;
2585 }
2586
2587 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2588 }
2589 return 1;
2590 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002591 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002592}
2593
Willy Tarreau62f52692017-10-08 23:01:42 +02002594
Willy Tarreau479998a2018-11-18 06:30:59 +01002595/* Attempt to read data, and subscribe if none available.
2596 * The function returns 1 if data has been received, otherwise zero.
2597 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002598static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002599{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002600 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002601 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002602 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002603 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002604
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002605 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002606 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002607
Willy Tarreau315d8072017-12-10 22:17:57 +01002608 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002609 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002610
Willy Tarreau44e973f2018-03-01 17:49:30 +01002611 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002612 if (!buf) {
2613 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002614 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002615 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002616
Olivier Houchard7505f942018-08-21 18:10:44 +02002617 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002618 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002619 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2620 /* HTX in use : try to pre-align the buffer like the
2621 * rxbufs will be to optimize memory copies. We'll make
2622 * sure that the frame header lands at the end of the
2623 * HTX block to alias it upon recv. We cannot use the
2624 * head because rcv_buf() will realign the buffer if
2625 * it's empty. Thus we cheat and pretend we already
2626 * have a few bytes there.
2627 */
2628 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002629 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002630 }
2631 else
2632 max = b_room(buf);
2633
Olivier Houchard7505f942018-08-21 18:10:44 +02002634 if (max)
2635 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2636 else
2637 ret = 0;
2638 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002639
Olivier Houchard53216e72018-10-10 15:46:36 +02002640 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002641 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002642
Olivier Houcharda1411e62018-08-17 18:42:48 +02002643 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002644 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002645 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002646 }
2647
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002648 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002649 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002650 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002651}
2652
Willy Tarreau479998a2018-11-18 06:30:59 +01002653/* Try to send data if possible.
2654 * The function returns 1 if data have been sent, otherwise zero.
2655 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002656static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002657{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002658 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002659 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002660 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002661
2662 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002663 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002664
Olivier Houchard7505f942018-08-21 18:10:44 +02002665
Willy Tarreaua2af5122017-10-09 11:56:46 +02002666 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2667 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002668 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002669 }
2670
Willy Tarreaubc933932017-10-09 16:21:43 +02002671 /* This loop is quite simple : it tries to fill as much as it can from
2672 * pending streams into the existing buffer until it's reportedly full
2673 * or the end of send requests is reached. Then it tries to send this
2674 * buffer's contents out, marks it not full if at least one byte could
2675 * be sent, and tries again.
2676 *
2677 * The snd_buf() function normally takes a "flags" argument which may
2678 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2679 * data immediately comes and CO_SFL_STREAMER to indicate that the
2680 * connection is streaming lots of data (used to increase TLS record
2681 * size at the expense of latency). The former can be sent any time
2682 * there's a buffer full flag, as it indicates at least one stream
2683 * attempted to send and failed so there are pending data. An
2684 * alternative would be to set it as long as there's an active stream
2685 * but that would be problematic for ACKs until we have an absolute
2686 * guarantee that all waiters have at least one byte to send. The
2687 * latter should possibly not be set for now.
2688 */
2689
2690 done = 0;
2691 while (!done) {
2692 unsigned int flags = 0;
2693
2694 /* fill as much as we can into the current buffer */
2695 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2696 done = h2_process_mux(h2c);
2697
2698 if (conn->flags & CO_FL_ERROR)
2699 break;
2700
2701 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2702 flags |= CO_SFL_MSG_MORE;
2703
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002704 if (b_data(&h2c->mbuf)) {
2705 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002706 if (!ret)
2707 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002708 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002709 b_del(&h2c->mbuf, ret);
2710 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002711 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002712
2713 /* wrote at least one byte, the buffer is not full anymore */
2714 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2715 }
2716
Willy Tarreaua2af5122017-10-09 11:56:46 +02002717 if (conn->flags & CO_FL_SOCK_WR_SH) {
2718 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002719 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002720 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002721 /* We're not full anymore, so we can wake any task that are waiting
2722 * for us.
2723 */
2724 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002725 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002726 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2727 struct h2s *, list);
2728 LIST_DEL(&h2s->list);
2729 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002730 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002731 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2732 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002733 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002734 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002735 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002736 /* We're done, no more to send */
2737 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002738 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002739schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002740 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2741 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002742 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002743}
2744
2745static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2746{
2747 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002748 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002749
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002750 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002751 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002752 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002753 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002754 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002755 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002756 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002757}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002758
Willy Tarreau62f52692017-10-08 23:01:42 +02002759/* callback called on any event by the connection handler.
2760 * It applies changes and returns zero, or < 0 if it wants immediate
2761 * destruction of the connection (which normally doesn not happen in h2).
2762 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002763static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002764{
Olivier Houchard7505f942018-08-21 18:10:44 +02002765 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002766
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002767 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002768 h2_process_demux(h2c);
2769
2770 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002771 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002772
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002773 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002774 h2c->flags &= ~H2_CF_DEM_DFULL;
2775 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002776 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002777
Willy Tarreau0b37d652018-10-03 10:33:02 +02002778 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002779 /* frontend is stopping, reload likely in progress, let's try
2780 * to announce a graceful shutdown if not yet done. We don't
2781 * care if it fails, it will be tried again later.
2782 */
2783 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2784 if (h2c->last_sid < 0)
2785 h2c->last_sid = (1U << 31) - 1;
2786 h2c_send_goaway_error(h2c, NULL);
2787 }
2788 }
2789
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002790 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002791 * If we received early data, and the handshake is done, wake
2792 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002793 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002794 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2795 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2796 struct eb32_node *node;
2797 struct h2s *h2s;
2798
2799 h2c->flags |= H2_CF_WAIT_FOR_HS;
2800 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2801
2802 while (node) {
2803 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002804 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002805 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002806 node = eb32_next(node);
2807 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002808 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002809
Willy Tarreau26bd7612017-10-09 16:47:04 +02002810 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002811 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2812 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2813 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002814 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002815
2816 if (eb_is_empty(&h2c->streams_by_id)) {
2817 /* no more stream, kill the connection now */
2818 h2_release(conn);
2819 return -1;
2820 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002821 }
2822
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002823 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002824 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002825
Olivier Houchard53216e72018-10-10 15:46:36 +02002826 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2827 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2828 (h2c->st0 != H2_CS_ERROR &&
2829 !b_data(&h2c->mbuf) &&
2830 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2831 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002832 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002833
Willy Tarreau3f133572017-10-31 19:21:06 +01002834 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002835 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002836 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002837 task_queue(h2c->task);
2838 }
2839 else
2840 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002841 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002842
Olivier Houchard7505f942018-08-21 18:10:44 +02002843 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002844 return 0;
2845}
2846
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002847static int h2_wake(struct connection *conn)
2848{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002849 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002850
2851 return (h2_process(h2c));
2852}
2853
Willy Tarreauea392822017-10-31 10:02:25 +01002854/* Connection timeout management. The principle is that if there's no receipt
2855 * nor sending for a certain amount of time, the connection is closed. If the
2856 * MUX buffer still has lying data or is not allocatable, the connection is
2857 * immediately killed. If it's allocatable and empty, we attempt to send a
2858 * GOAWAY frame.
2859 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002860static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002861{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002862 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002863 int expired = tick_is_expired(t->expire, now_ms);
2864
Willy Tarreau0975f112018-03-29 15:22:59 +02002865 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002866 return t;
2867
Willy Tarreau0975f112018-03-29 15:22:59 +02002868 task_delete(t);
2869 task_free(t);
2870
2871 if (!h2c) {
2872 /* resources were already deleted */
2873 return NULL;
2874 }
2875
2876 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002877 h2c_error(h2c, H2_ERR_NO_ERROR);
2878 h2_wake_some_streams(h2c, 0, 0);
2879
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002880 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002881 /* don't even try to send a GOAWAY, the buffer is stuck */
2882 h2c->flags |= H2_CF_GOAWAY_FAILED;
2883 }
2884
2885 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002886 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002887 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2888 h2c->flags |= H2_CF_GOAWAY_FAILED;
2889
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002890 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2891 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002892 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002893 b_del(&h2c->mbuf, ret);
2894 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002895 }
2896 }
Willy Tarreauea392822017-10-31 10:02:25 +01002897
Willy Tarreau0975f112018-03-29 15:22:59 +02002898 /* either we can release everything now or it will be done later once
2899 * the last stream closes.
2900 */
2901 if (eb_is_empty(&h2c->streams_by_id))
2902 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002903
Willy Tarreauea392822017-10-31 10:02:25 +01002904 return NULL;
2905}
2906
2907
Willy Tarreau62f52692017-10-08 23:01:42 +02002908/*******************************************/
2909/* functions below are used by the streams */
2910/*******************************************/
2911
2912/*
2913 * Attach a new stream to a connection
2914 * (Used for outgoing connections)
2915 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002916static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002917{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002918 struct conn_stream *cs;
2919 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002920 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002921
2922 cs = cs_new(conn);
2923 if (!cs)
2924 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002925 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002926 if (!h2s) {
2927 cs_free(cs);
2928 return NULL;
2929 }
2930 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002931}
2932
Willy Tarreaufafd3982018-11-18 21:29:20 +01002933/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2934 * We have to scan because we may have some orphan streams. It might be
2935 * beneficial to scan backwards from the end to reduce the likeliness to find
2936 * orphans.
2937 */
2938static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2939{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002940 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002941 struct h2s *h2s;
2942 struct eb32_node *node;
2943
2944 node = eb32_first(&h2c->streams_by_id);
2945 while (node) {
2946 h2s = container_of(node, struct h2s, by_id);
2947 if (h2s->cs)
2948 return h2s->cs;
2949 node = eb32_next(node);
2950 }
2951 return NULL;
2952}
2953
Willy Tarreau62f52692017-10-08 23:01:42 +02002954/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002955 * Destroy the mux and the associated connection, if it is no longer used
2956 */
2957static void h2_destroy(struct connection *conn)
2958{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002959 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002960
2961 if (eb_is_empty(&h2c->streams_by_id))
2962 h2_release(h2c->conn);
2963}
2964
2965/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002966 * Detach the stream from the connection and possibly release the connection.
2967 */
2968static void h2_detach(struct conn_stream *cs)
2969{
Willy Tarreau60935142017-10-16 18:11:19 +02002970 struct h2s *h2s = cs->ctx;
2971 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002972 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002973
2974 cs->ctx = NULL;
2975 if (!h2s)
2976 return;
2977
Olivier Houchardf502aca2018-12-14 19:42:40 +01002978 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002979 h2c = h2s->h2c;
2980 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002981 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002982 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2983 !h2_has_too_many_cs(h2c)) {
2984 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002985 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002986 }
Willy Tarreau60935142017-10-16 18:11:19 +02002987
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002988 /* this stream may be blocked waiting for some data to leave (possibly
2989 * an ES or RST frame), so orphan it in this case.
2990 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002991 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002992 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002993 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002994 return;
2995
Willy Tarreau45f752e2017-10-30 15:44:59 +01002996 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2997 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2998 /* unblock the connection if it was blocked on this
2999 * stream.
3000 */
3001 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3002 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003003 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003004 }
3005
Willy Tarreau71049cc2018-03-28 13:56:39 +02003006 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003007
Olivier Houchard8a786902018-12-15 16:05:40 +01003008 if (h2c->flags & H2_CF_IS_BACK &&
3009 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003010 if (!(h2c->conn->flags &
3011 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3012 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003013 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003014 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3015 h2c->conn->owner = NULL;
3016 if (eb_is_empty(&h2c->streams_by_id)) {
3017 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3018 /* The server doesn't want it, let's kill the connection right away */
3019 h2c->conn->mux->destroy(h2c->conn);
3020 return;
3021 }
3022 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003023 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003024 if (eb_is_empty(&h2c->streams_by_id)) {
3025 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3026 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3027 return;
3028 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003029 /* Never ever allow to reuse a connection from a non-reuse backend */
3030 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3031 h2c->conn->flags |= CO_FL_PRIVATE;
Olivier Houchard855ac252018-12-28 14:44:41 +01003032 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2_settings_max_concurrent_streams) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003033 struct server *srv = objt_server(h2c->conn->target);
3034
3035 if (srv) {
3036 if (h2c->conn->flags & CO_FL_PRIVATE)
3037 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3038 else
3039 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3040 }
3041
3042 }
3043 }
3044 }
3045
Willy Tarreaue323f342018-03-28 13:51:45 +02003046 /* We don't want to close right now unless we're removing the
3047 * last stream, and either the connection is in error, or it
3048 * reached the ID already specified in a GOAWAY frame received
3049 * or sent (as seen by last_sid >= 0).
3050 */
3051 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3052 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003053 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02003054 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01003055 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003056 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003057 (conn_xprt_read0_pending(h2c->conn) ||
3058 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3059 /* no more stream will come, kill it now */
3060 h2_release(h2c->conn);
3061 }
3062 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003063 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003064 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3065 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003066 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003067 else
3068 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003069 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003070}
3071
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003072static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003073{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003074 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003075 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003076
Willy Tarreau721c9742017-11-07 11:05:42 +01003077 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003078 return;
3079
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003080 /* if no outgoing data was seen on this stream, it means it was
3081 * closed with a "tcp-request content" rule that is normally
3082 * used to kill the connection ASAP (eg: limit abuse). In this
3083 * case we send a goaway to close the connection.
3084 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003085 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003086 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003087 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003088
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003089 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3090 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003091 h2c_send_goaway_error(h2c, h2s) <= 0)
3092 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003093
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003094 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003095 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003096 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003097
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003098 return;
3099add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003100 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003101 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003102 if (h2s->flags & H2_SF_BLK_MFCTL) {
3103 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3104 h2s->send_wait = sw;
3105 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3106 h2s->send_wait = sw;
3107 LIST_ADDQ(&h2c->send_list, &h2s->list);
3108 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003109 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003110 /* Let the handler know we want shutr */
3111 sw->handle = (void *)((long)sw->handle | 1);
3112
Willy Tarreau62f52692017-10-08 23:01:42 +02003113}
3114
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003115static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003116{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003117 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003118 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003119
Willy Tarreau721c9742017-11-07 11:05:42 +01003120 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003121 return;
3122
Willy Tarreau67434202017-11-06 20:20:51 +01003123 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003124 /* we can cleanly close using an empty data frame only after headers */
3125
3126 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3127 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003128 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003129
3130 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003131 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003132 else
3133 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003134 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003135 /* if no outgoing data was seen on this stream, it means it was
3136 * closed with a "tcp-request content" rule that is normally
3137 * used to kill the connection ASAP (eg: limit abuse). In this
3138 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003139 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003140 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003141 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003142 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003143
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003144 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3145 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003146 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003147 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003148
Willy Tarreau00dd0782018-03-01 16:31:34 +01003149 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003150 }
3151
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003152 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003153 tasklet_wakeup(h2c->wait_event.task);
3154 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003155
3156 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003157 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003158 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003159 if (h2s->flags & H2_SF_BLK_MFCTL) {
3160 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3161 h2s->send_wait = sw;
3162 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3163 h2s->send_wait = sw;
3164 LIST_ADDQ(&h2c->send_list, &h2s->list);
3165 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003166 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003167 /* let the handler know we want to shutw */
3168 sw->handle = (void *)((long)(sw->handle) | 2);
3169
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003170}
3171
3172static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3173{
3174 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003175 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003176
Olivier Houchard2c68a462018-12-15 22:42:20 +01003177 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003178 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003179 h2s->send_wait = NULL;
3180 LIST_DEL(&h2s->list);
3181 LIST_INIT(&h2s->list);
3182 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003183 if (reason & 2)
3184 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003185 if (reason & 1)
3186 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003187
Olivier Houchard2c68a462018-12-15 22:42:20 +01003188 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003189 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003190 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003191 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003192}
3193
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003194static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3195{
3196 struct h2s *h2s = cs->ctx;
3197
3198 if (!mode)
3199 return;
3200
3201 h2_do_shutr(h2s);
3202}
3203
3204static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3205{
3206 struct h2s *h2s = cs->ctx;
3207
3208 h2_do_shutw(h2s);
3209}
3210
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003211/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003212 * HTX request or response depending on the connection's side. Returns a
3213 * positive value on success, a negative value on failure, or 0 if it couldn't
3214 * proceed. May report connection errors in h2c->errcode if the frame is
3215 * non-decodable and the connection unrecoverable. In absence of connection
3216 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003217 *
3218 * The function may fold CONTINUATION frames into the initial HEADERS frame
3219 * by removing padding and next frame header, then moving the CONTINUATION
3220 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3221 * leaving a hole between the main frame and the beginning of the next one.
3222 * The possibly remaining incomplete or next frame at the end may be moved
3223 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3224 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3225 *
3226 * A buffer at the beginning of processing may look like this :
3227 *
3228 * ,---.---------.-----.--------------.--------------.------.---.
3229 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3230 * `---^---------^-----^--------------^--------------^------^---'
3231 * | | <-----> | |
3232 * area | dpl | wrap
3233 * |<--------------> |
3234 * | dfl |
3235 * |<-------------------------------------------------->|
3236 * head data
3237 *
3238 * Padding is automatically overwritten when folding, participating to the
3239 * hole size after dfl :
3240 *
3241 * ,---.------------------------.-----.--------------.------.---.
3242 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3243 * `---^------------------------^-----^--------------^------^---'
3244 * | | <-----> | |
3245 * area | hole | wrap
3246 * |<-----------------------> |
3247 * | dfl |
3248 * |<-------------------------------------------------->|
3249 * head data
3250 *
3251 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3252 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3253 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003254 *
3255 * The <flags> field must point to either the stream's flags or to a copy of it
3256 * so that the function can update the following flags :
3257 * - H2_SF_DATA_CLEN when content-length is seen
3258 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3259 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003260 *
3261 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3262 * decoding, in order to detect if we're dealing with a headers or a trailers
3263 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003264 */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003265static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags)
Willy Tarreau13278b42017-10-13 19:23:14 +02003266{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003267 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003268 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003269 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003270 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003271 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003272 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003273 int flen; // header frame len
3274 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003275 int ret = 0;
3276 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003277 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003278 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003279
Willy Tarreauea18f862018-12-22 20:19:26 +01003280next_frame:
3281 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3282 goto leave; // incomplete input frame
3283
3284 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3285 * this case, we'll try to paste it immediately after the initial
3286 * HEADERS frame payload and kill any possible padding. The initial
3287 * frame's length will be increased to represent the concatenation
3288 * of the two frames. The next frame is read from position <tlen>
3289 * and written at position <flen> (minus padding if some is present).
3290 */
3291 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3292 struct h2_fh hdr;
3293 int clen; // CONTINUATION frame's payload length
3294
3295 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3296 /* no more data, the buffer may be full, either due to
3297 * too large a frame or because of too large a hole that
3298 * we're going to compact at the end.
3299 */
3300 goto leave;
3301 }
3302
3303 if (hdr.ft != H2_FT_CONTINUATION) {
3304 /* RFC7540#6.10: frame of unexpected type */
3305 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3306 goto fail;
3307 }
3308
3309 if (hdr.sid != h2c->dsi) {
3310 /* RFC7540#6.10: frame of different stream */
3311 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3312 goto fail;
3313 }
3314
3315 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3316 /* RFC7540#4.2: invalid frame length */
3317 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3318 goto fail;
3319 }
3320
3321 /* detect when we must stop aggragating frames */
3322 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3323
3324 /* Take as much as we can of the CONTINUATION frame's payload */
3325 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3326 if (clen > hdr.len)
3327 clen = hdr.len;
3328
3329 /* Move the frame's payload over the padding, hole and frame
3330 * header. At least one of hole or dpl is null (see diagrams
3331 * above). The hole moves after the new aggragated frame.
3332 */
3333 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3334 h2c->dfl += clen - h2c->dpl;
3335 hole += h2c->dpl + 9;
3336 h2c->dpl = 0;
3337 goto next_frame;
3338 }
3339
3340 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003341
Willy Tarreau13278b42017-10-13 19:23:14 +02003342 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003343 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003344 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003345 copy = alloc_trash_chunk();
3346 if (!copy) {
3347 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3348 goto fail;
3349 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003350 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3351 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3352 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003353 }
3354
Willy Tarreau13278b42017-10-13 19:23:14 +02003355 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3356 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003357 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003358 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3359 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003360 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003361 }
3362
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003363 if (flen < 5) {
3364 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3365 goto fail;
3366 }
3367
Willy Tarreau13278b42017-10-13 19:23:14 +02003368 hdrs += 5; // stream dep = 4, weight = 1
3369 flen -= 5;
3370 }
3371
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003372 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003373 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003374 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003375 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003376
Willy Tarreau937f7602018-02-26 15:22:17 +01003377 /* we can't retry a failed decompression operation so we must be very
3378 * careful not to take any risks. In practice the output buffer is
3379 * always empty except maybe for trailers, in which case we simply have
3380 * to wait for the upper layer to finish consuming what is available.
3381 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003382
3383 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003384 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003385 if (!htx_is_empty(htx)) {
3386 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003387 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003388 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003389 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003390 if (b_data(rxbuf)) {
3391 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003392 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003393 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003394
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003395 rxbuf->head = 0;
3396 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003397 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003398
Willy Tarreau25919232019-01-03 14:48:18 +01003399 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003400 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3401 sizeof(list)/sizeof(list[0]), tmp);
3402 if (outlen < 0) {
3403 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3404 goto fail;
3405 }
3406
Willy Tarreau25919232019-01-03 14:48:18 +01003407 /* The PACK decompressor was updated, let's update the input buffer and
3408 * the parser's state to commit these changes and allow us to later
3409 * fail solely on the stream if needed.
3410 */
3411 b_del(&h2c->dbuf, h2c->dfl + hole);
3412 h2c->dfl = hole = 0;
3413 h2c->st0 = H2_CS_FRAME_H;
3414
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003415 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003416 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003417
Willy Tarreau88d138e2019-01-02 19:38:14 +01003418 if (*flags & H2_SF_HEADERS_RCVD)
3419 goto trailers;
3420
3421 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003422 if (htx) {
3423 /* HTX mode */
3424 if (h2c->flags & H2_CF_IS_BACK)
3425 outlen = h2_make_htx_response(list, htx, &msgf);
3426 else
3427 outlen = h2_make_htx_request(list, htx, &msgf);
3428 } else {
3429 /* HTTP/1 mode */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003430 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf);
Willy Tarreau83195932019-01-03 10:26:23 +01003431 if (outlen > 0)
3432 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003433 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003434
3435 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003436 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003437 goto fail;
3438 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003439
Willy Tarreau174b06a2018-04-25 18:13:58 +02003440 if (msgf & H2_MSGF_BODY) {
3441 /* a payload is present */
3442 if (msgf & H2_MSGF_BODY_CL)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003443 *flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003444 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003445 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003446 }
3447
Willy Tarreau88d138e2019-01-02 19:38:14 +01003448 done:
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003449 /* indicate that a HEADERS frame was received for this stream */
3450 *flags |= H2_SF_HEADERS_RCVD;
3451
Willy Tarreau88d138e2019-01-02 19:38:14 +01003452 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
3453 /* Mark the end of message, either using EOM in HTX or with the
3454 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3455 * is not set during headers with END_STREAM.
3456 */
3457 if (htx) {
3458 if (!htx_add_endof(htx, HTX_BLK_EOM))
3459 goto fail;
3460 }
3461 else if (*flags & H2_SF_DATA_CHNK) {
3462 if (!b_putblk(rxbuf, "\r\n", 2))
3463 goto fail;
3464 }
3465 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003466
Willy Tarreau86277d42019-01-02 15:36:11 +01003467 /* success */
3468 ret = 1;
3469
Willy Tarreau68dd9852017-07-03 14:44:26 +02003470 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003471 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003472 * move the remaining data over it.
3473 */
3474 if (hole) {
3475 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3476 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3477 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3478 b_sub(&h2c->dbuf, hole);
3479 }
3480
3481 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3482 /* too large frames */
3483 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003484 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003485 }
3486
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003487 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003488 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003489 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003490 return ret;
3491
Willy Tarreau68dd9852017-07-03 14:44:26 +02003492 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003493 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003494 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003495
3496 trailers:
3497 /* This is the last HEADERS frame hence a trailer */
3498
3499 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3500 /* It's a trailer but it's missing ES flag */
3501 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3502 goto fail;
3503 }
3504
3505 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3506 * block, and when using chunks we must send the 0 CRLF marker. For
3507 * other modes, the trailers are silently dropped.
3508 */
3509 if (htx) {
3510 if (!htx_add_endof(htx, HTX_BLK_EOD))
3511 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003512 if (h2_make_htx_trailers(list, htx) <= 0)
3513 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003514 }
3515 else if (*flags & H2_SF_DATA_CHNK) {
3516 /* Legacy mode with chunked encoding : we must finalize the
3517 * data block message emit the trailing CRLF */
3518 if (!b_putblk(rxbuf, "0\r\n", 3))
3519 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003520
3521 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3522 if (outlen > 0)
3523 b_add(rxbuf, outlen);
3524 else
3525 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003526 }
3527
3528 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003529}
3530
Willy Tarreau454f9052017-10-26 19:40:35 +02003531/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3532 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3533 * in use, a new chunk is emitted for each frame. This is supposed to fit
3534 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3535 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3536 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003537 * parser state is automatically updated. Returns > 0 if it could completely
3538 * send the current frame, 0 if it couldn't complete, in which case
3539 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3540 * DATA frame can return 0 as a valid result). Stream errors are reported in
3541 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3542 * have checked the frame header and ensured that the frame was complete or the
3543 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003544 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003545static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003546{
3547 struct h2c *h2c = h2s->h2c;
3548 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003549 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003550 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003551 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003552 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003553
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003554 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003555
Olivier Houchard638b7992018-08-16 15:41:52 +02003556 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003557 if (!csbuf) {
3558 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003559 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003560 }
3561
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003562try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003563 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003564 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3565 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003566 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003567 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003568
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003569 if (flen > b_data(&h2c->dbuf)) {
3570 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003571 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003572 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003573 }
3574
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003575 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003576 block1 = htx_free_data_space(htx);
3577 if (!block1) {
3578 h2c->flags |= H2_CF_DEM_SFULL;
3579 goto fail;
3580 }
3581 if (flen > block1)
3582 flen = block1;
3583
3584 /* here, flen is the max we can copy into the output buffer */
3585 block1 = b_contig_data(&h2c->dbuf, 0);
3586 if (flen > block1)
3587 flen = block1;
3588
3589 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3590 h2c->flags |= H2_CF_DEM_SFULL;
3591 goto fail;
3592 }
3593
3594 b_del(&h2c->dbuf, flen);
3595 h2c->dfl -= flen;
3596 h2c->rcvd_c += flen;
3597 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3598 goto try_again;
3599 }
3600 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003601 /* it doesn't fit and the buffer is fragmented,
3602 * so let's defragment it and try again.
3603 */
3604 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003605 }
3606
Willy Tarreaueba10f22018-04-25 20:44:22 +02003607 /* chunked-encoding requires more room */
3608 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003609 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003610 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3611 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3612 (chklen < 1048576) ? 4 : 8;
3613 chklen += 4; // CRLF, CRLF
3614 }
3615
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003616 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003617 if (flen + chklen > b_room(csbuf)) {
3618 if (chklen >= b_room(csbuf)) {
3619 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003620 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003621 }
3622 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003623 }
3624
3625 if (h2s->flags & H2_SF_DATA_CHNK) {
3626 /* emit the chunk size */
3627 unsigned int chksz = flen;
3628 char str[10];
3629 char *beg;
3630
3631 beg = str + sizeof(str);
3632 *--beg = '\n';
3633 *--beg = '\r';
3634 do {
3635 *--beg = hextab[chksz & 0xF];
3636 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003637 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003638 }
3639
Willy Tarreau454f9052017-10-26 19:40:35 +02003640 /* Block1 is the length of the first block before the buffer wraps,
3641 * block2 is the optional second block to reach the end of the frame.
3642 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003643 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003644 if (block1 > flen)
3645 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003646 block2 = flen - block1;
3647
3648 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003649 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003650
3651 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003652 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003653
Willy Tarreaueba10f22018-04-25 20:44:22 +02003654 if (h2s->flags & H2_SF_DATA_CHNK) {
3655 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003656 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003657 }
3658
Willy Tarreau454f9052017-10-26 19:40:35 +02003659 /* now mark the input data as consumed (will be deleted from the buffer
3660 * by the caller when seeing FRAME_A after sending the window update).
3661 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003662 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003663 h2c->dfl -= flen;
3664 h2c->rcvd_c += flen;
3665 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3666
3667 if (h2c->dfl > h2c->dpl) {
3668 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003669 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003670 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003671 }
3672
Willy Tarreau4a28da12018-01-04 14:41:00 +01003673 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003674 /* here we're done with the frame, all the payload (except padding) was
3675 * transferred.
3676 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003677
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003678 if (h2c->dff & H2_F_DATA_END_STREAM) {
3679 if (htx) {
3680 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3681 h2c->flags |= H2_CF_DEM_SFULL;
3682 goto fail;
3683 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003684 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003685 else if (h2s->flags & H2_SF_DATA_CHNK) {
3686 /* emit the trailing 0 CRLF CRLF */
3687 if (b_room(csbuf) < 5) {
3688 h2c->flags |= H2_CF_DEM_SFULL;
3689 goto fail;
3690 }
3691 chklen += 5;
3692 b_putblk(csbuf, "0\r\n\r\n", 5);
3693 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003694 }
3695
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003696 h2c->rcvd_c += h2c->dpl;
3697 h2c->rcvd_s += h2c->dpl;
3698 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003699 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3700
Willy Tarreau39d68502018-03-02 12:26:37 +01003701 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003702 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003703 h2s->cs->flags |= CS_FL_REOS;
3704 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003705 if (htx)
3706 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003707 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003708 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003709 if (htx)
3710 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003711 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003712}
3713
Willy Tarreau5dd17352018-06-14 13:33:30 +02003714/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3715 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3716 * number of bytes sent. The caller must check the stream's status to detect
3717 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003718 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003719static size_t h2s_frt_make_resp_headers(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003720{
3721 struct http_hdr list[MAX_HTTP_HDR];
3722 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003723 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003724 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003725 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003726 int es_now = 0;
3727 int ret = 0;
3728 int hdr;
3729
3730 if (h2c_mux_busy(h2c, h2s)) {
3731 h2s->flags |= H2_SF_BLK_MBUSY;
3732 return 0;
3733 }
3734
Willy Tarreau44e973f2018-03-01 17:49:30 +01003735 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003736 h2c->flags |= H2_CF_MUX_MALLOC;
3737 h2s->flags |= H2_SF_BLK_MROOM;
3738 return 0;
3739 }
3740
3741 /* First, try to parse the H1 response and index it into <list>.
3742 * NOTE! Since it comes from haproxy, we *know* that a response header
3743 * block does not wrap and we can safely read it this way without
3744 * having to realign the buffer.
3745 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003746 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003747 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003748 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003749 /* incomplete or invalid response, this is abnormal coming from
3750 * haproxy and may only result in a bad errorfile or bad Lua code
3751 * so that won't be fixed, raise an error now.
3752 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003753 * FIXME: we should instead add the ability to only return a
3754 * 502 bad gateway. But in theory this is not supposed to
3755 * happen.
3756 */
3757 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3758 ret = 0;
3759 goto end;
3760 }
3761
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003762 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003763
3764 /* certain statuses have no body or an empty one, regardless of
3765 * what the headers say.
3766 */
3767 if (sl.st.status >= 100 && sl.st.status < 200) {
3768 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3769 h1m->curr_len = h1m->body_len = 0;
3770 }
3771 else if (sl.st.status == 204 || sl.st.status == 304) {
3772 /* no contents, claim c-len is present and set to zero */
3773 h1m->flags &= ~H1_MF_CHNK;
3774 h1m->flags |= H1_MF_CLEN;
3775 h1m->curr_len = h1m->body_len = 0;
3776 }
3777
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003778 chunk_reset(&outbuf);
3779
3780 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003781 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003782 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003783 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003784
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003785 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003786 break;
3787 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003788 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003789 }
3790
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003791 if (outbuf.size < 9)
3792 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003793
3794 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003795 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3796 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3797 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003798
3799 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003800 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003801 /* this is an unparsable response */
3802 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3803 ret = 0;
3804 goto end;
3805 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003806
3807 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003808 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003809 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003810 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003811 }
3812
3813 /* encode all headers, stop at empty name */
3814 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003815 /* these ones do not exist in H2 and must be dropped. */
3816 if (isteq(list[hdr].n, ist("connection")) ||
3817 isteq(list[hdr].n, ist("proxy-connection")) ||
3818 isteq(list[hdr].n, ist("keep-alive")) ||
3819 isteq(list[hdr].n, ist("upgrade")) ||
3820 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003821 continue;
3822
3823 if (isteq(list[hdr].n, ist("")))
3824 break; // end
3825
3826 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3827 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003828 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003829 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003830 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003831 }
3832 }
3833
3834 /* we may need to add END_STREAM */
3835 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3836 es_now = 1;
3837
3838 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003839 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003840
3841 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003842 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003843
3844 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003845 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003846
3847 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003848 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003849 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003850
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003851 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003852 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003853 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003854
Willy Tarreau801250e2018-09-11 11:45:04 +02003855 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003856 h2s->flags |= H2_SF_ES_SENT;
3857 if (h2s->st == H2_SS_OPEN)
3858 h2s->st = H2_SS_HLOC;
3859 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003860 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003861 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003862 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003863 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003864 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003865 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003866 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003867 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003868 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003869
3870 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003871
3872 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003873 //fprintf(stderr, "[%d] sent simple H2 response (sid=%d) = %d bytes (%d in, ep=%u, es=%s)\n", h2c->st0, h2s->id, outbuf.len, ret, h1m->err_pos, h1m_state_str(h1m->err_state));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003874 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003875 full:
3876 h1m_init_res(h1m);
3877 h1m->err_pos = -1; // don't care about errors on the response path
3878 h2c->flags |= H2_CF_MUX_MFULL;
3879 h2s->flags |= H2_SF_BLK_MROOM;
3880 ret = 0;
3881 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003882}
3883
Willy Tarreau5dd17352018-06-14 13:33:30 +02003884/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3885 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3886 * the number of bytes sent. The caller must check the stream's status to
3887 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003888 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003889static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003890{
3891 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003892 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003893 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003894 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003895 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003896 int es_now = 0;
3897 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003898 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003899 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003900
3901 if (h2c_mux_busy(h2c, h2s)) {
3902 h2s->flags |= H2_SF_BLK_MBUSY;
3903 goto end;
3904 }
3905
Willy Tarreau44e973f2018-03-01 17:49:30 +01003906 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003907 h2c->flags |= H2_CF_MUX_MALLOC;
3908 h2s->flags |= H2_SF_BLK_MROOM;
3909 goto end;
3910 }
3911
3912 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003913 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003914 goto end;
3915
3916 chunk_reset(&outbuf);
3917
3918 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003919 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003920 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003921 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003922
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003923 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003924 break;
3925 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003926 /* If there are pending data in the output buffer, and we have
3927 * less than 1/4 of the mbuf's size and everything fits, we'll
3928 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3929 * is full and wait, to save some slow realign calls.
3930 */
3931 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3932 h2c->flags |= H2_CF_MUX_MFULL;
3933 h2s->flags |= H2_SF_BLK_MROOM;
3934 goto end;
3935 }
3936
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003937 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003938 }
3939
3940 if (outbuf.size < 9) {
3941 h2c->flags |= H2_CF_MUX_MFULL;
3942 h2s->flags |= H2_SF_BLK_MROOM;
3943 goto end;
3944 }
3945
3946 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003947 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3948 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3949 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003950
3951 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3952 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003953 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003954 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003955 break;
3956 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003957 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003958 if ((long long)size > h1m->curr_len)
3959 size = h1m->curr_len;
3960 break;
3961 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003962 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003963 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003964 if (!ret)
3965 goto end;
3966
3967 if (ret < 0) {
3968 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003969 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003970 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3971 goto end;
3972 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003973 max -= ret;
3974 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003975 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003976 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003977 }
3978
Willy Tarreau801250e2018-09-11 11:45:04 +02003979 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003980 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003981 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003982 if (!ret)
3983 goto end;
3984
3985 if (ret < 0) {
3986 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003987 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003988 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3989 goto end;
3990 }
3991
3992 size = chunk;
3993 h1m->curr_len = chunk;
3994 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003995 max -= ret;
3996 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003997 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003998 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003999 if (!size)
4000 goto send_empty;
4001 }
4002
4003 /* in MSG_DATA state, continue below */
4004 size = h1m->curr_len;
4005 break;
4006 }
4007
4008 /* we have in <size> the exact number of bytes we need to copy from
4009 * the H1 buffer. We need to check this against the connection's and
4010 * the stream's send windows, and to ensure that this fits in the max
4011 * frame size and in the buffer's available space minus 9 bytes (for
4012 * the frame header). The connection's flow control is applied last so
4013 * that we can use a separate list of streams which are immediately
4014 * unblocked on window opening. Note: we don't implement padding.
4015 */
4016
Willy Tarreau5dd17352018-06-14 13:33:30 +02004017 if (size > max)
4018 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004019
4020 if (size > h2s->mws)
4021 size = h2s->mws;
4022
4023 if (size <= 0) {
4024 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004025 if (h2s->send_wait) {
4026 LIST_DEL(&h2s->list);
4027 LIST_INIT(&h2s->list);
4028 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004029 goto end;
4030 }
4031
4032 if (h2c->mfs && size > h2c->mfs)
4033 size = h2c->mfs;
4034
4035 if (size + 9 > outbuf.size) {
4036 /* we have an opportunity for enlarging the too small
4037 * available space, let's try.
4038 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004039 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004040 goto realign_again;
4041 size = outbuf.size - 9;
4042 }
4043
4044 if (size <= 0) {
4045 h2c->flags |= H2_CF_MUX_MFULL;
4046 h2s->flags |= H2_SF_BLK_MROOM;
4047 goto end;
4048 }
4049
4050 if (size > h2c->mws)
4051 size = h2c->mws;
4052
4053 if (size <= 0) {
4054 h2s->flags |= H2_SF_BLK_MFCTL;
4055 goto end;
4056 }
4057
4058 /* copy whatever we can */
4059 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004060 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004061 if (ret == 1)
4062 len2 = 0;
4063
4064 if (!ret || len1 + len2 < size) {
4065 /* FIXME: must normally never happen */
4066 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4067 goto end;
4068 }
4069
4070 /* limit len1/len2 to size */
4071 if (len1 + len2 > size) {
4072 int sub = len1 + len2 - size;
4073
4074 if (len2 > sub)
4075 len2 -= sub;
4076 else {
4077 sub -= len2;
4078 len2 = 0;
4079 len1 -= sub;
4080 }
4081 }
4082
4083 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004084 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004085 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004086 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004087
4088 send_empty:
4089 /* we may need to add END_STREAM */
4090 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4091 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004092 *
4093 * FIXME: what we do here is not correct because we send end_stream
4094 * before knowing if we'll have to send a HEADERS frame for the
4095 * trailers. More importantly we're not consuming the trailing CRLF
4096 * after the end of trailers, so it will be left to the caller to
4097 * eat it. The right way to do it would be to measure trailers here
4098 * and to send ES only if there are no trailers.
4099 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004100 */
4101 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004102 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004103 es_now = 1;
4104
4105 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004106 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004107
4108 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004109 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004110
4111 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004112 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004113
4114 /* consume incoming H1 response */
4115 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004116 max -= size;
4117 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004118 total += size;
4119 h1m->curr_len -= size;
4120 h2s->mws -= size;
4121 h2c->mws -= size;
4122
4123 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004124 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004125 goto new_frame;
4126 }
4127 }
4128
4129 if (es_now) {
4130 if (h2s->st == H2_SS_OPEN)
4131 h2s->st = H2_SS_HLOC;
4132 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004133 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004134
Willy Tarreau35a62702018-02-27 15:37:25 +01004135 if (!(h1m->flags & H1_MF_CHNK)) {
4136 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004137 total += max;
4138 ofs += max;
4139 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004140
Willy Tarreau801250e2018-09-11 11:45:04 +02004141 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004142 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004143
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004144 h2s->flags |= H2_SF_ES_SENT;
4145 }
4146
4147 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004148 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004149 return total;
4150}
4151
Willy Tarreau115e83b2018-12-01 19:17:53 +01004152/* Try to send a HEADERS frame matching HTX response present in HTX message
4153 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4154 * must check the stream's status to detect any error which might have happened
4155 * subsequently to a successful send. The htx blocks are automatically removed
4156 * from the message. The htx message is assumed to be valid since produced from
4157 * the internal code, hence it contains a start line, an optional series of
4158 * header blocks and an end of header, otherwise an invalid frame could be
4159 * emitted and the resulting htx message could be left in an inconsistent state.
4160 */
4161static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4162{
4163 struct http_hdr list[MAX_HTTP_HDR];
4164 struct h2c *h2c = h2s->h2c;
4165 struct htx_blk *blk;
4166 struct htx_blk *blk_end;
4167 struct buffer outbuf;
4168 struct htx_sl *sl;
4169 enum htx_blk_type type;
4170 int es_now = 0;
4171 int ret = 0;
4172 int hdr;
4173 int idx;
4174
4175 if (h2c_mux_busy(h2c, h2s)) {
4176 h2s->flags |= H2_SF_BLK_MBUSY;
4177 return 0;
4178 }
4179
4180 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4181 h2c->flags |= H2_CF_MUX_MALLOC;
4182 h2s->flags |= H2_SF_BLK_MROOM;
4183 return 0;
4184 }
4185
4186 /* determine the first block which must not be deleted, blk_end may
4187 * be NULL if all blocks have to be deleted.
4188 */
4189 idx = htx_get_head(htx);
4190 blk_end = NULL;
4191 while (idx != -1) {
4192 type = htx_get_blk_type(htx_get_blk(htx, idx));
4193 idx = htx_get_next(htx, idx);
4194 if (type == HTX_BLK_EOH) {
4195 if (idx != -1)
4196 blk_end = htx_get_blk(htx, idx);
4197 break;
4198 }
4199 }
4200
4201 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004202 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004203 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004204 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004205 if (h2s->status < 100 || h2s->status > 999)
4206 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004207
4208 /* and the rest of the headers, that we dump starting at header 0 */
4209 hdr = 0;
4210
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004211 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004212 while ((idx = htx_get_next(htx, idx)) != -1) {
4213 blk = htx_get_blk(htx, idx);
4214 type = htx_get_blk_type(blk);
4215
4216 if (type == HTX_BLK_UNUSED)
4217 continue;
4218
4219 if (type != HTX_BLK_HDR)
4220 break;
4221
4222 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4223 goto fail;
4224
4225 list[hdr].n = htx_get_blk_name(htx, blk);
4226 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004227 hdr++;
4228 }
4229
4230 /* marker for end of headers */
4231 list[hdr].n = ist("");
4232
4233 if (h2s->status == 204 || h2s->status == 304) {
4234 /* no contents, claim c-len is present and set to zero */
4235 es_now = 1;
4236 }
4237
4238 chunk_reset(&outbuf);
4239
4240 while (1) {
4241 outbuf.area = b_tail(&h2c->mbuf);
4242 outbuf.size = b_contig_space(&h2c->mbuf);
4243 outbuf.data = 0;
4244
4245 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4246 break;
4247 realign_again:
4248 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4249 }
4250
4251 if (outbuf.size < 9)
4252 goto full;
4253
4254 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4255 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4256 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4257 outbuf.data = 9;
4258
4259 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004260 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004261 if (b_space_wraps(&h2c->mbuf))
4262 goto realign_again;
4263 goto full;
4264 }
4265
4266 /* encode all headers, stop at empty name */
4267 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4268 /* these ones do not exist in H2 and must be dropped. */
4269 if (isteq(list[hdr].n, ist("connection")) ||
4270 isteq(list[hdr].n, ist("proxy-connection")) ||
4271 isteq(list[hdr].n, ist("keep-alive")) ||
4272 isteq(list[hdr].n, ist("upgrade")) ||
4273 isteq(list[hdr].n, ist("transfer-encoding")))
4274 continue;
4275
4276 if (isteq(list[hdr].n, ist("")))
4277 break; // end
4278
4279 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4280 /* output full */
4281 if (b_space_wraps(&h2c->mbuf))
4282 goto realign_again;
4283 goto full;
4284 }
4285 }
4286
4287 /* we may need to add END_STREAM.
4288 * FIXME: we should also set it when we know for sure that the
4289 * content-length is zero as well as on 204/304
4290 */
4291 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4292 es_now = 1;
4293
4294 if (h2s->cs->flags & CS_FL_SHW)
4295 es_now = 1;
4296
4297 /* update the frame's size */
4298 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4299
4300 if (es_now)
4301 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4302
4303 /* commit the H2 response */
4304 b_add(&h2c->mbuf, outbuf.data);
4305 h2s->flags |= H2_SF_HEADERS_SENT;
4306
Willy Tarreau115e83b2018-12-01 19:17:53 +01004307 if (es_now) {
4308 h2s->flags |= H2_SF_ES_SENT;
4309 if (h2s->st == H2_SS_OPEN)
4310 h2s->st = H2_SS_HLOC;
4311 else
4312 h2s_close(h2s);
4313 }
4314
4315 /* OK we could properly deliver the response */
4316
4317 /* remove all header blocks including the EOH and compute the
4318 * corresponding size.
4319 *
4320 * FIXME: We should remove everything when es_now is set.
4321 */
4322 ret = 0;
4323 idx = htx_get_head(htx);
4324 blk = htx_get_blk(htx, idx);
4325 while (blk != blk_end) {
4326 ret += htx_get_blksz(blk);
4327 blk = htx_remove_blk(htx, blk);
4328 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004329
4330 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4331 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004332 end:
4333 return ret;
4334 full:
4335 h2c->flags |= H2_CF_MUX_MFULL;
4336 h2s->flags |= H2_SF_BLK_MROOM;
4337 ret = 0;
4338 goto end;
4339 fail:
4340 /* unparsable HTX messages, too large ones to be produced in the local
4341 * list etc go here (unrecoverable errors).
4342 */
4343 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4344 ret = 0;
4345 goto end;
4346}
4347
Willy Tarreau80739692018-10-05 11:35:57 +02004348/* Try to send a HEADERS frame matching HTX request present in HTX message
4349 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4350 * must check the stream's status to detect any error which might have happened
4351 * subsequently to a successful send. The htx blocks are automatically removed
4352 * from the message. The htx message is assumed to be valid since produced from
4353 * the internal code, hence it contains a start line, an optional series of
4354 * header blocks and an end of header, otherwise an invalid frame could be
4355 * emitted and the resulting htx message could be left in an inconsistent state.
4356 */
4357static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4358{
4359 struct http_hdr list[MAX_HTTP_HDR];
4360 struct h2c *h2c = h2s->h2c;
4361 struct htx_blk *blk;
4362 struct htx_blk *blk_end;
4363 struct buffer outbuf;
4364 struct htx_sl *sl;
4365 struct ist meth, path;
4366 enum htx_blk_type type;
4367 int es_now = 0;
4368 int ret = 0;
4369 int hdr;
4370 int idx;
4371
4372 if (h2c_mux_busy(h2c, h2s)) {
4373 h2s->flags |= H2_SF_BLK_MBUSY;
4374 return 0;
4375 }
4376
4377 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4378 h2c->flags |= H2_CF_MUX_MALLOC;
4379 h2s->flags |= H2_SF_BLK_MROOM;
4380 return 0;
4381 }
4382
4383 /* determine the first block which must not be deleted, blk_end may
4384 * be NULL if all blocks have to be deleted.
4385 */
4386 idx = htx_get_head(htx);
4387 blk_end = NULL;
4388 while (idx != -1) {
4389 type = htx_get_blk_type(htx_get_blk(htx, idx));
4390 idx = htx_get_next(htx, idx);
4391 if (type == HTX_BLK_EOH) {
4392 if (idx != -1)
4393 blk_end = htx_get_blk(htx, idx);
4394 break;
4395 }
4396 }
4397
4398 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004399 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004400 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004401 meth = htx_sl_req_meth(sl);
4402 path = htx_sl_req_uri(sl);
4403
4404 /* and the rest of the headers, that we dump starting at header 0 */
4405 hdr = 0;
4406
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004407 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004408 while ((idx = htx_get_next(htx, idx)) != -1) {
4409 blk = htx_get_blk(htx, idx);
4410 type = htx_get_blk_type(blk);
4411
4412 if (type == HTX_BLK_UNUSED)
4413 continue;
4414
4415 if (type != HTX_BLK_HDR)
4416 break;
4417
4418 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4419 goto fail;
4420
4421 list[hdr].n = htx_get_blk_name(htx, blk);
4422 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004423 hdr++;
4424 }
4425
4426 /* marker for end of headers */
4427 list[hdr].n = ist("");
4428
4429 chunk_reset(&outbuf);
4430
4431 while (1) {
4432 outbuf.area = b_tail(&h2c->mbuf);
4433 outbuf.size = b_contig_space(&h2c->mbuf);
4434 outbuf.data = 0;
4435
4436 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4437 break;
4438 realign_again:
4439 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4440 }
4441
4442 if (outbuf.size < 9)
4443 goto full;
4444
4445 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4446 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4447 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4448 outbuf.data = 9;
4449
4450 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004451 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004452 if (b_space_wraps(&h2c->mbuf))
4453 goto realign_again;
4454 goto full;
4455 }
4456
4457 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004458 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4459 /* output full */
4460 if (b_space_wraps(&h2c->mbuf))
4461 goto realign_again;
4462 goto full;
4463 }
Willy Tarreau80739692018-10-05 11:35:57 +02004464
4465 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004466 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004467 /* output full */
4468 if (b_space_wraps(&h2c->mbuf))
4469 goto realign_again;
4470 goto full;
4471 }
4472
4473 /* encode all headers, stop at empty name */
4474 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4475 /* these ones do not exist in H2 and must be dropped. */
4476 if (isteq(list[hdr].n, ist("connection")) ||
4477 isteq(list[hdr].n, ist("proxy-connection")) ||
4478 isteq(list[hdr].n, ist("keep-alive")) ||
4479 isteq(list[hdr].n, ist("upgrade")) ||
4480 isteq(list[hdr].n, ist("transfer-encoding")))
4481 continue;
4482
4483 if (isteq(list[hdr].n, ist("")))
4484 break; // end
4485
4486 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4487 /* output full */
4488 if (b_space_wraps(&h2c->mbuf))
4489 goto realign_again;
4490 goto full;
4491 }
4492 }
4493
4494 /* we may need to add END_STREAM if we have no body :
4495 * - request already closed, or :
4496 * - no transfer-encoding, and :
4497 * - no content-length or content-length:0
4498 * Fixme: this doesn't take into account CONNECT requests.
4499 */
4500 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4501 es_now = 1;
4502
4503 if (sl->flags & HTX_SL_F_BODYLESS)
4504 es_now = 1;
4505
4506 if (h2s->cs->flags & CS_FL_SHW)
4507 es_now = 1;
4508
4509 /* update the frame's size */
4510 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4511
4512 if (es_now)
4513 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4514
4515 /* commit the H2 response */
4516 b_add(&h2c->mbuf, outbuf.data);
4517 h2s->flags |= H2_SF_HEADERS_SENT;
4518 h2s->st = H2_SS_OPEN;
4519
Willy Tarreau80739692018-10-05 11:35:57 +02004520 if (es_now) {
4521 // trim any possibly pending data (eg: inconsistent content-length)
4522 h2s->flags |= H2_SF_ES_SENT;
4523 h2s->st = H2_SS_HLOC;
4524 }
4525
4526 /* remove all header blocks including the EOH and compute the
4527 * corresponding size.
4528 *
4529 * FIXME: We should remove everything when es_now is set.
4530 */
4531 ret = 0;
4532 idx = htx_get_head(htx);
4533 blk = htx_get_blk(htx, idx);
4534 while (blk != blk_end) {
4535 ret += htx_get_blksz(blk);
4536 blk = htx_remove_blk(htx, blk);
4537 }
4538
4539 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4540 htx_remove_blk(htx, blk_end);
4541
4542 end:
4543 return ret;
4544 full:
4545 h2c->flags |= H2_CF_MUX_MFULL;
4546 h2s->flags |= H2_SF_BLK_MROOM;
4547 ret = 0;
4548 goto end;
4549 fail:
4550 /* unparsable HTX messages, too large ones to be produced in the local
4551 * list etc go here (unrecoverable errors).
4552 */
4553 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4554 ret = 0;
4555 goto end;
4556}
4557
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004558/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004559 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4560 * caller must check the stream's status to detect any error which might have
4561 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004562 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4563 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004564static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004565{
4566 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004567 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004568 struct buffer outbuf;
4569 size_t total = 0;
4570 int es_now = 0;
4571 int bsize; /* htx block size */
4572 int fsize; /* h2 frame size */
4573 struct htx_blk *blk;
4574 enum htx_blk_type type;
4575 int idx;
4576
4577 if (h2c_mux_busy(h2c, h2s)) {
4578 h2s->flags |= H2_SF_BLK_MBUSY;
4579 goto end;
4580 }
4581
4582 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4583 h2c->flags |= H2_CF_MUX_MALLOC;
4584 h2s->flags |= H2_SF_BLK_MROOM;
4585 goto end;
4586 }
4587
Willy Tarreau98de12a2018-12-12 07:03:00 +01004588 htx = htx_from_buf(buf);
4589
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004590 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4591 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4592 * the caller to handle.
4593 */
4594
4595 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004596 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004597 goto end;
4598
4599 idx = htx_get_head(htx);
4600 blk = htx_get_blk(htx, idx);
4601 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4602 bsize = htx_get_blksz(blk);
4603 fsize = bsize;
4604
4605 if (type == HTX_BLK_EOD) {
4606 /* if we have an EOD, we're dealing with chunked data. We may
4607 * have a set of trailers after us that the caller will want to
4608 * deal with. Let's simply remove the EOD and return.
4609 */
4610 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004611 total++; // EOD counts as one byte
4612 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004613 goto end;
4614 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004615 else if (type == HTX_BLK_EOM) {
4616 if (h2s->flags & H2_SF_ES_SENT) {
4617 /* ES already sent */
4618 htx_remove_blk(htx, blk);
4619 total++; // EOM counts as one byte
4620 count--;
4621 goto end;
4622 }
4623 }
4624 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004625 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004626
4627 /* Perform some optimizations to reduce the number of buffer copies.
4628 * First, if the mux's buffer is empty and the htx area contains
4629 * exactly one data block of the same size as the requested count, and
4630 * this count fits within the frame size, the stream's window size, and
4631 * the connection's window size, then it's possible to simply swap the
4632 * caller's buffer with the mux's output buffer and adjust offsets and
4633 * length to match the entire DATA HTX block in the middle. In this
4634 * case we perform a true zero-copy operation from end-to-end. This is
4635 * the situation that happens all the time with large files. Second, if
4636 * this is not possible, but the mux's output buffer is empty, we still
4637 * have an opportunity to avoid the copy to the intermediary buffer, by
4638 * making the intermediary buffer's area point to the output buffer's
4639 * area. In this case we want to skip the HTX header to make sure that
4640 * copies remain aligned and that this operation remains possible all
4641 * the time. This goes for headers, data blocks and any data extracted
4642 * from the HTX blocks.
4643 */
4644 if (unlikely(fsize == count &&
4645 htx->used == 1 && type == HTX_BLK_DATA &&
4646 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4647 void *old_area = h2c->mbuf.area;
4648
4649 if (b_data(&h2c->mbuf)) {
4650 /* too bad there are data left there. If we have less
4651 * than 1/4 of the mbuf's size and everything fits,
4652 * we'll perform a copy anyway. Otherwise we'll pretend
4653 * the mbuf is full and wait.
4654 */
4655 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4656 goto copy;
4657 h2c->flags |= H2_CF_MUX_MFULL;
4658 h2s->flags |= H2_SF_BLK_MROOM;
4659 goto end;
4660 }
4661
4662 /* map an H2 frame to the HTX block so that we can put the
4663 * frame header there.
4664 */
4665 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004666 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004667 h2c->mbuf.data = fsize + 9;
4668 outbuf.area = b_head(&h2c->mbuf);
4669
4670 /* prepend an H2 DATA frame header just before the DATA block */
4671 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4672 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4673 h2_set_frame_size(outbuf.area, fsize);
4674
4675 /* update windows */
4676 h2s->mws -= fsize;
4677 h2c->mws -= fsize;
4678
4679 /* and exchange with our old area */
4680 buf->area = old_area;
4681 buf->data = buf->head = 0;
4682 total += fsize;
4683 goto end;
4684 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004685
Willy Tarreau98de12a2018-12-12 07:03:00 +01004686 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004687 /* for DATA and EOM we'll have to emit a frame, even if empty */
4688
4689 while (1) {
4690 outbuf.area = b_tail(&h2c->mbuf);
4691 outbuf.size = b_contig_space(&h2c->mbuf);
4692 outbuf.data = 0;
4693
4694 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4695 break;
4696 realign_again:
4697 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4698 }
4699
4700 if (outbuf.size < 9) {
4701 h2c->flags |= H2_CF_MUX_MFULL;
4702 h2s->flags |= H2_SF_BLK_MROOM;
4703 goto end;
4704 }
4705
4706 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4707 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4708 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4709 outbuf.data = 9;
4710
4711 /* we have in <fsize> the exact number of bytes we need to copy from
4712 * the HTX buffer. We need to check this against the connection's and
4713 * the stream's send windows, and to ensure that this fits in the max
4714 * frame size and in the buffer's available space minus 9 bytes (for
4715 * the frame header). The connection's flow control is applied last so
4716 * that we can use a separate list of streams which are immediately
4717 * unblocked on window opening. Note: we don't implement padding.
4718 */
4719
4720 /* EOM is presented with bsize==1 but would lead to the emission of an
4721 * empty frame, thus we force it to zero here.
4722 */
4723 if (type == HTX_BLK_EOM)
4724 bsize = fsize = 0;
4725
4726 if (!fsize)
4727 goto send_empty;
4728
4729 if (h2s->mws <= 0) {
4730 h2s->flags |= H2_SF_BLK_SFCTL;
4731 if (h2s->send_wait) {
4732 LIST_DEL(&h2s->list);
4733 LIST_INIT(&h2s->list);
4734 }
4735 goto end;
4736 }
4737
Willy Tarreauee573762018-12-04 15:25:57 +01004738 if (fsize > count)
4739 fsize = count;
4740
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004741 if (fsize > h2s->mws)
4742 fsize = h2s->mws; // >0
4743
4744 if (h2c->mfs && fsize > h2c->mfs)
4745 fsize = h2c->mfs; // >0
4746
4747 if (fsize + 9 > outbuf.size) {
4748 /* we have an opportunity for enlarging the too small
4749 * available space, let's try.
4750 * FIXME: is this really interesting to do? Maybe we'll
4751 * spend lots of time realigning instead of using two
4752 * frames.
4753 */
4754 if (b_space_wraps(&h2c->mbuf))
4755 goto realign_again;
4756 fsize = outbuf.size - 9;
4757
4758 if (fsize <= 0) {
4759 /* no need to send an empty frame here */
4760 h2c->flags |= H2_CF_MUX_MFULL;
4761 h2s->flags |= H2_SF_BLK_MROOM;
4762 goto end;
4763 }
4764 }
4765
4766 if (h2c->mws <= 0) {
4767 h2s->flags |= H2_SF_BLK_MFCTL;
4768 goto end;
4769 }
4770
4771 if (fsize > h2c->mws)
4772 fsize = h2c->mws;
4773
4774 /* now let's copy this this into the output buffer */
4775 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004776 h2s->mws -= fsize;
4777 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004778 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004779
4780 send_empty:
4781 /* update the frame's size */
4782 h2_set_frame_size(outbuf.area, fsize);
4783
4784 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4785 * meeting EOM. We should optimize this later.
4786 */
4787 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004788 total++; // EOM counts as one byte
4789 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004790 es_now = 1;
4791 }
4792
4793 if (es_now)
4794 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4795
4796 /* commit the H2 response */
4797 b_add(&h2c->mbuf, fsize + 9);
4798
4799 /* consume incoming HTX block, including EOM */
4800 total += fsize;
4801 if (fsize == bsize) {
4802 htx_remove_blk(htx, blk);
4803 if (fsize)
4804 goto new_frame;
4805 } else {
4806 /* we've truncated this block */
4807 htx_cut_data_blk(htx, blk, fsize);
4808 }
4809
4810 if (es_now) {
4811 if (h2s->st == H2_SS_OPEN)
4812 h2s->st = H2_SS_HLOC;
4813 else
4814 h2s_close(h2s);
4815
4816 h2s->flags |= H2_SF_ES_SENT;
4817 }
4818
4819 end:
4820 return total;
4821}
4822
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004823/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4824 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4825 * processed. The caller must check the stream's status to detect any error
4826 * which might have happened subsequently to a successful send. The htx blocks
4827 * are automatically removed from the message. The htx message is assumed to be
4828 * valid since produced from the internal code. Processing stops when meeting
4829 * the EOM, which is also removed. All trailers are processed at once and sent
4830 * as a single frame. The ES flag is always set.
4831 */
4832static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4833{
4834 struct http_hdr list[MAX_HTTP_HDR];
4835 struct h2c *h2c = h2s->h2c;
4836 struct htx_blk *blk;
4837 struct htx_blk *blk_end;
4838 struct buffer outbuf;
4839 struct h1m h1m;
4840 enum htx_blk_type type;
4841 uint32_t size;
4842 int ret = 0;
4843 int hdr;
4844 int idx;
4845 void *start;
4846
4847 if (h2c_mux_busy(h2c, h2s)) {
4848 h2s->flags |= H2_SF_BLK_MBUSY;
4849 goto end;
4850 }
4851
4852 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4853 h2c->flags |= H2_CF_MUX_MALLOC;
4854 h2s->flags |= H2_SF_BLK_MROOM;
4855 goto end;
4856 }
4857
4858 /* The principle is that we parse each and every trailers block using
4859 * the H1 headers parser, and append it to the list. We don't proceed
4860 * until EOM is met. blk_end will point to the EOM block.
4861 */
4862 hdr = 0;
4863 memset(list, 0, sizeof(list));
4864 blk_end = NULL;
4865
4866 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4867 blk = htx_get_blk(htx, idx);
4868 type = htx_get_blk_type(blk);
4869
4870 if (type == HTX_BLK_UNUSED)
4871 continue;
4872
4873 if (type != HTX_BLK_TLR) {
4874 if (type == HTX_BLK_EOM)
4875 blk_end = blk;
4876 break;
4877 }
4878
4879 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4880 goto fail;
4881
4882 size = htx_get_blksz(blk);
4883 start = htx_get_blk_ptr(htx, blk);
4884
4885 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4886 h1m.err_pos = 0;
4887 ret = h1_headers_to_hdr_list(start, start + size,
4888 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4889 &h1m, NULL);
4890 if (ret < 0)
4891 goto fail;
4892
4893 /* ret == 0 if an incomplete trailers block was found (missing
4894 * empty line), or > 0 if it was found. We have to continue on
4895 * incomplete messages because the trailers block might be
4896 * incomplete.
4897 */
4898
4899 /* search the new end */
4900 while (hdr <= sizeof(list)/sizeof(list[0])) {
4901 if (!list[hdr].n.len)
4902 break;
4903 hdr++;
4904 }
4905 }
4906
4907 if (!blk_end)
4908 goto end; // end not found yet
4909
4910 if (!hdr)
4911 goto done;
4912
4913 chunk_reset(&outbuf);
4914
4915 while (1) {
4916 outbuf.area = b_tail(&h2c->mbuf);
4917 outbuf.size = b_contig_space(&h2c->mbuf);
4918 outbuf.data = 0;
4919
4920 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4921 break;
4922 realign_again:
4923 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4924 }
4925
4926 if (outbuf.size < 9)
4927 goto full;
4928
4929 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4930 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4931 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4932 outbuf.data = 9;
4933
4934 /* encode status, which necessarily is the first one */
4935 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
4936 if (b_space_wraps(&h2c->mbuf))
4937 goto realign_again;
4938 goto full;
4939 }
4940
4941 /* encode all headers */
4942 for (idx = 0; idx < hdr; idx++) {
4943 /* these ones do not exist in H2 or must not appear in
4944 * trailers and must be dropped.
4945 */
4946 if (isteq(list[idx].n, ist("host")) ||
4947 isteq(list[idx].n, ist("content-length")) ||
4948 isteq(list[idx].n, ist("connection")) ||
4949 isteq(list[idx].n, ist("proxy-connection")) ||
4950 isteq(list[idx].n, ist("keep-alive")) ||
4951 isteq(list[idx].n, ist("upgrade")) ||
4952 isteq(list[idx].n, ist("te")) ||
4953 isteq(list[idx].n, ist("transfer-encoding")))
4954 continue;
4955
4956 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
4957 /* output full */
4958 if (b_space_wraps(&h2c->mbuf))
4959 goto realign_again;
4960 goto full;
4961 }
4962 }
4963
4964 /* update the frame's size */
4965 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4966
4967 /* commit the H2 response */
4968 b_add(&h2c->mbuf, outbuf.data);
4969 h2s->flags |= H2_SF_ES_SENT;
4970
4971 if (h2s->st == H2_SS_OPEN)
4972 h2s->st = H2_SS_HLOC;
4973 else
4974 h2s_close(h2s);
4975
4976 /* OK we could properly deliver the response */
4977 done:
4978 /* remove all header blocks including EOM and compute the corresponding size. */
4979 ret = 0;
4980 idx = htx_get_head(htx);
4981 blk = htx_get_blk(htx, idx);
4982 while (blk != blk_end) {
4983 ret += htx_get_blksz(blk);
4984 blk = htx_remove_blk(htx, blk);
4985 }
4986 blk = htx_remove_blk(htx, blk);
4987 end:
4988 return ret;
4989 full:
4990 h2c->flags |= H2_CF_MUX_MFULL;
4991 h2s->flags |= H2_SF_BLK_MROOM;
4992 ret = 0;
4993 goto end;
4994 fail:
4995 /* unparsable HTX messages, too large ones to be produced in the local
4996 * list etc go here (unrecoverable errors).
4997 */
4998 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4999 ret = 0;
5000 goto end;
5001}
5002
Olivier Houchard6ff20392018-07-17 18:46:31 +02005003/* Called from the upper layer, to subscribe to events, such as being able to send */
5004static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5005{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005006 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005007 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005008 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005009
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005010 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005011 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005012 if (!(sw->events & SUB_RETRY_RECV)) {
5013 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005014 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005015 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005016 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005017 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005018 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005019 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005020 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005021 if (!(sw->events & SUB_RETRY_SEND)) {
5022 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005023 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005024 h2s->send_wait = sw;
5025 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5026 if (h2s->flags & H2_SF_BLK_MFCTL)
5027 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5028 else
5029 LIST_ADDQ(&h2c->send_list, &h2s->list);
5030 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005031 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005032 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005033 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005034 if (event_type != 0)
5035 return -1;
5036 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005037
5038
5039}
5040
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005041static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5042{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005043 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005044 struct h2s *h2s = cs->ctx;
5045
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005046 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005047 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005048 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005049 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005050 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005051 }
5052 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005053 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005054 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005055 if (h2s->send_wait == sw) {
5056 LIST_DEL(&h2s->list);
5057 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005058 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005059 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005060 }
5061 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005062 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5063 sw = param;
5064 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005065 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005066 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005067 LIST_DEL(&h2s->list);
5068 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005069 }
5070 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005071 return 0;
5072}
5073
5074
Olivier Houchard511efea2018-08-16 15:30:32 +02005075/* Called from the upper layer, to receive data */
5076static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5077{
Olivier Houchard638b7992018-08-16 15:41:52 +02005078 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005079 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005080 struct htx *h2s_htx = NULL;
5081 struct htx *buf_htx = NULL;
5082 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005083 size_t ret = 0;
5084
5085 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005086 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5087 /* in HTX mode we ignore the count argument */
5088 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005089 if (htx_is_empty(h2s_htx)) {
5090 if (cs->flags & CS_FL_REOS)
5091 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005092 if (cs->flags & CS_FL_ERR_PENDING)
5093 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01005094 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005095 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005096
5097 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005098 count = htx_free_data_space(buf_htx);
5099 if (flags & CO_RFL_KEEP_RSV) {
5100 if (count <= global.tune.maxrewrite)
5101 goto end;
5102 count -= global.tune.maxrewrite;
5103 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005104
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005105 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01005106
5107 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005108 htx_to_buf(buf_htx, buf);
5109 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005110 ret = htx_ret.ret;
5111 }
5112 else {
5113 ret = b_xfer(buf, &h2s->rxbuf, count);
5114 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005115
Olivier Houchard638b7992018-08-16 15:41:52 +02005116 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005117 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005118 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005119 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005120 if (cs->flags & CS_FL_REOS)
5121 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005122 if (cs->flags & CS_FL_ERR_PENDING)
5123 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005124 if (b_size(&h2s->rxbuf)) {
5125 b_free(&h2s->rxbuf);
5126 offer_buffers(NULL, tasks_run_queue);
5127 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005128 }
5129
Willy Tarreau082f5592018-11-25 08:03:32 +01005130 if (ret && h2c->dsi == h2s->id) {
5131 /* demux is blocking on this stream's buffer */
5132 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005133 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005134 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005135end:
Olivier Houchard511efea2018-08-16 15:30:32 +02005136 return ret;
5137}
5138
Olivier Houchardd846c262018-10-19 17:24:29 +02005139static void h2_stop_senders(struct h2c *h2c)
5140{
5141 struct h2s *h2s, *h2s_back;
5142
5143 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5144 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5145 if (h2c->msi == h2s_id(h2s))
5146 continue;
5147 LIST_DEL(&h2s->list);
5148 LIST_INIT(&h2s->list);
5149 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005150 h2s->send_wait->events |= SUB_RETRY_SEND;
5151 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005152 LIST_ADD(&h2c->send_list, &h2s->list);
5153 }
5154}
5155
Willy Tarreau62f52692017-10-08 23:01:42 +02005156/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005157static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005158{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005159 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005160 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005161 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005162 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005163 struct htx *htx;
5164 struct htx_blk *blk;
5165 enum htx_blk_type btype;
5166 uint32_t bsize;
5167 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005168
Olivier Houchardd846c262018-10-19 17:24:29 +02005169 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005170 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005171 h2s->send_wait = NULL;
5172 LIST_DEL(&h2s->list);
5173 LIST_INIT(&h2s->list);
5174 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005175 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5176 return 0;
5177
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005178 /* htx will be enough to decide if we're using HTX or legacy */
5179 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5180
Willy Tarreau0bad0432018-06-14 16:54:01 +02005181 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005182 h2s->flags |= H2_SF_OUTGOING_DATA;
5183
Willy Tarreau751f2d02018-10-05 09:35:00 +02005184 if (h2s->id == 0) {
5185 int32_t id = h2c_get_next_sid(h2s->h2c);
5186
5187 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005188 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005189 return 0;
5190 }
5191
5192 eb32_delete(&h2s->by_id);
5193 h2s->by_id.key = h2s->id = id;
5194 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005195 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005196 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5197 }
5198
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005199 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005200 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5201 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005202 idx = htx_get_head(htx);
5203 blk = htx_get_blk(htx, idx);
5204 btype = htx_get_blk_type(blk);
5205 bsize = htx_get_blksz(blk);
5206
5207 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005208 case HTX_BLK_REQ_SL:
5209 /* start-line before headers */
5210 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5211 if (ret > 0) {
5212 total += ret;
5213 count -= ret;
5214 if (ret < bsize)
5215 goto done;
5216 }
5217 break;
5218
Willy Tarreau115e83b2018-12-01 19:17:53 +01005219 case HTX_BLK_RES_SL:
5220 /* start-line before headers */
5221 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5222 if (ret > 0) {
5223 total += ret;
5224 count -= ret;
5225 if (ret < bsize)
5226 goto done;
5227 }
5228 break;
5229
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005230 case HTX_BLK_DATA:
5231 case HTX_BLK_EOD:
5232 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005233 /* all these cause the emission of a DATA frame (possibly empty).
5234 * This EOM necessarily is one before trailers, as the EOM following
5235 * trailers would have been consumed by the trailers parser.
5236 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005237 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005238 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005239 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005240 total += ret;
5241 count -= ret;
5242 if (ret < bsize)
5243 goto done;
5244 }
5245 break;
5246
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005247 case HTX_BLK_TLR:
5248 /* This is the first trailers block, all the subsequent ones AND
5249 * the EOM will be swallowed by the parser.
5250 */
5251 ret = h2s_htx_make_trailers(h2s, htx);
5252 if (ret > 0) {
5253 total += ret;
5254 count -= ret;
5255 if (ret < bsize)
5256 goto done;
5257 }
5258 break;
5259
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005260 default:
5261 htx_remove_blk(htx, blk);
5262 total += bsize;
5263 count -= bsize;
5264 break;
5265 }
5266 }
5267 goto done;
5268 }
5269
5270 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005271 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005272 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005273 if (h2s->h2c->flags & H2_CF_IS_BACK)
5274 ret = -1;
5275 else
5276 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005277 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005278 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005279 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005280 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005281 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005282 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005283 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005284
Willy Tarreau5dd17352018-06-14 13:33:30 +02005285 if (unlikely((int)ret <= 0)) {
5286 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005287 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5288 break;
5289 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005290 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005291 total += count;
5292 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005293 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005294 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005295 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005296 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005297 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005298 break;
5299 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005300
5301 total += ret;
5302 count -= ret;
5303
5304 if (h2s->st >= H2_SS_ERROR)
5305 break;
5306
5307 if (h2s->flags & H2_SF_BLK_ANY)
5308 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005309 }
5310
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005311 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005312 if (h2s->st >= H2_SS_ERROR) {
5313 /* trim any possibly pending data after we close (extra CR-LF,
5314 * unprocessed trailers, abnormal extra data, ...)
5315 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005316 total += count;
5317 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005318 }
5319
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005320 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005321 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005322 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005323 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005324 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005325 }
5326
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005327 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005328 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005329 } else {
5330 b_del(buf, total);
5331 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005332
5333 /* The mux is full, cancel the pending tasks */
5334 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5335 (h2s->flags & H2_SF_BLK_MBUSY))
5336 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005337
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005338 /* If we're running HTX, and we read the whole buffer, then pretend
5339 * we read exactly what the caller specified, as with HTX the caller
5340 * will always give the buffer size, instead of the amount of data
5341 * available.
5342 */
5343 if (htx && !b_data(buf))
5344 total = orig_count;
5345
Olivier Houchard7505f942018-08-21 18:10:44 +02005346 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005347 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005348 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005349
Olivier Houchard7505f942018-08-21 18:10:44 +02005350 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005351 /* If we're waiting for flow control, and we got a shutr on the
5352 * connection, we will never be unlocked, so add an error on
5353 * the conn_stream.
5354 */
5355 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5356 !b_data(&h2s->h2c->dbuf) &&
5357 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5358 if (cs->flags & CS_FL_EOS)
5359 cs->flags |= CS_FL_ERROR;
5360 else
5361 cs->flags |= CS_FL_ERR_PENDING;
5362 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005363 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005364}
5365
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005366/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005367static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005368{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005369 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005370 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005371 struct eb32_node *node;
5372 int fctl_cnt = 0;
5373 int send_cnt = 0;
5374 int tree_cnt = 0;
5375 int orph_cnt = 0;
5376
5377 if (!h2c)
5378 return;
5379
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005380 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005381 fctl_cnt++;
5382
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005383 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005384 send_cnt++;
5385
Willy Tarreau3af37712018-12-18 14:34:41 +01005386 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005387 node = eb32_first(&h2c->streams_by_id);
5388 while (node) {
5389 h2s = container_of(node, struct h2s, by_id);
5390 tree_cnt++;
5391 if (!h2s->cs)
5392 orph_cnt++;
5393 node = eb32_next(node);
5394 }
5395
Willy Tarreau987c0632018-12-18 10:32:05 +01005396 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5397 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5398 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u",
Willy Tarreau616ac812018-07-24 14:12:42 +02005399 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5400 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005401 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005402 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5403 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5404 h2c->msi,
5405 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5406 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5407
5408 if (h2s) {
5409 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5410 h2s, h2s->id, h2s->flags,
5411 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5412 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5413 h2s->cs);
5414 if (h2s->cs)
5415 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5416 h2s->cs->flags, h2s->cs->data);
5417 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005418}
Willy Tarreau62f52692017-10-08 23:01:42 +02005419
5420/*******************************************************/
5421/* functions below are dedicated to the config parsers */
5422/*******************************************************/
5423
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005424/* config parser for global "tune.h2.header-table-size" */
5425static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5426 struct proxy *defpx, const char *file, int line,
5427 char **err)
5428{
5429 if (too_many_args(1, args, err, NULL))
5430 return -1;
5431
5432 h2_settings_header_table_size = atoi(args[1]);
5433 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5434 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5435 return -1;
5436 }
5437 return 0;
5438}
Willy Tarreau62f52692017-10-08 23:01:42 +02005439
Willy Tarreaue6baec02017-07-27 11:45:11 +02005440/* config parser for global "tune.h2.initial-window-size" */
5441static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5442 struct proxy *defpx, const char *file, int line,
5443 char **err)
5444{
5445 if (too_many_args(1, args, err, NULL))
5446 return -1;
5447
5448 h2_settings_initial_window_size = atoi(args[1]);
5449 if (h2_settings_initial_window_size < 0) {
5450 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5451 return -1;
5452 }
5453 return 0;
5454}
5455
Willy Tarreau5242ef82017-07-27 11:47:28 +02005456/* config parser for global "tune.h2.max-concurrent-streams" */
5457static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5458 struct proxy *defpx, const char *file, int line,
5459 char **err)
5460{
5461 if (too_many_args(1, args, err, NULL))
5462 return -1;
5463
5464 h2_settings_max_concurrent_streams = atoi(args[1]);
5465 if (h2_settings_max_concurrent_streams < 0) {
5466 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5467 return -1;
5468 }
5469 return 0;
5470}
5471
Willy Tarreau62f52692017-10-08 23:01:42 +02005472
5473/****************************************/
5474/* MUX initialization and instanciation */
5475/***************************************/
5476
5477/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005478static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005479 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005480 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005481 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005482 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005483 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005484 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005485 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005486 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005487 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005488 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005489 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005490 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005491 .shutr = h2_shutr,
5492 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005493 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005494 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005495 .name = "H2",
5496};
5497
Christopher Faulet32f61c02018-04-10 14:33:41 +02005498/* PROTO selection : this mux registers PROTO token "h2" */
5499static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005500 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005501
Willy Tarreau0108d902018-11-25 19:14:37 +01005502INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5503
Willy Tarreauf8957272018-10-03 10:25:20 +02005504static struct mux_proto_list mux_proto_h2_htx =
5505 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5506
5507INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5508
Willy Tarreau62f52692017-10-08 23:01:42 +02005509/* config keyword parsers */
5510static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005511 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005512 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005513 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005514 { 0, NULL, NULL }
5515}};
5516
Willy Tarreau0108d902018-11-25 19:14:37 +01005517INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);