blob: 20ff9882188bdd08b363f6dfbfd5b90eb4d80bd4 [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 Tarreau0b37d652018-10-03 10:33:02 +0200121 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100122 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200123 struct eb_root streams_by_id; /* all active streams by their ID */
124 struct list send_list; /* list of blocked streams requesting to send */
125 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200126 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100127 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200128 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200129};
130
Willy Tarreau18312642017-10-11 07:57:07 +0200131/* H2 stream state, in h2s->st */
132enum h2_ss {
133 H2_SS_IDLE = 0, // idle
134 H2_SS_RLOC, // reserved(local)
135 H2_SS_RREM, // reserved(remote)
136 H2_SS_OPEN, // open
137 H2_SS_HREM, // half-closed(remote)
138 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200139 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200140 H2_SS_CLOSED, // closed
141 H2_SS_ENTRIES // must be last
142} __attribute__((packed));
143
144/* HTTP/2 stream flags (32 bit), in h2s->flags */
145#define H2_SF_NONE 0x00000000
146#define H2_SF_ES_RCVD 0x00000001
147#define H2_SF_ES_SENT 0x00000002
148
149#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
150#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
151
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200152/* stream flags indicating the reason the stream is blocked */
153#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
154#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
155#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
156#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
157#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
158
Willy Tarreau454f9052017-10-26 19:40:35 +0200159/* stream flags indicating how data is supposed to be sent */
160#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
161#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
162
163/* step we're currently in when sending chunks. This is needed because we may
164 * have to transfer chunks as large as a full buffer so there's no room left
165 * for size nor crlf around.
166 */
167#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
168#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
169#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
170
171#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
172
Willy Tarreau67434202017-11-06 20:20:51 +0100173#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100174#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100175
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100176#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
177
Willy Tarreau18312642017-10-11 07:57:07 +0200178/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
179 * it is being processed in the internal HTTP representation (H1 for now).
180 */
181struct h2s {
182 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100183 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200184 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200185 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200186 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200187 int32_t id; /* stream ID */
188 uint32_t flags; /* H2_SF_* */
189 int mws; /* mux window size for this stream */
190 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
191 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200192 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200193 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200194 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
195 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
196 struct wait_event *send_wait; /* The streeam is waiting for flow control */
197 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200198};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200199
Willy Tarreauc6405142017-09-21 20:23:50 +0200200/* descriptor for an h2 frame header */
201struct h2_fh {
202 uint32_t len; /* length, host order, 24 bits */
203 uint32_t sid; /* stream id, host order, 31 bits */
204 uint8_t ft; /* frame type */
205 uint8_t ff; /* frame flags */
206};
207
Willy Tarreau8ceae722018-11-26 11:58:30 +0100208/* the h2c connection pool */
209DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
210
211/* the h2s stream pool */
212DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
213
Willy Tarreaudc572362018-12-12 08:08:05 +0100214/* The default connection window size is 65535, it may only be enlarged using
215 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
216 * we'll pretend we already received the difference between the two to send
217 * an equivalent window update to enlarge it to 2G-1.
218 */
219#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
220
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200221/* a few settings from the global section */
222static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200223static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200224static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200225
Willy Tarreau2a856182017-05-16 15:20:39 +0200226/* a dmumy closed stream */
227static const struct h2s *h2_closed_stream = &(const struct h2s){
228 .cs = NULL,
229 .h2c = NULL,
230 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100231 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100232 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200233 .id = 0,
234};
235
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100236/* a dmumy closed stream returning a PROTOCOL_ERROR error */
237static const struct h2s *h2_error_stream = &(const struct h2s){
238 .cs = NULL,
239 .h2c = NULL,
240 .st = H2_SS_CLOSED,
241 .errcode = H2_ERR_PROTOCOL_ERROR,
242 .flags = 0,
243 .id = 0,
244};
245
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100246/* a dmumy closed stream returning a REFUSED_STREAM error */
247static const struct h2s *h2_refused_stream = &(const struct h2s){
248 .cs = NULL,
249 .h2c = NULL,
250 .st = H2_SS_CLOSED,
251 .errcode = H2_ERR_REFUSED_STREAM,
252 .flags = 0,
253 .id = 0,
254};
255
Willy Tarreau2a856182017-05-16 15:20:39 +0200256/* and a dummy idle stream for use with any unannounced stream */
257static const struct h2s *h2_idle_stream = &(const struct h2s){
258 .cs = NULL,
259 .h2c = NULL,
260 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100261 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200262 .id = 0,
263};
264
Olivier Houchard9f6af332018-05-25 14:04:04 +0200265static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200266static int h2_send(struct h2c *h2c);
267static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200268static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200269static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100270static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +0100271static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100272static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200273static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100274static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100275static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200276
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200277/*****************************************************/
278/* functions below are for dynamic buffer management */
279/*****************************************************/
280
Willy Tarreau315d8072017-12-10 22:17:57 +0100281/* indicates whether or not the we may call the h2_recv() function to attempt
282 * to receive data into the buffer and/or demux pending data. The condition is
283 * a bit complex due to some API limits for now. The rules are the following :
284 * - if an error or a shutdown was detected on the connection and the buffer
285 * is empty, we must not attempt to receive
286 * - if the demux buf failed to be allocated, we must not try to receive and
287 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100288 * - if no flag indicates a blocking condition, we may attempt to receive,
289 * regardless of whether the demux buffer is full or not, so that only
290 * de demux part decides whether or not to block. This is needed because
291 * the connection API indeed prevents us from re-enabling receipt that is
292 * already enabled in a polled state, so we must always immediately stop
293 * as soon as the demux can't proceed so as never to hit an end of read
294 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100295 * - otherwise must may not attempt
296 */
297static inline int h2_recv_allowed(const struct h2c *h2c)
298{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200299 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100300 (h2c->st0 >= H2_CS_ERROR ||
301 h2c->conn->flags & CO_FL_ERROR ||
302 conn_xprt_read0_pending(h2c->conn)))
303 return 0;
304
305 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100306 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100307 return 1;
308
309 return 0;
310}
311
Willy Tarreau47b515a2018-12-21 16:09:41 +0100312/* restarts reading on the connection if it was not enabled */
313static inline void h2c_restart_reading(const struct h2c *h2c)
314{
315 if (!h2_recv_allowed(h2c))
316 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100317 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100318 return;
319 tasklet_wakeup(h2c->wait_event.task);
320}
321
322
Willy Tarreauf2101912018-07-19 10:11:38 +0200323/* returns true if the connection has too many conn_streams attached */
324static inline int h2_has_too_many_cs(const struct h2c *h2c)
325{
Willy Tarreaua8754662018-12-23 20:43:58 +0100326 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200327}
328
Willy Tarreau44e973f2018-03-01 17:49:30 +0100329/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
330 * flags are used to figure what buffer was requested. It returns 1 if the
331 * allocation succeeds, in which case the connection is woken up, or 0 if it's
332 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200333 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100334static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200335{
336 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100337 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200338
Willy Tarreau44e973f2018-03-01 17:49:30 +0100339 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200340 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100341 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200342 return 1;
343 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200344
Willy Tarreau44e973f2018-03-01 17:49:30 +0100345 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
346 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200347
348 if (h2c->flags & H2_CF_DEM_MROOM) {
349 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100350 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200351 }
Willy Tarreau14398122017-09-22 14:26:04 +0200352 return 1;
353 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100354
355 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
356 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200357 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100358 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100359 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100360 return 1;
361 }
362
Willy Tarreau14398122017-09-22 14:26:04 +0200363 return 0;
364}
365
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200366static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200367{
368 struct buffer *buf = NULL;
369
Willy Tarreau44e973f2018-03-01 17:49:30 +0100370 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
371 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
372 h2c->buf_wait.target = h2c;
373 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100374 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100375 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100376 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200377 __conn_xprt_stop_recv(h2c->conn);
378 }
379 return buf;
380}
381
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200382static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200383{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200384 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100385 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200386 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200387 }
388}
389
Olivier Houchardd540b362018-11-05 18:37:53 +0100390static int h2_avail_streams(struct connection *conn)
391{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100392 struct h2c *h2c = conn->ctx;
Olivier Houchardd540b362018-11-05 18:37:53 +0100393
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100394 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100395 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
396}
397
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100398static int h2_max_streams(struct connection *conn)
399{
400 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
401 return h2_settings_max_concurrent_streams;
402}
403
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200404
Willy Tarreau62f52692017-10-08 23:01:42 +0200405/*****************************************************************/
406/* functions below are dedicated to the mux setup and management */
407/*****************************************************************/
408
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200409/* Initialize the mux once it's attached. For outgoing connections, the context
410 * is already initialized before installing the mux, so we detect incoming
411 * connections from the fact that the context is still NULL. Returns < 0 on
412 * error.
413 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100414static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200415{
416 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100417 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200418
Willy Tarreaubafbe012017-11-24 17:34:44 +0100419 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200421 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200422
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100423 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200424 h2c->flags = H2_CF_IS_BACK;
425 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
426 if (tick_isset(prx->timeout.serverfin))
427 h2c->shut_timeout = prx->timeout.serverfin;
428 } else {
429 h2c->flags = H2_CF_NONE;
430 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
431 if (tick_isset(prx->timeout.clientfin))
432 h2c->shut_timeout = prx->timeout.clientfin;
433 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100434
Willy Tarreau0b37d652018-10-03 10:33:02 +0200435 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100436 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100437 if (tick_isset(h2c->timeout)) {
438 t = task_new(tid_bit);
439 if (!t)
440 goto fail;
441
442 h2c->task = t;
443 t->process = h2_timeout_task;
444 t->context = h2c;
445 t->expire = tick_add(now_ms, h2c->timeout);
446 }
Willy Tarreauea392822017-10-31 10:02:25 +0100447
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200448 h2c->wait_event.task = tasklet_new();
449 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200450 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200451 h2c->wait_event.task->process = h2_io_cb;
452 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100453 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200454
Willy Tarreau32218eb2017-09-22 08:07:25 +0200455 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
456 if (!h2c->ddht)
457 goto fail;
458
459 /* Initialise the context. */
460 h2c->st0 = H2_CS_PREFACE;
461 h2c->conn = conn;
462 h2c->max_id = -1;
463 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100464 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200465 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100466 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200467 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200468
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200469 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200470 h2c->dsi = -1;
471 h2c->msi = -1;
472 h2c->last_sid = -1;
473
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200474 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200475 h2c->miw = 65535; /* mux initial window size */
476 h2c->mws = 65535; /* mux window size */
477 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200478 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200479 LIST_INIT(&h2c->send_list);
480 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200481 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100482 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200483
Willy Tarreau3f133572017-10-31 19:21:06 +0100484 if (t)
485 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100486
Willy Tarreau01b44822018-10-03 14:26:37 +0200487 if (h2c->flags & H2_CF_IS_BACK) {
488 /* FIXME: this is temporary, for outgoing connections we need
489 * to immediately allocate a stream until the code is modified
490 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100491 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200492 */
493 struct h2s *h2s;
494
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100495 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200496 if (!h2s)
497 goto fail_stream;
498 }
499
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100500 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200501
Willy Tarreau0f383582018-10-03 14:22:21 +0200502 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100503 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200504 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200505 fail_stream:
506 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200507 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100508 if (t)
509 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200510 if (h2c->wait_event.task)
511 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100512 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200513 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200514 return -1;
515}
516
Willy Tarreau751f2d02018-10-05 09:35:00 +0200517/* returns the next allocatable outgoing stream ID for the H2 connection, or
518 * -1 if no more is allocatable.
519 */
520static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
521{
522 int32_t id = (h2c->max_id + 1) | 1;
523 if (id & 0x80000000U)
524 id = -1;
525 return id;
526}
527
Willy Tarreau2373acc2017-10-12 17:35:14 +0200528/* returns the stream associated with id <id> or NULL if not found */
529static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
530{
531 struct eb32_node *node;
532
Willy Tarreau751f2d02018-10-05 09:35:00 +0200533 if (id == 0)
534 return (struct h2s *)h2_closed_stream;
535
Willy Tarreau2a856182017-05-16 15:20:39 +0200536 if (id > h2c->max_id)
537 return (struct h2s *)h2_idle_stream;
538
Willy Tarreau2373acc2017-10-12 17:35:14 +0200539 node = eb32_lookup(&h2c->streams_by_id, id);
540 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200541 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200542
543 return container_of(node, struct h2s, by_id);
544}
545
Willy Tarreau62f52692017-10-08 23:01:42 +0200546/* release function for a connection. This one should be called to free all
547 * resources allocated to the mux.
548 */
549static void h2_release(struct connection *conn)
550{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100551 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200552
553 LIST_DEL(&conn->list);
554
555 if (h2c) {
556 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200557
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100558 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100559 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100560 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200561
Willy Tarreau44e973f2018-03-01 17:49:30 +0100562 h2_release_buf(h2c, &h2c->dbuf);
563 h2_release_buf(h2c, &h2c->mbuf);
564
Willy Tarreauea392822017-10-31 10:02:25 +0100565 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200566 h2c->task->context = NULL;
567 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100568 h2c->task = NULL;
569 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200570 if (h2c->wait_event.task)
571 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100572 if (h2c->wait_event.events != 0)
573 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200574 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100575
Willy Tarreaubafbe012017-11-24 17:34:44 +0100576 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200577 }
578
579 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100580 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200581
582 conn_stop_tracking(conn);
583 conn_full_close(conn);
584 if (conn->destroy_cb)
585 conn->destroy_cb(conn);
586 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200587}
588
589
Willy Tarreau71681172017-10-23 14:39:06 +0200590/******************************************************/
591/* functions below are for the H2 protocol processing */
592/******************************************************/
593
594/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100595static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200596{
597 return h2s ? h2s->id : 0;
598}
599
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200600/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100601static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200602{
603 if (h2c->msi < 0)
604 return 0;
605
606 if (h2c->msi == h2s_id(h2s))
607 return 0;
608
609 return 1;
610}
611
Willy Tarreau741d6df2017-10-17 08:00:59 +0200612/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100613static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200614{
615 h2c->errcode = err;
616 h2c->st0 = H2_CS_ERROR;
617}
618
Willy Tarreau2e43f082017-10-17 08:03:59 +0200619/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100620static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200621{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200622 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200623 h2s->errcode = err;
624 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100625 if (h2s->cs)
626 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200627 }
628}
629
Willy Tarreau7e094452018-12-19 18:08:52 +0100630/* attempt to notify the data layer of recv availability */
631static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
632{
633 struct wait_event *sw;
634
635 if (h2s->recv_wait) {
636 sw = h2s->recv_wait;
637 sw->events &= ~SUB_RETRY_RECV;
638 tasklet_wakeup(sw->task);
639 h2s->recv_wait = NULL;
640 }
641}
642
643/* attempt to notify the data layer of send availability */
644static void __maybe_unused h2s_notify_send(struct h2s *h2s)
645{
646 struct wait_event *sw;
647
648 if (h2s->send_wait) {
649 sw = h2s->send_wait;
650 sw->events &= ~SUB_RETRY_SEND;
651 tasklet_wakeup(sw->task);
652 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100653 LIST_DEL(&h2s->list);
654 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100655 }
656}
657
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100658/* alerts the data layer, trying to wake it up by all means, following
659 * this sequence :
660 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
661 * - if its subscribed to send, then it's woken up for send
662 * - if it was subscribed to neither, its ->wake() callback is called
663 * It is safe to call this function with a closed stream which doesn't have a
664 * conn_stream anymore.
665 */
666static void __maybe_unused h2s_alert(struct h2s *h2s)
667{
668 if (h2s->recv_wait || h2s->send_wait) {
669 h2s_notify_recv(h2s);
670 h2s_notify_send(h2s);
671 }
672 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
673 h2s->cs->data_cb->wake(h2s->cs);
674}
675
Willy Tarreaue4820742017-07-27 13:37:23 +0200676/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100677static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200678{
679 uint8_t *out = frame;
680
681 *out = len >> 16;
682 write_n16(out + 1, len);
683}
684
Willy Tarreau54c15062017-10-10 17:10:03 +0200685/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
686 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
687 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200688 * available in the buffer's input prior to calling this function. The buffer
689 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200690 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100691static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200692 const struct buffer *b, int o)
693{
Willy Tarreau591d4452018-06-15 17:21:00 +0200694 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 +0200695}
696
Willy Tarreau1f094672017-11-20 21:27:45 +0100697static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200698{
Willy Tarreau591d4452018-06-15 17:21:00 +0200699 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200700}
701
Willy Tarreau1f094672017-11-20 21:27:45 +0100702static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200703{
Willy Tarreau591d4452018-06-15 17:21:00 +0200704 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200705}
706
Willy Tarreau1f094672017-11-20 21:27:45 +0100707static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200708{
Willy Tarreau591d4452018-06-15 17:21:00 +0200709 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200710}
711
712
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100713/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
714 * The algorithm is not obvious. It turns out that H2 headers are neither
715 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
716 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200717 *
718 * b0 b1 b2 b3 b4 b5..b8
719 * +----------+---------+--------+----+----+----------------------+
720 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
721 * +----------+---------+--------+----+----+----------------------+
722 *
723 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
724 * we get the sid properly aligned and ordered, and 16 bits of len properly
725 * ordered as well. The type and flags can be extracted using bit shifts from
726 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200727 * Returns zero if some bytes are missing, otherwise non-zero on success. The
728 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200729 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100730static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200731{
732 uint64_t w;
733
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100734 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200735 return 0;
736
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100737 w = h2_get_n64(b, o + 1);
738 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200739 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
740 h->ff = w >> 32;
741 h->ft = w >> 40;
742 h->len += w >> 48;
743 return 1;
744}
745
746/* skip the next 9 bytes corresponding to the frame header possibly parsed by
747 * h2_peek_frame_hdr() above.
748 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100749static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200750{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200751 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200752}
753
754/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100755static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200756{
757 int ret;
758
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100759 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200760 if (ret > 0)
761 h2_skip_frame_hdr(b);
762 return ret;
763}
764
Willy Tarreau00dd0782018-03-01 16:31:34 +0100765/* marks stream <h2s> as CLOSED and decrement the number of active streams for
766 * its connection if the stream was not yet closed. Please use this exclusively
767 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100768 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100769static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100770{
771 if (h2s->st != H2_SS_CLOSED)
772 h2s->h2c->nb_streams--;
773 h2s->st = H2_SS_CLOSED;
774}
775
Willy Tarreau71049cc2018-03-28 13:56:39 +0200776/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
777static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100778{
779 h2s_close(h2s);
780 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200781 if (b_size(&h2s->rxbuf)) {
782 b_free(&h2s->rxbuf);
783 offer_buffers(NULL, tasks_run_queue);
784 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200785 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100786 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200787 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100788 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800789 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200790 * reference left would be in the h2c send_list/fctl_list, and if
791 * we're in it, we're getting out anyway
792 */
793 LIST_DEL(&h2s->list);
794 LIST_INIT(&h2s->list);
795 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100796 pool_free(pool_head_h2s, h2s);
797}
798
Willy Tarreaua8e49542018-10-03 18:53:55 +0200799/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
800 * stream tree. In case of error, nothing is added and NULL is returned. The
801 * causes of errors can be any failed memory allocation. The caller is
802 * responsible for checking if the connection may support an extra stream
803 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200804 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200805static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200806{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200807 struct h2s *h2s;
808
Willy Tarreaubafbe012017-11-24 17:34:44 +0100809 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200810 if (!h2s)
811 goto out;
812
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200813 h2s->wait_event.task = tasklet_new();
814 if (!h2s->wait_event.task) {
815 pool_free(pool_head_h2s, h2s);
816 goto out;
817 }
818 h2s->send_wait = NULL;
819 h2s->recv_wait = NULL;
820 h2s->wait_event.task->process = h2_deferred_shut;
821 h2s->wait_event.task->context = h2s;
822 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100823 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200824 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200825 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200826 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200827 h2s->mws = h2c->miw;
828 h2s->flags = H2_SF_NONE;
829 h2s->errcode = H2_ERR_NO_ERROR;
830 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200831 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200832 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200833
834 if (h2c->flags & H2_CF_IS_BACK) {
835 h1m_init_req(&h2s->h1m);
836 h2s->h1m.err_pos = -1; // don't care about errors on the request path
837 h2s->h1m.flags |= H1_MF_TOLOWER;
838 } else {
839 h1m_init_res(&h2s->h1m);
840 h2s->h1m.err_pos = -1; // don't care about errors on the response path
841 h2s->h1m.flags |= H1_MF_TOLOWER;
842 }
843
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200844 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200845 if (id > 0)
846 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200847
848 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100849 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200850
851 return h2s;
852
853 out_free_h2s:
854 pool_free(pool_head_h2s, h2s);
855 out:
856 return NULL;
857}
858
859/* creates a new stream <id> on the h2c connection and returns it, or NULL in
860 * case of memory allocation error.
861 */
862static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
863{
864 struct session *sess = h2c->conn->owner;
865 struct conn_stream *cs;
866 struct h2s *h2s;
867
868 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
869 goto out;
870
871 h2s = h2s_new(h2c, id);
872 if (!h2s)
873 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200874
875 cs = cs_new(h2c->conn);
876 if (!cs)
877 goto out_close;
878
Olivier Houchard746fb772018-12-15 19:42:00 +0100879 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200880 h2s->cs = cs;
881 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200882 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200883
884 if (stream_create_from_cs(cs) < 0)
885 goto out_free_cs;
886
Willy Tarreau590a0512018-09-05 11:56:48 +0200887 /* We want the accept date presented to the next stream to be the one
888 * we have now, the handshake time to be null (since the next stream
889 * is not delayed by a handshake), and the idle time to count since
890 * right now.
891 */
892 sess->accept_date = date;
893 sess->tv_accept = now;
894 sess->t_handshake = 0;
895
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200896 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200897 if (h2_has_too_many_cs(h2c))
898 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200899 return h2s;
900
901 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200902 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200903 cs_free(cs);
904 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200905 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200906 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200907 sess_log(sess);
908 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200909}
910
Willy Tarreau751f2d02018-10-05 09:35:00 +0200911/* allocates a new stream associated to conn_stream <cs> on the h2c connection
912 * and returns it, or NULL in case of memory allocation error or if the highest
913 * possible stream ID was reached.
914 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100915static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200916{
917 struct h2s *h2s = NULL;
918
919 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
920 goto out;
921
922 /* Defer choosing the ID until we send the first message to create the stream */
923 h2s = h2s_new(h2c, 0);
924 if (!h2s)
925 goto out;
926
927 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100928 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200929 cs->ctx = h2s;
930 h2c->nb_cs++;
931
Willy Tarreau751f2d02018-10-05 09:35:00 +0200932 out:
933 return h2s;
934}
935
Willy Tarreaube5b7152017-09-25 16:25:39 +0200936/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
937 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
938 * the various settings codes.
939 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200940static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200941{
942 struct buffer *res;
943 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200944 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200945 int ret;
946
947 if (h2c_mux_busy(h2c, NULL)) {
948 h2c->flags |= H2_CF_DEM_MBUSY;
949 return 0;
950 }
951
Willy Tarreau44e973f2018-03-01 17:49:30 +0100952 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200953 if (!res) {
954 h2c->flags |= H2_CF_MUX_MALLOC;
955 h2c->flags |= H2_CF_DEM_MROOM;
956 return 0;
957 }
958
959 chunk_init(&buf, buf_data, sizeof(buf_data));
960 chunk_memcpy(&buf,
961 "\x00\x00\x00" /* length : 0 for now */
962 "\x04\x00" /* type : 4 (settings), flags : 0 */
963 "\x00\x00\x00\x00", /* stream ID : 0 */
964 9);
965
966 if (h2_settings_header_table_size != 4096) {
967 char str[6] = "\x00\x01"; /* header_table_size */
968
969 write_n32(str + 2, h2_settings_header_table_size);
970 chunk_memcat(&buf, str, 6);
971 }
972
973 if (h2_settings_initial_window_size != 65535) {
974 char str[6] = "\x00\x04"; /* initial_window_size */
975
976 write_n32(str + 2, h2_settings_initial_window_size);
977 chunk_memcat(&buf, str, 6);
978 }
979
980 if (h2_settings_max_concurrent_streams != 0) {
981 char str[6] = "\x00\x03"; /* max_concurrent_streams */
982
983 /* Note: 0 means "unlimited" for haproxy's config but not for
984 * the protocol, so never send this value!
985 */
986 write_n32(str + 2, h2_settings_max_concurrent_streams);
987 chunk_memcat(&buf, str, 6);
988 }
989
990 if (global.tune.bufsize != 16384) {
991 char str[6] = "\x00\x05"; /* max_frame_size */
992
993 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
994 * match bufsize - rewrite size, but at the moment it seems
995 * that clients don't take care of it.
996 */
997 write_n32(str + 2, global.tune.bufsize);
998 chunk_memcat(&buf, str, 6);
999 }
1000
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001001 h2_set_frame_size(buf.area, buf.data - 9);
1002 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001003 if (unlikely(ret <= 0)) {
1004 if (!ret) {
1005 h2c->flags |= H2_CF_MUX_MFULL;
1006 h2c->flags |= H2_CF_DEM_MROOM;
1007 return 0;
1008 }
1009 else {
1010 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1011 return 0;
1012 }
1013 }
1014 return ret;
1015}
1016
Willy Tarreau52eed752017-09-22 15:05:09 +02001017/* Try to receive a connection preface, then upon success try to send our
1018 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1019 * missing data. It may return an error in h2c.
1020 */
1021static int h2c_frt_recv_preface(struct h2c *h2c)
1022{
1023 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001024 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001025
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001026 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001027
1028 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001029 if (ret1 < 0)
1030 sess_log(h2c->conn->owner);
1031
Willy Tarreau52eed752017-09-22 15:05:09 +02001032 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1033 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1034 return 0;
1035 }
1036
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001037 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001038 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001039 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001040
Willy Tarreaube5b7152017-09-25 16:25:39 +02001041 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001042}
1043
Willy Tarreau01b44822018-10-03 14:26:37 +02001044/* Try to send a connection preface, then upon success try to send our
1045 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1046 * missing data. It may return an error in h2c.
1047 */
1048static int h2c_bck_send_preface(struct h2c *h2c)
1049{
1050 struct buffer *res;
1051
1052 if (h2c_mux_busy(h2c, NULL)) {
1053 h2c->flags |= H2_CF_DEM_MBUSY;
1054 return 0;
1055 }
1056
1057 res = h2_get_buf(h2c, &h2c->mbuf);
1058 if (!res) {
1059 h2c->flags |= H2_CF_MUX_MALLOC;
1060 h2c->flags |= H2_CF_DEM_MROOM;
1061 return 0;
1062 }
1063
1064 if (!b_data(res)) {
1065 /* preface not yet sent */
1066 b_istput(res, ist(H2_CONN_PREFACE));
1067 }
1068
1069 return h2c_send_settings(h2c);
1070}
1071
Willy Tarreau081d4722017-05-16 21:51:05 +02001072/* try to send a GOAWAY frame on the connection to report an error or a graceful
1073 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1074 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1075 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1076 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1077 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1078 * on unrecoverable failure. It will not attempt to send one again in this last
1079 * case so that it is safe to use h2c_error() to report such errors.
1080 */
1081static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1082{
1083 struct buffer *res;
1084 char str[17];
1085 int ret;
1086
1087 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1088 return 1; // claim that it worked
1089
1090 if (h2c_mux_busy(h2c, h2s)) {
1091 if (h2s)
1092 h2s->flags |= H2_SF_BLK_MBUSY;
1093 else
1094 h2c->flags |= H2_CF_DEM_MBUSY;
1095 return 0;
1096 }
1097
Willy Tarreau44e973f2018-03-01 17:49:30 +01001098 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001099 if (!res) {
1100 h2c->flags |= H2_CF_MUX_MALLOC;
1101 if (h2s)
1102 h2s->flags |= H2_SF_BLK_MROOM;
1103 else
1104 h2c->flags |= H2_CF_DEM_MROOM;
1105 return 0;
1106 }
1107
1108 /* len: 8, type: 7, flags: none, sid: 0 */
1109 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1110
1111 if (h2c->last_sid < 0)
1112 h2c->last_sid = h2c->max_id;
1113
1114 write_n32(str + 9, h2c->last_sid);
1115 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001116 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001117 if (unlikely(ret <= 0)) {
1118 if (!ret) {
1119 h2c->flags |= H2_CF_MUX_MFULL;
1120 if (h2s)
1121 h2s->flags |= H2_SF_BLK_MROOM;
1122 else
1123 h2c->flags |= H2_CF_DEM_MROOM;
1124 return 0;
1125 }
1126 else {
1127 /* we cannot report this error using GOAWAY, so we mark
1128 * it and claim a success.
1129 */
1130 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1131 h2c->flags |= H2_CF_GOAWAY_FAILED;
1132 return 1;
1133 }
1134 }
1135 h2c->flags |= H2_CF_GOAWAY_SENT;
1136 return ret;
1137}
1138
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001139/* Try to send an RST_STREAM frame on the connection for the indicated stream
1140 * during mux operations. This stream must be valid and cannot be closed
1141 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1142 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1143 * not yet.
1144 *
1145 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1146 * to write the message, it subscribes the stream to future notifications.
1147 */
1148static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1149{
1150 struct buffer *res;
1151 char str[13];
1152 int ret;
1153
1154 if (!h2s || h2s->st == H2_SS_CLOSED)
1155 return 1;
1156
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001157 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1158 * RST_STREAM in response to a RST_STREAM frame.
1159 */
1160 if (h2c->dft == H2_FT_RST_STREAM) {
1161 ret = 1;
1162 goto ignore;
1163 }
1164
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001165 if (h2c_mux_busy(h2c, h2s)) {
1166 h2s->flags |= H2_SF_BLK_MBUSY;
1167 return 0;
1168 }
1169
Willy Tarreau44e973f2018-03-01 17:49:30 +01001170 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001171 if (!res) {
1172 h2c->flags |= H2_CF_MUX_MALLOC;
1173 h2s->flags |= H2_SF_BLK_MROOM;
1174 return 0;
1175 }
1176
1177 /* len: 4, type: 3, flags: none */
1178 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1179 write_n32(str + 5, h2s->id);
1180 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001181 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001182
1183 if (unlikely(ret <= 0)) {
1184 if (!ret) {
1185 h2c->flags |= H2_CF_MUX_MFULL;
1186 h2s->flags |= H2_SF_BLK_MROOM;
1187 return 0;
1188 }
1189 else {
1190 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1191 return 0;
1192 }
1193 }
1194
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001195 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001196 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001197 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001198 return ret;
1199}
1200
1201/* Try to send an RST_STREAM frame on the connection for the stream being
1202 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001203 * error code, even if the stream is one of the dummy ones, and will update
1204 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001205 *
1206 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1207 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001208 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001209 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001210 */
1211static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1212{
1213 struct buffer *res;
1214 char str[13];
1215 int ret;
1216
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001217 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1218 * RST_STREAM in response to a RST_STREAM frame.
1219 */
1220 if (h2c->dft == H2_FT_RST_STREAM) {
1221 ret = 1;
1222 goto ignore;
1223 }
1224
Willy Tarreau27a84c92017-10-17 08:10:17 +02001225 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001226 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001227 return 0;
1228 }
1229
Willy Tarreau44e973f2018-03-01 17:49:30 +01001230 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001231 if (!res) {
1232 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001233 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001234 return 0;
1235 }
1236
1237 /* len: 4, type: 3, flags: none */
1238 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001239
Willy Tarreau27a84c92017-10-17 08:10:17 +02001240 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001241 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001242 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001243
Willy Tarreau27a84c92017-10-17 08:10:17 +02001244 if (unlikely(ret <= 0)) {
1245 if (!ret) {
1246 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001247 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001248 return 0;
1249 }
1250 else {
1251 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1252 return 0;
1253 }
1254 }
1255
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001256 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001257 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001258 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001259 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001260 }
1261
Willy Tarreau27a84c92017-10-17 08:10:17 +02001262 return ret;
1263}
1264
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001265/* try to send an empty DATA frame with the ES flag set to notify about the
1266 * end of stream and match a shutdown(write). If an ES was already sent as
1267 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1268 * on success or zero if nothing was done. In case of lack of room to write the
1269 * message, it subscribes the requesting stream to future notifications.
1270 */
1271static int h2_send_empty_data_es(struct h2s *h2s)
1272{
1273 struct h2c *h2c = h2s->h2c;
1274 struct buffer *res;
1275 char str[9];
1276 int ret;
1277
Willy Tarreau721c9742017-11-07 11:05:42 +01001278 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001279 return 1;
1280
1281 if (h2c_mux_busy(h2c, h2s)) {
1282 h2s->flags |= H2_SF_BLK_MBUSY;
1283 return 0;
1284 }
1285
Willy Tarreau44e973f2018-03-01 17:49:30 +01001286 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001287 if (!res) {
1288 h2c->flags |= H2_CF_MUX_MALLOC;
1289 h2s->flags |= H2_SF_BLK_MROOM;
1290 return 0;
1291 }
1292
1293 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1294 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1295 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001296 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001297 if (likely(ret > 0)) {
1298 h2s->flags |= H2_SF_ES_SENT;
1299 }
1300 else if (!ret) {
1301 h2c->flags |= H2_CF_MUX_MFULL;
1302 h2s->flags |= H2_SF_BLK_MROOM;
1303 return 0;
1304 }
1305 else {
1306 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1307 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001308 }
1309 return ret;
1310}
1311
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001312/* wake the streams attached to the connection, whose id is greater than <last>,
1313 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001314 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1315 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001316 */
1317static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1318{
1319 struct eb32_node *node;
1320 struct h2s *h2s;
1321
1322 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001323 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001324
1325 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001326 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001327
1328 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1329 while (node) {
1330 h2s = container_of(node, struct h2s, by_id);
1331 if (h2s->id <= last)
1332 break;
1333 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001334
1335 if (!h2s->cs) {
1336 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001337 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001338 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001339 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001340
1341 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001342 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1343 h2s->cs->flags |= CS_FL_ERROR;
1344
Willy Tarreauf830f012018-12-19 17:44:55 +01001345 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001346
Willy Tarreaua8519352018-12-18 16:44:28 +01001347 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001348 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001349 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001350 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001351 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001352 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001353 }
1354}
1355
Willy Tarreau3421aba2017-07-27 15:41:03 +02001356/* Increase all streams' outgoing window size by the difference passed in
1357 * argument. This is needed upon receipt of the settings frame if the initial
1358 * window size is different. The difference may be negative and the resulting
1359 * window size as well, for the time it takes to receive some window updates.
1360 */
1361static void h2c_update_all_ws(struct h2c *h2c, int diff)
1362{
1363 struct h2s *h2s;
1364 struct eb32_node *node;
1365
1366 if (!diff)
1367 return;
1368
1369 node = eb32_first(&h2c->streams_by_id);
1370 while (node) {
1371 h2s = container_of(node, struct h2s, by_id);
1372 h2s->mws += diff;
1373 node = eb32_next(node);
1374 }
1375}
1376
1377/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1378 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1379 * return an error in h2c. Described in RFC7540#6.5.
1380 */
1381static int h2c_handle_settings(struct h2c *h2c)
1382{
1383 unsigned int offset;
1384 int error;
1385
1386 if (h2c->dff & H2_F_SETTINGS_ACK) {
1387 if (h2c->dfl) {
1388 error = H2_ERR_FRAME_SIZE_ERROR;
1389 goto fail;
1390 }
1391 return 1;
1392 }
1393
1394 if (h2c->dsi != 0) {
1395 error = H2_ERR_PROTOCOL_ERROR;
1396 goto fail;
1397 }
1398
1399 if (h2c->dfl % 6) {
1400 error = H2_ERR_FRAME_SIZE_ERROR;
1401 goto fail;
1402 }
1403
1404 /* that's the limit we can process */
1405 if (h2c->dfl > global.tune.bufsize) {
1406 error = H2_ERR_FRAME_SIZE_ERROR;
1407 goto fail;
1408 }
1409
1410 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001411 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001412 return 0;
1413
1414 /* parse the frame */
1415 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001416 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1417 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001418
1419 switch (type) {
1420 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1421 /* we need to update all existing streams with the
1422 * difference from the previous iws.
1423 */
1424 if (arg < 0) { // RFC7540#6.5.2
1425 error = H2_ERR_FLOW_CONTROL_ERROR;
1426 goto fail;
1427 }
1428 h2c_update_all_ws(h2c, arg - h2c->miw);
1429 h2c->miw = arg;
1430 break;
1431 case H2_SETTINGS_MAX_FRAME_SIZE:
1432 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1433 error = H2_ERR_PROTOCOL_ERROR;
1434 goto fail;
1435 }
1436 h2c->mfs = arg;
1437 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001438 case H2_SETTINGS_ENABLE_PUSH:
1439 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1440 error = H2_ERR_PROTOCOL_ERROR;
1441 goto fail;
1442 }
1443 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001444 }
1445 }
1446
1447 /* need to ACK this frame now */
1448 h2c->st0 = H2_CS_FRAME_A;
1449 return 1;
1450 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001451 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001452 h2c_error(h2c, error);
1453 return 0;
1454}
1455
1456/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1457 * success or one of the h2_status values.
1458 */
1459static int h2c_ack_settings(struct h2c *h2c)
1460{
1461 struct buffer *res;
1462 char str[9];
1463 int ret = -1;
1464
1465 if (h2c_mux_busy(h2c, NULL)) {
1466 h2c->flags |= H2_CF_DEM_MBUSY;
1467 return 0;
1468 }
1469
Willy Tarreau44e973f2018-03-01 17:49:30 +01001470 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001471 if (!res) {
1472 h2c->flags |= H2_CF_MUX_MALLOC;
1473 h2c->flags |= H2_CF_DEM_MROOM;
1474 return 0;
1475 }
1476
1477 memcpy(str,
1478 "\x00\x00\x00" /* length : 0 (no data) */
1479 "\x04" "\x01" /* type : 4, flags : ACK */
1480 "\x00\x00\x00\x00" /* stream ID */, 9);
1481
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001482 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001483 if (unlikely(ret <= 0)) {
1484 if (!ret) {
1485 h2c->flags |= H2_CF_MUX_MFULL;
1486 h2c->flags |= H2_CF_DEM_MROOM;
1487 return 0;
1488 }
1489 else {
1490 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1491 return 0;
1492 }
1493 }
1494 return ret;
1495}
1496
Willy Tarreaucf68c782017-10-10 17:11:41 +02001497/* processes a PING frame and schedules an ACK if needed. The caller must pass
1498 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1499 * missing data. It may return an error in h2c.
1500 */
1501static int h2c_handle_ping(struct h2c *h2c)
1502{
1503 /* frame length must be exactly 8 */
1504 if (h2c->dfl != 8) {
1505 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1506 return 0;
1507 }
1508
1509 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001510 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001511 h2c->st0 = H2_CS_FRAME_A;
1512 return 1;
1513}
1514
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001515/* Try to send a window update for stream id <sid> and value <increment>.
1516 * Returns > 0 on success or zero on missing room or failure. It may return an
1517 * error in h2c.
1518 */
1519static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1520{
1521 struct buffer *res;
1522 char str[13];
1523 int ret = -1;
1524
1525 if (h2c_mux_busy(h2c, NULL)) {
1526 h2c->flags |= H2_CF_DEM_MBUSY;
1527 return 0;
1528 }
1529
Willy Tarreau44e973f2018-03-01 17:49:30 +01001530 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001531 if (!res) {
1532 h2c->flags |= H2_CF_MUX_MALLOC;
1533 h2c->flags |= H2_CF_DEM_MROOM;
1534 return 0;
1535 }
1536
1537 /* length: 4, type: 8, flags: none */
1538 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1539 write_n32(str + 5, sid);
1540 write_n32(str + 9, increment);
1541
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001542 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001543
1544 if (unlikely(ret <= 0)) {
1545 if (!ret) {
1546 h2c->flags |= H2_CF_MUX_MFULL;
1547 h2c->flags |= H2_CF_DEM_MROOM;
1548 return 0;
1549 }
1550 else {
1551 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1552 return 0;
1553 }
1554 }
1555 return ret;
1556}
1557
1558/* try to send pending window update for the connection. It's safe to call it
1559 * with no pending updates. Returns > 0 on success or zero on missing room or
1560 * failure. It may return an error in h2c.
1561 */
1562static int h2c_send_conn_wu(struct h2c *h2c)
1563{
1564 int ret = 1;
1565
1566 if (h2c->rcvd_c <= 0)
1567 return 1;
1568
Willy Tarreau97aaa672018-12-23 09:49:04 +01001569 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1570 /* increase the advertised connection window to 2G on
1571 * first update.
1572 */
1573 h2c->flags |= H2_CF_WINDOW_OPENED;
1574 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1575 }
1576
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001577 /* send WU for the connection */
1578 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1579 if (ret > 0)
1580 h2c->rcvd_c = 0;
1581
1582 return ret;
1583}
1584
1585/* try to send pending window update for the current dmux stream. It's safe to
1586 * call it with no pending updates. Returns > 0 on success or zero on missing
1587 * room or failure. It may return an error in h2c.
1588 */
1589static int h2c_send_strm_wu(struct h2c *h2c)
1590{
1591 int ret = 1;
1592
1593 if (h2c->rcvd_s <= 0)
1594 return 1;
1595
1596 /* send WU for the stream */
1597 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1598 if (ret > 0)
1599 h2c->rcvd_s = 0;
1600
1601 return ret;
1602}
1603
Willy Tarreaucf68c782017-10-10 17:11:41 +02001604/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1605 * success, 0 on missing data or one of the h2_status values.
1606 */
1607static int h2c_ack_ping(struct h2c *h2c)
1608{
1609 struct buffer *res;
1610 char str[17];
1611 int ret = -1;
1612
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001613 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001614 return 0;
1615
1616 if (h2c_mux_busy(h2c, NULL)) {
1617 h2c->flags |= H2_CF_DEM_MBUSY;
1618 return 0;
1619 }
1620
Willy Tarreau44e973f2018-03-01 17:49:30 +01001621 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001622 if (!res) {
1623 h2c->flags |= H2_CF_MUX_MALLOC;
1624 h2c->flags |= H2_CF_DEM_MROOM;
1625 return 0;
1626 }
1627
1628 memcpy(str,
1629 "\x00\x00\x08" /* length : 8 (same payload) */
1630 "\x06" "\x01" /* type : 6, flags : ACK */
1631 "\x00\x00\x00\x00" /* stream ID */, 9);
1632
1633 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001634 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001635
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001636 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001637 if (unlikely(ret <= 0)) {
1638 if (!ret) {
1639 h2c->flags |= H2_CF_MUX_MFULL;
1640 h2c->flags |= H2_CF_DEM_MROOM;
1641 return 0;
1642 }
1643 else {
1644 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1645 return 0;
1646 }
1647 }
1648 return ret;
1649}
1650
Willy Tarreau26f95952017-07-27 17:18:30 +02001651/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1652 * Returns > 0 on success or zero on missing data. It may return an error in
1653 * h2c or h2s. Described in RFC7540#6.9.
1654 */
1655static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1656{
1657 int32_t inc;
1658 int error;
1659
1660 if (h2c->dfl != 4) {
1661 error = H2_ERR_FRAME_SIZE_ERROR;
1662 goto conn_err;
1663 }
1664
1665 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001666 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001667 return 0;
1668
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001669 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001670
1671 if (h2c->dsi != 0) {
1672 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001673
1674 /* it's not an error to receive WU on a closed stream */
1675 if (h2s->st == H2_SS_CLOSED)
1676 return 1;
1677
1678 if (!inc) {
1679 error = H2_ERR_PROTOCOL_ERROR;
1680 goto strm_err;
1681 }
1682
1683 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1684 error = H2_ERR_FLOW_CONTROL_ERROR;
1685 goto strm_err;
1686 }
1687
1688 h2s->mws += inc;
1689 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1690 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001691 if (h2s->send_wait)
1692 LIST_ADDQ(&h2c->send_list, &h2s->list);
1693
Willy Tarreau26f95952017-07-27 17:18:30 +02001694 }
1695 }
1696 else {
1697 /* connection window update */
1698 if (!inc) {
1699 error = H2_ERR_PROTOCOL_ERROR;
1700 goto conn_err;
1701 }
1702
1703 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1704 error = H2_ERR_FLOW_CONTROL_ERROR;
1705 goto conn_err;
1706 }
1707
1708 h2c->mws += inc;
1709 }
1710
1711 return 1;
1712
1713 conn_err:
1714 h2c_error(h2c, error);
1715 return 0;
1716
1717 strm_err:
1718 if (h2s) {
1719 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001720 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001721 }
1722 else
1723 h2c_error(h2c, error);
1724 return 0;
1725}
1726
Willy Tarreaue96b0922017-10-30 00:28:29 +01001727/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1728 * the last ID. Returns > 0 on success or zero on missing data. It may return
1729 * an error in h2c. Described in RFC7540#6.8.
1730 */
1731static int h2c_handle_goaway(struct h2c *h2c)
1732{
1733 int error;
1734 int last;
1735
1736 if (h2c->dsi != 0) {
1737 error = H2_ERR_PROTOCOL_ERROR;
1738 goto conn_err;
1739 }
1740
1741 if (h2c->dfl < 8) {
1742 error = H2_ERR_FRAME_SIZE_ERROR;
1743 goto conn_err;
1744 }
1745
1746 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001747 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001748 return 0;
1749
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001750 last = h2_get_n32(&h2c->dbuf, 0);
1751 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001752 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001753 if (h2c->last_sid < 0)
1754 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001755 return 1;
1756
1757 conn_err:
1758 h2c_error(h2c, error);
1759 return 0;
1760}
1761
Willy Tarreau92153fc2017-12-03 19:46:19 +01001762/* processes a PRIORITY frame, and either skips it or rejects if it is
1763 * invalid. Returns > 0 on success or zero on missing data. It may return
1764 * an error in h2c. Described in RFC7540#6.3.
1765 */
1766static int h2c_handle_priority(struct h2c *h2c)
1767{
1768 int error;
1769
1770 if (h2c->dsi == 0) {
1771 error = H2_ERR_PROTOCOL_ERROR;
1772 goto conn_err;
1773 }
1774
1775 if (h2c->dfl != 5) {
1776 error = H2_ERR_FRAME_SIZE_ERROR;
1777 goto conn_err;
1778 }
1779
1780 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001781 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001782 return 0;
1783
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001784 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001785 /* 7540#5.3 : can't depend on itself */
1786 error = H2_ERR_PROTOCOL_ERROR;
1787 goto conn_err;
1788 }
1789 return 1;
1790
1791 conn_err:
1792 h2c_error(h2c, error);
1793 return 0;
1794}
1795
Willy Tarreaucd234e92017-08-18 10:59:39 +02001796/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1797 * Returns > 0 on success or zero on missing data. It may return an error in
1798 * h2c. Described in RFC7540#6.4.
1799 */
1800static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1801{
1802 int error;
1803
1804 if (h2c->dsi == 0) {
1805 error = H2_ERR_PROTOCOL_ERROR;
1806 goto conn_err;
1807 }
1808
Willy Tarreaucd234e92017-08-18 10:59:39 +02001809 if (h2c->dfl != 4) {
1810 error = H2_ERR_FRAME_SIZE_ERROR;
1811 goto conn_err;
1812 }
1813
1814 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001815 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001816 return 0;
1817
1818 /* late RST, already handled */
1819 if (h2s->st == H2_SS_CLOSED)
1820 return 1;
1821
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001822 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001823 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001824
1825 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001826 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001827 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001828 }
1829
1830 h2s->flags |= H2_SF_RST_RCVD;
1831 return 1;
1832
1833 conn_err:
1834 h2c_error(h2c, error);
1835 return 0;
1836}
1837
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001838/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1839 * It may return an error in h2c or h2s. The caller must consider that the
1840 * return value is the new h2s in case one was allocated (most common case).
1841 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001842 * errors here are reported as connection errors since it's impossible to
1843 * recover from such errors after the compression context has been altered.
1844 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001845static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001846{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001847 struct buffer rxbuf = BUF_NULL;
1848 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001849 int error;
1850
1851 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001852 /* RFC7540#4.2 */
1853 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001854 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001855 goto conn_err;
Willy Tarreau13278b42017-10-13 19:23:14 +02001856 }
1857
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001858 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001859 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001860
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001861 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001862 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001863
1864 /* now either the frame is complete or the buffer is complete */
1865 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001866 /* The stream exists/existed, this must be a trailers frame */
1867 if (h2s->st != H2_SS_CLOSED) {
1868 if (!h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags))
1869 goto out;
1870 goto done;
1871 }
Willy Tarreau13278b42017-10-13 19:23:14 +02001872 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001873 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001874 goto conn_err;
1875 }
1876 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1877 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1878 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001879 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001880 goto conn_err;
1881 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001882 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1883 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001884
Willy Tarreau25919232019-01-03 14:48:18 +01001885 error = h2c_decode_headers(h2c, &rxbuf, &flags);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001886
Willy Tarreau25919232019-01-03 14:48:18 +01001887 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001888 if (h2c->st0 >= H2_CS_ERROR)
1889 goto out;
1890
Willy Tarreau25919232019-01-03 14:48:18 +01001891 if (error <= 0) {
1892 if (error == 0)
1893 goto out; // missing data
1894
1895 /* Failed to decode this stream (e.g. too large request)
1896 * but the HPACK decompressor is still synchronized.
1897 */
1898 h2s = (struct h2s*)h2_error_stream;
1899 goto send_rst;
1900 }
1901
Willy Tarreau22de8d32018-09-05 19:55:58 +02001902 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001903 * positively from h2c_frt_stream_new(), the stream will report the error,
1904 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001905 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001906 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001907 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001908 h2s = (struct h2s*)h2_refused_stream;
1909 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001910 }
1911
1912 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001913 h2s->rxbuf = rxbuf;
1914 h2s->flags |= flags;
1915
Willy Tarreau88d138e2019-01-02 19:38:14 +01001916 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001917 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001918 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001919
1920 if (h2s->flags & H2_SF_ES_RCVD) {
1921 h2s->st = H2_SS_HREM;
Willy Tarreau39d68502018-03-02 12:26:37 +01001922 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001923 }
1924
Willy Tarreau3a429f02019-01-03 11:41:50 +01001925 /* update the max stream ID if the request is being processed */
1926 if (h2s->id > h2c->max_id)
1927 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001928
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001929 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001930
1931 conn_err:
1932 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001933 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001934
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001935 out:
1936 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001937 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001938
1939 send_rst:
1940 /* make the demux send an RST for the current stream. We may only
1941 * do this if we're certain that the HEADERS frame was properly
1942 * decompressed so that the HPACK decoder is still kept up to date.
1943 */
1944 h2_release_buf(h2c, &rxbuf);
1945 h2c->st0 = H2_CS_FRAME_E;
1946 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001947}
1948
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001949/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1950 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1951 * errors here are reported as connection errors since it's impossible to
1952 * recover from such errors after the compression context has been altered.
1953 */
1954static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1955{
1956 int error;
1957
1958 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001959 /* RFC7540#4.2 */
1960 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001961 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001962 goto conn_err;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001963 }
1964
1965 if (!b_size(&h2c->dbuf))
1966 return NULL; // empty buffer
1967
1968 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1969 return NULL; // incomplete frame
1970
Willy Tarreau25919232019-01-03 14:48:18 +01001971 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001972
Willy Tarreau25919232019-01-03 14:48:18 +01001973 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001974 if (h2c->st0 >= H2_CS_ERROR)
1975 return NULL;
1976
Willy Tarreau25919232019-01-03 14:48:18 +01001977 if (error <= 0) {
1978 if (error == 0)
1979 return NULL; // missing data
1980
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001981 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01001982 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001983 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01001984 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001985 }
1986
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01001987 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1988 h2s->flags |= H2_SF_ES_RCVD;
1989 h2s->cs->flags |= CS_FL_REOS;
1990 }
1991
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001992 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1993 h2s->st = H2_SS_ERROR;
1994 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1995 h2s->st = H2_SS_HREM;
1996 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1997 h2s_close(h2s);
1998
1999 return h2s;
2000
2001 conn_err:
2002 h2c_error(h2c, error);
2003 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002004}
2005
Willy Tarreau454f9052017-10-26 19:40:35 +02002006/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2007 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2008 */
2009static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2010{
2011 int error;
2012
2013 /* note that empty DATA frames are perfectly valid and sometimes used
2014 * to signal an end of stream (with the ES flag).
2015 */
2016
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002017 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002018 return 0; // empty buffer
2019
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002020 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002021 return 0; // incomplete frame
2022
2023 /* now either the frame is complete or the buffer is complete */
2024
2025 if (!h2c->dsi) {
2026 /* RFC7540#6.1 */
2027 error = H2_ERR_PROTOCOL_ERROR;
2028 goto conn_err;
2029 }
2030
2031 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2032 /* RFC7540#6.1 */
2033 error = H2_ERR_STREAM_CLOSED;
2034 goto strm_err;
2035 }
2036
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002037 if (!h2_frt_transfer_data(h2s))
2038 return 0;
2039
Willy Tarreau454f9052017-10-26 19:40:35 +02002040 /* call the upper layers to process the frame, then let the upper layer
2041 * notify the stream about any change.
2042 */
2043 if (!h2s->cs) {
2044 error = H2_ERR_STREAM_CLOSED;
2045 goto strm_err;
2046 }
2047
Willy Tarreau8f650c32017-11-21 19:36:21 +01002048 if (h2c->st0 >= H2_CS_ERROR)
2049 return 0;
2050
Willy Tarreau721c9742017-11-07 11:05:42 +01002051 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002052 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002053 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002054 }
2055
2056 /* check for completion : the callee will change this to FRAME_A or
2057 * FRAME_H once done.
2058 */
2059 if (h2c->st0 == H2_CS_FRAME_P)
2060 return 0;
2061
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002062
2063 /* last frame */
2064 if (h2c->dff & H2_F_DATA_END_STREAM) {
2065 h2s->st = H2_SS_HREM;
2066 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002067 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002068 }
2069
Willy Tarreau454f9052017-10-26 19:40:35 +02002070 return 1;
2071
2072 conn_err:
2073 h2c_error(h2c, error);
2074 return 0;
2075
2076 strm_err:
2077 if (h2s) {
2078 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002079 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002080 }
2081 else
2082 h2c_error(h2c, error);
2083 return 0;
2084}
2085
Willy Tarreaubc933932017-10-09 16:21:43 +02002086/* process Rx frames to be demultiplexed */
2087static void h2_process_demux(struct h2c *h2c)
2088{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002089 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002090
Willy Tarreau081d4722017-05-16 21:51:05 +02002091 if (h2c->st0 >= H2_CS_ERROR)
2092 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002093
2094 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2095 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002096 if (h2c->flags & H2_CF_IS_BACK)
2097 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002098 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2099 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002100 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002101 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002102 sess_log(h2c->conn->owner);
2103 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002104 goto fail;
2105 }
2106
2107 h2c->max_id = 0;
2108 h2c->st0 = H2_CS_SETTINGS1;
2109 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002110
2111 if (h2c->st0 == H2_CS_SETTINGS1) {
2112 struct h2_fh hdr;
2113
2114 /* ensure that what is pending is a valid SETTINGS frame
2115 * without an ACK.
2116 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002117 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002118 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002119 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002120 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002121 sess_log(h2c->conn->owner);
2122 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002123 goto fail;
2124 }
2125
2126 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2127 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2128 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2129 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002130 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002131 goto fail;
2132 }
2133
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002134 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002135 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2136 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2137 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002138 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002139 goto fail;
2140 }
2141
Willy Tarreau3bf69182018-12-21 15:34:50 +01002142 /* that's OK, switch to FRAME_P to process it. This is
2143 * a SETTINGS frame whose header has already been
2144 * deleted above.
2145 */
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002146 h2c->dfl = hdr.len;
2147 h2c->dsi = hdr.sid;
2148 h2c->dft = hdr.ft;
2149 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002150 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002151 h2c->st0 = H2_CS_FRAME_P;
2152 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002153 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002154
2155 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002156 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002157 int ret = 0;
2158
2159 if (h2c->st0 >= H2_CS_ERROR)
2160 break;
2161
2162 if (h2c->st0 == H2_CS_FRAME_H) {
2163 struct h2_fh hdr;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002164 unsigned int padlen = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002165
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002166 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002167 break;
2168
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002169 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002170 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2171 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002172 if (!h2c->nb_streams) {
2173 /* only log if no other stream can report the error */
2174 sess_log(h2c->conn->owner);
2175 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002176 break;
2177 }
2178
Willy Tarreau3bf69182018-12-21 15:34:50 +01002179 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2180 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2181 * we read the pad length and drop it from the remaining
2182 * payload (one byte + the 9 remaining ones = 10 total
2183 * removed), so we have a frame payload starting after the
2184 * pad len. Flow controlled frames (DATA) also count the
2185 * padlen in the flow control, so it must be adjusted.
2186 */
2187 if (hdr.len < 1) {
2188 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2189 sess_log(h2c->conn->owner);
2190 goto fail;
2191 }
2192 hdr.len--;
2193
2194 if (b_data(&h2c->dbuf) < 10)
2195 break; // missing padlen
2196
2197 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2198
2199 if (padlen > hdr.len) {
2200 /* RFC7540#6.1 : pad length = length of
2201 * frame payload or greater => error.
2202 */
2203 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2204 sess_log(h2c->conn->owner);
2205 goto fail;
2206 }
2207
2208 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2209 h2c->rcvd_c++;
2210 h2c->rcvd_s++;
2211 }
2212 b_del(&h2c->dbuf, 1);
2213 }
2214 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002215 h2c->dfl = hdr.len;
2216 h2c->dsi = hdr.sid;
2217 h2c->dft = hdr.ft;
2218 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002219 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002220 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002221 }
2222
2223 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002224 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2225
Willy Tarreau567beb82018-12-18 16:52:44 +01002226 if (tmp_h2s != h2s && h2s && h2s->cs &&
2227 (b_data(&h2s->rxbuf) ||
2228 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002229 /* we may have to signal the upper layers */
2230 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002231 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002232 }
2233 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002234
Willy Tarreaud7901432017-12-29 11:34:40 +01002235 if (h2c->st0 == H2_CS_FRAME_E)
2236 goto strm_err;
2237
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002238 if (h2s->st == H2_SS_IDLE &&
2239 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2240 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2241 * this state MUST be treated as a connection error
2242 */
2243 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2244 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002245 if (!h2c->nb_streams) {
2246 /* only log if no other stream can report the error */
2247 sess_log(h2c->conn->owner);
2248 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002249 break;
2250 }
2251
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002252 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2253 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2254 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2255 * this state MUST be treated as a stream error
2256 */
2257 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2258 goto strm_err;
2259 }
2260
Willy Tarreauab837502017-12-27 15:07:30 +01002261 /* Below the management of frames received in closed state is a
2262 * bit hackish because the spec makes strong differences between
2263 * streams closed by receiving RST, sending RST, and seeing ES
2264 * in both directions. In addition to this, the creation of a
2265 * new stream reusing the identifier of a closed one will be
2266 * detected here. Given that we cannot keep track of all closed
2267 * streams forever, we consider that unknown closed streams were
2268 * closed on RST received, which allows us to respond with an
2269 * RST without breaking the connection (eg: to abort a transfer).
2270 * Some frames have to be silently ignored as well.
2271 */
2272 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2273 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2274 /* #5.1.1: The identifier of a newly
2275 * established stream MUST be numerically
2276 * greater than all streams that the initiating
2277 * endpoint has opened or reserved. This
2278 * governs streams that are opened using a
2279 * HEADERS frame and streams that are reserved
2280 * using PUSH_PROMISE. An endpoint that
2281 * receives an unexpected stream identifier
2282 * MUST respond with a connection error.
2283 */
2284 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2285 goto strm_err;
2286 }
2287
2288 if (h2s->flags & H2_SF_RST_RCVD) {
2289 /* RFC7540#5.1:closed: an endpoint that
2290 * receives any frame other than PRIORITY after
2291 * receiving a RST_STREAM MUST treat that as a
2292 * stream error of type STREAM_CLOSED.
2293 *
2294 * Note that old streams fall into this category
2295 * and will lead to an RST being sent.
2296 */
2297 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2298 h2c->st0 = H2_CS_FRAME_E;
2299 goto strm_err;
2300 }
2301
2302 /* RFC7540#5.1:closed: if this state is reached as a
2303 * result of sending a RST_STREAM frame, the peer that
2304 * receives the RST_STREAM might have already sent
2305 * frames on the stream that cannot be withdrawn. An
2306 * endpoint MUST ignore frames that it receives on
2307 * closed streams after it has sent a RST_STREAM
2308 * frame. An endpoint MAY choose to limit the period
2309 * over which it ignores frames and treat frames that
2310 * arrive after this time as being in error.
2311 */
2312 if (!(h2s->flags & H2_SF_RST_SENT)) {
2313 /* RFC7540#5.1:closed: any frame other than
2314 * PRIO/WU/RST in this state MUST be treated as
2315 * a connection error
2316 */
2317 if (h2c->dft != H2_FT_RST_STREAM &&
2318 h2c->dft != H2_FT_PRIORITY &&
2319 h2c->dft != H2_FT_WINDOW_UPDATE) {
2320 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2321 goto strm_err;
2322 }
2323 }
2324 }
2325
Willy Tarreauc0da1962017-10-30 18:38:00 +01002326#if 0
2327 // problem below: it is not possible to completely ignore such
2328 // streams as we need to maintain the compression state as well
2329 // and for this we need to completely process these frames (eg:
2330 // HEADERS frames) as well as counting DATA frames to emit
2331 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2332 // This is a typical case of layer violation where the
2333 // transported contents are critical to the connection's
2334 // validity and must be ignored at the same time :-(
2335
2336 /* graceful shutdown, ignore streams whose ID is higher than
2337 * the one advertised in GOAWAY. RFC7540#6.8.
2338 */
2339 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002340 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2341 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002342 h2c->dfl -= ret;
2343 ret = h2c->dfl == 0;
2344 goto strm_err;
2345 }
2346#endif
2347
Willy Tarreau7e98c052017-10-10 15:56:59 +02002348 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002349 case H2_FT_SETTINGS:
2350 if (h2c->st0 == H2_CS_FRAME_P)
2351 ret = h2c_handle_settings(h2c);
2352
2353 if (h2c->st0 == H2_CS_FRAME_A)
2354 ret = h2c_ack_settings(h2c);
2355 break;
2356
Willy Tarreaucf68c782017-10-10 17:11:41 +02002357 case H2_FT_PING:
2358 if (h2c->st0 == H2_CS_FRAME_P)
2359 ret = h2c_handle_ping(h2c);
2360
2361 if (h2c->st0 == H2_CS_FRAME_A)
2362 ret = h2c_ack_ping(h2c);
2363 break;
2364
Willy Tarreau26f95952017-07-27 17:18:30 +02002365 case H2_FT_WINDOW_UPDATE:
2366 if (h2c->st0 == H2_CS_FRAME_P)
2367 ret = h2c_handle_window_update(h2c, h2s);
2368 break;
2369
Willy Tarreau61290ec2017-10-17 08:19:21 +02002370 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002371 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2372 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2373 * frames' parsers consume all following CONTINUATION
2374 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002375 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002376 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2377 sess_log(h2c->conn->owner);
2378 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002379
Willy Tarreau13278b42017-10-13 19:23:14 +02002380 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002381 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002382 if (h2c->flags & H2_CF_IS_BACK)
2383 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2384 else
2385 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002386 if (tmp_h2s) {
2387 h2s = tmp_h2s;
2388 ret = 1;
2389 }
2390 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002391 break;
2392
Willy Tarreau454f9052017-10-26 19:40:35 +02002393 case H2_FT_DATA:
2394 if (h2c->st0 == H2_CS_FRAME_P)
2395 ret = h2c_frt_handle_data(h2c, h2s);
2396
2397 if (h2c->st0 == H2_CS_FRAME_A)
2398 ret = h2c_send_strm_wu(h2c);
2399 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002400
Willy Tarreau92153fc2017-12-03 19:46:19 +01002401 case H2_FT_PRIORITY:
2402 if (h2c->st0 == H2_CS_FRAME_P)
2403 ret = h2c_handle_priority(h2c);
2404 break;
2405
Willy Tarreaucd234e92017-08-18 10:59:39 +02002406 case H2_FT_RST_STREAM:
2407 if (h2c->st0 == H2_CS_FRAME_P)
2408 ret = h2c_handle_rst_stream(h2c, h2s);
2409 break;
2410
Willy Tarreaue96b0922017-10-30 00:28:29 +01002411 case H2_FT_GOAWAY:
2412 if (h2c->st0 == H2_CS_FRAME_P)
2413 ret = h2c_handle_goaway(h2c);
2414 break;
2415
Willy Tarreau1c661982017-10-30 13:52:01 +01002416 case H2_FT_PUSH_PROMISE:
2417 /* not permitted here, RFC7540#5.1 */
2418 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002419 if (!h2c->nb_streams) {
2420 /* only log if no other stream can report the error */
2421 sess_log(h2c->conn->owner);
2422 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002423 break;
2424
2425 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002426 default:
2427 /* drop frames that we ignore. They may be larger than
2428 * the buffer so we drain all of their contents until
2429 * we reach the end.
2430 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002431 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2432 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002433 h2c->dfl -= ret;
2434 ret = h2c->dfl == 0;
2435 }
2436
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002437 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002438 /* We may have to send an RST if not done yet */
2439 if (h2s->st == H2_SS_ERROR)
2440 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002441
Willy Tarreaua20a5192017-12-27 11:02:06 +01002442 if (h2c->st0 == H2_CS_FRAME_E)
2443 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002444
Willy Tarreau7e98c052017-10-10 15:56:59 +02002445 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002446 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002447 break;
2448
2449 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002450 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002451 h2c->st0 = H2_CS_FRAME_H;
2452 }
2453 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002454
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002455 if (h2c->rcvd_c > 0 &&
2456 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2457 h2c_send_conn_wu(h2c);
2458
Willy Tarreau52eed752017-09-22 15:05:09 +02002459 fail:
2460 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002461 if (h2s && h2s->cs &&
2462 (b_data(&h2s->rxbuf) ||
2463 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002464 /* we may have to signal the upper layers */
2465 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002466 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002467 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002468
Willy Tarreau47b515a2018-12-21 16:09:41 +01002469 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002470}
2471
2472/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2473 * the end.
2474 */
2475static int h2_process_mux(struct h2c *h2c)
2476{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002477 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002478
Willy Tarreau01b44822018-10-03 14:26:37 +02002479 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2480 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2481 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2482 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2483 if (h2c->st0 == H2_CS_ERROR) {
2484 h2c->st0 = H2_CS_ERROR2;
2485 sess_log(h2c->conn->owner);
2486 }
2487 goto fail;
2488 }
2489 h2c->st0 = H2_CS_SETTINGS1;
2490 }
2491 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002492 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002493 return 1;
2494 }
2495
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002496 /* start by sending possibly pending window updates */
2497 if (h2c->rcvd_c > 0 &&
2498 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2499 h2c_send_conn_wu(h2c) < 0)
2500 goto fail;
2501
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002502 /* First we always process the flow control list because the streams
2503 * waiting there were already elected for immediate emission but were
2504 * blocked just on this.
2505 */
2506
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002507 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002508 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2509 h2c->st0 >= H2_CS_ERROR)
2510 break;
2511
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002512 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002513 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2514 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002515 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002516 LIST_DEL(&h2s->list);
2517 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002518 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002519 }
2520
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002521 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002522 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2523 break;
2524
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002525 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002526 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2527 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002528 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002529 LIST_DEL(&h2s->list);
2530 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002531 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002532 }
2533
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002534 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002535 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002536 if (h2c->st0 == H2_CS_ERROR) {
2537 if (h2c->max_id >= 0) {
2538 h2c_send_goaway_error(h2c, NULL);
2539 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2540 return 0;
2541 }
2542
2543 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2544 }
2545 return 1;
2546 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002547 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002548}
2549
Willy Tarreau62f52692017-10-08 23:01:42 +02002550
Willy Tarreau479998a2018-11-18 06:30:59 +01002551/* Attempt to read data, and subscribe if none available.
2552 * The function returns 1 if data has been received, otherwise zero.
2553 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002554static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002555{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002556 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002557 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002558 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002559 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002560
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002561 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002562 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002563
Willy Tarreau315d8072017-12-10 22:17:57 +01002564 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002565 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002566
Willy Tarreau44e973f2018-03-01 17:49:30 +01002567 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002568 if (!buf) {
2569 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002570 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002571 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002572
Olivier Houchard7505f942018-08-21 18:10:44 +02002573 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002574 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002575 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2576 /* HTX in use : try to pre-align the buffer like the
2577 * rxbufs will be to optimize memory copies. We'll make
2578 * sure that the frame header lands at the end of the
2579 * HTX block to alias it upon recv. We cannot use the
2580 * head because rcv_buf() will realign the buffer if
2581 * it's empty. Thus we cheat and pretend we already
2582 * have a few bytes there.
2583 */
2584 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002585 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002586 }
2587 else
2588 max = b_room(buf);
2589
Olivier Houchard7505f942018-08-21 18:10:44 +02002590 if (max)
2591 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2592 else
2593 ret = 0;
2594 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002595
Olivier Houchard53216e72018-10-10 15:46:36 +02002596 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002597 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002598
Olivier Houcharda1411e62018-08-17 18:42:48 +02002599 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002600 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002601 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002602 }
2603
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002604 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002605 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002606 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002607}
2608
Willy Tarreau479998a2018-11-18 06:30:59 +01002609/* Try to send data if possible.
2610 * The function returns 1 if data have been sent, otherwise zero.
2611 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002612static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002613{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002614 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002615 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002616 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002617
2618 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002619 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002620
Olivier Houchard7505f942018-08-21 18:10:44 +02002621
Willy Tarreaua2af5122017-10-09 11:56:46 +02002622 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2623 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002624 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002625 }
2626
Willy Tarreaubc933932017-10-09 16:21:43 +02002627 /* This loop is quite simple : it tries to fill as much as it can from
2628 * pending streams into the existing buffer until it's reportedly full
2629 * or the end of send requests is reached. Then it tries to send this
2630 * buffer's contents out, marks it not full if at least one byte could
2631 * be sent, and tries again.
2632 *
2633 * The snd_buf() function normally takes a "flags" argument which may
2634 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2635 * data immediately comes and CO_SFL_STREAMER to indicate that the
2636 * connection is streaming lots of data (used to increase TLS record
2637 * size at the expense of latency). The former can be sent any time
2638 * there's a buffer full flag, as it indicates at least one stream
2639 * attempted to send and failed so there are pending data. An
2640 * alternative would be to set it as long as there's an active stream
2641 * but that would be problematic for ACKs until we have an absolute
2642 * guarantee that all waiters have at least one byte to send. The
2643 * latter should possibly not be set for now.
2644 */
2645
2646 done = 0;
2647 while (!done) {
2648 unsigned int flags = 0;
2649
2650 /* fill as much as we can into the current buffer */
2651 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2652 done = h2_process_mux(h2c);
2653
2654 if (conn->flags & CO_FL_ERROR)
2655 break;
2656
2657 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2658 flags |= CO_SFL_MSG_MORE;
2659
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002660 if (b_data(&h2c->mbuf)) {
2661 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002662 if (!ret)
2663 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002664 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002665 b_del(&h2c->mbuf, ret);
2666 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002667 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002668
2669 /* wrote at least one byte, the buffer is not full anymore */
2670 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2671 }
2672
Willy Tarreaua2af5122017-10-09 11:56:46 +02002673 if (conn->flags & CO_FL_SOCK_WR_SH) {
2674 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002675 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002676 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002677 /* We're not full anymore, so we can wake any task that are waiting
2678 * for us.
2679 */
2680 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002681 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002682 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2683 struct h2s *, list);
2684 LIST_DEL(&h2s->list);
2685 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002686 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002687 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2688 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002689 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002690 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002691 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002692 /* We're done, no more to send */
2693 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002694 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002695schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002696 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2697 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002698 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002699}
2700
2701static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2702{
2703 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002704 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002705
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002706 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002707 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002708 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002709 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002710 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002711 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002712 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002713}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002714
Willy Tarreau62f52692017-10-08 23:01:42 +02002715/* callback called on any event by the connection handler.
2716 * It applies changes and returns zero, or < 0 if it wants immediate
2717 * destruction of the connection (which normally doesn not happen in h2).
2718 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002719static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002720{
Olivier Houchard7505f942018-08-21 18:10:44 +02002721 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002722
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002723 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002724 h2_process_demux(h2c);
2725
2726 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002727 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002728
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002729 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002730 h2c->flags &= ~H2_CF_DEM_DFULL;
2731 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002732 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002733
Willy Tarreau0b37d652018-10-03 10:33:02 +02002734 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002735 /* frontend is stopping, reload likely in progress, let's try
2736 * to announce a graceful shutdown if not yet done. We don't
2737 * care if it fails, it will be tried again later.
2738 */
2739 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2740 if (h2c->last_sid < 0)
2741 h2c->last_sid = (1U << 31) - 1;
2742 h2c_send_goaway_error(h2c, NULL);
2743 }
2744 }
2745
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002746 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002747 * If we received early data, and the handshake is done, wake
2748 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002749 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002750 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2751 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2752 struct eb32_node *node;
2753 struct h2s *h2s;
2754
2755 h2c->flags |= H2_CF_WAIT_FOR_HS;
2756 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2757
2758 while (node) {
2759 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002760 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002761 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002762 node = eb32_next(node);
2763 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002764 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002765
Willy Tarreau26bd7612017-10-09 16:47:04 +02002766 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002767 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2768 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2769 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002770 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002771
2772 if (eb_is_empty(&h2c->streams_by_id)) {
2773 /* no more stream, kill the connection now */
2774 h2_release(conn);
2775 return -1;
2776 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002777 }
2778
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002779 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002780 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002781
Olivier Houchard53216e72018-10-10 15:46:36 +02002782 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2783 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2784 (h2c->st0 != H2_CS_ERROR &&
2785 !b_data(&h2c->mbuf) &&
2786 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2787 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002788 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002789
Willy Tarreau3f133572017-10-31 19:21:06 +01002790 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002791 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002792 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002793 task_queue(h2c->task);
2794 }
2795 else
2796 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002797 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002798
Olivier Houchard7505f942018-08-21 18:10:44 +02002799 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002800 return 0;
2801}
2802
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002803static int h2_wake(struct connection *conn)
2804{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002805 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002806
2807 return (h2_process(h2c));
2808}
2809
Willy Tarreauea392822017-10-31 10:02:25 +01002810/* Connection timeout management. The principle is that if there's no receipt
2811 * nor sending for a certain amount of time, the connection is closed. If the
2812 * MUX buffer still has lying data or is not allocatable, the connection is
2813 * immediately killed. If it's allocatable and empty, we attempt to send a
2814 * GOAWAY frame.
2815 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002816static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002817{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002818 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002819 int expired = tick_is_expired(t->expire, now_ms);
2820
Willy Tarreau0975f112018-03-29 15:22:59 +02002821 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002822 return t;
2823
Willy Tarreau0975f112018-03-29 15:22:59 +02002824 task_delete(t);
2825 task_free(t);
2826
2827 if (!h2c) {
2828 /* resources were already deleted */
2829 return NULL;
2830 }
2831
2832 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002833 h2c_error(h2c, H2_ERR_NO_ERROR);
2834 h2_wake_some_streams(h2c, 0, 0);
2835
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002836 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002837 /* don't even try to send a GOAWAY, the buffer is stuck */
2838 h2c->flags |= H2_CF_GOAWAY_FAILED;
2839 }
2840
2841 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002842 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002843 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2844 h2c->flags |= H2_CF_GOAWAY_FAILED;
2845
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002846 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2847 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002848 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002849 b_del(&h2c->mbuf, ret);
2850 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002851 }
2852 }
Willy Tarreauea392822017-10-31 10:02:25 +01002853
Willy Tarreau0975f112018-03-29 15:22:59 +02002854 /* either we can release everything now or it will be done later once
2855 * the last stream closes.
2856 */
2857 if (eb_is_empty(&h2c->streams_by_id))
2858 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002859
Willy Tarreauea392822017-10-31 10:02:25 +01002860 return NULL;
2861}
2862
2863
Willy Tarreau62f52692017-10-08 23:01:42 +02002864/*******************************************/
2865/* functions below are used by the streams */
2866/*******************************************/
2867
2868/*
2869 * Attach a new stream to a connection
2870 * (Used for outgoing connections)
2871 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002872static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002873{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002874 struct conn_stream *cs;
2875 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002876 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002877
2878 cs = cs_new(conn);
2879 if (!cs)
2880 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002881 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002882 if (!h2s) {
2883 cs_free(cs);
2884 return NULL;
2885 }
2886 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002887}
2888
Willy Tarreaufafd3982018-11-18 21:29:20 +01002889/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2890 * We have to scan because we may have some orphan streams. It might be
2891 * beneficial to scan backwards from the end to reduce the likeliness to find
2892 * orphans.
2893 */
2894static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2895{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002896 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002897 struct h2s *h2s;
2898 struct eb32_node *node;
2899
2900 node = eb32_first(&h2c->streams_by_id);
2901 while (node) {
2902 h2s = container_of(node, struct h2s, by_id);
2903 if (h2s->cs)
2904 return h2s->cs;
2905 node = eb32_next(node);
2906 }
2907 return NULL;
2908}
2909
Willy Tarreau62f52692017-10-08 23:01:42 +02002910/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002911 * Destroy the mux and the associated connection, if it is no longer used
2912 */
2913static void h2_destroy(struct connection *conn)
2914{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002915 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002916
2917 if (eb_is_empty(&h2c->streams_by_id))
2918 h2_release(h2c->conn);
2919}
2920
2921/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002922 * Detach the stream from the connection and possibly release the connection.
2923 */
2924static void h2_detach(struct conn_stream *cs)
2925{
Willy Tarreau60935142017-10-16 18:11:19 +02002926 struct h2s *h2s = cs->ctx;
2927 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002928 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002929
2930 cs->ctx = NULL;
2931 if (!h2s)
2932 return;
2933
Olivier Houchardf502aca2018-12-14 19:42:40 +01002934 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002935 h2c = h2s->h2c;
2936 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002937 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002938 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2939 !h2_has_too_many_cs(h2c)) {
2940 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002941 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002942 }
Willy Tarreau60935142017-10-16 18:11:19 +02002943
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002944 /* this stream may be blocked waiting for some data to leave (possibly
2945 * an ES or RST frame), so orphan it in this case.
2946 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002947 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002948 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002949 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002950 return;
2951
Willy Tarreau45f752e2017-10-30 15:44:59 +01002952 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2953 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2954 /* unblock the connection if it was blocked on this
2955 * stream.
2956 */
2957 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2958 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002959 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002960 }
2961
Willy Tarreau71049cc2018-03-28 13:56:39 +02002962 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002963
Olivier Houchard8a786902018-12-15 16:05:40 +01002964 if (h2c->flags & H2_CF_IS_BACK &&
2965 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002966 if (!(h2c->conn->flags &
2967 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2968 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002969 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002970 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
2971 h2c->conn->owner = NULL;
2972 if (eb_is_empty(&h2c->streams_by_id)) {
2973 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
2974 /* The server doesn't want it, let's kill the connection right away */
2975 h2c->conn->mux->destroy(h2c->conn);
2976 return;
2977 }
2978 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002979 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002980 if (eb_is_empty(&h2c->streams_by_id)) {
2981 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
2982 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
2983 return;
2984 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002985 /* Never ever allow to reuse a connection from a non-reuse backend */
2986 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2987 h2c->conn->flags |= CO_FL_PRIVATE;
Olivier Houchard855ac252018-12-28 14:44:41 +01002988 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2_settings_max_concurrent_streams) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002989 struct server *srv = objt_server(h2c->conn->target);
2990
2991 if (srv) {
2992 if (h2c->conn->flags & CO_FL_PRIVATE)
2993 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
2994 else
2995 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
2996 }
2997
2998 }
2999 }
3000 }
3001
Willy Tarreaue323f342018-03-28 13:51:45 +02003002 /* We don't want to close right now unless we're removing the
3003 * last stream, and either the connection is in error, or it
3004 * reached the ID already specified in a GOAWAY frame received
3005 * or sent (as seen by last_sid >= 0).
3006 */
3007 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3008 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003009 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02003010 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01003011 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003012 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003013 (conn_xprt_read0_pending(h2c->conn) ||
3014 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3015 /* no more stream will come, kill it now */
3016 h2_release(h2c->conn);
3017 }
3018 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003019 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003020 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3021 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003022 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003023 else
3024 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003025 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003026}
3027
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003028static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003029{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003030 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003031 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003032
Willy Tarreau721c9742017-11-07 11:05:42 +01003033 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003034 return;
3035
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003036 /* if no outgoing data was seen on this stream, it means it was
3037 * closed with a "tcp-request content" rule that is normally
3038 * used to kill the connection ASAP (eg: limit abuse). In this
3039 * case we send a goaway to close the connection.
3040 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003041 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003042 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003043 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003044
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003045 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3046 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003047 h2c_send_goaway_error(h2c, h2s) <= 0)
3048 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003049
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003050 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003051 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003052 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003053
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003054 return;
3055add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003056 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003057 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003058 if (h2s->flags & H2_SF_BLK_MFCTL) {
3059 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3060 h2s->send_wait = sw;
3061 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3062 h2s->send_wait = sw;
3063 LIST_ADDQ(&h2c->send_list, &h2s->list);
3064 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003065 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003066 /* Let the handler know we want shutr */
3067 sw->handle = (void *)((long)sw->handle | 1);
3068
Willy Tarreau62f52692017-10-08 23:01:42 +02003069}
3070
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003071static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003072{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003073 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003074 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003075
Willy Tarreau721c9742017-11-07 11:05:42 +01003076 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003077 return;
3078
Willy Tarreau67434202017-11-06 20:20:51 +01003079 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003080 /* we can cleanly close using an empty data frame only after headers */
3081
3082 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3083 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003084 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003085
3086 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003087 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003088 else
3089 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003090 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003091 /* if no outgoing data was seen on this stream, it means it was
3092 * closed with a "tcp-request content" rule that is normally
3093 * used to kill the connection ASAP (eg: limit abuse). In this
3094 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003095 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003096 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003097 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003098 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003099
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003100 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3101 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003102 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003103 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003104
Willy Tarreau00dd0782018-03-01 16:31:34 +01003105 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003106 }
3107
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003108 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003109 tasklet_wakeup(h2c->wait_event.task);
3110 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003111
3112 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003113 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003114 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003115 if (h2s->flags & H2_SF_BLK_MFCTL) {
3116 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3117 h2s->send_wait = sw;
3118 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3119 h2s->send_wait = sw;
3120 LIST_ADDQ(&h2c->send_list, &h2s->list);
3121 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003122 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003123 /* let the handler know we want to shutw */
3124 sw->handle = (void *)((long)(sw->handle) | 2);
3125
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003126}
3127
3128static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3129{
3130 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003131 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003132
Olivier Houchard2c68a462018-12-15 22:42:20 +01003133 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003134 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003135 h2s->send_wait = NULL;
3136 LIST_DEL(&h2s->list);
3137 LIST_INIT(&h2s->list);
3138 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003139 if (reason & 2)
3140 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003141 if (reason & 1)
3142 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003143
Olivier Houchard2c68a462018-12-15 22:42:20 +01003144 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003145 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003146 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003147 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003148}
3149
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003150static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3151{
3152 struct h2s *h2s = cs->ctx;
3153
3154 if (!mode)
3155 return;
3156
3157 h2_do_shutr(h2s);
3158}
3159
3160static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3161{
3162 struct h2s *h2s = cs->ctx;
3163
3164 h2_do_shutw(h2s);
3165}
3166
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003167/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003168 * HTX request or response depending on the connection's side. Returns a
3169 * positive value on success, a negative value on failure, or 0 if it couldn't
3170 * proceed. May report connection errors in h2c->errcode if the frame is
3171 * non-decodable and the connection unrecoverable. In absence of connection
3172 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003173 *
3174 * The function may fold CONTINUATION frames into the initial HEADERS frame
3175 * by removing padding and next frame header, then moving the CONTINUATION
3176 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3177 * leaving a hole between the main frame and the beginning of the next one.
3178 * The possibly remaining incomplete or next frame at the end may be moved
3179 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3180 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3181 *
3182 * A buffer at the beginning of processing may look like this :
3183 *
3184 * ,---.---------.-----.--------------.--------------.------.---.
3185 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3186 * `---^---------^-----^--------------^--------------^------^---'
3187 * | | <-----> | |
3188 * area | dpl | wrap
3189 * |<--------------> |
3190 * | dfl |
3191 * |<-------------------------------------------------->|
3192 * head data
3193 *
3194 * Padding is automatically overwritten when folding, participating to the
3195 * hole size after dfl :
3196 *
3197 * ,---.------------------------.-----.--------------.------.---.
3198 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3199 * `---^------------------------^-----^--------------^------^---'
3200 * | | <-----> | |
3201 * area | hole | wrap
3202 * |<-----------------------> |
3203 * | dfl |
3204 * |<-------------------------------------------------->|
3205 * head data
3206 *
3207 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3208 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3209 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003210 *
3211 * The <flags> field must point to either the stream's flags or to a copy of it
3212 * so that the function can update the following flags :
3213 * - H2_SF_DATA_CLEN when content-length is seen
3214 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3215 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003216 *
3217 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3218 * decoding, in order to detect if we're dealing with a headers or a trailers
3219 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003220 */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003221static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags)
Willy Tarreau13278b42017-10-13 19:23:14 +02003222{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003223 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003224 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003225 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003226 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003227 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003228 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003229 int flen; // header frame len
3230 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003231 int ret = 0;
3232 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003233 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003234 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003235
Willy Tarreauea18f862018-12-22 20:19:26 +01003236next_frame:
3237 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3238 goto leave; // incomplete input frame
3239
3240 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3241 * this case, we'll try to paste it immediately after the initial
3242 * HEADERS frame payload and kill any possible padding. The initial
3243 * frame's length will be increased to represent the concatenation
3244 * of the two frames. The next frame is read from position <tlen>
3245 * and written at position <flen> (minus padding if some is present).
3246 */
3247 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3248 struct h2_fh hdr;
3249 int clen; // CONTINUATION frame's payload length
3250
3251 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3252 /* no more data, the buffer may be full, either due to
3253 * too large a frame or because of too large a hole that
3254 * we're going to compact at the end.
3255 */
3256 goto leave;
3257 }
3258
3259 if (hdr.ft != H2_FT_CONTINUATION) {
3260 /* RFC7540#6.10: frame of unexpected type */
3261 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3262 goto fail;
3263 }
3264
3265 if (hdr.sid != h2c->dsi) {
3266 /* RFC7540#6.10: frame of different stream */
3267 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3268 goto fail;
3269 }
3270
3271 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3272 /* RFC7540#4.2: invalid frame length */
3273 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3274 goto fail;
3275 }
3276
3277 /* detect when we must stop aggragating frames */
3278 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3279
3280 /* Take as much as we can of the CONTINUATION frame's payload */
3281 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3282 if (clen > hdr.len)
3283 clen = hdr.len;
3284
3285 /* Move the frame's payload over the padding, hole and frame
3286 * header. At least one of hole or dpl is null (see diagrams
3287 * above). The hole moves after the new aggragated frame.
3288 */
3289 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3290 h2c->dfl += clen - h2c->dpl;
3291 hole += h2c->dpl + 9;
3292 h2c->dpl = 0;
3293 goto next_frame;
3294 }
3295
3296 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003297
Willy Tarreau13278b42017-10-13 19:23:14 +02003298 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003299 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003300 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003301 copy = alloc_trash_chunk();
3302 if (!copy) {
3303 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3304 goto fail;
3305 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003306 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3307 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3308 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003309 }
3310
Willy Tarreau13278b42017-10-13 19:23:14 +02003311 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3312 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003313 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003314 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3315 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003316 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003317 }
3318
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003319 if (flen < 5) {
3320 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3321 goto fail;
3322 }
3323
Willy Tarreau13278b42017-10-13 19:23:14 +02003324 hdrs += 5; // stream dep = 4, weight = 1
3325 flen -= 5;
3326 }
3327
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003328 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003329 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003330 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003331 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003332
Willy Tarreau937f7602018-02-26 15:22:17 +01003333 /* we can't retry a failed decompression operation so we must be very
3334 * careful not to take any risks. In practice the output buffer is
3335 * always empty except maybe for trailers, in which case we simply have
3336 * to wait for the upper layer to finish consuming what is available.
3337 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003338
3339 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003340 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003341 if (!htx_is_empty(htx)) {
3342 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003343 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003344 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003345 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003346 if (b_data(rxbuf)) {
3347 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003348 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003349 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003350
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003351 rxbuf->head = 0;
3352 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003353 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003354
Willy Tarreau25919232019-01-03 14:48:18 +01003355 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003356 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3357 sizeof(list)/sizeof(list[0]), tmp);
3358 if (outlen < 0) {
3359 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3360 goto fail;
3361 }
3362
Willy Tarreau25919232019-01-03 14:48:18 +01003363 /* The PACK decompressor was updated, let's update the input buffer and
3364 * the parser's state to commit these changes and allow us to later
3365 * fail solely on the stream if needed.
3366 */
3367 b_del(&h2c->dbuf, h2c->dfl + hole);
3368 h2c->dfl = hole = 0;
3369 h2c->st0 = H2_CS_FRAME_H;
3370
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003371 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003372 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003373
Willy Tarreau88d138e2019-01-02 19:38:14 +01003374 if (*flags & H2_SF_HEADERS_RCVD)
3375 goto trailers;
3376
3377 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003378 if (htx) {
3379 /* HTX mode */
3380 if (h2c->flags & H2_CF_IS_BACK)
3381 outlen = h2_make_htx_response(list, htx, &msgf);
3382 else
3383 outlen = h2_make_htx_request(list, htx, &msgf);
3384 } else {
3385 /* HTTP/1 mode */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003386 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf);
Willy Tarreau83195932019-01-03 10:26:23 +01003387 if (outlen > 0)
3388 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003389 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003390
3391 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003392 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003393 goto fail;
3394 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003395
Willy Tarreau174b06a2018-04-25 18:13:58 +02003396 if (msgf & H2_MSGF_BODY) {
3397 /* a payload is present */
3398 if (msgf & H2_MSGF_BODY_CL)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003399 *flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003400 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003401 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003402 }
3403
Willy Tarreau88d138e2019-01-02 19:38:14 +01003404 done:
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003405 /* indicate that a HEADERS frame was received for this stream */
3406 *flags |= H2_SF_HEADERS_RCVD;
3407
Willy Tarreau88d138e2019-01-02 19:38:14 +01003408 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
3409 /* Mark the end of message, either using EOM in HTX or with the
3410 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3411 * is not set during headers with END_STREAM.
3412 */
3413 if (htx) {
3414 if (!htx_add_endof(htx, HTX_BLK_EOM))
3415 goto fail;
3416 }
3417 else if (*flags & H2_SF_DATA_CHNK) {
3418 if (!b_putblk(rxbuf, "\r\n", 2))
3419 goto fail;
3420 }
3421 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003422
Willy Tarreau86277d42019-01-02 15:36:11 +01003423 /* success */
3424 ret = 1;
3425
Willy Tarreau68dd9852017-07-03 14:44:26 +02003426 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003427 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003428 * move the remaining data over it.
3429 */
3430 if (hole) {
3431 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3432 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3433 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3434 b_sub(&h2c->dbuf, hole);
3435 }
3436
3437 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3438 /* too large frames */
3439 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003440 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003441 }
3442
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003443 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003444 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003445 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003446 return ret;
3447
Willy Tarreau68dd9852017-07-03 14:44:26 +02003448 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003449 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003450 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003451
3452 trailers:
3453 /* This is the last HEADERS frame hence a trailer */
3454
3455 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3456 /* It's a trailer but it's missing ES flag */
3457 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3458 goto fail;
3459 }
3460
3461 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3462 * block, and when using chunks we must send the 0 CRLF marker. For
3463 * other modes, the trailers are silently dropped.
3464 */
3465 if (htx) {
3466 if (!htx_add_endof(htx, HTX_BLK_EOD))
3467 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003468 if (h2_make_htx_trailers(list, htx) <= 0)
3469 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003470 }
3471 else if (*flags & H2_SF_DATA_CHNK) {
3472 /* Legacy mode with chunked encoding : we must finalize the
3473 * data block message emit the trailing CRLF */
3474 if (!b_putblk(rxbuf, "0\r\n", 3))
3475 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003476
3477 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3478 if (outlen > 0)
3479 b_add(rxbuf, outlen);
3480 else
3481 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003482 }
3483
3484 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003485}
3486
Willy Tarreau454f9052017-10-26 19:40:35 +02003487/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3488 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3489 * in use, a new chunk is emitted for each frame. This is supposed to fit
3490 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3491 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3492 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003493 * parser state is automatically updated. Returns > 0 if it could completely
3494 * send the current frame, 0 if it couldn't complete, in which case
3495 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3496 * DATA frame can return 0 as a valid result). Stream errors are reported in
3497 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3498 * have checked the frame header and ensured that the frame was complete or the
3499 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003500 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003501static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003502{
3503 struct h2c *h2c = h2s->h2c;
3504 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003505 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003506 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003507 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003508 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003509
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003510 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003511
Olivier Houchard638b7992018-08-16 15:41:52 +02003512 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003513 if (!csbuf) {
3514 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003515 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003516 }
3517
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003518try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003519 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003520 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3521 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003522 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003523 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003524
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003525 if (flen > b_data(&h2c->dbuf)) {
3526 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003527 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003528 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003529 }
3530
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003531 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003532 block1 = htx_free_data_space(htx);
3533 if (!block1) {
3534 h2c->flags |= H2_CF_DEM_SFULL;
3535 goto fail;
3536 }
3537 if (flen > block1)
3538 flen = block1;
3539
3540 /* here, flen is the max we can copy into the output buffer */
3541 block1 = b_contig_data(&h2c->dbuf, 0);
3542 if (flen > block1)
3543 flen = block1;
3544
3545 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3546 h2c->flags |= H2_CF_DEM_SFULL;
3547 goto fail;
3548 }
3549
3550 b_del(&h2c->dbuf, flen);
3551 h2c->dfl -= flen;
3552 h2c->rcvd_c += flen;
3553 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3554 goto try_again;
3555 }
3556 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003557 /* it doesn't fit and the buffer is fragmented,
3558 * so let's defragment it and try again.
3559 */
3560 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003561 }
3562
Willy Tarreaueba10f22018-04-25 20:44:22 +02003563 /* chunked-encoding requires more room */
3564 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003565 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003566 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3567 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3568 (chklen < 1048576) ? 4 : 8;
3569 chklen += 4; // CRLF, CRLF
3570 }
3571
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003572 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003573 if (flen + chklen > b_room(csbuf)) {
3574 if (chklen >= b_room(csbuf)) {
3575 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003576 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003577 }
3578 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003579 }
3580
3581 if (h2s->flags & H2_SF_DATA_CHNK) {
3582 /* emit the chunk size */
3583 unsigned int chksz = flen;
3584 char str[10];
3585 char *beg;
3586
3587 beg = str + sizeof(str);
3588 *--beg = '\n';
3589 *--beg = '\r';
3590 do {
3591 *--beg = hextab[chksz & 0xF];
3592 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003593 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003594 }
3595
Willy Tarreau454f9052017-10-26 19:40:35 +02003596 /* Block1 is the length of the first block before the buffer wraps,
3597 * block2 is the optional second block to reach the end of the frame.
3598 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003599 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003600 if (block1 > flen)
3601 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003602 block2 = flen - block1;
3603
3604 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003605 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003606
3607 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003608 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003609
Willy Tarreaueba10f22018-04-25 20:44:22 +02003610 if (h2s->flags & H2_SF_DATA_CHNK) {
3611 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003612 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003613 }
3614
Willy Tarreau454f9052017-10-26 19:40:35 +02003615 /* now mark the input data as consumed (will be deleted from the buffer
3616 * by the caller when seeing FRAME_A after sending the window update).
3617 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003618 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003619 h2c->dfl -= flen;
3620 h2c->rcvd_c += flen;
3621 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3622
3623 if (h2c->dfl > h2c->dpl) {
3624 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003625 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003626 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003627 }
3628
Willy Tarreau4a28da12018-01-04 14:41:00 +01003629 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003630 /* here we're done with the frame, all the payload (except padding) was
3631 * transferred.
3632 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003633
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003634 if (h2c->dff & H2_F_DATA_END_STREAM) {
3635 if (htx) {
3636 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3637 h2c->flags |= H2_CF_DEM_SFULL;
3638 goto fail;
3639 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003640 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003641 else if (h2s->flags & H2_SF_DATA_CHNK) {
3642 /* emit the trailing 0 CRLF CRLF */
3643 if (b_room(csbuf) < 5) {
3644 h2c->flags |= H2_CF_DEM_SFULL;
3645 goto fail;
3646 }
3647 chklen += 5;
3648 b_putblk(csbuf, "0\r\n\r\n", 5);
3649 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003650 }
3651
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003652 h2c->rcvd_c += h2c->dpl;
3653 h2c->rcvd_s += h2c->dpl;
3654 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003655 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3656
Willy Tarreau39d68502018-03-02 12:26:37 +01003657 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003658 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003659 h2s->cs->flags |= CS_FL_REOS;
3660 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003661 if (htx)
3662 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003663 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003664 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003665 if (htx)
3666 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003667 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003668}
3669
Willy Tarreau5dd17352018-06-14 13:33:30 +02003670/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3671 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3672 * number of bytes sent. The caller must check the stream's status to detect
3673 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003674 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003675static 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 +02003676{
3677 struct http_hdr list[MAX_HTTP_HDR];
3678 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003679 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003680 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003681 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003682 int es_now = 0;
3683 int ret = 0;
3684 int hdr;
3685
3686 if (h2c_mux_busy(h2c, h2s)) {
3687 h2s->flags |= H2_SF_BLK_MBUSY;
3688 return 0;
3689 }
3690
Willy Tarreau44e973f2018-03-01 17:49:30 +01003691 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003692 h2c->flags |= H2_CF_MUX_MALLOC;
3693 h2s->flags |= H2_SF_BLK_MROOM;
3694 return 0;
3695 }
3696
3697 /* First, try to parse the H1 response and index it into <list>.
3698 * NOTE! Since it comes from haproxy, we *know* that a response header
3699 * block does not wrap and we can safely read it this way without
3700 * having to realign the buffer.
3701 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003702 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003703 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003704 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003705 /* incomplete or invalid response, this is abnormal coming from
3706 * haproxy and may only result in a bad errorfile or bad Lua code
3707 * so that won't be fixed, raise an error now.
3708 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003709 * FIXME: we should instead add the ability to only return a
3710 * 502 bad gateway. But in theory this is not supposed to
3711 * happen.
3712 */
3713 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3714 ret = 0;
3715 goto end;
3716 }
3717
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003718 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003719
3720 /* certain statuses have no body or an empty one, regardless of
3721 * what the headers say.
3722 */
3723 if (sl.st.status >= 100 && sl.st.status < 200) {
3724 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3725 h1m->curr_len = h1m->body_len = 0;
3726 }
3727 else if (sl.st.status == 204 || sl.st.status == 304) {
3728 /* no contents, claim c-len is present and set to zero */
3729 h1m->flags &= ~H1_MF_CHNK;
3730 h1m->flags |= H1_MF_CLEN;
3731 h1m->curr_len = h1m->body_len = 0;
3732 }
3733
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003734 chunk_reset(&outbuf);
3735
3736 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003737 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003738 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003739 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003740
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003741 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003742 break;
3743 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003744 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003745 }
3746
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003747 if (outbuf.size < 9)
3748 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003749
3750 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003751 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3752 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3753 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003754
3755 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003756 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003757 /* this is an unparsable response */
3758 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3759 ret = 0;
3760 goto end;
3761 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003762
3763 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003764 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003765 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003766 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003767 }
3768
3769 /* encode all headers, stop at empty name */
3770 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003771 /* these ones do not exist in H2 and must be dropped. */
3772 if (isteq(list[hdr].n, ist("connection")) ||
3773 isteq(list[hdr].n, ist("proxy-connection")) ||
3774 isteq(list[hdr].n, ist("keep-alive")) ||
3775 isteq(list[hdr].n, ist("upgrade")) ||
3776 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003777 continue;
3778
3779 if (isteq(list[hdr].n, ist("")))
3780 break; // end
3781
3782 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3783 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003784 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003785 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003786 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003787 }
3788 }
3789
3790 /* we may need to add END_STREAM */
3791 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3792 es_now = 1;
3793
3794 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003795 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003796
3797 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003798 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003799
3800 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003801 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003802
3803 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003804 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003805 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003806
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003807 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003808 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003809 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003810
Willy Tarreau801250e2018-09-11 11:45:04 +02003811 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003812 h2s->flags |= H2_SF_ES_SENT;
3813 if (h2s->st == H2_SS_OPEN)
3814 h2s->st = H2_SS_HLOC;
3815 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003816 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003817 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003818 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003819 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003820 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003821 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003822 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003823 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003824 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003825
3826 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003827
3828 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003829 //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 +02003830 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003831 full:
3832 h1m_init_res(h1m);
3833 h1m->err_pos = -1; // don't care about errors on the response path
3834 h2c->flags |= H2_CF_MUX_MFULL;
3835 h2s->flags |= H2_SF_BLK_MROOM;
3836 ret = 0;
3837 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003838}
3839
Willy Tarreau5dd17352018-06-14 13:33:30 +02003840/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3841 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3842 * the number of bytes sent. The caller must check the stream's status to
3843 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003844 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003845static 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 +02003846{
3847 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003848 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003849 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003850 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003851 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003852 int es_now = 0;
3853 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003854 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003855 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003856
3857 if (h2c_mux_busy(h2c, h2s)) {
3858 h2s->flags |= H2_SF_BLK_MBUSY;
3859 goto end;
3860 }
3861
Willy Tarreau44e973f2018-03-01 17:49:30 +01003862 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003863 h2c->flags |= H2_CF_MUX_MALLOC;
3864 h2s->flags |= H2_SF_BLK_MROOM;
3865 goto end;
3866 }
3867
3868 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003869 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003870 goto end;
3871
3872 chunk_reset(&outbuf);
3873
3874 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003875 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003876 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003877 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003878
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003879 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003880 break;
3881 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003882 /* If there are pending data in the output buffer, and we have
3883 * less than 1/4 of the mbuf's size and everything fits, we'll
3884 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3885 * is full and wait, to save some slow realign calls.
3886 */
3887 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3888 h2c->flags |= H2_CF_MUX_MFULL;
3889 h2s->flags |= H2_SF_BLK_MROOM;
3890 goto end;
3891 }
3892
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003893 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003894 }
3895
3896 if (outbuf.size < 9) {
3897 h2c->flags |= H2_CF_MUX_MFULL;
3898 h2s->flags |= H2_SF_BLK_MROOM;
3899 goto end;
3900 }
3901
3902 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003903 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3904 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3905 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003906
3907 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3908 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003909 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003910 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003911 break;
3912 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003913 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003914 if ((long long)size > h1m->curr_len)
3915 size = h1m->curr_len;
3916 break;
3917 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003918 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003919 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003920 if (!ret)
3921 goto end;
3922
3923 if (ret < 0) {
3924 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003925 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003926 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3927 goto end;
3928 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003929 max -= ret;
3930 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003931 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003932 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003933 }
3934
Willy Tarreau801250e2018-09-11 11:45:04 +02003935 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003936 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003937 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003938 if (!ret)
3939 goto end;
3940
3941 if (ret < 0) {
3942 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003943 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003944 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3945 goto end;
3946 }
3947
3948 size = chunk;
3949 h1m->curr_len = chunk;
3950 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003951 max -= ret;
3952 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003953 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003954 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003955 if (!size)
3956 goto send_empty;
3957 }
3958
3959 /* in MSG_DATA state, continue below */
3960 size = h1m->curr_len;
3961 break;
3962 }
3963
3964 /* we have in <size> the exact number of bytes we need to copy from
3965 * the H1 buffer. We need to check this against the connection's and
3966 * the stream's send windows, and to ensure that this fits in the max
3967 * frame size and in the buffer's available space minus 9 bytes (for
3968 * the frame header). The connection's flow control is applied last so
3969 * that we can use a separate list of streams which are immediately
3970 * unblocked on window opening. Note: we don't implement padding.
3971 */
3972
Willy Tarreau5dd17352018-06-14 13:33:30 +02003973 if (size > max)
3974 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003975
3976 if (size > h2s->mws)
3977 size = h2s->mws;
3978
3979 if (size <= 0) {
3980 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003981 if (h2s->send_wait) {
3982 LIST_DEL(&h2s->list);
3983 LIST_INIT(&h2s->list);
3984 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003985 goto end;
3986 }
3987
3988 if (h2c->mfs && size > h2c->mfs)
3989 size = h2c->mfs;
3990
3991 if (size + 9 > outbuf.size) {
3992 /* we have an opportunity for enlarging the too small
3993 * available space, let's try.
3994 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003995 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003996 goto realign_again;
3997 size = outbuf.size - 9;
3998 }
3999
4000 if (size <= 0) {
4001 h2c->flags |= H2_CF_MUX_MFULL;
4002 h2s->flags |= H2_SF_BLK_MROOM;
4003 goto end;
4004 }
4005
4006 if (size > h2c->mws)
4007 size = h2c->mws;
4008
4009 if (size <= 0) {
4010 h2s->flags |= H2_SF_BLK_MFCTL;
4011 goto end;
4012 }
4013
4014 /* copy whatever we can */
4015 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004016 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004017 if (ret == 1)
4018 len2 = 0;
4019
4020 if (!ret || len1 + len2 < size) {
4021 /* FIXME: must normally never happen */
4022 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4023 goto end;
4024 }
4025
4026 /* limit len1/len2 to size */
4027 if (len1 + len2 > size) {
4028 int sub = len1 + len2 - size;
4029
4030 if (len2 > sub)
4031 len2 -= sub;
4032 else {
4033 sub -= len2;
4034 len2 = 0;
4035 len1 -= sub;
4036 }
4037 }
4038
4039 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004040 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004041 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004042 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004043
4044 send_empty:
4045 /* we may need to add END_STREAM */
4046 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4047 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004048 *
4049 * FIXME: what we do here is not correct because we send end_stream
4050 * before knowing if we'll have to send a HEADERS frame for the
4051 * trailers. More importantly we're not consuming the trailing CRLF
4052 * after the end of trailers, so it will be left to the caller to
4053 * eat it. The right way to do it would be to measure trailers here
4054 * and to send ES only if there are no trailers.
4055 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004056 */
4057 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004058 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004059 es_now = 1;
4060
4061 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004062 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004063
4064 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004065 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004066
4067 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004068 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004069
4070 /* consume incoming H1 response */
4071 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004072 max -= size;
4073 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004074 total += size;
4075 h1m->curr_len -= size;
4076 h2s->mws -= size;
4077 h2c->mws -= size;
4078
4079 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004080 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004081 goto new_frame;
4082 }
4083 }
4084
4085 if (es_now) {
4086 if (h2s->st == H2_SS_OPEN)
4087 h2s->st = H2_SS_HLOC;
4088 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004089 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004090
Willy Tarreau35a62702018-02-27 15:37:25 +01004091 if (!(h1m->flags & H1_MF_CHNK)) {
4092 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004093 total += max;
4094 ofs += max;
4095 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004096
Willy Tarreau801250e2018-09-11 11:45:04 +02004097 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004098 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004099
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004100 h2s->flags |= H2_SF_ES_SENT;
4101 }
4102
4103 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004104 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 +02004105 return total;
4106}
4107
Willy Tarreau115e83b2018-12-01 19:17:53 +01004108/* Try to send a HEADERS frame matching HTX response present in HTX message
4109 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4110 * must check the stream's status to detect any error which might have happened
4111 * subsequently to a successful send. The htx blocks are automatically removed
4112 * from the message. The htx message is assumed to be valid since produced from
4113 * the internal code, hence it contains a start line, an optional series of
4114 * header blocks and an end of header, otherwise an invalid frame could be
4115 * emitted and the resulting htx message could be left in an inconsistent state.
4116 */
4117static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4118{
4119 struct http_hdr list[MAX_HTTP_HDR];
4120 struct h2c *h2c = h2s->h2c;
4121 struct htx_blk *blk;
4122 struct htx_blk *blk_end;
4123 struct buffer outbuf;
4124 struct htx_sl *sl;
4125 enum htx_blk_type type;
4126 int es_now = 0;
4127 int ret = 0;
4128 int hdr;
4129 int idx;
4130
4131 if (h2c_mux_busy(h2c, h2s)) {
4132 h2s->flags |= H2_SF_BLK_MBUSY;
4133 return 0;
4134 }
4135
4136 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4137 h2c->flags |= H2_CF_MUX_MALLOC;
4138 h2s->flags |= H2_SF_BLK_MROOM;
4139 return 0;
4140 }
4141
4142 /* determine the first block which must not be deleted, blk_end may
4143 * be NULL if all blocks have to be deleted.
4144 */
4145 idx = htx_get_head(htx);
4146 blk_end = NULL;
4147 while (idx != -1) {
4148 type = htx_get_blk_type(htx_get_blk(htx, idx));
4149 idx = htx_get_next(htx, idx);
4150 if (type == HTX_BLK_EOH) {
4151 if (idx != -1)
4152 blk_end = htx_get_blk(htx, idx);
4153 break;
4154 }
4155 }
4156
4157 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004158 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004159 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004160 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004161 if (h2s->status < 100 || h2s->status > 999)
4162 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004163
4164 /* and the rest of the headers, that we dump starting at header 0 */
4165 hdr = 0;
4166
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004167 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004168 while ((idx = htx_get_next(htx, idx)) != -1) {
4169 blk = htx_get_blk(htx, idx);
4170 type = htx_get_blk_type(blk);
4171
4172 if (type == HTX_BLK_UNUSED)
4173 continue;
4174
4175 if (type != HTX_BLK_HDR)
4176 break;
4177
4178 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4179 goto fail;
4180
4181 list[hdr].n = htx_get_blk_name(htx, blk);
4182 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004183 hdr++;
4184 }
4185
4186 /* marker for end of headers */
4187 list[hdr].n = ist("");
4188
4189 if (h2s->status == 204 || h2s->status == 304) {
4190 /* no contents, claim c-len is present and set to zero */
4191 es_now = 1;
4192 }
4193
4194 chunk_reset(&outbuf);
4195
4196 while (1) {
4197 outbuf.area = b_tail(&h2c->mbuf);
4198 outbuf.size = b_contig_space(&h2c->mbuf);
4199 outbuf.data = 0;
4200
4201 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4202 break;
4203 realign_again:
4204 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4205 }
4206
4207 if (outbuf.size < 9)
4208 goto full;
4209
4210 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4211 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4212 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4213 outbuf.data = 9;
4214
4215 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004216 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004217 if (b_space_wraps(&h2c->mbuf))
4218 goto realign_again;
4219 goto full;
4220 }
4221
4222 /* encode all headers, stop at empty name */
4223 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4224 /* these ones do not exist in H2 and must be dropped. */
4225 if (isteq(list[hdr].n, ist("connection")) ||
4226 isteq(list[hdr].n, ist("proxy-connection")) ||
4227 isteq(list[hdr].n, ist("keep-alive")) ||
4228 isteq(list[hdr].n, ist("upgrade")) ||
4229 isteq(list[hdr].n, ist("transfer-encoding")))
4230 continue;
4231
4232 if (isteq(list[hdr].n, ist("")))
4233 break; // end
4234
4235 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4236 /* output full */
4237 if (b_space_wraps(&h2c->mbuf))
4238 goto realign_again;
4239 goto full;
4240 }
4241 }
4242
4243 /* we may need to add END_STREAM.
4244 * FIXME: we should also set it when we know for sure that the
4245 * content-length is zero as well as on 204/304
4246 */
4247 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4248 es_now = 1;
4249
4250 if (h2s->cs->flags & CS_FL_SHW)
4251 es_now = 1;
4252
4253 /* update the frame's size */
4254 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4255
4256 if (es_now)
4257 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4258
4259 /* commit the H2 response */
4260 b_add(&h2c->mbuf, outbuf.data);
4261 h2s->flags |= H2_SF_HEADERS_SENT;
4262
Willy Tarreau115e83b2018-12-01 19:17:53 +01004263 if (es_now) {
4264 h2s->flags |= H2_SF_ES_SENT;
4265 if (h2s->st == H2_SS_OPEN)
4266 h2s->st = H2_SS_HLOC;
4267 else
4268 h2s_close(h2s);
4269 }
4270
4271 /* OK we could properly deliver the response */
4272
4273 /* remove all header blocks including the EOH and compute the
4274 * corresponding size.
4275 *
4276 * FIXME: We should remove everything when es_now is set.
4277 */
4278 ret = 0;
4279 idx = htx_get_head(htx);
4280 blk = htx_get_blk(htx, idx);
4281 while (blk != blk_end) {
4282 ret += htx_get_blksz(blk);
4283 blk = htx_remove_blk(htx, blk);
4284 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004285
4286 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4287 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004288 end:
4289 return ret;
4290 full:
4291 h2c->flags |= H2_CF_MUX_MFULL;
4292 h2s->flags |= H2_SF_BLK_MROOM;
4293 ret = 0;
4294 goto end;
4295 fail:
4296 /* unparsable HTX messages, too large ones to be produced in the local
4297 * list etc go here (unrecoverable errors).
4298 */
4299 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4300 ret = 0;
4301 goto end;
4302}
4303
Willy Tarreau80739692018-10-05 11:35:57 +02004304/* Try to send a HEADERS frame matching HTX request present in HTX message
4305 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4306 * must check the stream's status to detect any error which might have happened
4307 * subsequently to a successful send. The htx blocks are automatically removed
4308 * from the message. The htx message is assumed to be valid since produced from
4309 * the internal code, hence it contains a start line, an optional series of
4310 * header blocks and an end of header, otherwise an invalid frame could be
4311 * emitted and the resulting htx message could be left in an inconsistent state.
4312 */
4313static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4314{
4315 struct http_hdr list[MAX_HTTP_HDR];
4316 struct h2c *h2c = h2s->h2c;
4317 struct htx_blk *blk;
4318 struct htx_blk *blk_end;
4319 struct buffer outbuf;
4320 struct htx_sl *sl;
4321 struct ist meth, path;
4322 enum htx_blk_type type;
4323 int es_now = 0;
4324 int ret = 0;
4325 int hdr;
4326 int idx;
4327
4328 if (h2c_mux_busy(h2c, h2s)) {
4329 h2s->flags |= H2_SF_BLK_MBUSY;
4330 return 0;
4331 }
4332
4333 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4334 h2c->flags |= H2_CF_MUX_MALLOC;
4335 h2s->flags |= H2_SF_BLK_MROOM;
4336 return 0;
4337 }
4338
4339 /* determine the first block which must not be deleted, blk_end may
4340 * be NULL if all blocks have to be deleted.
4341 */
4342 idx = htx_get_head(htx);
4343 blk_end = NULL;
4344 while (idx != -1) {
4345 type = htx_get_blk_type(htx_get_blk(htx, idx));
4346 idx = htx_get_next(htx, idx);
4347 if (type == HTX_BLK_EOH) {
4348 if (idx != -1)
4349 blk_end = htx_get_blk(htx, idx);
4350 break;
4351 }
4352 }
4353
4354 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004355 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004356 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004357 meth = htx_sl_req_meth(sl);
4358 path = htx_sl_req_uri(sl);
4359
4360 /* and the rest of the headers, that we dump starting at header 0 */
4361 hdr = 0;
4362
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004363 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004364 while ((idx = htx_get_next(htx, idx)) != -1) {
4365 blk = htx_get_blk(htx, idx);
4366 type = htx_get_blk_type(blk);
4367
4368 if (type == HTX_BLK_UNUSED)
4369 continue;
4370
4371 if (type != HTX_BLK_HDR)
4372 break;
4373
4374 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4375 goto fail;
4376
4377 list[hdr].n = htx_get_blk_name(htx, blk);
4378 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004379 hdr++;
4380 }
4381
4382 /* marker for end of headers */
4383 list[hdr].n = ist("");
4384
4385 chunk_reset(&outbuf);
4386
4387 while (1) {
4388 outbuf.area = b_tail(&h2c->mbuf);
4389 outbuf.size = b_contig_space(&h2c->mbuf);
4390 outbuf.data = 0;
4391
4392 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4393 break;
4394 realign_again:
4395 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4396 }
4397
4398 if (outbuf.size < 9)
4399 goto full;
4400
4401 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4402 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4403 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4404 outbuf.data = 9;
4405
4406 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004407 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004408 if (b_space_wraps(&h2c->mbuf))
4409 goto realign_again;
4410 goto full;
4411 }
4412
4413 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004414 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4415 /* output full */
4416 if (b_space_wraps(&h2c->mbuf))
4417 goto realign_again;
4418 goto full;
4419 }
Willy Tarreau80739692018-10-05 11:35:57 +02004420
4421 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004422 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004423 /* output full */
4424 if (b_space_wraps(&h2c->mbuf))
4425 goto realign_again;
4426 goto full;
4427 }
4428
4429 /* encode all headers, stop at empty name */
4430 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4431 /* these ones do not exist in H2 and must be dropped. */
4432 if (isteq(list[hdr].n, ist("connection")) ||
4433 isteq(list[hdr].n, ist("proxy-connection")) ||
4434 isteq(list[hdr].n, ist("keep-alive")) ||
4435 isteq(list[hdr].n, ist("upgrade")) ||
4436 isteq(list[hdr].n, ist("transfer-encoding")))
4437 continue;
4438
4439 if (isteq(list[hdr].n, ist("")))
4440 break; // end
4441
4442 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4443 /* output full */
4444 if (b_space_wraps(&h2c->mbuf))
4445 goto realign_again;
4446 goto full;
4447 }
4448 }
4449
4450 /* we may need to add END_STREAM if we have no body :
4451 * - request already closed, or :
4452 * - no transfer-encoding, and :
4453 * - no content-length or content-length:0
4454 * Fixme: this doesn't take into account CONNECT requests.
4455 */
4456 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4457 es_now = 1;
4458
4459 if (sl->flags & HTX_SL_F_BODYLESS)
4460 es_now = 1;
4461
4462 if (h2s->cs->flags & CS_FL_SHW)
4463 es_now = 1;
4464
4465 /* update the frame's size */
4466 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4467
4468 if (es_now)
4469 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4470
4471 /* commit the H2 response */
4472 b_add(&h2c->mbuf, outbuf.data);
4473 h2s->flags |= H2_SF_HEADERS_SENT;
4474 h2s->st = H2_SS_OPEN;
4475
Willy Tarreau80739692018-10-05 11:35:57 +02004476 if (es_now) {
4477 // trim any possibly pending data (eg: inconsistent content-length)
4478 h2s->flags |= H2_SF_ES_SENT;
4479 h2s->st = H2_SS_HLOC;
4480 }
4481
4482 /* remove all header blocks including the EOH and compute the
4483 * corresponding size.
4484 *
4485 * FIXME: We should remove everything when es_now is set.
4486 */
4487 ret = 0;
4488 idx = htx_get_head(htx);
4489 blk = htx_get_blk(htx, idx);
4490 while (blk != blk_end) {
4491 ret += htx_get_blksz(blk);
4492 blk = htx_remove_blk(htx, blk);
4493 }
4494
4495 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4496 htx_remove_blk(htx, blk_end);
4497
4498 end:
4499 return ret;
4500 full:
4501 h2c->flags |= H2_CF_MUX_MFULL;
4502 h2s->flags |= H2_SF_BLK_MROOM;
4503 ret = 0;
4504 goto end;
4505 fail:
4506 /* unparsable HTX messages, too large ones to be produced in the local
4507 * list etc go here (unrecoverable errors).
4508 */
4509 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4510 ret = 0;
4511 goto end;
4512}
4513
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004514/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004515 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4516 * caller must check the stream's status to detect any error which might have
4517 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004518 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4519 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004520static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004521{
4522 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004523 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004524 struct buffer outbuf;
4525 size_t total = 0;
4526 int es_now = 0;
4527 int bsize; /* htx block size */
4528 int fsize; /* h2 frame size */
4529 struct htx_blk *blk;
4530 enum htx_blk_type type;
4531 int idx;
4532
4533 if (h2c_mux_busy(h2c, h2s)) {
4534 h2s->flags |= H2_SF_BLK_MBUSY;
4535 goto end;
4536 }
4537
4538 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4539 h2c->flags |= H2_CF_MUX_MALLOC;
4540 h2s->flags |= H2_SF_BLK_MROOM;
4541 goto end;
4542 }
4543
Willy Tarreau98de12a2018-12-12 07:03:00 +01004544 htx = htx_from_buf(buf);
4545
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004546 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4547 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4548 * the caller to handle.
4549 */
4550
4551 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004552 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004553 goto end;
4554
4555 idx = htx_get_head(htx);
4556 blk = htx_get_blk(htx, idx);
4557 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4558 bsize = htx_get_blksz(blk);
4559 fsize = bsize;
4560
4561 if (type == HTX_BLK_EOD) {
4562 /* if we have an EOD, we're dealing with chunked data. We may
4563 * have a set of trailers after us that the caller will want to
4564 * deal with. Let's simply remove the EOD and return.
4565 */
4566 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004567 total++; // EOD counts as one byte
4568 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004569 goto end;
4570 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004571 else if (type == HTX_BLK_EOM) {
4572 if (h2s->flags & H2_SF_ES_SENT) {
4573 /* ES already sent */
4574 htx_remove_blk(htx, blk);
4575 total++; // EOM counts as one byte
4576 count--;
4577 goto end;
4578 }
4579 }
4580 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004581 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004582
4583 /* Perform some optimizations to reduce the number of buffer copies.
4584 * First, if the mux's buffer is empty and the htx area contains
4585 * exactly one data block of the same size as the requested count, and
4586 * this count fits within the frame size, the stream's window size, and
4587 * the connection's window size, then it's possible to simply swap the
4588 * caller's buffer with the mux's output buffer and adjust offsets and
4589 * length to match the entire DATA HTX block in the middle. In this
4590 * case we perform a true zero-copy operation from end-to-end. This is
4591 * the situation that happens all the time with large files. Second, if
4592 * this is not possible, but the mux's output buffer is empty, we still
4593 * have an opportunity to avoid the copy to the intermediary buffer, by
4594 * making the intermediary buffer's area point to the output buffer's
4595 * area. In this case we want to skip the HTX header to make sure that
4596 * copies remain aligned and that this operation remains possible all
4597 * the time. This goes for headers, data blocks and any data extracted
4598 * from the HTX blocks.
4599 */
4600 if (unlikely(fsize == count &&
4601 htx->used == 1 && type == HTX_BLK_DATA &&
4602 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4603 void *old_area = h2c->mbuf.area;
4604
4605 if (b_data(&h2c->mbuf)) {
4606 /* too bad there are data left there. If we have less
4607 * than 1/4 of the mbuf's size and everything fits,
4608 * we'll perform a copy anyway. Otherwise we'll pretend
4609 * the mbuf is full and wait.
4610 */
4611 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4612 goto copy;
4613 h2c->flags |= H2_CF_MUX_MFULL;
4614 h2s->flags |= H2_SF_BLK_MROOM;
4615 goto end;
4616 }
4617
4618 /* map an H2 frame to the HTX block so that we can put the
4619 * frame header there.
4620 */
4621 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004622 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004623 h2c->mbuf.data = fsize + 9;
4624 outbuf.area = b_head(&h2c->mbuf);
4625
4626 /* prepend an H2 DATA frame header just before the DATA block */
4627 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4628 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4629 h2_set_frame_size(outbuf.area, fsize);
4630
4631 /* update windows */
4632 h2s->mws -= fsize;
4633 h2c->mws -= fsize;
4634
4635 /* and exchange with our old area */
4636 buf->area = old_area;
4637 buf->data = buf->head = 0;
4638 total += fsize;
4639 goto end;
4640 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004641
Willy Tarreau98de12a2018-12-12 07:03:00 +01004642 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004643 /* for DATA and EOM we'll have to emit a frame, even if empty */
4644
4645 while (1) {
4646 outbuf.area = b_tail(&h2c->mbuf);
4647 outbuf.size = b_contig_space(&h2c->mbuf);
4648 outbuf.data = 0;
4649
4650 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4651 break;
4652 realign_again:
4653 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4654 }
4655
4656 if (outbuf.size < 9) {
4657 h2c->flags |= H2_CF_MUX_MFULL;
4658 h2s->flags |= H2_SF_BLK_MROOM;
4659 goto end;
4660 }
4661
4662 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4663 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4664 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4665 outbuf.data = 9;
4666
4667 /* we have in <fsize> the exact number of bytes we need to copy from
4668 * the HTX buffer. We need to check this against the connection's and
4669 * the stream's send windows, and to ensure that this fits in the max
4670 * frame size and in the buffer's available space minus 9 bytes (for
4671 * the frame header). The connection's flow control is applied last so
4672 * that we can use a separate list of streams which are immediately
4673 * unblocked on window opening. Note: we don't implement padding.
4674 */
4675
4676 /* EOM is presented with bsize==1 but would lead to the emission of an
4677 * empty frame, thus we force it to zero here.
4678 */
4679 if (type == HTX_BLK_EOM)
4680 bsize = fsize = 0;
4681
4682 if (!fsize)
4683 goto send_empty;
4684
4685 if (h2s->mws <= 0) {
4686 h2s->flags |= H2_SF_BLK_SFCTL;
4687 if (h2s->send_wait) {
4688 LIST_DEL(&h2s->list);
4689 LIST_INIT(&h2s->list);
4690 }
4691 goto end;
4692 }
4693
Willy Tarreauee573762018-12-04 15:25:57 +01004694 if (fsize > count)
4695 fsize = count;
4696
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004697 if (fsize > h2s->mws)
4698 fsize = h2s->mws; // >0
4699
4700 if (h2c->mfs && fsize > h2c->mfs)
4701 fsize = h2c->mfs; // >0
4702
4703 if (fsize + 9 > outbuf.size) {
4704 /* we have an opportunity for enlarging the too small
4705 * available space, let's try.
4706 * FIXME: is this really interesting to do? Maybe we'll
4707 * spend lots of time realigning instead of using two
4708 * frames.
4709 */
4710 if (b_space_wraps(&h2c->mbuf))
4711 goto realign_again;
4712 fsize = outbuf.size - 9;
4713
4714 if (fsize <= 0) {
4715 /* no need to send an empty frame here */
4716 h2c->flags |= H2_CF_MUX_MFULL;
4717 h2s->flags |= H2_SF_BLK_MROOM;
4718 goto end;
4719 }
4720 }
4721
4722 if (h2c->mws <= 0) {
4723 h2s->flags |= H2_SF_BLK_MFCTL;
4724 goto end;
4725 }
4726
4727 if (fsize > h2c->mws)
4728 fsize = h2c->mws;
4729
4730 /* now let's copy this this into the output buffer */
4731 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004732 h2s->mws -= fsize;
4733 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004734 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004735
4736 send_empty:
4737 /* update the frame's size */
4738 h2_set_frame_size(outbuf.area, fsize);
4739
4740 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4741 * meeting EOM. We should optimize this later.
4742 */
4743 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004744 total++; // EOM counts as one byte
4745 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004746 es_now = 1;
4747 }
4748
4749 if (es_now)
4750 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4751
4752 /* commit the H2 response */
4753 b_add(&h2c->mbuf, fsize + 9);
4754
4755 /* consume incoming HTX block, including EOM */
4756 total += fsize;
4757 if (fsize == bsize) {
4758 htx_remove_blk(htx, blk);
4759 if (fsize)
4760 goto new_frame;
4761 } else {
4762 /* we've truncated this block */
4763 htx_cut_data_blk(htx, blk, fsize);
4764 }
4765
4766 if (es_now) {
4767 if (h2s->st == H2_SS_OPEN)
4768 h2s->st = H2_SS_HLOC;
4769 else
4770 h2s_close(h2s);
4771
4772 h2s->flags |= H2_SF_ES_SENT;
4773 }
4774
4775 end:
4776 return total;
4777}
4778
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004779/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4780 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4781 * processed. The caller must check the stream's status to detect any error
4782 * which might have happened subsequently to a successful send. The htx blocks
4783 * are automatically removed from the message. The htx message is assumed to be
4784 * valid since produced from the internal code. Processing stops when meeting
4785 * the EOM, which is also removed. All trailers are processed at once and sent
4786 * as a single frame. The ES flag is always set.
4787 */
4788static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4789{
4790 struct http_hdr list[MAX_HTTP_HDR];
4791 struct h2c *h2c = h2s->h2c;
4792 struct htx_blk *blk;
4793 struct htx_blk *blk_end;
4794 struct buffer outbuf;
4795 struct h1m h1m;
4796 enum htx_blk_type type;
4797 uint32_t size;
4798 int ret = 0;
4799 int hdr;
4800 int idx;
4801 void *start;
4802
4803 if (h2c_mux_busy(h2c, h2s)) {
4804 h2s->flags |= H2_SF_BLK_MBUSY;
4805 goto end;
4806 }
4807
4808 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4809 h2c->flags |= H2_CF_MUX_MALLOC;
4810 h2s->flags |= H2_SF_BLK_MROOM;
4811 goto end;
4812 }
4813
4814 /* The principle is that we parse each and every trailers block using
4815 * the H1 headers parser, and append it to the list. We don't proceed
4816 * until EOM is met. blk_end will point to the EOM block.
4817 */
4818 hdr = 0;
4819 memset(list, 0, sizeof(list));
4820 blk_end = NULL;
4821
4822 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4823 blk = htx_get_blk(htx, idx);
4824 type = htx_get_blk_type(blk);
4825
4826 if (type == HTX_BLK_UNUSED)
4827 continue;
4828
4829 if (type != HTX_BLK_TLR) {
4830 if (type == HTX_BLK_EOM)
4831 blk_end = blk;
4832 break;
4833 }
4834
4835 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4836 goto fail;
4837
4838 size = htx_get_blksz(blk);
4839 start = htx_get_blk_ptr(htx, blk);
4840
4841 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4842 h1m.err_pos = 0;
4843 ret = h1_headers_to_hdr_list(start, start + size,
4844 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4845 &h1m, NULL);
4846 if (ret < 0)
4847 goto fail;
4848
4849 /* ret == 0 if an incomplete trailers block was found (missing
4850 * empty line), or > 0 if it was found. We have to continue on
4851 * incomplete messages because the trailers block might be
4852 * incomplete.
4853 */
4854
4855 /* search the new end */
4856 while (hdr <= sizeof(list)/sizeof(list[0])) {
4857 if (!list[hdr].n.len)
4858 break;
4859 hdr++;
4860 }
4861 }
4862
4863 if (!blk_end)
4864 goto end; // end not found yet
4865
4866 if (!hdr)
4867 goto done;
4868
4869 chunk_reset(&outbuf);
4870
4871 while (1) {
4872 outbuf.area = b_tail(&h2c->mbuf);
4873 outbuf.size = b_contig_space(&h2c->mbuf);
4874 outbuf.data = 0;
4875
4876 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4877 break;
4878 realign_again:
4879 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4880 }
4881
4882 if (outbuf.size < 9)
4883 goto full;
4884
4885 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4886 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4887 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4888 outbuf.data = 9;
4889
4890 /* encode status, which necessarily is the first one */
4891 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
4892 if (b_space_wraps(&h2c->mbuf))
4893 goto realign_again;
4894 goto full;
4895 }
4896
4897 /* encode all headers */
4898 for (idx = 0; idx < hdr; idx++) {
4899 /* these ones do not exist in H2 or must not appear in
4900 * trailers and must be dropped.
4901 */
4902 if (isteq(list[idx].n, ist("host")) ||
4903 isteq(list[idx].n, ist("content-length")) ||
4904 isteq(list[idx].n, ist("connection")) ||
4905 isteq(list[idx].n, ist("proxy-connection")) ||
4906 isteq(list[idx].n, ist("keep-alive")) ||
4907 isteq(list[idx].n, ist("upgrade")) ||
4908 isteq(list[idx].n, ist("te")) ||
4909 isteq(list[idx].n, ist("transfer-encoding")))
4910 continue;
4911
4912 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
4913 /* output full */
4914 if (b_space_wraps(&h2c->mbuf))
4915 goto realign_again;
4916 goto full;
4917 }
4918 }
4919
4920 /* update the frame's size */
4921 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4922
4923 /* commit the H2 response */
4924 b_add(&h2c->mbuf, outbuf.data);
4925 h2s->flags |= H2_SF_ES_SENT;
4926
4927 if (h2s->st == H2_SS_OPEN)
4928 h2s->st = H2_SS_HLOC;
4929 else
4930 h2s_close(h2s);
4931
4932 /* OK we could properly deliver the response */
4933 done:
4934 /* remove all header blocks including EOM and compute the corresponding size. */
4935 ret = 0;
4936 idx = htx_get_head(htx);
4937 blk = htx_get_blk(htx, idx);
4938 while (blk != blk_end) {
4939 ret += htx_get_blksz(blk);
4940 blk = htx_remove_blk(htx, blk);
4941 }
4942 blk = htx_remove_blk(htx, blk);
4943 end:
4944 return ret;
4945 full:
4946 h2c->flags |= H2_CF_MUX_MFULL;
4947 h2s->flags |= H2_SF_BLK_MROOM;
4948 ret = 0;
4949 goto end;
4950 fail:
4951 /* unparsable HTX messages, too large ones to be produced in the local
4952 * list etc go here (unrecoverable errors).
4953 */
4954 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4955 ret = 0;
4956 goto end;
4957}
4958
Olivier Houchard6ff20392018-07-17 18:46:31 +02004959/* Called from the upper layer, to subscribe to events, such as being able to send */
4960static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4961{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004962 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004963 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004964 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004965
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004966 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004967 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004968 if (!(sw->events & SUB_RETRY_RECV)) {
4969 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004970 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004971 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004972 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004973 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004974 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004975 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004976 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004977 if (!(sw->events & SUB_RETRY_SEND)) {
4978 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004979 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004980 h2s->send_wait = sw;
4981 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4982 if (h2s->flags & H2_SF_BLK_MFCTL)
4983 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4984 else
4985 LIST_ADDQ(&h2c->send_list, &h2s->list);
4986 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004987 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004988 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004989 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004990 if (event_type != 0)
4991 return -1;
4992 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004993
4994
4995}
4996
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004997static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4998{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004999 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005000 struct h2s *h2s = cs->ctx;
5001
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005002 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005003 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005004 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005005 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005006 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005007 }
5008 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005009 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005010 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005011 if (h2s->send_wait == sw) {
5012 LIST_DEL(&h2s->list);
5013 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005014 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005015 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005016 }
5017 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005018 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5019 sw = param;
5020 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005021 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005022 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005023 LIST_DEL(&h2s->list);
5024 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005025 }
5026 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005027 return 0;
5028}
5029
5030
Olivier Houchard511efea2018-08-16 15:30:32 +02005031/* Called from the upper layer, to receive data */
5032static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5033{
Olivier Houchard638b7992018-08-16 15:41:52 +02005034 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005035 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005036 struct htx *h2s_htx = NULL;
5037 struct htx *buf_htx = NULL;
5038 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005039 size_t ret = 0;
5040
5041 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005042 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5043 /* in HTX mode we ignore the count argument */
5044 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005045 if (htx_is_empty(h2s_htx)) {
5046 if (cs->flags & CS_FL_REOS)
5047 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005048 if (cs->flags & CS_FL_ERR_PENDING)
5049 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01005050 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005051 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005052
5053 buf_htx = htx_from_buf(buf);
5054 count = htx_free_space(buf_htx);
5055
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005056 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01005057
5058 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005059 htx_to_buf(buf_htx, buf);
5060 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005061 ret = htx_ret.ret;
5062 }
5063 else {
5064 ret = b_xfer(buf, &h2s->rxbuf, count);
5065 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005066
Olivier Houchard638b7992018-08-16 15:41:52 +02005067 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005068 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005069 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005070 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005071 if (cs->flags & CS_FL_REOS)
5072 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005073 if (cs->flags & CS_FL_ERR_PENDING)
5074 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005075 if (b_size(&h2s->rxbuf)) {
5076 b_free(&h2s->rxbuf);
5077 offer_buffers(NULL, tasks_run_queue);
5078 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005079 }
5080
Willy Tarreau082f5592018-11-25 08:03:32 +01005081 if (ret && h2c->dsi == h2s->id) {
5082 /* demux is blocking on this stream's buffer */
5083 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005084 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005085 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005086end:
Olivier Houchard511efea2018-08-16 15:30:32 +02005087 return ret;
5088}
5089
Olivier Houchardd846c262018-10-19 17:24:29 +02005090static void h2_stop_senders(struct h2c *h2c)
5091{
5092 struct h2s *h2s, *h2s_back;
5093
5094 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5095 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5096 if (h2c->msi == h2s_id(h2s))
5097 continue;
5098 LIST_DEL(&h2s->list);
5099 LIST_INIT(&h2s->list);
5100 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005101 h2s->send_wait->events |= SUB_RETRY_SEND;
5102 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005103 LIST_ADD(&h2c->send_list, &h2s->list);
5104 }
5105}
5106
Willy Tarreau62f52692017-10-08 23:01:42 +02005107/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005108static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005109{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005110 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005111 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005112 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005113 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005114 struct htx *htx;
5115 struct htx_blk *blk;
5116 enum htx_blk_type btype;
5117 uint32_t bsize;
5118 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005119
Olivier Houchardd846c262018-10-19 17:24:29 +02005120 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005121 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005122 h2s->send_wait = NULL;
5123 LIST_DEL(&h2s->list);
5124 LIST_INIT(&h2s->list);
5125 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005126 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5127 return 0;
5128
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005129 /* htx will be enough to decide if we're using HTX or legacy */
5130 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5131
Willy Tarreau0bad0432018-06-14 16:54:01 +02005132 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005133 h2s->flags |= H2_SF_OUTGOING_DATA;
5134
Willy Tarreau751f2d02018-10-05 09:35:00 +02005135 if (h2s->id == 0) {
5136 int32_t id = h2c_get_next_sid(h2s->h2c);
5137
5138 if (id < 0) {
5139 cs->ctx = NULL;
5140 cs->flags |= CS_FL_ERROR;
5141 h2s_destroy(h2s);
5142 return 0;
5143 }
5144
5145 eb32_delete(&h2s->by_id);
5146 h2s->by_id.key = h2s->id = id;
5147 h2s->h2c->max_id = id;
5148 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5149 }
5150
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005151 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005152 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5153 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005154 idx = htx_get_head(htx);
5155 blk = htx_get_blk(htx, idx);
5156 btype = htx_get_blk_type(blk);
5157 bsize = htx_get_blksz(blk);
5158
5159 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005160 case HTX_BLK_REQ_SL:
5161 /* start-line before headers */
5162 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5163 if (ret > 0) {
5164 total += ret;
5165 count -= ret;
5166 if (ret < bsize)
5167 goto done;
5168 }
5169 break;
5170
Willy Tarreau115e83b2018-12-01 19:17:53 +01005171 case HTX_BLK_RES_SL:
5172 /* start-line before headers */
5173 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5174 if (ret > 0) {
5175 total += ret;
5176 count -= ret;
5177 if (ret < bsize)
5178 goto done;
5179 }
5180 break;
5181
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005182 case HTX_BLK_DATA:
5183 case HTX_BLK_EOD:
5184 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005185 /* all these cause the emission of a DATA frame (possibly empty).
5186 * This EOM necessarily is one before trailers, as the EOM following
5187 * trailers would have been consumed by the trailers parser.
5188 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005189 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005190 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005191 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005192 total += ret;
5193 count -= ret;
5194 if (ret < bsize)
5195 goto done;
5196 }
5197 break;
5198
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005199 case HTX_BLK_TLR:
5200 /* This is the first trailers block, all the subsequent ones AND
5201 * the EOM will be swallowed by the parser.
5202 */
5203 ret = h2s_htx_make_trailers(h2s, htx);
5204 if (ret > 0) {
5205 total += ret;
5206 count -= ret;
5207 if (ret < bsize)
5208 goto done;
5209 }
5210 break;
5211
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005212 default:
5213 htx_remove_blk(htx, blk);
5214 total += bsize;
5215 count -= bsize;
5216 break;
5217 }
5218 }
5219 goto done;
5220 }
5221
5222 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005223 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005224 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005225 if (h2s->h2c->flags & H2_CF_IS_BACK)
5226 ret = -1;
5227 else
5228 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005229 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005230 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005231 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005232 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005233 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005234 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005235 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005236
Willy Tarreau5dd17352018-06-14 13:33:30 +02005237 if (unlikely((int)ret <= 0)) {
5238 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005239 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5240 break;
5241 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005242 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005243 total += count;
5244 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005245 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005246 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005247 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005248 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005249 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005250 break;
5251 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005252
5253 total += ret;
5254 count -= ret;
5255
5256 if (h2s->st >= H2_SS_ERROR)
5257 break;
5258
5259 if (h2s->flags & H2_SF_BLK_ANY)
5260 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005261 }
5262
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005263 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005264 if (h2s->st >= H2_SS_ERROR) {
5265 /* trim any possibly pending data after we close (extra CR-LF,
5266 * unprocessed trailers, abnormal extra data, ...)
5267 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005268 total += count;
5269 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005270 }
5271
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005272 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005273 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005274 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005275 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005276 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005277 }
5278
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005279 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005280 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005281 } else {
5282 b_del(buf, total);
5283 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005284
5285 /* The mux is full, cancel the pending tasks */
5286 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5287 (h2s->flags & H2_SF_BLK_MBUSY))
5288 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005289
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005290 /* If we're running HTX, and we read the whole buffer, then pretend
5291 * we read exactly what the caller specified, as with HTX the caller
5292 * will always give the buffer size, instead of the amount of data
5293 * available.
5294 */
5295 if (htx && !b_data(buf))
5296 total = orig_count;
5297
Olivier Houchard7505f942018-08-21 18:10:44 +02005298 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005299 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005300 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005301
Olivier Houchard7505f942018-08-21 18:10:44 +02005302 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005303 /* If we're waiting for flow control, and we got a shutr on the
5304 * connection, we will never be unlocked, so add an error on
5305 * the conn_stream.
5306 */
5307 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5308 !b_data(&h2s->h2c->dbuf) &&
5309 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5310 if (cs->flags & CS_FL_EOS)
5311 cs->flags |= CS_FL_ERROR;
5312 else
5313 cs->flags |= CS_FL_ERR_PENDING;
5314 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005315 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005316}
5317
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005318/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005319static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005320{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005321 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005322 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005323 struct eb32_node *node;
5324 int fctl_cnt = 0;
5325 int send_cnt = 0;
5326 int tree_cnt = 0;
5327 int orph_cnt = 0;
5328
5329 if (!h2c)
5330 return;
5331
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005332 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005333 fctl_cnt++;
5334
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005335 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005336 send_cnt++;
5337
Willy Tarreau3af37712018-12-18 14:34:41 +01005338 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005339 node = eb32_first(&h2c->streams_by_id);
5340 while (node) {
5341 h2s = container_of(node, struct h2s, by_id);
5342 tree_cnt++;
5343 if (!h2s->cs)
5344 orph_cnt++;
5345 node = eb32_next(node);
5346 }
5347
Willy Tarreau987c0632018-12-18 10:32:05 +01005348 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5349 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5350 " .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 +02005351 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5352 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005353 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005354 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5355 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5356 h2c->msi,
5357 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5358 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5359
5360 if (h2s) {
5361 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5362 h2s, h2s->id, h2s->flags,
5363 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5364 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5365 h2s->cs);
5366 if (h2s->cs)
5367 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5368 h2s->cs->flags, h2s->cs->data);
5369 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005370}
Willy Tarreau62f52692017-10-08 23:01:42 +02005371
5372/*******************************************************/
5373/* functions below are dedicated to the config parsers */
5374/*******************************************************/
5375
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005376/* config parser for global "tune.h2.header-table-size" */
5377static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5378 struct proxy *defpx, const char *file, int line,
5379 char **err)
5380{
5381 if (too_many_args(1, args, err, NULL))
5382 return -1;
5383
5384 h2_settings_header_table_size = atoi(args[1]);
5385 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5386 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5387 return -1;
5388 }
5389 return 0;
5390}
Willy Tarreau62f52692017-10-08 23:01:42 +02005391
Willy Tarreaue6baec02017-07-27 11:45:11 +02005392/* config parser for global "tune.h2.initial-window-size" */
5393static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5394 struct proxy *defpx, const char *file, int line,
5395 char **err)
5396{
5397 if (too_many_args(1, args, err, NULL))
5398 return -1;
5399
5400 h2_settings_initial_window_size = atoi(args[1]);
5401 if (h2_settings_initial_window_size < 0) {
5402 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5403 return -1;
5404 }
5405 return 0;
5406}
5407
Willy Tarreau5242ef82017-07-27 11:47:28 +02005408/* config parser for global "tune.h2.max-concurrent-streams" */
5409static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5410 struct proxy *defpx, const char *file, int line,
5411 char **err)
5412{
5413 if (too_many_args(1, args, err, NULL))
5414 return -1;
5415
5416 h2_settings_max_concurrent_streams = atoi(args[1]);
5417 if (h2_settings_max_concurrent_streams < 0) {
5418 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5419 return -1;
5420 }
5421 return 0;
5422}
5423
Willy Tarreau62f52692017-10-08 23:01:42 +02005424
5425/****************************************/
5426/* MUX initialization and instanciation */
5427/***************************************/
5428
5429/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005430static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005431 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005432 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005433 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005434 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005435 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005436 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005437 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005438 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005439 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005440 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005441 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005442 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005443 .shutr = h2_shutr,
5444 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005445 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005446 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005447 .name = "H2",
5448};
5449
Christopher Faulet32f61c02018-04-10 14:33:41 +02005450/* PROTO selection : this mux registers PROTO token "h2" */
5451static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005452 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005453
Willy Tarreau0108d902018-11-25 19:14:37 +01005454INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5455
Willy Tarreauf8957272018-10-03 10:25:20 +02005456static struct mux_proto_list mux_proto_h2_htx =
5457 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5458
5459INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5460
Willy Tarreau62f52692017-10-08 23:01:42 +02005461/* config keyword parsers */
5462static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005463 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005464 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005465 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005466 { 0, NULL, NULL }
5467}};
5468
Willy Tarreau0108d902018-11-25 19:14:37 +01005469INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);