blob: dc67bc6730e60a6f6a66fa9ae6faa517b629c606 [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 Tarreau13278b42017-10-13 19:23:14 +02003319 hdrs += 5; // stream dep = 4, weight = 1
3320 flen -= 5;
3321 }
3322
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003323 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003324 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003325 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003326 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003327
Willy Tarreau937f7602018-02-26 15:22:17 +01003328 /* we can't retry a failed decompression operation so we must be very
3329 * careful not to take any risks. In practice the output buffer is
3330 * always empty except maybe for trailers, in which case we simply have
3331 * to wait for the upper layer to finish consuming what is available.
3332 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003333
3334 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003335 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003336 if (!htx_is_empty(htx)) {
3337 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003338 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003339 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003340 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003341 if (b_data(rxbuf)) {
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 Tarreau59a10fb2017-11-21 20:03:02 +01003345
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003346 rxbuf->head = 0;
3347 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003348 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003349
Willy Tarreau25919232019-01-03 14:48:18 +01003350 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003351 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3352 sizeof(list)/sizeof(list[0]), tmp);
3353 if (outlen < 0) {
3354 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3355 goto fail;
3356 }
3357
Willy Tarreau25919232019-01-03 14:48:18 +01003358 /* The PACK decompressor was updated, let's update the input buffer and
3359 * the parser's state to commit these changes and allow us to later
3360 * fail solely on the stream if needed.
3361 */
3362 b_del(&h2c->dbuf, h2c->dfl + hole);
3363 h2c->dfl = hole = 0;
3364 h2c->st0 = H2_CS_FRAME_H;
3365
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003366 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003367 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003368
Willy Tarreau88d138e2019-01-02 19:38:14 +01003369 if (*flags & H2_SF_HEADERS_RCVD)
3370 goto trailers;
3371
3372 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003373 if (htx) {
3374 /* HTX mode */
3375 if (h2c->flags & H2_CF_IS_BACK)
3376 outlen = h2_make_htx_response(list, htx, &msgf);
3377 else
3378 outlen = h2_make_htx_request(list, htx, &msgf);
3379 } else {
3380 /* HTTP/1 mode */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003381 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf);
Willy Tarreau83195932019-01-03 10:26:23 +01003382 if (outlen > 0)
3383 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003384 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003385
3386 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003387 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003388 goto fail;
3389 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003390
Willy Tarreau174b06a2018-04-25 18:13:58 +02003391 if (msgf & H2_MSGF_BODY) {
3392 /* a payload is present */
3393 if (msgf & H2_MSGF_BODY_CL)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003394 *flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003395 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003396 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003397 }
3398
Willy Tarreau88d138e2019-01-02 19:38:14 +01003399 done:
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003400 /* indicate that a HEADERS frame was received for this stream */
3401 *flags |= H2_SF_HEADERS_RCVD;
3402
Willy Tarreau88d138e2019-01-02 19:38:14 +01003403 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
3404 /* Mark the end of message, either using EOM in HTX or with the
3405 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3406 * is not set during headers with END_STREAM.
3407 */
3408 if (htx) {
3409 if (!htx_add_endof(htx, HTX_BLK_EOM))
3410 goto fail;
3411 }
3412 else if (*flags & H2_SF_DATA_CHNK) {
3413 if (!b_putblk(rxbuf, "\r\n", 2))
3414 goto fail;
3415 }
3416 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003417
Willy Tarreau86277d42019-01-02 15:36:11 +01003418 /* success */
3419 ret = 1;
3420
Willy Tarreau68dd9852017-07-03 14:44:26 +02003421 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003422 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003423 * move the remaining data over it.
3424 */
3425 if (hole) {
3426 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3427 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3428 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3429 b_sub(&h2c->dbuf, hole);
3430 }
3431
3432 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3433 /* too large frames */
3434 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003435 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003436 }
3437
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003438 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003439 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003440 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003441 return ret;
3442
Willy Tarreau68dd9852017-07-03 14:44:26 +02003443 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003444 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003445 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003446
3447 trailers:
3448 /* This is the last HEADERS frame hence a trailer */
3449
3450 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3451 /* It's a trailer but it's missing ES flag */
3452 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3453 goto fail;
3454 }
3455
3456 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3457 * block, and when using chunks we must send the 0 CRLF marker. For
3458 * other modes, the trailers are silently dropped.
3459 */
3460 if (htx) {
3461 if (!htx_add_endof(htx, HTX_BLK_EOD))
3462 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003463 if (h2_make_htx_trailers(list, htx) <= 0)
3464 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003465 }
3466 else if (*flags & H2_SF_DATA_CHNK) {
3467 /* Legacy mode with chunked encoding : we must finalize the
3468 * data block message emit the trailing CRLF */
3469 if (!b_putblk(rxbuf, "0\r\n", 3))
3470 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003471
3472 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3473 if (outlen > 0)
3474 b_add(rxbuf, outlen);
3475 else
3476 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003477 }
3478
3479 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003480}
3481
Willy Tarreau454f9052017-10-26 19:40:35 +02003482/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3483 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3484 * in use, a new chunk is emitted for each frame. This is supposed to fit
3485 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3486 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3487 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003488 * parser state is automatically updated. Returns > 0 if it could completely
3489 * send the current frame, 0 if it couldn't complete, in which case
3490 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3491 * DATA frame can return 0 as a valid result). Stream errors are reported in
3492 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3493 * have checked the frame header and ensured that the frame was complete or the
3494 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003495 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003496static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003497{
3498 struct h2c *h2c = h2s->h2c;
3499 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003500 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003501 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003502 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003503 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003504
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003505 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003506
Olivier Houchard638b7992018-08-16 15:41:52 +02003507 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003508 if (!csbuf) {
3509 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003510 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003511 }
3512
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003513try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003514 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003515 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3516 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003517 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003518 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003519
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003520 if (flen > b_data(&h2c->dbuf)) {
3521 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003522 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003523 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003524 }
3525
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003526 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003527 block1 = htx_free_data_space(htx);
3528 if (!block1) {
3529 h2c->flags |= H2_CF_DEM_SFULL;
3530 goto fail;
3531 }
3532 if (flen > block1)
3533 flen = block1;
3534
3535 /* here, flen is the max we can copy into the output buffer */
3536 block1 = b_contig_data(&h2c->dbuf, 0);
3537 if (flen > block1)
3538 flen = block1;
3539
3540 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3541 h2c->flags |= H2_CF_DEM_SFULL;
3542 goto fail;
3543 }
3544
3545 b_del(&h2c->dbuf, flen);
3546 h2c->dfl -= flen;
3547 h2c->rcvd_c += flen;
3548 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3549 goto try_again;
3550 }
3551 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003552 /* it doesn't fit and the buffer is fragmented,
3553 * so let's defragment it and try again.
3554 */
3555 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003556 }
3557
Willy Tarreaueba10f22018-04-25 20:44:22 +02003558 /* chunked-encoding requires more room */
3559 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003560 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003561 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3562 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3563 (chklen < 1048576) ? 4 : 8;
3564 chklen += 4; // CRLF, CRLF
3565 }
3566
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003567 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003568 if (flen + chklen > b_room(csbuf)) {
3569 if (chklen >= b_room(csbuf)) {
3570 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003571 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003572 }
3573 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003574 }
3575
3576 if (h2s->flags & H2_SF_DATA_CHNK) {
3577 /* emit the chunk size */
3578 unsigned int chksz = flen;
3579 char str[10];
3580 char *beg;
3581
3582 beg = str + sizeof(str);
3583 *--beg = '\n';
3584 *--beg = '\r';
3585 do {
3586 *--beg = hextab[chksz & 0xF];
3587 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003588 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003589 }
3590
Willy Tarreau454f9052017-10-26 19:40:35 +02003591 /* Block1 is the length of the first block before the buffer wraps,
3592 * block2 is the optional second block to reach the end of the frame.
3593 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003594 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003595 if (block1 > flen)
3596 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003597 block2 = flen - block1;
3598
3599 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003600 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003601
3602 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003603 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003604
Willy Tarreaueba10f22018-04-25 20:44:22 +02003605 if (h2s->flags & H2_SF_DATA_CHNK) {
3606 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003607 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003608 }
3609
Willy Tarreau454f9052017-10-26 19:40:35 +02003610 /* now mark the input data as consumed (will be deleted from the buffer
3611 * by the caller when seeing FRAME_A after sending the window update).
3612 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003613 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003614 h2c->dfl -= flen;
3615 h2c->rcvd_c += flen;
3616 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3617
3618 if (h2c->dfl > h2c->dpl) {
3619 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003620 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003621 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003622 }
3623
Willy Tarreau4a28da12018-01-04 14:41:00 +01003624 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003625 /* here we're done with the frame, all the payload (except padding) was
3626 * transferred.
3627 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003628
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003629 if (h2c->dff & H2_F_DATA_END_STREAM) {
3630 if (htx) {
3631 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3632 h2c->flags |= H2_CF_DEM_SFULL;
3633 goto fail;
3634 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003635 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003636 else if (h2s->flags & H2_SF_DATA_CHNK) {
3637 /* emit the trailing 0 CRLF CRLF */
3638 if (b_room(csbuf) < 5) {
3639 h2c->flags |= H2_CF_DEM_SFULL;
3640 goto fail;
3641 }
3642 chklen += 5;
3643 b_putblk(csbuf, "0\r\n\r\n", 5);
3644 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003645 }
3646
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003647 h2c->rcvd_c += h2c->dpl;
3648 h2c->rcvd_s += h2c->dpl;
3649 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003650 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3651
Willy Tarreau39d68502018-03-02 12:26:37 +01003652 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003653 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003654 h2s->cs->flags |= CS_FL_REOS;
3655 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003656 if (htx)
3657 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003658 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003659 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003660 if (htx)
3661 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003662 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003663}
3664
Willy Tarreau5dd17352018-06-14 13:33:30 +02003665/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3666 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3667 * number of bytes sent. The caller must check the stream's status to detect
3668 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003669 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003670static 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 +02003671{
3672 struct http_hdr list[MAX_HTTP_HDR];
3673 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003674 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003675 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003676 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003677 int es_now = 0;
3678 int ret = 0;
3679 int hdr;
3680
3681 if (h2c_mux_busy(h2c, h2s)) {
3682 h2s->flags |= H2_SF_BLK_MBUSY;
3683 return 0;
3684 }
3685
Willy Tarreau44e973f2018-03-01 17:49:30 +01003686 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003687 h2c->flags |= H2_CF_MUX_MALLOC;
3688 h2s->flags |= H2_SF_BLK_MROOM;
3689 return 0;
3690 }
3691
3692 /* First, try to parse the H1 response and index it into <list>.
3693 * NOTE! Since it comes from haproxy, we *know* that a response header
3694 * block does not wrap and we can safely read it this way without
3695 * having to realign the buffer.
3696 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003697 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003698 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003699 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003700 /* incomplete or invalid response, this is abnormal coming from
3701 * haproxy and may only result in a bad errorfile or bad Lua code
3702 * so that won't be fixed, raise an error now.
3703 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003704 * FIXME: we should instead add the ability to only return a
3705 * 502 bad gateway. But in theory this is not supposed to
3706 * happen.
3707 */
3708 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3709 ret = 0;
3710 goto end;
3711 }
3712
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003713 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003714
3715 /* certain statuses have no body or an empty one, regardless of
3716 * what the headers say.
3717 */
3718 if (sl.st.status >= 100 && sl.st.status < 200) {
3719 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3720 h1m->curr_len = h1m->body_len = 0;
3721 }
3722 else if (sl.st.status == 204 || sl.st.status == 304) {
3723 /* no contents, claim c-len is present and set to zero */
3724 h1m->flags &= ~H1_MF_CHNK;
3725 h1m->flags |= H1_MF_CLEN;
3726 h1m->curr_len = h1m->body_len = 0;
3727 }
3728
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003729 chunk_reset(&outbuf);
3730
3731 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003732 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003733 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003734 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003735
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003736 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003737 break;
3738 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003739 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003740 }
3741
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003742 if (outbuf.size < 9)
3743 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003744
3745 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003746 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3747 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3748 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003749
3750 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003751 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003752 /* this is an unparsable response */
3753 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3754 ret = 0;
3755 goto end;
3756 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003757
3758 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003759 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003760 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003761 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003762 }
3763
3764 /* encode all headers, stop at empty name */
3765 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003766 /* these ones do not exist in H2 and must be dropped. */
3767 if (isteq(list[hdr].n, ist("connection")) ||
3768 isteq(list[hdr].n, ist("proxy-connection")) ||
3769 isteq(list[hdr].n, ist("keep-alive")) ||
3770 isteq(list[hdr].n, ist("upgrade")) ||
3771 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003772 continue;
3773
3774 if (isteq(list[hdr].n, ist("")))
3775 break; // end
3776
3777 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3778 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003779 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003780 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003781 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003782 }
3783 }
3784
3785 /* we may need to add END_STREAM */
3786 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3787 es_now = 1;
3788
3789 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003790 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003791
3792 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003793 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003794
3795 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003796 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003797
3798 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003799 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003800 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003801
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003802 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003803 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003804 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003805
Willy Tarreau801250e2018-09-11 11:45:04 +02003806 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003807 h2s->flags |= H2_SF_ES_SENT;
3808 if (h2s->st == H2_SS_OPEN)
3809 h2s->st = H2_SS_HLOC;
3810 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003811 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003812 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003813 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003814 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003815 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003816 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003817 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003818 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003819 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003820
3821 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003822
3823 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003824 //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 +02003825 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003826 full:
3827 h1m_init_res(h1m);
3828 h1m->err_pos = -1; // don't care about errors on the response path
3829 h2c->flags |= H2_CF_MUX_MFULL;
3830 h2s->flags |= H2_SF_BLK_MROOM;
3831 ret = 0;
3832 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003833}
3834
Willy Tarreau5dd17352018-06-14 13:33:30 +02003835/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3836 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3837 * the number of bytes sent. The caller must check the stream's status to
3838 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003839 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003840static 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 +02003841{
3842 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003843 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003844 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003845 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003846 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003847 int es_now = 0;
3848 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003849 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003850 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003851
3852 if (h2c_mux_busy(h2c, h2s)) {
3853 h2s->flags |= H2_SF_BLK_MBUSY;
3854 goto end;
3855 }
3856
Willy Tarreau44e973f2018-03-01 17:49:30 +01003857 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003858 h2c->flags |= H2_CF_MUX_MALLOC;
3859 h2s->flags |= H2_SF_BLK_MROOM;
3860 goto end;
3861 }
3862
3863 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003864 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003865 goto end;
3866
3867 chunk_reset(&outbuf);
3868
3869 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003870 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003871 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003872 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003873
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003874 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003875 break;
3876 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003877 /* If there are pending data in the output buffer, and we have
3878 * less than 1/4 of the mbuf's size and everything fits, we'll
3879 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3880 * is full and wait, to save some slow realign calls.
3881 */
3882 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3883 h2c->flags |= H2_CF_MUX_MFULL;
3884 h2s->flags |= H2_SF_BLK_MROOM;
3885 goto end;
3886 }
3887
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003888 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003889 }
3890
3891 if (outbuf.size < 9) {
3892 h2c->flags |= H2_CF_MUX_MFULL;
3893 h2s->flags |= H2_SF_BLK_MROOM;
3894 goto end;
3895 }
3896
3897 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003898 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3899 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3900 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003901
3902 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3903 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003904 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003905 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003906 break;
3907 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003908 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003909 if ((long long)size > h1m->curr_len)
3910 size = h1m->curr_len;
3911 break;
3912 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003913 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003914 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003915 if (!ret)
3916 goto end;
3917
3918 if (ret < 0) {
3919 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003920 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003921 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3922 goto end;
3923 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003924 max -= ret;
3925 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003926 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003927 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003928 }
3929
Willy Tarreau801250e2018-09-11 11:45:04 +02003930 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003931 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003932 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003933 if (!ret)
3934 goto end;
3935
3936 if (ret < 0) {
3937 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003938 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003939 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3940 goto end;
3941 }
3942
3943 size = chunk;
3944 h1m->curr_len = chunk;
3945 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003946 max -= ret;
3947 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003948 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003949 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003950 if (!size)
3951 goto send_empty;
3952 }
3953
3954 /* in MSG_DATA state, continue below */
3955 size = h1m->curr_len;
3956 break;
3957 }
3958
3959 /* we have in <size> the exact number of bytes we need to copy from
3960 * the H1 buffer. We need to check this against the connection's and
3961 * the stream's send windows, and to ensure that this fits in the max
3962 * frame size and in the buffer's available space minus 9 bytes (for
3963 * the frame header). The connection's flow control is applied last so
3964 * that we can use a separate list of streams which are immediately
3965 * unblocked on window opening. Note: we don't implement padding.
3966 */
3967
Willy Tarreau5dd17352018-06-14 13:33:30 +02003968 if (size > max)
3969 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003970
3971 if (size > h2s->mws)
3972 size = h2s->mws;
3973
3974 if (size <= 0) {
3975 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003976 if (h2s->send_wait) {
3977 LIST_DEL(&h2s->list);
3978 LIST_INIT(&h2s->list);
3979 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003980 goto end;
3981 }
3982
3983 if (h2c->mfs && size > h2c->mfs)
3984 size = h2c->mfs;
3985
3986 if (size + 9 > outbuf.size) {
3987 /* we have an opportunity for enlarging the too small
3988 * available space, let's try.
3989 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003990 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003991 goto realign_again;
3992 size = outbuf.size - 9;
3993 }
3994
3995 if (size <= 0) {
3996 h2c->flags |= H2_CF_MUX_MFULL;
3997 h2s->flags |= H2_SF_BLK_MROOM;
3998 goto end;
3999 }
4000
4001 if (size > h2c->mws)
4002 size = h2c->mws;
4003
4004 if (size <= 0) {
4005 h2s->flags |= H2_SF_BLK_MFCTL;
4006 goto end;
4007 }
4008
4009 /* copy whatever we can */
4010 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004011 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004012 if (ret == 1)
4013 len2 = 0;
4014
4015 if (!ret || len1 + len2 < size) {
4016 /* FIXME: must normally never happen */
4017 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4018 goto end;
4019 }
4020
4021 /* limit len1/len2 to size */
4022 if (len1 + len2 > size) {
4023 int sub = len1 + len2 - size;
4024
4025 if (len2 > sub)
4026 len2 -= sub;
4027 else {
4028 sub -= len2;
4029 len2 = 0;
4030 len1 -= sub;
4031 }
4032 }
4033
4034 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004035 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004036 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004037 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004038
4039 send_empty:
4040 /* we may need to add END_STREAM */
4041 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4042 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004043 *
4044 * FIXME: what we do here is not correct because we send end_stream
4045 * before knowing if we'll have to send a HEADERS frame for the
4046 * trailers. More importantly we're not consuming the trailing CRLF
4047 * after the end of trailers, so it will be left to the caller to
4048 * eat it. The right way to do it would be to measure trailers here
4049 * and to send ES only if there are no trailers.
4050 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004051 */
4052 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004053 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004054 es_now = 1;
4055
4056 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004057 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004058
4059 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004060 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004061
4062 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004063 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004064
4065 /* consume incoming H1 response */
4066 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004067 max -= size;
4068 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004069 total += size;
4070 h1m->curr_len -= size;
4071 h2s->mws -= size;
4072 h2c->mws -= size;
4073
4074 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004075 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004076 goto new_frame;
4077 }
4078 }
4079
4080 if (es_now) {
4081 if (h2s->st == H2_SS_OPEN)
4082 h2s->st = H2_SS_HLOC;
4083 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004084 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004085
Willy Tarreau35a62702018-02-27 15:37:25 +01004086 if (!(h1m->flags & H1_MF_CHNK)) {
4087 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004088 total += max;
4089 ofs += max;
4090 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004091
Willy Tarreau801250e2018-09-11 11:45:04 +02004092 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004093 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004094
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004095 h2s->flags |= H2_SF_ES_SENT;
4096 }
4097
4098 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004099 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 +02004100 return total;
4101}
4102
Willy Tarreau115e83b2018-12-01 19:17:53 +01004103/* Try to send a HEADERS frame matching HTX response present in HTX message
4104 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4105 * must check the stream's status to detect any error which might have happened
4106 * subsequently to a successful send. The htx blocks are automatically removed
4107 * from the message. The htx message is assumed to be valid since produced from
4108 * the internal code, hence it contains a start line, an optional series of
4109 * header blocks and an end of header, otherwise an invalid frame could be
4110 * emitted and the resulting htx message could be left in an inconsistent state.
4111 */
4112static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4113{
4114 struct http_hdr list[MAX_HTTP_HDR];
4115 struct h2c *h2c = h2s->h2c;
4116 struct htx_blk *blk;
4117 struct htx_blk *blk_end;
4118 struct buffer outbuf;
4119 struct htx_sl *sl;
4120 enum htx_blk_type type;
4121 int es_now = 0;
4122 int ret = 0;
4123 int hdr;
4124 int idx;
4125
4126 if (h2c_mux_busy(h2c, h2s)) {
4127 h2s->flags |= H2_SF_BLK_MBUSY;
4128 return 0;
4129 }
4130
4131 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4132 h2c->flags |= H2_CF_MUX_MALLOC;
4133 h2s->flags |= H2_SF_BLK_MROOM;
4134 return 0;
4135 }
4136
4137 /* determine the first block which must not be deleted, blk_end may
4138 * be NULL if all blocks have to be deleted.
4139 */
4140 idx = htx_get_head(htx);
4141 blk_end = NULL;
4142 while (idx != -1) {
4143 type = htx_get_blk_type(htx_get_blk(htx, idx));
4144 idx = htx_get_next(htx, idx);
4145 if (type == HTX_BLK_EOH) {
4146 if (idx != -1)
4147 blk_end = htx_get_blk(htx, idx);
4148 break;
4149 }
4150 }
4151
4152 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004153 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004154 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004155 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004156 if (h2s->status < 100 || h2s->status > 999)
4157 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004158
4159 /* and the rest of the headers, that we dump starting at header 0 */
4160 hdr = 0;
4161
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004162 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004163 while ((idx = htx_get_next(htx, idx)) != -1) {
4164 blk = htx_get_blk(htx, idx);
4165 type = htx_get_blk_type(blk);
4166
4167 if (type == HTX_BLK_UNUSED)
4168 continue;
4169
4170 if (type != HTX_BLK_HDR)
4171 break;
4172
4173 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4174 goto fail;
4175
4176 list[hdr].n = htx_get_blk_name(htx, blk);
4177 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004178 hdr++;
4179 }
4180
4181 /* marker for end of headers */
4182 list[hdr].n = ist("");
4183
4184 if (h2s->status == 204 || h2s->status == 304) {
4185 /* no contents, claim c-len is present and set to zero */
4186 es_now = 1;
4187 }
4188
4189 chunk_reset(&outbuf);
4190
4191 while (1) {
4192 outbuf.area = b_tail(&h2c->mbuf);
4193 outbuf.size = b_contig_space(&h2c->mbuf);
4194 outbuf.data = 0;
4195
4196 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4197 break;
4198 realign_again:
4199 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4200 }
4201
4202 if (outbuf.size < 9)
4203 goto full;
4204
4205 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4206 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4207 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4208 outbuf.data = 9;
4209
4210 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004211 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004212 if (b_space_wraps(&h2c->mbuf))
4213 goto realign_again;
4214 goto full;
4215 }
4216
4217 /* encode all headers, stop at empty name */
4218 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4219 /* these ones do not exist in H2 and must be dropped. */
4220 if (isteq(list[hdr].n, ist("connection")) ||
4221 isteq(list[hdr].n, ist("proxy-connection")) ||
4222 isteq(list[hdr].n, ist("keep-alive")) ||
4223 isteq(list[hdr].n, ist("upgrade")) ||
4224 isteq(list[hdr].n, ist("transfer-encoding")))
4225 continue;
4226
4227 if (isteq(list[hdr].n, ist("")))
4228 break; // end
4229
4230 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4231 /* output full */
4232 if (b_space_wraps(&h2c->mbuf))
4233 goto realign_again;
4234 goto full;
4235 }
4236 }
4237
4238 /* we may need to add END_STREAM.
4239 * FIXME: we should also set it when we know for sure that the
4240 * content-length is zero as well as on 204/304
4241 */
4242 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4243 es_now = 1;
4244
4245 if (h2s->cs->flags & CS_FL_SHW)
4246 es_now = 1;
4247
4248 /* update the frame's size */
4249 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4250
4251 if (es_now)
4252 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4253
4254 /* commit the H2 response */
4255 b_add(&h2c->mbuf, outbuf.data);
4256 h2s->flags |= H2_SF_HEADERS_SENT;
4257
Willy Tarreau115e83b2018-12-01 19:17:53 +01004258 if (es_now) {
4259 h2s->flags |= H2_SF_ES_SENT;
4260 if (h2s->st == H2_SS_OPEN)
4261 h2s->st = H2_SS_HLOC;
4262 else
4263 h2s_close(h2s);
4264 }
4265
4266 /* OK we could properly deliver the response */
4267
4268 /* remove all header blocks including the EOH and compute the
4269 * corresponding size.
4270 *
4271 * FIXME: We should remove everything when es_now is set.
4272 */
4273 ret = 0;
4274 idx = htx_get_head(htx);
4275 blk = htx_get_blk(htx, idx);
4276 while (blk != blk_end) {
4277 ret += htx_get_blksz(blk);
4278 blk = htx_remove_blk(htx, blk);
4279 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004280
4281 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4282 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004283 end:
4284 return ret;
4285 full:
4286 h2c->flags |= H2_CF_MUX_MFULL;
4287 h2s->flags |= H2_SF_BLK_MROOM;
4288 ret = 0;
4289 goto end;
4290 fail:
4291 /* unparsable HTX messages, too large ones to be produced in the local
4292 * list etc go here (unrecoverable errors).
4293 */
4294 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4295 ret = 0;
4296 goto end;
4297}
4298
Willy Tarreau80739692018-10-05 11:35:57 +02004299/* Try to send a HEADERS frame matching HTX request present in HTX message
4300 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4301 * must check the stream's status to detect any error which might have happened
4302 * subsequently to a successful send. The htx blocks are automatically removed
4303 * from the message. The htx message is assumed to be valid since produced from
4304 * the internal code, hence it contains a start line, an optional series of
4305 * header blocks and an end of header, otherwise an invalid frame could be
4306 * emitted and the resulting htx message could be left in an inconsistent state.
4307 */
4308static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4309{
4310 struct http_hdr list[MAX_HTTP_HDR];
4311 struct h2c *h2c = h2s->h2c;
4312 struct htx_blk *blk;
4313 struct htx_blk *blk_end;
4314 struct buffer outbuf;
4315 struct htx_sl *sl;
4316 struct ist meth, path;
4317 enum htx_blk_type type;
4318 int es_now = 0;
4319 int ret = 0;
4320 int hdr;
4321 int idx;
4322
4323 if (h2c_mux_busy(h2c, h2s)) {
4324 h2s->flags |= H2_SF_BLK_MBUSY;
4325 return 0;
4326 }
4327
4328 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4329 h2c->flags |= H2_CF_MUX_MALLOC;
4330 h2s->flags |= H2_SF_BLK_MROOM;
4331 return 0;
4332 }
4333
4334 /* determine the first block which must not be deleted, blk_end may
4335 * be NULL if all blocks have to be deleted.
4336 */
4337 idx = htx_get_head(htx);
4338 blk_end = NULL;
4339 while (idx != -1) {
4340 type = htx_get_blk_type(htx_get_blk(htx, idx));
4341 idx = htx_get_next(htx, idx);
4342 if (type == HTX_BLK_EOH) {
4343 if (idx != -1)
4344 blk_end = htx_get_blk(htx, idx);
4345 break;
4346 }
4347 }
4348
4349 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004350 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004351 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004352 meth = htx_sl_req_meth(sl);
4353 path = htx_sl_req_uri(sl);
4354
4355 /* and the rest of the headers, that we dump starting at header 0 */
4356 hdr = 0;
4357
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004358 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004359 while ((idx = htx_get_next(htx, idx)) != -1) {
4360 blk = htx_get_blk(htx, idx);
4361 type = htx_get_blk_type(blk);
4362
4363 if (type == HTX_BLK_UNUSED)
4364 continue;
4365
4366 if (type != HTX_BLK_HDR)
4367 break;
4368
4369 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4370 goto fail;
4371
4372 list[hdr].n = htx_get_blk_name(htx, blk);
4373 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004374 hdr++;
4375 }
4376
4377 /* marker for end of headers */
4378 list[hdr].n = ist("");
4379
4380 chunk_reset(&outbuf);
4381
4382 while (1) {
4383 outbuf.area = b_tail(&h2c->mbuf);
4384 outbuf.size = b_contig_space(&h2c->mbuf);
4385 outbuf.data = 0;
4386
4387 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4388 break;
4389 realign_again:
4390 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4391 }
4392
4393 if (outbuf.size < 9)
4394 goto full;
4395
4396 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4397 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4398 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4399 outbuf.data = 9;
4400
4401 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004402 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004403 if (b_space_wraps(&h2c->mbuf))
4404 goto realign_again;
4405 goto full;
4406 }
4407
4408 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004409 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4410 /* output full */
4411 if (b_space_wraps(&h2c->mbuf))
4412 goto realign_again;
4413 goto full;
4414 }
Willy Tarreau80739692018-10-05 11:35:57 +02004415
4416 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004417 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004418 /* output full */
4419 if (b_space_wraps(&h2c->mbuf))
4420 goto realign_again;
4421 goto full;
4422 }
4423
4424 /* encode all headers, stop at empty name */
4425 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4426 /* these ones do not exist in H2 and must be dropped. */
4427 if (isteq(list[hdr].n, ist("connection")) ||
4428 isteq(list[hdr].n, ist("proxy-connection")) ||
4429 isteq(list[hdr].n, ist("keep-alive")) ||
4430 isteq(list[hdr].n, ist("upgrade")) ||
4431 isteq(list[hdr].n, ist("transfer-encoding")))
4432 continue;
4433
4434 if (isteq(list[hdr].n, ist("")))
4435 break; // end
4436
4437 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4438 /* output full */
4439 if (b_space_wraps(&h2c->mbuf))
4440 goto realign_again;
4441 goto full;
4442 }
4443 }
4444
4445 /* we may need to add END_STREAM if we have no body :
4446 * - request already closed, or :
4447 * - no transfer-encoding, and :
4448 * - no content-length or content-length:0
4449 * Fixme: this doesn't take into account CONNECT requests.
4450 */
4451 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4452 es_now = 1;
4453
4454 if (sl->flags & HTX_SL_F_BODYLESS)
4455 es_now = 1;
4456
4457 if (h2s->cs->flags & CS_FL_SHW)
4458 es_now = 1;
4459
4460 /* update the frame's size */
4461 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4462
4463 if (es_now)
4464 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4465
4466 /* commit the H2 response */
4467 b_add(&h2c->mbuf, outbuf.data);
4468 h2s->flags |= H2_SF_HEADERS_SENT;
4469 h2s->st = H2_SS_OPEN;
4470
Willy Tarreau80739692018-10-05 11:35:57 +02004471 if (es_now) {
4472 // trim any possibly pending data (eg: inconsistent content-length)
4473 h2s->flags |= H2_SF_ES_SENT;
4474 h2s->st = H2_SS_HLOC;
4475 }
4476
4477 /* remove all header blocks including the EOH and compute the
4478 * corresponding size.
4479 *
4480 * FIXME: We should remove everything when es_now is set.
4481 */
4482 ret = 0;
4483 idx = htx_get_head(htx);
4484 blk = htx_get_blk(htx, idx);
4485 while (blk != blk_end) {
4486 ret += htx_get_blksz(blk);
4487 blk = htx_remove_blk(htx, blk);
4488 }
4489
4490 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4491 htx_remove_blk(htx, blk_end);
4492
4493 end:
4494 return ret;
4495 full:
4496 h2c->flags |= H2_CF_MUX_MFULL;
4497 h2s->flags |= H2_SF_BLK_MROOM;
4498 ret = 0;
4499 goto end;
4500 fail:
4501 /* unparsable HTX messages, too large ones to be produced in the local
4502 * list etc go here (unrecoverable errors).
4503 */
4504 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4505 ret = 0;
4506 goto end;
4507}
4508
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004509/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004510 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4511 * caller must check the stream's status to detect any error which might have
4512 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004513 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4514 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004515static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004516{
4517 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004518 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004519 struct buffer outbuf;
4520 size_t total = 0;
4521 int es_now = 0;
4522 int bsize; /* htx block size */
4523 int fsize; /* h2 frame size */
4524 struct htx_blk *blk;
4525 enum htx_blk_type type;
4526 int idx;
4527
4528 if (h2c_mux_busy(h2c, h2s)) {
4529 h2s->flags |= H2_SF_BLK_MBUSY;
4530 goto end;
4531 }
4532
4533 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4534 h2c->flags |= H2_CF_MUX_MALLOC;
4535 h2s->flags |= H2_SF_BLK_MROOM;
4536 goto end;
4537 }
4538
Willy Tarreau98de12a2018-12-12 07:03:00 +01004539 htx = htx_from_buf(buf);
4540
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004541 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4542 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4543 * the caller to handle.
4544 */
4545
4546 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004547 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004548 goto end;
4549
4550 idx = htx_get_head(htx);
4551 blk = htx_get_blk(htx, idx);
4552 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4553 bsize = htx_get_blksz(blk);
4554 fsize = bsize;
4555
4556 if (type == HTX_BLK_EOD) {
4557 /* if we have an EOD, we're dealing with chunked data. We may
4558 * have a set of trailers after us that the caller will want to
4559 * deal with. Let's simply remove the EOD and return.
4560 */
4561 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004562 total++; // EOD counts as one byte
4563 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004564 goto end;
4565 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004566 else if (type == HTX_BLK_EOM) {
4567 if (h2s->flags & H2_SF_ES_SENT) {
4568 /* ES already sent */
4569 htx_remove_blk(htx, blk);
4570 total++; // EOM counts as one byte
4571 count--;
4572 goto end;
4573 }
4574 }
4575 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004576 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004577
4578 /* Perform some optimizations to reduce the number of buffer copies.
4579 * First, if the mux's buffer is empty and the htx area contains
4580 * exactly one data block of the same size as the requested count, and
4581 * this count fits within the frame size, the stream's window size, and
4582 * the connection's window size, then it's possible to simply swap the
4583 * caller's buffer with the mux's output buffer and adjust offsets and
4584 * length to match the entire DATA HTX block in the middle. In this
4585 * case we perform a true zero-copy operation from end-to-end. This is
4586 * the situation that happens all the time with large files. Second, if
4587 * this is not possible, but the mux's output buffer is empty, we still
4588 * have an opportunity to avoid the copy to the intermediary buffer, by
4589 * making the intermediary buffer's area point to the output buffer's
4590 * area. In this case we want to skip the HTX header to make sure that
4591 * copies remain aligned and that this operation remains possible all
4592 * the time. This goes for headers, data blocks and any data extracted
4593 * from the HTX blocks.
4594 */
4595 if (unlikely(fsize == count &&
4596 htx->used == 1 && type == HTX_BLK_DATA &&
4597 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4598 void *old_area = h2c->mbuf.area;
4599
4600 if (b_data(&h2c->mbuf)) {
4601 /* too bad there are data left there. If we have less
4602 * than 1/4 of the mbuf's size and everything fits,
4603 * we'll perform a copy anyway. Otherwise we'll pretend
4604 * the mbuf is full and wait.
4605 */
4606 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4607 goto copy;
4608 h2c->flags |= H2_CF_MUX_MFULL;
4609 h2s->flags |= H2_SF_BLK_MROOM;
4610 goto end;
4611 }
4612
4613 /* map an H2 frame to the HTX block so that we can put the
4614 * frame header there.
4615 */
4616 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004617 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004618 h2c->mbuf.data = fsize + 9;
4619 outbuf.area = b_head(&h2c->mbuf);
4620
4621 /* prepend an H2 DATA frame header just before the DATA block */
4622 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4623 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4624 h2_set_frame_size(outbuf.area, fsize);
4625
4626 /* update windows */
4627 h2s->mws -= fsize;
4628 h2c->mws -= fsize;
4629
4630 /* and exchange with our old area */
4631 buf->area = old_area;
4632 buf->data = buf->head = 0;
4633 total += fsize;
4634 goto end;
4635 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004636
Willy Tarreau98de12a2018-12-12 07:03:00 +01004637 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004638 /* for DATA and EOM we'll have to emit a frame, even if empty */
4639
4640 while (1) {
4641 outbuf.area = b_tail(&h2c->mbuf);
4642 outbuf.size = b_contig_space(&h2c->mbuf);
4643 outbuf.data = 0;
4644
4645 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4646 break;
4647 realign_again:
4648 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4649 }
4650
4651 if (outbuf.size < 9) {
4652 h2c->flags |= H2_CF_MUX_MFULL;
4653 h2s->flags |= H2_SF_BLK_MROOM;
4654 goto end;
4655 }
4656
4657 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4658 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4659 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4660 outbuf.data = 9;
4661
4662 /* we have in <fsize> the exact number of bytes we need to copy from
4663 * the HTX buffer. We need to check this against the connection's and
4664 * the stream's send windows, and to ensure that this fits in the max
4665 * frame size and in the buffer's available space minus 9 bytes (for
4666 * the frame header). The connection's flow control is applied last so
4667 * that we can use a separate list of streams which are immediately
4668 * unblocked on window opening. Note: we don't implement padding.
4669 */
4670
4671 /* EOM is presented with bsize==1 but would lead to the emission of an
4672 * empty frame, thus we force it to zero here.
4673 */
4674 if (type == HTX_BLK_EOM)
4675 bsize = fsize = 0;
4676
4677 if (!fsize)
4678 goto send_empty;
4679
4680 if (h2s->mws <= 0) {
4681 h2s->flags |= H2_SF_BLK_SFCTL;
4682 if (h2s->send_wait) {
4683 LIST_DEL(&h2s->list);
4684 LIST_INIT(&h2s->list);
4685 }
4686 goto end;
4687 }
4688
Willy Tarreauee573762018-12-04 15:25:57 +01004689 if (fsize > count)
4690 fsize = count;
4691
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004692 if (fsize > h2s->mws)
4693 fsize = h2s->mws; // >0
4694
4695 if (h2c->mfs && fsize > h2c->mfs)
4696 fsize = h2c->mfs; // >0
4697
4698 if (fsize + 9 > outbuf.size) {
4699 /* we have an opportunity for enlarging the too small
4700 * available space, let's try.
4701 * FIXME: is this really interesting to do? Maybe we'll
4702 * spend lots of time realigning instead of using two
4703 * frames.
4704 */
4705 if (b_space_wraps(&h2c->mbuf))
4706 goto realign_again;
4707 fsize = outbuf.size - 9;
4708
4709 if (fsize <= 0) {
4710 /* no need to send an empty frame here */
4711 h2c->flags |= H2_CF_MUX_MFULL;
4712 h2s->flags |= H2_SF_BLK_MROOM;
4713 goto end;
4714 }
4715 }
4716
4717 if (h2c->mws <= 0) {
4718 h2s->flags |= H2_SF_BLK_MFCTL;
4719 goto end;
4720 }
4721
4722 if (fsize > h2c->mws)
4723 fsize = h2c->mws;
4724
4725 /* now let's copy this this into the output buffer */
4726 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004727 h2s->mws -= fsize;
4728 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004729 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004730
4731 send_empty:
4732 /* update the frame's size */
4733 h2_set_frame_size(outbuf.area, fsize);
4734
4735 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4736 * meeting EOM. We should optimize this later.
4737 */
4738 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004739 total++; // EOM counts as one byte
4740 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004741 es_now = 1;
4742 }
4743
4744 if (es_now)
4745 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4746
4747 /* commit the H2 response */
4748 b_add(&h2c->mbuf, fsize + 9);
4749
4750 /* consume incoming HTX block, including EOM */
4751 total += fsize;
4752 if (fsize == bsize) {
4753 htx_remove_blk(htx, blk);
4754 if (fsize)
4755 goto new_frame;
4756 } else {
4757 /* we've truncated this block */
4758 htx_cut_data_blk(htx, blk, fsize);
4759 }
4760
4761 if (es_now) {
4762 if (h2s->st == H2_SS_OPEN)
4763 h2s->st = H2_SS_HLOC;
4764 else
4765 h2s_close(h2s);
4766
4767 h2s->flags |= H2_SF_ES_SENT;
4768 }
4769
4770 end:
4771 return total;
4772}
4773
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004774/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4775 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4776 * processed. The caller must check the stream's status to detect any error
4777 * which might have happened subsequently to a successful send. The htx blocks
4778 * are automatically removed from the message. The htx message is assumed to be
4779 * valid since produced from the internal code. Processing stops when meeting
4780 * the EOM, which is also removed. All trailers are processed at once and sent
4781 * as a single frame. The ES flag is always set.
4782 */
4783static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4784{
4785 struct http_hdr list[MAX_HTTP_HDR];
4786 struct h2c *h2c = h2s->h2c;
4787 struct htx_blk *blk;
4788 struct htx_blk *blk_end;
4789 struct buffer outbuf;
4790 struct h1m h1m;
4791 enum htx_blk_type type;
4792 uint32_t size;
4793 int ret = 0;
4794 int hdr;
4795 int idx;
4796 void *start;
4797
4798 if (h2c_mux_busy(h2c, h2s)) {
4799 h2s->flags |= H2_SF_BLK_MBUSY;
4800 goto end;
4801 }
4802
4803 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4804 h2c->flags |= H2_CF_MUX_MALLOC;
4805 h2s->flags |= H2_SF_BLK_MROOM;
4806 goto end;
4807 }
4808
4809 /* The principle is that we parse each and every trailers block using
4810 * the H1 headers parser, and append it to the list. We don't proceed
4811 * until EOM is met. blk_end will point to the EOM block.
4812 */
4813 hdr = 0;
4814 memset(list, 0, sizeof(list));
4815 blk_end = NULL;
4816
4817 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4818 blk = htx_get_blk(htx, idx);
4819 type = htx_get_blk_type(blk);
4820
4821 if (type == HTX_BLK_UNUSED)
4822 continue;
4823
4824 if (type != HTX_BLK_TLR) {
4825 if (type == HTX_BLK_EOM)
4826 blk_end = blk;
4827 break;
4828 }
4829
4830 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4831 goto fail;
4832
4833 size = htx_get_blksz(blk);
4834 start = htx_get_blk_ptr(htx, blk);
4835
4836 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4837 h1m.err_pos = 0;
4838 ret = h1_headers_to_hdr_list(start, start + size,
4839 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4840 &h1m, NULL);
4841 if (ret < 0)
4842 goto fail;
4843
4844 /* ret == 0 if an incomplete trailers block was found (missing
4845 * empty line), or > 0 if it was found. We have to continue on
4846 * incomplete messages because the trailers block might be
4847 * incomplete.
4848 */
4849
4850 /* search the new end */
4851 while (hdr <= sizeof(list)/sizeof(list[0])) {
4852 if (!list[hdr].n.len)
4853 break;
4854 hdr++;
4855 }
4856 }
4857
4858 if (!blk_end)
4859 goto end; // end not found yet
4860
4861 if (!hdr)
4862 goto done;
4863
4864 chunk_reset(&outbuf);
4865
4866 while (1) {
4867 outbuf.area = b_tail(&h2c->mbuf);
4868 outbuf.size = b_contig_space(&h2c->mbuf);
4869 outbuf.data = 0;
4870
4871 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4872 break;
4873 realign_again:
4874 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4875 }
4876
4877 if (outbuf.size < 9)
4878 goto full;
4879
4880 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4881 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4882 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4883 outbuf.data = 9;
4884
4885 /* encode status, which necessarily is the first one */
4886 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
4887 if (b_space_wraps(&h2c->mbuf))
4888 goto realign_again;
4889 goto full;
4890 }
4891
4892 /* encode all headers */
4893 for (idx = 0; idx < hdr; idx++) {
4894 /* these ones do not exist in H2 or must not appear in
4895 * trailers and must be dropped.
4896 */
4897 if (isteq(list[idx].n, ist("host")) ||
4898 isteq(list[idx].n, ist("content-length")) ||
4899 isteq(list[idx].n, ist("connection")) ||
4900 isteq(list[idx].n, ist("proxy-connection")) ||
4901 isteq(list[idx].n, ist("keep-alive")) ||
4902 isteq(list[idx].n, ist("upgrade")) ||
4903 isteq(list[idx].n, ist("te")) ||
4904 isteq(list[idx].n, ist("transfer-encoding")))
4905 continue;
4906
4907 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
4908 /* output full */
4909 if (b_space_wraps(&h2c->mbuf))
4910 goto realign_again;
4911 goto full;
4912 }
4913 }
4914
4915 /* update the frame's size */
4916 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4917
4918 /* commit the H2 response */
4919 b_add(&h2c->mbuf, outbuf.data);
4920 h2s->flags |= H2_SF_ES_SENT;
4921
4922 if (h2s->st == H2_SS_OPEN)
4923 h2s->st = H2_SS_HLOC;
4924 else
4925 h2s_close(h2s);
4926
4927 /* OK we could properly deliver the response */
4928 done:
4929 /* remove all header blocks including EOM and compute the corresponding size. */
4930 ret = 0;
4931 idx = htx_get_head(htx);
4932 blk = htx_get_blk(htx, idx);
4933 while (blk != blk_end) {
4934 ret += htx_get_blksz(blk);
4935 blk = htx_remove_blk(htx, blk);
4936 }
4937 blk = htx_remove_blk(htx, blk);
4938 end:
4939 return ret;
4940 full:
4941 h2c->flags |= H2_CF_MUX_MFULL;
4942 h2s->flags |= H2_SF_BLK_MROOM;
4943 ret = 0;
4944 goto end;
4945 fail:
4946 /* unparsable HTX messages, too large ones to be produced in the local
4947 * list etc go here (unrecoverable errors).
4948 */
4949 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4950 ret = 0;
4951 goto end;
4952}
4953
Olivier Houchard6ff20392018-07-17 18:46:31 +02004954/* Called from the upper layer, to subscribe to events, such as being able to send */
4955static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4956{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004957 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004958 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004959 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004960
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004961 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004962 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004963 if (!(sw->events & SUB_RETRY_RECV)) {
4964 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004965 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004966 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004967 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004968 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004969 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004970 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004971 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004972 if (!(sw->events & SUB_RETRY_SEND)) {
4973 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004974 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004975 h2s->send_wait = sw;
4976 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4977 if (h2s->flags & H2_SF_BLK_MFCTL)
4978 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4979 else
4980 LIST_ADDQ(&h2c->send_list, &h2s->list);
4981 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004982 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004983 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004984 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004985 if (event_type != 0)
4986 return -1;
4987 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004988
4989
4990}
4991
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004992static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4993{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004994 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004995 struct h2s *h2s = cs->ctx;
4996
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004997 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004998 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004999 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005000 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005001 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005002 }
5003 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005004 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005005 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005006 if (h2s->send_wait == sw) {
5007 LIST_DEL(&h2s->list);
5008 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005009 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005010 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005011 }
5012 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005013 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5014 sw = param;
5015 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005016 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005017 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005018 LIST_DEL(&h2s->list);
5019 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005020 }
5021 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005022 return 0;
5023}
5024
5025
Olivier Houchard511efea2018-08-16 15:30:32 +02005026/* Called from the upper layer, to receive data */
5027static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5028{
Olivier Houchard638b7992018-08-16 15:41:52 +02005029 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005030 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005031 struct htx *h2s_htx = NULL;
5032 struct htx *buf_htx = NULL;
5033 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005034 size_t ret = 0;
5035
5036 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005037 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5038 /* in HTX mode we ignore the count argument */
5039 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005040 if (htx_is_empty(h2s_htx)) {
5041 if (cs->flags & CS_FL_REOS)
5042 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005043 if (cs->flags & CS_FL_ERR_PENDING)
5044 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01005045 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005046 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005047
5048 buf_htx = htx_from_buf(buf);
5049 count = htx_free_space(buf_htx);
5050
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005051 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01005052
5053 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005054 htx_to_buf(buf_htx, buf);
5055 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005056 ret = htx_ret.ret;
5057 }
5058 else {
5059 ret = b_xfer(buf, &h2s->rxbuf, count);
5060 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005061
Olivier Houchard638b7992018-08-16 15:41:52 +02005062 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005063 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005064 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005065 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005066 if (cs->flags & CS_FL_REOS)
5067 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005068 if (cs->flags & CS_FL_ERR_PENDING)
5069 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005070 if (b_size(&h2s->rxbuf)) {
5071 b_free(&h2s->rxbuf);
5072 offer_buffers(NULL, tasks_run_queue);
5073 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005074 }
5075
Willy Tarreau082f5592018-11-25 08:03:32 +01005076 if (ret && h2c->dsi == h2s->id) {
5077 /* demux is blocking on this stream's buffer */
5078 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005079 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005080 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005081end:
Olivier Houchard511efea2018-08-16 15:30:32 +02005082 return ret;
5083}
5084
Olivier Houchardd846c262018-10-19 17:24:29 +02005085static void h2_stop_senders(struct h2c *h2c)
5086{
5087 struct h2s *h2s, *h2s_back;
5088
5089 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5090 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5091 if (h2c->msi == h2s_id(h2s))
5092 continue;
5093 LIST_DEL(&h2s->list);
5094 LIST_INIT(&h2s->list);
5095 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005096 h2s->send_wait->events |= SUB_RETRY_SEND;
5097 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005098 LIST_ADD(&h2c->send_list, &h2s->list);
5099 }
5100}
5101
Willy Tarreau62f52692017-10-08 23:01:42 +02005102/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005103static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005104{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005105 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005106 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005107 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005108 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005109 struct htx *htx;
5110 struct htx_blk *blk;
5111 enum htx_blk_type btype;
5112 uint32_t bsize;
5113 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005114
Olivier Houchardd846c262018-10-19 17:24:29 +02005115 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005116 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005117 h2s->send_wait = NULL;
5118 LIST_DEL(&h2s->list);
5119 LIST_INIT(&h2s->list);
5120 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005121 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5122 return 0;
5123
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005124 /* htx will be enough to decide if we're using HTX or legacy */
5125 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5126
Willy Tarreau0bad0432018-06-14 16:54:01 +02005127 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005128 h2s->flags |= H2_SF_OUTGOING_DATA;
5129
Willy Tarreau751f2d02018-10-05 09:35:00 +02005130 if (h2s->id == 0) {
5131 int32_t id = h2c_get_next_sid(h2s->h2c);
5132
5133 if (id < 0) {
5134 cs->ctx = NULL;
5135 cs->flags |= CS_FL_ERROR;
5136 h2s_destroy(h2s);
5137 return 0;
5138 }
5139
5140 eb32_delete(&h2s->by_id);
5141 h2s->by_id.key = h2s->id = id;
5142 h2s->h2c->max_id = id;
5143 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5144 }
5145
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005146 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005147 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5148 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005149 idx = htx_get_head(htx);
5150 blk = htx_get_blk(htx, idx);
5151 btype = htx_get_blk_type(blk);
5152 bsize = htx_get_blksz(blk);
5153
5154 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005155 case HTX_BLK_REQ_SL:
5156 /* start-line before headers */
5157 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5158 if (ret > 0) {
5159 total += ret;
5160 count -= ret;
5161 if (ret < bsize)
5162 goto done;
5163 }
5164 break;
5165
Willy Tarreau115e83b2018-12-01 19:17:53 +01005166 case HTX_BLK_RES_SL:
5167 /* start-line before headers */
5168 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5169 if (ret > 0) {
5170 total += ret;
5171 count -= ret;
5172 if (ret < bsize)
5173 goto done;
5174 }
5175 break;
5176
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005177 case HTX_BLK_DATA:
5178 case HTX_BLK_EOD:
5179 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005180 /* all these cause the emission of a DATA frame (possibly empty).
5181 * This EOM necessarily is one before trailers, as the EOM following
5182 * trailers would have been consumed by the trailers parser.
5183 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005184 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005185 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005186 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005187 total += ret;
5188 count -= ret;
5189 if (ret < bsize)
5190 goto done;
5191 }
5192 break;
5193
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005194 case HTX_BLK_TLR:
5195 /* This is the first trailers block, all the subsequent ones AND
5196 * the EOM will be swallowed by the parser.
5197 */
5198 ret = h2s_htx_make_trailers(h2s, htx);
5199 if (ret > 0) {
5200 total += ret;
5201 count -= ret;
5202 if (ret < bsize)
5203 goto done;
5204 }
5205 break;
5206
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005207 default:
5208 htx_remove_blk(htx, blk);
5209 total += bsize;
5210 count -= bsize;
5211 break;
5212 }
5213 }
5214 goto done;
5215 }
5216
5217 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005218 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005219 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005220 if (h2s->h2c->flags & H2_CF_IS_BACK)
5221 ret = -1;
5222 else
5223 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005224 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005225 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005226 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005227 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005228 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005229 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005230 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005231
Willy Tarreau5dd17352018-06-14 13:33:30 +02005232 if (unlikely((int)ret <= 0)) {
5233 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005234 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5235 break;
5236 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005237 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005238 total += count;
5239 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005240 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005241 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005242 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005243 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005244 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005245 break;
5246 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005247
5248 total += ret;
5249 count -= ret;
5250
5251 if (h2s->st >= H2_SS_ERROR)
5252 break;
5253
5254 if (h2s->flags & H2_SF_BLK_ANY)
5255 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005256 }
5257
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005258 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005259 if (h2s->st >= H2_SS_ERROR) {
5260 /* trim any possibly pending data after we close (extra CR-LF,
5261 * unprocessed trailers, abnormal extra data, ...)
5262 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005263 total += count;
5264 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005265 }
5266
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005267 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005268 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005269 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005270 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005271 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005272 }
5273
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005274 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005275 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005276 } else {
5277 b_del(buf, total);
5278 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005279
5280 /* The mux is full, cancel the pending tasks */
5281 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5282 (h2s->flags & H2_SF_BLK_MBUSY))
5283 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005284
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005285 /* If we're running HTX, and we read the whole buffer, then pretend
5286 * we read exactly what the caller specified, as with HTX the caller
5287 * will always give the buffer size, instead of the amount of data
5288 * available.
5289 */
5290 if (htx && !b_data(buf))
5291 total = orig_count;
5292
Olivier Houchard7505f942018-08-21 18:10:44 +02005293 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005294 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005295 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005296
Olivier Houchard7505f942018-08-21 18:10:44 +02005297 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005298 /* If we're waiting for flow control, and we got a shutr on the
5299 * connection, we will never be unlocked, so add an error on
5300 * the conn_stream.
5301 */
5302 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5303 !b_data(&h2s->h2c->dbuf) &&
5304 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5305 if (cs->flags & CS_FL_EOS)
5306 cs->flags |= CS_FL_ERROR;
5307 else
5308 cs->flags |= CS_FL_ERR_PENDING;
5309 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005310 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005311}
5312
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005313/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005314static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005315{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005316 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005317 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005318 struct eb32_node *node;
5319 int fctl_cnt = 0;
5320 int send_cnt = 0;
5321 int tree_cnt = 0;
5322 int orph_cnt = 0;
5323
5324 if (!h2c)
5325 return;
5326
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005327 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005328 fctl_cnt++;
5329
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005330 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005331 send_cnt++;
5332
Willy Tarreau3af37712018-12-18 14:34:41 +01005333 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005334 node = eb32_first(&h2c->streams_by_id);
5335 while (node) {
5336 h2s = container_of(node, struct h2s, by_id);
5337 tree_cnt++;
5338 if (!h2s->cs)
5339 orph_cnt++;
5340 node = eb32_next(node);
5341 }
5342
Willy Tarreau987c0632018-12-18 10:32:05 +01005343 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5344 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5345 " .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 +02005346 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5347 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005348 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005349 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5350 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5351 h2c->msi,
5352 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5353 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5354
5355 if (h2s) {
5356 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5357 h2s, h2s->id, h2s->flags,
5358 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5359 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5360 h2s->cs);
5361 if (h2s->cs)
5362 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5363 h2s->cs->flags, h2s->cs->data);
5364 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005365}
Willy Tarreau62f52692017-10-08 23:01:42 +02005366
5367/*******************************************************/
5368/* functions below are dedicated to the config parsers */
5369/*******************************************************/
5370
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005371/* config parser for global "tune.h2.header-table-size" */
5372static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5373 struct proxy *defpx, const char *file, int line,
5374 char **err)
5375{
5376 if (too_many_args(1, args, err, NULL))
5377 return -1;
5378
5379 h2_settings_header_table_size = atoi(args[1]);
5380 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5381 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5382 return -1;
5383 }
5384 return 0;
5385}
Willy Tarreau62f52692017-10-08 23:01:42 +02005386
Willy Tarreaue6baec02017-07-27 11:45:11 +02005387/* config parser for global "tune.h2.initial-window-size" */
5388static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5389 struct proxy *defpx, const char *file, int line,
5390 char **err)
5391{
5392 if (too_many_args(1, args, err, NULL))
5393 return -1;
5394
5395 h2_settings_initial_window_size = atoi(args[1]);
5396 if (h2_settings_initial_window_size < 0) {
5397 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5398 return -1;
5399 }
5400 return 0;
5401}
5402
Willy Tarreau5242ef82017-07-27 11:47:28 +02005403/* config parser for global "tune.h2.max-concurrent-streams" */
5404static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5405 struct proxy *defpx, const char *file, int line,
5406 char **err)
5407{
5408 if (too_many_args(1, args, err, NULL))
5409 return -1;
5410
5411 h2_settings_max_concurrent_streams = atoi(args[1]);
5412 if (h2_settings_max_concurrent_streams < 0) {
5413 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5414 return -1;
5415 }
5416 return 0;
5417}
5418
Willy Tarreau62f52692017-10-08 23:01:42 +02005419
5420/****************************************/
5421/* MUX initialization and instanciation */
5422/***************************************/
5423
5424/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005425static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005426 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005427 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005428 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005429 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005430 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005431 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005432 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005433 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005434 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005435 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005436 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005437 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005438 .shutr = h2_shutr,
5439 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005440 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005441 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005442 .name = "H2",
5443};
5444
Christopher Faulet32f61c02018-04-10 14:33:41 +02005445/* PROTO selection : this mux registers PROTO token "h2" */
5446static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005447 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005448
Willy Tarreau0108d902018-11-25 19:14:37 +01005449INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5450
Willy Tarreauf8957272018-10-03 10:25:20 +02005451static struct mux_proto_list mux_proto_h2_htx =
5452 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5453
5454INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5455
Willy Tarreau62f52692017-10-08 23:01:42 +02005456/* config keyword parsers */
5457static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005458 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005459 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005460 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005461 { 0, NULL, NULL }
5462}};
5463
Willy Tarreau0108d902018-11-25 19:14:37 +01005464INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);