blob: 959bcfbc032493b2c044db7fc4833bfcf43c7d2c [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_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
147/* HTTP/2 stream flags (32 bit), in h2s->flags */
148#define H2_SF_NONE 0x00000000
149#define H2_SF_ES_RCVD 0x00000001
150#define H2_SF_ES_SENT 0x00000002
151
152#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
153#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
154
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200155/* stream flags indicating the reason the stream is blocked */
156#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
157#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
158#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
159#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
160#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
161
Willy Tarreau454f9052017-10-26 19:40:35 +0200162/* stream flags indicating how data is supposed to be sent */
163#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
164#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
165
166/* step we're currently in when sending chunks. This is needed because we may
167 * have to transfer chunks as large as a full buffer so there's no room left
168 * for size nor crlf around.
169 */
170#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
171#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
172#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
173
174#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
175
Willy Tarreau67434202017-11-06 20:20:51 +0100176#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100177#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100178
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100179#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
180
Willy Tarreau18312642017-10-11 07:57:07 +0200181/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
182 * it is being processed in the internal HTTP representation (H1 for now).
183 */
184struct h2s {
185 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100186 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200187 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200188 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200190 int32_t id; /* stream ID */
191 uint32_t flags; /* H2_SF_* */
192 int mws; /* mux window size for this stream */
193 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
194 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200195 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100196 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200197 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200198 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100199 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
200 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100202 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200203};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200204
Willy Tarreauc6405142017-09-21 20:23:50 +0200205/* descriptor for an h2 frame header */
206struct h2_fh {
207 uint32_t len; /* length, host order, 24 bits */
208 uint32_t sid; /* stream id, host order, 31 bits */
209 uint8_t ft; /* frame type */
210 uint8_t ff; /* frame flags */
211};
212
Willy Tarreau8ceae722018-11-26 11:58:30 +0100213/* the h2c connection pool */
214DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
215
216/* the h2s stream pool */
217DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
218
Willy Tarreaudc572362018-12-12 08:08:05 +0100219/* The default connection window size is 65535, it may only be enlarged using
220 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
221 * we'll pretend we already received the difference between the two to send
222 * an equivalent window update to enlarge it to 2G-1.
223 */
224#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
225
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226/* a few settings from the global section */
227static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200228static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100229static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100230static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau2a856182017-05-16 15:20:39 +0200232/* a dmumy closed stream */
233static const struct h2s *h2_closed_stream = &(const struct h2s){
234 .cs = NULL,
235 .h2c = NULL,
236 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100237 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100238 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200239 .id = 0,
240};
241
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100242/* a dmumy closed stream returning a PROTOCOL_ERROR error */
243static const struct h2s *h2_error_stream = &(const struct h2s){
244 .cs = NULL,
245 .h2c = NULL,
246 .st = H2_SS_CLOSED,
247 .errcode = H2_ERR_PROTOCOL_ERROR,
248 .flags = 0,
249 .id = 0,
250};
251
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100252/* a dmumy closed stream returning a REFUSED_STREAM error */
253static const struct h2s *h2_refused_stream = &(const struct h2s){
254 .cs = NULL,
255 .h2c = NULL,
256 .st = H2_SS_CLOSED,
257 .errcode = H2_ERR_REFUSED_STREAM,
258 .flags = 0,
259 .id = 0,
260};
261
Willy Tarreau2a856182017-05-16 15:20:39 +0200262/* and a dummy idle stream for use with any unannounced stream */
263static const struct h2s *h2_idle_stream = &(const struct h2s){
264 .cs = NULL,
265 .h2c = NULL,
266 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100267 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200268 .id = 0,
269};
270
Olivier Houchard9f6af332018-05-25 14:04:04 +0200271static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200272static int h2_send(struct h2c *h2c);
273static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200274static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200275static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100276static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100277static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100278static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200279static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100280static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100281static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200282
Olivier Houchard7a977432019-03-21 15:47:13 +0100283static __inline int
284h2c_is_dead(struct h2c *h2c)
285{
286 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
287 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
288 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
289 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
290 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
291 (conn_xprt_read0_pending(h2c->conn) ||
292 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
293 return 1;
294
295 return 0;
296
297}
298
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200299/*****************************************************/
300/* functions below are for dynamic buffer management */
301/*****************************************************/
302
Willy Tarreau315d8072017-12-10 22:17:57 +0100303/* indicates whether or not the we may call the h2_recv() function to attempt
304 * to receive data into the buffer and/or demux pending data. The condition is
305 * a bit complex due to some API limits for now. The rules are the following :
306 * - if an error or a shutdown was detected on the connection and the buffer
307 * is empty, we must not attempt to receive
308 * - if the demux buf failed to be allocated, we must not try to receive and
309 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100310 * - if no flag indicates a blocking condition, we may attempt to receive,
311 * regardless of whether the demux buffer is full or not, so that only
312 * de demux part decides whether or not to block. This is needed because
313 * the connection API indeed prevents us from re-enabling receipt that is
314 * already enabled in a polled state, so we must always immediately stop
315 * as soon as the demux can't proceed so as never to hit an end of read
316 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100317 * - otherwise must may not attempt
318 */
319static inline int h2_recv_allowed(const struct h2c *h2c)
320{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100322 (h2c->st0 >= H2_CS_ERROR ||
323 h2c->conn->flags & CO_FL_ERROR ||
324 conn_xprt_read0_pending(h2c->conn)))
325 return 0;
326
327 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100328 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100329 return 1;
330
331 return 0;
332}
333
Willy Tarreau47b515a2018-12-21 16:09:41 +0100334/* restarts reading on the connection if it was not enabled */
335static inline void h2c_restart_reading(const struct h2c *h2c)
336{
337 if (!h2_recv_allowed(h2c))
338 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100339 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100340 return;
341 tasklet_wakeup(h2c->wait_event.task);
342}
343
344
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100345/* returns true if the front connection has too many conn_streams attached */
346static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200347{
Willy Tarreaua8754662018-12-23 20:43:58 +0100348 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200349}
350
Willy Tarreau44e973f2018-03-01 17:49:30 +0100351/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
352 * flags are used to figure what buffer was requested. It returns 1 if the
353 * allocation succeeds, in which case the connection is woken up, or 0 if it's
354 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200355 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100356static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200357{
358 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100359 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200360
Willy Tarreau44e973f2018-03-01 17:49:30 +0100361 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200362 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100363 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200364 return 1;
365 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200366
Willy Tarreau44e973f2018-03-01 17:49:30 +0100367 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
368 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200369
370 if (h2c->flags & H2_CF_DEM_MROOM) {
371 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100372 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200373 }
Willy Tarreau14398122017-09-22 14:26:04 +0200374 return 1;
375 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100376
377 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
378 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200379 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100380 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100381 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100382 return 1;
383 }
384
Willy Tarreau14398122017-09-22 14:26:04 +0200385 return 0;
386}
387
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200388static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200389{
390 struct buffer *buf = NULL;
391
Willy Tarreau44e973f2018-03-01 17:49:30 +0100392 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
393 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
394 h2c->buf_wait.target = h2c;
395 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100396 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100397 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100398 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200399 __conn_xprt_stop_recv(h2c->conn);
400 }
401 return buf;
402}
403
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200404static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200405{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200406 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100407 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200408 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200409 }
410}
411
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100412/* returns the number of allocatable outgoing streams for the connection taking
413 * the last_sid and the reserved ones into account.
414 */
415static inline int h2_streams_left(const struct h2c *h2c)
416{
417 int ret;
418
419 /* consider the number of outgoing streams we're allowed to create before
420 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
421 * nb_reserved is the number of streams which don't yet have an ID.
422 */
423 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
424 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
425 if (ret < 0)
426 ret = 0;
427 return ret;
428}
429
Willy Tarreau00f18a32019-01-26 12:19:01 +0100430/* returns the number of streams in use on a connection to figure if it's
431 * idle or not. We check nb_cs and not nb_streams as the caller will want
432 * to know if it was the last one after a detach().
433 */
434static int h2_used_streams(struct connection *conn)
435{
436 struct h2c *h2c = conn->ctx;
437
438 return h2c->nb_cs;
439}
440
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100441/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100442static int h2_avail_streams(struct connection *conn)
443{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100444 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100445 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100446 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100447
Willy Tarreau6afec462019-01-28 06:40:19 +0100448 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
449 * streams on the connection.
450 */
451 if (h2c->last_sid >= 0)
452 return 0;
453
Willy Tarreau86949782019-01-31 10:42:05 +0100454 /* note: may be negative if a SETTINGS frame changes the limit */
455 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100456
457 /* we must also consider the limit imposed by stream IDs */
458 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100459 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100460 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100461 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
462 ret1 = MIN(ret1, ret2);
463 }
464 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100465}
466
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200467
Willy Tarreau62f52692017-10-08 23:01:42 +0200468/*****************************************************************/
469/* functions below are dedicated to the mux setup and management */
470/*****************************************************************/
471
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200472/* Initialize the mux once it's attached. For outgoing connections, the context
473 * is already initialized before installing the mux, so we detect incoming
474 * connections from the fact that the context is still NULL. Returns < 0 on
475 * error.
476 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100477static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200478{
479 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100480 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481
Willy Tarreaubafbe012017-11-24 17:34:44 +0100482 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200483 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200484 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200485
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100486 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200487 h2c->flags = H2_CF_IS_BACK;
488 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
489 if (tick_isset(prx->timeout.serverfin))
490 h2c->shut_timeout = prx->timeout.serverfin;
491 } else {
492 h2c->flags = H2_CF_NONE;
493 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
494 if (tick_isset(prx->timeout.clientfin))
495 h2c->shut_timeout = prx->timeout.clientfin;
496 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100497
Willy Tarreau0b37d652018-10-03 10:33:02 +0200498 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100499 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100500 if (tick_isset(h2c->timeout)) {
501 t = task_new(tid_bit);
502 if (!t)
503 goto fail;
504
505 h2c->task = t;
506 t->process = h2_timeout_task;
507 t->context = h2c;
508 t->expire = tick_add(now_ms, h2c->timeout);
509 }
Willy Tarreauea392822017-10-31 10:02:25 +0100510
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200511 h2c->wait_event.task = tasklet_new();
512 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200513 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200514 h2c->wait_event.task->process = h2_io_cb;
515 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100516 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200517
Willy Tarreau32218eb2017-09-22 08:07:25 +0200518 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
519 if (!h2c->ddht)
520 goto fail;
521
522 /* Initialise the context. */
523 h2c->st0 = H2_CS_PREFACE;
524 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100525 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200526 h2c->max_id = -1;
527 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100528 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100530 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200531 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100532 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100533 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200534
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200535 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200536 h2c->dsi = -1;
537 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100538
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539 h2c->last_sid = -1;
540
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200541 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 h2c->miw = 65535; /* mux initial window size */
543 h2c->mws = 65535; /* mux window size */
544 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200545 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200546 LIST_INIT(&h2c->send_list);
547 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200548 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100549 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200550
Willy Tarreau3f133572017-10-31 19:21:06 +0100551 if (t)
552 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100553
Willy Tarreau01b44822018-10-03 14:26:37 +0200554 if (h2c->flags & H2_CF_IS_BACK) {
555 /* FIXME: this is temporary, for outgoing connections we need
556 * to immediately allocate a stream until the code is modified
557 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100558 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200559 */
560 struct h2s *h2s;
561
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100562 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200563 if (!h2s)
564 goto fail_stream;
565 }
566
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100567 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200568
Willy Tarreau0f383582018-10-03 14:22:21 +0200569 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100570 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200571 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200572 fail_stream:
573 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200574 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100575 if (t)
576 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200577 if (h2c->wait_event.task)
578 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100579 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200580 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200581 return -1;
582}
583
Willy Tarreau751f2d02018-10-05 09:35:00 +0200584/* returns the next allocatable outgoing stream ID for the H2 connection, or
585 * -1 if no more is allocatable.
586 */
587static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
588{
589 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100590
591 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200592 id = -1;
593 return id;
594}
595
Willy Tarreau2373acc2017-10-12 17:35:14 +0200596/* returns the stream associated with id <id> or NULL if not found */
597static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
598{
599 struct eb32_node *node;
600
Willy Tarreau751f2d02018-10-05 09:35:00 +0200601 if (id == 0)
602 return (struct h2s *)h2_closed_stream;
603
Willy Tarreau2a856182017-05-16 15:20:39 +0200604 if (id > h2c->max_id)
605 return (struct h2s *)h2_idle_stream;
606
Willy Tarreau2373acc2017-10-12 17:35:14 +0200607 node = eb32_lookup(&h2c->streams_by_id, id);
608 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200609 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200610
611 return container_of(node, struct h2s, by_id);
612}
613
Willy Tarreau62f52692017-10-08 23:01:42 +0200614/* release function for a connection. This one should be called to free all
615 * resources allocated to the mux.
616 */
617static void h2_release(struct connection *conn)
618{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100619 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200620
Willy Tarreau32218eb2017-09-22 08:07:25 +0200621 if (h2c) {
622 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200623
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100624 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100625 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100626 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200627
Willy Tarreau44e973f2018-03-01 17:49:30 +0100628 h2_release_buf(h2c, &h2c->dbuf);
629 h2_release_buf(h2c, &h2c->mbuf);
630
Willy Tarreauea392822017-10-31 10:02:25 +0100631 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200632 h2c->task->context = NULL;
633 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100634 h2c->task = NULL;
635 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200636 if (h2c->wait_event.task)
637 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100638 if (h2c->wait_event.events != 0)
639 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200640 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100641
Willy Tarreaubafbe012017-11-24 17:34:44 +0100642 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200643 }
644
645 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100646 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200647
648 conn_stop_tracking(conn);
649 conn_full_close(conn);
650 if (conn->destroy_cb)
651 conn->destroy_cb(conn);
652 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200653}
654
655
Willy Tarreau71681172017-10-23 14:39:06 +0200656/******************************************************/
657/* functions below are for the H2 protocol processing */
658/******************************************************/
659
660/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100661static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200662{
663 return h2s ? h2s->id : 0;
664}
665
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200666/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100667static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200668{
669 if (h2c->msi < 0)
670 return 0;
671
672 if (h2c->msi == h2s_id(h2s))
673 return 0;
674
675 return 1;
676}
677
Willy Tarreau741d6df2017-10-17 08:00:59 +0200678/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100679static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200680{
681 h2c->errcode = err;
682 h2c->st0 = H2_CS_ERROR;
683}
684
Willy Tarreau175cebb2019-01-24 10:02:24 +0100685/* marks an error on the stream. It may also update an already closed stream
686 * (e.g. to report an error after an RST was received).
687 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100688static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200689{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100690 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200691 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100692 if (h2s->st < H2_SS_ERROR)
693 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100694 if (h2s->cs)
695 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200696 }
697}
698
Willy Tarreau7e094452018-12-19 18:08:52 +0100699/* attempt to notify the data layer of recv availability */
700static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
701{
702 struct wait_event *sw;
703
704 if (h2s->recv_wait) {
705 sw = h2s->recv_wait;
706 sw->events &= ~SUB_RETRY_RECV;
707 tasklet_wakeup(sw->task);
708 h2s->recv_wait = NULL;
709 }
710}
711
712/* attempt to notify the data layer of send availability */
713static void __maybe_unused h2s_notify_send(struct h2s *h2s)
714{
715 struct wait_event *sw;
716
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100717 if (h2s->send_wait && !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100718 sw = h2s->send_wait;
719 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100720 sw->events |= SUB_CALL_UNSUBSCRIBE;
721 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100722 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100723 }
724}
725
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100726/* alerts the data layer, trying to wake it up by all means, following
727 * this sequence :
728 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
729 * - if its subscribed to send, then it's woken up for send
730 * - if it was subscribed to neither, its ->wake() callback is called
731 * It is safe to call this function with a closed stream which doesn't have a
732 * conn_stream anymore.
733 */
734static void __maybe_unused h2s_alert(struct h2s *h2s)
735{
736 if (h2s->recv_wait || h2s->send_wait) {
737 h2s_notify_recv(h2s);
738 h2s_notify_send(h2s);
739 }
740 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
741 h2s->cs->data_cb->wake(h2s->cs);
742}
743
Willy Tarreaue4820742017-07-27 13:37:23 +0200744/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100745static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200746{
747 uint8_t *out = frame;
748
749 *out = len >> 16;
750 write_n16(out + 1, len);
751}
752
Willy Tarreau54c15062017-10-10 17:10:03 +0200753/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
754 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
755 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200756 * available in the buffer's input prior to calling this function. The buffer
757 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200758 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100759static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200760 const struct buffer *b, int o)
761{
Willy Tarreau591d4452018-06-15 17:21:00 +0200762 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 +0200763}
764
Willy Tarreau1f094672017-11-20 21:27:45 +0100765static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200766{
Willy Tarreau591d4452018-06-15 17:21:00 +0200767 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200768}
769
Willy Tarreau1f094672017-11-20 21:27:45 +0100770static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200771{
Willy Tarreau591d4452018-06-15 17:21:00 +0200772 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200773}
774
Willy Tarreau1f094672017-11-20 21:27:45 +0100775static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200776{
Willy Tarreau591d4452018-06-15 17:21:00 +0200777 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200778}
779
780
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100781/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
782 * The algorithm is not obvious. It turns out that H2 headers are neither
783 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
784 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200785 *
786 * b0 b1 b2 b3 b4 b5..b8
787 * +----------+---------+--------+----+----+----------------------+
788 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
789 * +----------+---------+--------+----+----+----------------------+
790 *
791 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
792 * we get the sid properly aligned and ordered, and 16 bits of len properly
793 * ordered as well. The type and flags can be extracted using bit shifts from
794 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200795 * Returns zero if some bytes are missing, otherwise non-zero on success. The
796 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200797 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100798static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200799{
800 uint64_t w;
801
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100802 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200803 return 0;
804
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100805 w = h2_get_n64(b, o + 1);
806 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200807 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
808 h->ff = w >> 32;
809 h->ft = w >> 40;
810 h->len += w >> 48;
811 return 1;
812}
813
814/* skip the next 9 bytes corresponding to the frame header possibly parsed by
815 * h2_peek_frame_hdr() above.
816 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100817static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200818{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200819 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200820}
821
822/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100823static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200824{
825 int ret;
826
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100827 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200828 if (ret > 0)
829 h2_skip_frame_hdr(b);
830 return ret;
831}
832
Willy Tarreau00dd0782018-03-01 16:31:34 +0100833/* marks stream <h2s> as CLOSED and decrement the number of active streams for
834 * its connection if the stream was not yet closed. Please use this exclusively
835 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100836 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100837static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100838{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100839 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100840 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100841 if (!h2s->id)
842 h2s->h2c->nb_reserved--;
Christopher Faulet63768a62019-03-22 14:05:52 +0100843 if (h2s->cs)
844 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100845 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100846 h2s->st = H2_SS_CLOSED;
847}
848
Willy Tarreau71049cc2018-03-28 13:56:39 +0200849/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
850static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100851{
852 h2s_close(h2s);
853 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200854 if (b_size(&h2s->rxbuf)) {
855 b_free(&h2s->rxbuf);
856 offer_buffers(NULL, tasks_run_queue);
857 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200858 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100859 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200860 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100861 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800862 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200863 * reference left would be in the h2c send_list/fctl_list, and if
864 * we're in it, we're getting out anyway
865 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100866 LIST_DEL_INIT(&h2s->list);
867 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200868 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100869 pool_free(pool_head_h2s, h2s);
870}
871
Willy Tarreaua8e49542018-10-03 18:53:55 +0200872/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
873 * stream tree. In case of error, nothing is added and NULL is returned. The
874 * causes of errors can be any failed memory allocation. The caller is
875 * responsible for checking if the connection may support an extra stream
876 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200877 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200878static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200879{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200880 struct h2s *h2s;
881
Willy Tarreaubafbe012017-11-24 17:34:44 +0100882 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200883 if (!h2s)
884 goto out;
885
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200886 h2s->wait_event.task = tasklet_new();
887 if (!h2s->wait_event.task) {
888 pool_free(pool_head_h2s, h2s);
889 goto out;
890 }
891 h2s->send_wait = NULL;
892 h2s->recv_wait = NULL;
893 h2s->wait_event.task->process = h2_deferred_shut;
894 h2s->wait_event.task->context = h2s;
895 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100896 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200897 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100898 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200899 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200900 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200901 h2s->mws = h2c->miw;
902 h2s->flags = H2_SF_NONE;
903 h2s->errcode = H2_ERR_NO_ERROR;
904 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200905 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100906 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200907 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200908
909 if (h2c->flags & H2_CF_IS_BACK) {
910 h1m_init_req(&h2s->h1m);
911 h2s->h1m.err_pos = -1; // don't care about errors on the request path
912 h2s->h1m.flags |= H1_MF_TOLOWER;
913 } else {
914 h1m_init_res(&h2s->h1m);
915 h2s->h1m.err_pos = -1; // don't care about errors on the response path
916 h2s->h1m.flags |= H1_MF_TOLOWER;
917 }
918
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200919 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200920 if (id > 0)
921 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100922 else
923 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200924
925 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100926 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100927 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200928
929 return h2s;
930
931 out_free_h2s:
932 pool_free(pool_head_h2s, h2s);
933 out:
934 return NULL;
935}
936
937/* creates a new stream <id> on the h2c connection and returns it, or NULL in
938 * case of memory allocation error.
939 */
940static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
941{
942 struct session *sess = h2c->conn->owner;
943 struct conn_stream *cs;
944 struct h2s *h2s;
945
946 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
947 goto out;
948
949 h2s = h2s_new(h2c, id);
950 if (!h2s)
951 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200952
953 cs = cs_new(h2c->conn);
954 if (!cs)
955 goto out_close;
956
Olivier Houchard746fb772018-12-15 19:42:00 +0100957 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200958 h2s->cs = cs;
959 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200960 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200961
962 if (stream_create_from_cs(cs) < 0)
963 goto out_free_cs;
964
Willy Tarreau590a0512018-09-05 11:56:48 +0200965 /* We want the accept date presented to the next stream to be the one
966 * we have now, the handshake time to be null (since the next stream
967 * is not delayed by a handshake), and the idle time to count since
968 * right now.
969 */
970 sess->accept_date = date;
971 sess->tv_accept = now;
972 sess->t_handshake = 0;
973
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200974 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100975 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200976 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200977 return h2s;
978
979 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200980 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200981 cs_free(cs);
982 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200983 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200984 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200985 sess_log(sess);
986 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200987}
988
Willy Tarreau751f2d02018-10-05 09:35:00 +0200989/* allocates a new stream associated to conn_stream <cs> on the h2c connection
990 * and returns it, or NULL in case of memory allocation error or if the highest
991 * possible stream ID was reached.
992 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100993static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200994{
995 struct h2s *h2s = NULL;
996
Willy Tarreau86949782019-01-31 10:42:05 +0100997 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200998 goto out;
999
Willy Tarreaua80dca82019-01-24 17:08:28 +01001000 if (h2_streams_left(h2c) < 1)
1001 goto out;
1002
Willy Tarreau751f2d02018-10-05 09:35:00 +02001003 /* Defer choosing the ID until we send the first message to create the stream */
1004 h2s = h2s_new(h2c, 0);
1005 if (!h2s)
1006 goto out;
1007
1008 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001009 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001010 cs->ctx = h2s;
1011 h2c->nb_cs++;
1012
Willy Tarreau751f2d02018-10-05 09:35:00 +02001013 out:
1014 return h2s;
1015}
1016
Willy Tarreaube5b7152017-09-25 16:25:39 +02001017/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1018 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1019 * the various settings codes.
1020 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001021static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001022{
1023 struct buffer *res;
1024 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001025 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001026 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001027 int ret;
1028
1029 if (h2c_mux_busy(h2c, NULL)) {
1030 h2c->flags |= H2_CF_DEM_MBUSY;
1031 return 0;
1032 }
1033
Willy Tarreau44e973f2018-03-01 17:49:30 +01001034 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001035 if (!res) {
1036 h2c->flags |= H2_CF_MUX_MALLOC;
1037 h2c->flags |= H2_CF_DEM_MROOM;
1038 return 0;
1039 }
1040
1041 chunk_init(&buf, buf_data, sizeof(buf_data));
1042 chunk_memcpy(&buf,
1043 "\x00\x00\x00" /* length : 0 for now */
1044 "\x04\x00" /* type : 4 (settings), flags : 0 */
1045 "\x00\x00\x00\x00", /* stream ID : 0 */
1046 9);
1047
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001048 if (h2c->flags & H2_CF_IS_BACK) {
1049 /* send settings_enable_push=0 */
1050 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1051 }
1052
Willy Tarreaube5b7152017-09-25 16:25:39 +02001053 if (h2_settings_header_table_size != 4096) {
1054 char str[6] = "\x00\x01"; /* header_table_size */
1055
1056 write_n32(str + 2, h2_settings_header_table_size);
1057 chunk_memcat(&buf, str, 6);
1058 }
1059
1060 if (h2_settings_initial_window_size != 65535) {
1061 char str[6] = "\x00\x04"; /* initial_window_size */
1062
1063 write_n32(str + 2, h2_settings_initial_window_size);
1064 chunk_memcat(&buf, str, 6);
1065 }
1066
1067 if (h2_settings_max_concurrent_streams != 0) {
1068 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1069
1070 /* Note: 0 means "unlimited" for haproxy's config but not for
1071 * the protocol, so never send this value!
1072 */
1073 write_n32(str + 2, h2_settings_max_concurrent_streams);
1074 chunk_memcat(&buf, str, 6);
1075 }
1076
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001077 mfs = h2_settings_max_frame_size;
1078 if (mfs > global.tune.bufsize)
1079 mfs = global.tune.bufsize;
1080
1081 if (!mfs)
1082 mfs = global.tune.bufsize;
1083
1084 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001085 char str[6] = "\x00\x05"; /* max_frame_size */
1086
1087 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1088 * match bufsize - rewrite size, but at the moment it seems
1089 * that clients don't take care of it.
1090 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001091 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001092 chunk_memcat(&buf, str, 6);
1093 }
1094
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001095 h2_set_frame_size(buf.area, buf.data - 9);
1096 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001097 if (unlikely(ret <= 0)) {
1098 if (!ret) {
1099 h2c->flags |= H2_CF_MUX_MFULL;
1100 h2c->flags |= H2_CF_DEM_MROOM;
1101 return 0;
1102 }
1103 else {
1104 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1105 return 0;
1106 }
1107 }
1108 return ret;
1109}
1110
Willy Tarreau52eed752017-09-22 15:05:09 +02001111/* Try to receive a connection preface, then upon success try to send our
1112 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1113 * missing data. It may return an error in h2c.
1114 */
1115static int h2c_frt_recv_preface(struct h2c *h2c)
1116{
1117 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001118 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001119
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001120 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001121
1122 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001123 if (ret1 < 0)
1124 sess_log(h2c->conn->owner);
1125
Willy Tarreau52eed752017-09-22 15:05:09 +02001126 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1127 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1128 return 0;
1129 }
1130
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001131 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001132 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001133 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001134
Willy Tarreaube5b7152017-09-25 16:25:39 +02001135 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001136}
1137
Willy Tarreau01b44822018-10-03 14:26:37 +02001138/* Try to send a connection preface, then upon success try to send our
1139 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1140 * missing data. It may return an error in h2c.
1141 */
1142static int h2c_bck_send_preface(struct h2c *h2c)
1143{
1144 struct buffer *res;
1145
1146 if (h2c_mux_busy(h2c, NULL)) {
1147 h2c->flags |= H2_CF_DEM_MBUSY;
1148 return 0;
1149 }
1150
1151 res = h2_get_buf(h2c, &h2c->mbuf);
1152 if (!res) {
1153 h2c->flags |= H2_CF_MUX_MALLOC;
1154 h2c->flags |= H2_CF_DEM_MROOM;
1155 return 0;
1156 }
1157
1158 if (!b_data(res)) {
1159 /* preface not yet sent */
1160 b_istput(res, ist(H2_CONN_PREFACE));
1161 }
1162
1163 return h2c_send_settings(h2c);
1164}
1165
Willy Tarreau081d4722017-05-16 21:51:05 +02001166/* try to send a GOAWAY frame on the connection to report an error or a graceful
1167 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1168 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1169 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1170 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1171 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1172 * on unrecoverable failure. It will not attempt to send one again in this last
1173 * case so that it is safe to use h2c_error() to report such errors.
1174 */
1175static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1176{
1177 struct buffer *res;
1178 char str[17];
1179 int ret;
1180
1181 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1182 return 1; // claim that it worked
1183
1184 if (h2c_mux_busy(h2c, h2s)) {
1185 if (h2s)
1186 h2s->flags |= H2_SF_BLK_MBUSY;
1187 else
1188 h2c->flags |= H2_CF_DEM_MBUSY;
1189 return 0;
1190 }
1191
Willy Tarreau44e973f2018-03-01 17:49:30 +01001192 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001193 if (!res) {
1194 h2c->flags |= H2_CF_MUX_MALLOC;
1195 if (h2s)
1196 h2s->flags |= H2_SF_BLK_MROOM;
1197 else
1198 h2c->flags |= H2_CF_DEM_MROOM;
1199 return 0;
1200 }
1201
1202 /* len: 8, type: 7, flags: none, sid: 0 */
1203 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1204
1205 if (h2c->last_sid < 0)
1206 h2c->last_sid = h2c->max_id;
1207
1208 write_n32(str + 9, h2c->last_sid);
1209 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001210 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001211 if (unlikely(ret <= 0)) {
1212 if (!ret) {
1213 h2c->flags |= H2_CF_MUX_MFULL;
1214 if (h2s)
1215 h2s->flags |= H2_SF_BLK_MROOM;
1216 else
1217 h2c->flags |= H2_CF_DEM_MROOM;
1218 return 0;
1219 }
1220 else {
1221 /* we cannot report this error using GOAWAY, so we mark
1222 * it and claim a success.
1223 */
1224 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1225 h2c->flags |= H2_CF_GOAWAY_FAILED;
1226 return 1;
1227 }
1228 }
1229 h2c->flags |= H2_CF_GOAWAY_SENT;
1230 return ret;
1231}
1232
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001233/* Try to send an RST_STREAM frame on the connection for the indicated stream
1234 * during mux operations. This stream must be valid and cannot be closed
1235 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1236 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1237 * not yet.
1238 *
1239 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1240 * to write the message, it subscribes the stream to future notifications.
1241 */
1242static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1243{
1244 struct buffer *res;
1245 char str[13];
1246 int ret;
1247
1248 if (!h2s || h2s->st == H2_SS_CLOSED)
1249 return 1;
1250
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001251 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1252 * RST_STREAM in response to a RST_STREAM frame.
1253 */
1254 if (h2c->dft == H2_FT_RST_STREAM) {
1255 ret = 1;
1256 goto ignore;
1257 }
1258
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001259 if (h2c_mux_busy(h2c, h2s)) {
1260 h2s->flags |= H2_SF_BLK_MBUSY;
1261 return 0;
1262 }
1263
Willy Tarreau44e973f2018-03-01 17:49:30 +01001264 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001265 if (!res) {
1266 h2c->flags |= H2_CF_MUX_MALLOC;
1267 h2s->flags |= H2_SF_BLK_MROOM;
1268 return 0;
1269 }
1270
1271 /* len: 4, type: 3, flags: none */
1272 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1273 write_n32(str + 5, h2s->id);
1274 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001275 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001276
1277 if (unlikely(ret <= 0)) {
1278 if (!ret) {
1279 h2c->flags |= H2_CF_MUX_MFULL;
1280 h2s->flags |= H2_SF_BLK_MROOM;
1281 return 0;
1282 }
1283 else {
1284 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1285 return 0;
1286 }
1287 }
1288
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001289 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001290 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001291 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001292 return ret;
1293}
1294
1295/* Try to send an RST_STREAM frame on the connection for the stream being
1296 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001297 * error code, even if the stream is one of the dummy ones, and will update
1298 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001299 *
1300 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1301 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001302 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001303 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001304 */
1305static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1306{
1307 struct buffer *res;
1308 char str[13];
1309 int ret;
1310
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001311 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1312 * RST_STREAM in response to a RST_STREAM frame.
1313 */
1314 if (h2c->dft == H2_FT_RST_STREAM) {
1315 ret = 1;
1316 goto ignore;
1317 }
1318
Willy Tarreau27a84c92017-10-17 08:10:17 +02001319 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001320 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001321 return 0;
1322 }
1323
Willy Tarreau44e973f2018-03-01 17:49:30 +01001324 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001325 if (!res) {
1326 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001327 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001328 return 0;
1329 }
1330
1331 /* len: 4, type: 3, flags: none */
1332 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001333
Willy Tarreau27a84c92017-10-17 08:10:17 +02001334 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001335 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001336 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001337
Willy Tarreau27a84c92017-10-17 08:10:17 +02001338 if (unlikely(ret <= 0)) {
1339 if (!ret) {
1340 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001341 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001342 return 0;
1343 }
1344 else {
1345 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1346 return 0;
1347 }
1348 }
1349
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001350 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001351 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001352 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001353 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001354 }
1355
Willy Tarreau27a84c92017-10-17 08:10:17 +02001356 return ret;
1357}
1358
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001359/* try to send an empty DATA frame with the ES flag set to notify about the
1360 * end of stream and match a shutdown(write). If an ES was already sent as
1361 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1362 * on success or zero if nothing was done. In case of lack of room to write the
1363 * message, it subscribes the requesting stream to future notifications.
1364 */
1365static int h2_send_empty_data_es(struct h2s *h2s)
1366{
1367 struct h2c *h2c = h2s->h2c;
1368 struct buffer *res;
1369 char str[9];
1370 int ret;
1371
Willy Tarreau721c9742017-11-07 11:05:42 +01001372 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001373 return 1;
1374
1375 if (h2c_mux_busy(h2c, h2s)) {
1376 h2s->flags |= H2_SF_BLK_MBUSY;
1377 return 0;
1378 }
1379
Willy Tarreau44e973f2018-03-01 17:49:30 +01001380 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001381 if (!res) {
1382 h2c->flags |= H2_CF_MUX_MALLOC;
1383 h2s->flags |= H2_SF_BLK_MROOM;
1384 return 0;
1385 }
1386
1387 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1388 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1389 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001390 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001391 if (likely(ret > 0)) {
1392 h2s->flags |= H2_SF_ES_SENT;
1393 }
1394 else if (!ret) {
1395 h2c->flags |= H2_CF_MUX_MFULL;
1396 h2s->flags |= H2_SF_BLK_MROOM;
1397 return 0;
1398 }
1399 else {
1400 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1401 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001402 }
1403 return ret;
1404}
1405
Christopher Fauletf02ca002019-03-07 16:21:34 +01001406/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1407 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1408 * connection. The stream's state is automatically updated accordingly. If the
1409 * stream is orphaned, it is destroyed.
1410 */
1411static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1412{
1413 if (!h2s->cs) {
1414 /* this stream was already orphaned */
1415 h2s_destroy(h2s);
1416 return;
1417 }
1418
1419 h2s->cs->flags |= flags;
1420 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1421 h2s->cs->flags |= CS_FL_ERROR;
1422
1423 h2s_alert(h2s);
1424
1425 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1426 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001427 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001428 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001429 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001430 h2s_close(h2s);
1431}
1432
1433/* wake the streams attached to the connection, whose id is greater than <last>
1434 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001435 */
1436static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1437{
1438 struct eb32_node *node;
1439 struct h2s *h2s;
1440
1441 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001442 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001443
1444 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001445 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001446
Christopher Fauletf02ca002019-03-07 16:21:34 +01001447 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001448 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1449 while (node) {
1450 h2s = container_of(node, struct h2s, by_id);
1451 if (h2s->id <= last)
1452 break;
1453 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001454 h2s_wake_one_stream(h2s, flags);
1455 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001456
Christopher Fauletf02ca002019-03-07 16:21:34 +01001457 /* Wake all streams with unassigned ID (ID == 0) */
1458 node = eb32_lookup(&h2c->streams_by_id, 0);
1459 while (node) {
1460 h2s = container_of(node, struct h2s, by_id);
1461 if (h2s->id > 0)
1462 break;
1463 node = eb32_next(node);
1464 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001465 }
1466}
1467
Willy Tarreau3421aba2017-07-27 15:41:03 +02001468/* Increase all streams' outgoing window size by the difference passed in
1469 * argument. This is needed upon receipt of the settings frame if the initial
1470 * window size is different. The difference may be negative and the resulting
1471 * window size as well, for the time it takes to receive some window updates.
1472 */
1473static void h2c_update_all_ws(struct h2c *h2c, int diff)
1474{
1475 struct h2s *h2s;
1476 struct eb32_node *node;
1477
1478 if (!diff)
1479 return;
1480
1481 node = eb32_first(&h2c->streams_by_id);
1482 while (node) {
1483 h2s = container_of(node, struct h2s, by_id);
1484 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001485
1486 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1487 h2s->flags &= ~H2_SF_BLK_SFCTL;
1488 if (h2s->send_wait)
1489 LIST_ADDQ(&h2c->send_list, &h2s->list);
1490
1491 }
1492
Willy Tarreau3421aba2017-07-27 15:41:03 +02001493 node = eb32_next(node);
1494 }
1495}
1496
1497/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1498 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001499 * return an error in h2c. The caller must have already verified frame length
1500 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001501 */
1502static int h2c_handle_settings(struct h2c *h2c)
1503{
1504 unsigned int offset;
1505 int error;
1506
1507 if (h2c->dff & H2_F_SETTINGS_ACK) {
1508 if (h2c->dfl) {
1509 error = H2_ERR_FRAME_SIZE_ERROR;
1510 goto fail;
1511 }
1512 return 1;
1513 }
1514
Willy Tarreau3421aba2017-07-27 15:41:03 +02001515 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001516 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001517 return 0;
1518
1519 /* parse the frame */
1520 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001521 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1522 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001523
1524 switch (type) {
1525 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1526 /* we need to update all existing streams with the
1527 * difference from the previous iws.
1528 */
1529 if (arg < 0) { // RFC7540#6.5.2
1530 error = H2_ERR_FLOW_CONTROL_ERROR;
1531 goto fail;
1532 }
1533 h2c_update_all_ws(h2c, arg - h2c->miw);
1534 h2c->miw = arg;
1535 break;
1536 case H2_SETTINGS_MAX_FRAME_SIZE:
1537 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1538 error = H2_ERR_PROTOCOL_ERROR;
1539 goto fail;
1540 }
1541 h2c->mfs = arg;
1542 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001543 case H2_SETTINGS_ENABLE_PUSH:
1544 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1545 error = H2_ERR_PROTOCOL_ERROR;
1546 goto fail;
1547 }
1548 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001549 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1550 if (h2c->flags & H2_CF_IS_BACK) {
1551 /* the limit is only for the backend; for the frontend it is our limit */
1552 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1553 arg = h2_settings_max_concurrent_streams;
1554 h2c->streams_limit = arg;
1555 }
1556 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001557 }
1558 }
1559
1560 /* need to ACK this frame now */
1561 h2c->st0 = H2_CS_FRAME_A;
1562 return 1;
1563 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001564 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001565 h2c_error(h2c, error);
1566 return 0;
1567}
1568
1569/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1570 * success or one of the h2_status values.
1571 */
1572static int h2c_ack_settings(struct h2c *h2c)
1573{
1574 struct buffer *res;
1575 char str[9];
1576 int ret = -1;
1577
1578 if (h2c_mux_busy(h2c, NULL)) {
1579 h2c->flags |= H2_CF_DEM_MBUSY;
1580 return 0;
1581 }
1582
Willy Tarreau44e973f2018-03-01 17:49:30 +01001583 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001584 if (!res) {
1585 h2c->flags |= H2_CF_MUX_MALLOC;
1586 h2c->flags |= H2_CF_DEM_MROOM;
1587 return 0;
1588 }
1589
1590 memcpy(str,
1591 "\x00\x00\x00" /* length : 0 (no data) */
1592 "\x04" "\x01" /* type : 4, flags : ACK */
1593 "\x00\x00\x00\x00" /* stream ID */, 9);
1594
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001595 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001596 if (unlikely(ret <= 0)) {
1597 if (!ret) {
1598 h2c->flags |= H2_CF_MUX_MFULL;
1599 h2c->flags |= H2_CF_DEM_MROOM;
1600 return 0;
1601 }
1602 else {
1603 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1604 return 0;
1605 }
1606 }
1607 return ret;
1608}
1609
Willy Tarreaucf68c782017-10-10 17:11:41 +02001610/* processes a PING frame and schedules an ACK if needed. The caller must pass
1611 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001612 * missing data. The caller must have already verified frame length
1613 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001614 */
1615static int h2c_handle_ping(struct h2c *h2c)
1616{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001617 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001618 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001619 h2c->st0 = H2_CS_FRAME_A;
1620 return 1;
1621}
1622
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001623/* Try to send a window update for stream id <sid> and value <increment>.
1624 * Returns > 0 on success or zero on missing room or failure. It may return an
1625 * error in h2c.
1626 */
1627static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1628{
1629 struct buffer *res;
1630 char str[13];
1631 int ret = -1;
1632
1633 if (h2c_mux_busy(h2c, NULL)) {
1634 h2c->flags |= H2_CF_DEM_MBUSY;
1635 return 0;
1636 }
1637
Willy Tarreau44e973f2018-03-01 17:49:30 +01001638 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001639 if (!res) {
1640 h2c->flags |= H2_CF_MUX_MALLOC;
1641 h2c->flags |= H2_CF_DEM_MROOM;
1642 return 0;
1643 }
1644
1645 /* length: 4, type: 8, flags: none */
1646 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1647 write_n32(str + 5, sid);
1648 write_n32(str + 9, increment);
1649
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001650 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001651
1652 if (unlikely(ret <= 0)) {
1653 if (!ret) {
1654 h2c->flags |= H2_CF_MUX_MFULL;
1655 h2c->flags |= H2_CF_DEM_MROOM;
1656 return 0;
1657 }
1658 else {
1659 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1660 return 0;
1661 }
1662 }
1663 return ret;
1664}
1665
1666/* try to send pending window update for the connection. It's safe to call it
1667 * with no pending updates. Returns > 0 on success or zero on missing room or
1668 * failure. It may return an error in h2c.
1669 */
1670static int h2c_send_conn_wu(struct h2c *h2c)
1671{
1672 int ret = 1;
1673
1674 if (h2c->rcvd_c <= 0)
1675 return 1;
1676
Willy Tarreau97aaa672018-12-23 09:49:04 +01001677 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1678 /* increase the advertised connection window to 2G on
1679 * first update.
1680 */
1681 h2c->flags |= H2_CF_WINDOW_OPENED;
1682 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1683 }
1684
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001685 /* send WU for the connection */
1686 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1687 if (ret > 0)
1688 h2c->rcvd_c = 0;
1689
1690 return ret;
1691}
1692
1693/* try to send pending window update for the current dmux stream. It's safe to
1694 * call it with no pending updates. Returns > 0 on success or zero on missing
1695 * room or failure. It may return an error in h2c.
1696 */
1697static int h2c_send_strm_wu(struct h2c *h2c)
1698{
1699 int ret = 1;
1700
1701 if (h2c->rcvd_s <= 0)
1702 return 1;
1703
1704 /* send WU for the stream */
1705 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1706 if (ret > 0)
1707 h2c->rcvd_s = 0;
1708
1709 return ret;
1710}
1711
Willy Tarreaucf68c782017-10-10 17:11:41 +02001712/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1713 * success, 0 on missing data or one of the h2_status values.
1714 */
1715static int h2c_ack_ping(struct h2c *h2c)
1716{
1717 struct buffer *res;
1718 char str[17];
1719 int ret = -1;
1720
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001721 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001722 return 0;
1723
1724 if (h2c_mux_busy(h2c, NULL)) {
1725 h2c->flags |= H2_CF_DEM_MBUSY;
1726 return 0;
1727 }
1728
Willy Tarreau44e973f2018-03-01 17:49:30 +01001729 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001730 if (!res) {
1731 h2c->flags |= H2_CF_MUX_MALLOC;
1732 h2c->flags |= H2_CF_DEM_MROOM;
1733 return 0;
1734 }
1735
1736 memcpy(str,
1737 "\x00\x00\x08" /* length : 8 (same payload) */
1738 "\x06" "\x01" /* type : 6, flags : ACK */
1739 "\x00\x00\x00\x00" /* stream ID */, 9);
1740
1741 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001742 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001743
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001744 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001745 if (unlikely(ret <= 0)) {
1746 if (!ret) {
1747 h2c->flags |= H2_CF_MUX_MFULL;
1748 h2c->flags |= H2_CF_DEM_MROOM;
1749 return 0;
1750 }
1751 else {
1752 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1753 return 0;
1754 }
1755 }
1756 return ret;
1757}
1758
Willy Tarreau26f95952017-07-27 17:18:30 +02001759/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1760 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001761 * h2c or h2s. The caller must have already verified frame length and stream ID
1762 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001763 */
1764static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1765{
1766 int32_t inc;
1767 int error;
1768
Willy Tarreau26f95952017-07-27 17:18:30 +02001769 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001770 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001771 return 0;
1772
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001773 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001774
1775 if (h2c->dsi != 0) {
1776 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001777
1778 /* it's not an error to receive WU on a closed stream */
1779 if (h2s->st == H2_SS_CLOSED)
1780 return 1;
1781
1782 if (!inc) {
1783 error = H2_ERR_PROTOCOL_ERROR;
1784 goto strm_err;
1785 }
1786
1787 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1788 error = H2_ERR_FLOW_CONTROL_ERROR;
1789 goto strm_err;
1790 }
1791
1792 h2s->mws += inc;
1793 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1794 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001795 if (h2s->send_wait)
1796 LIST_ADDQ(&h2c->send_list, &h2s->list);
1797
Willy Tarreau26f95952017-07-27 17:18:30 +02001798 }
1799 }
1800 else {
1801 /* connection window update */
1802 if (!inc) {
1803 error = H2_ERR_PROTOCOL_ERROR;
1804 goto conn_err;
1805 }
1806
1807 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1808 error = H2_ERR_FLOW_CONTROL_ERROR;
1809 goto conn_err;
1810 }
1811
1812 h2c->mws += inc;
1813 }
1814
1815 return 1;
1816
1817 conn_err:
1818 h2c_error(h2c, error);
1819 return 0;
1820
1821 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001822 h2s_error(h2s, error);
1823 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001824 return 0;
1825}
1826
Willy Tarreaue96b0922017-10-30 00:28:29 +01001827/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001828 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1829 * have already verified frame length and stream ID validity. Described in
1830 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001831 */
1832static int h2c_handle_goaway(struct h2c *h2c)
1833{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001834 int last;
1835
Willy Tarreaue96b0922017-10-30 00:28:29 +01001836 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001837 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001838 return 0;
1839
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001840 last = h2_get_n32(&h2c->dbuf, 0);
1841 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001842 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001843 if (h2c->last_sid < 0)
1844 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001845 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001846}
1847
Willy Tarreau92153fc2017-12-03 19:46:19 +01001848/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001849 * invalid. Returns > 0 on success or zero on missing data. It may return an
1850 * error in h2c. The caller must have already verified frame length and stream
1851 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001852 */
1853static int h2c_handle_priority(struct h2c *h2c)
1854{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001855 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001856 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001857 return 0;
1858
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001859 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001860 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001861 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1862 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001863 }
1864 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001865}
1866
Willy Tarreaucd234e92017-08-18 10:59:39 +02001867/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001868 * Returns > 0 on success or zero on missing data. The caller must have already
1869 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001870 */
1871static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1872{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001873 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001874 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001875 return 0;
1876
1877 /* late RST, already handled */
1878 if (h2s->st == H2_SS_CLOSED)
1879 return 1;
1880
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001881 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001882 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001883
1884 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001885 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001886 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001887 }
1888
1889 h2s->flags |= H2_SF_RST_RCVD;
1890 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001891}
1892
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001893/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1894 * It may return an error in h2c or h2s. The caller must consider that the
1895 * return value is the new h2s in case one was allocated (most common case).
1896 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001897 * errors here are reported as connection errors since it's impossible to
1898 * recover from such errors after the compression context has been altered.
1899 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001900static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001901{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001902 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001903 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001904 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001905 int error;
1906
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001907 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001908 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001909
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001910 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001911 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001912
1913 /* now either the frame is complete or the buffer is complete */
1914 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001915 /* The stream exists/existed, this must be a trailers frame */
1916 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001917 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001918 goto out;
1919 goto done;
1920 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001921 /* the connection was already killed by an RST, let's consume
1922 * the data and send another RST.
1923 */
1924 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1925 h2s = (struct h2s*)h2_error_stream;
1926 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001927 }
1928 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1929 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1930 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001931 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001932 goto conn_err;
1933 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001934 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1935 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001936
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001937 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001938
Willy Tarreau25919232019-01-03 14:48:18 +01001939 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001940 if (h2c->st0 >= H2_CS_ERROR)
1941 goto out;
1942
Willy Tarreau25919232019-01-03 14:48:18 +01001943 if (error <= 0) {
1944 if (error == 0)
1945 goto out; // missing data
1946
1947 /* Failed to decode this stream (e.g. too large request)
1948 * but the HPACK decompressor is still synchronized.
1949 */
1950 h2s = (struct h2s*)h2_error_stream;
1951 goto send_rst;
1952 }
1953
Willy Tarreau22de8d32018-09-05 19:55:58 +02001954 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001955 * positively from h2c_frt_stream_new(), the stream will report the error,
1956 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001957 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001958 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001959 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001960 h2s = (struct h2s*)h2_refused_stream;
1961 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001962 }
1963
1964 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001965 h2s->rxbuf = rxbuf;
1966 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001967 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001968
Willy Tarreau88d138e2019-01-02 19:38:14 +01001969 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001970 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001971 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001972
1973 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001974 if (h2s->cs)
1975 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01001976 if (h2s->st == H2_SS_OPEN)
1977 h2s->st = H2_SS_HREM;
1978 else
1979 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02001980 }
1981
Willy Tarreau3a429f02019-01-03 11:41:50 +01001982 /* update the max stream ID if the request is being processed */
1983 if (h2s->id > h2c->max_id)
1984 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001985
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001986 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001987
1988 conn_err:
1989 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001990 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001991
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001992 out:
1993 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001994 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001995
1996 send_rst:
1997 /* make the demux send an RST for the current stream. We may only
1998 * do this if we're certain that the HEADERS frame was properly
1999 * decompressed so that the HPACK decoder is still kept up to date.
2000 */
2001 h2_release_buf(h2c, &rxbuf);
2002 h2c->st0 = H2_CS_FRAME_E;
2003 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002004}
2005
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002006/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2007 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2008 * errors here are reported as connection errors since it's impossible to
2009 * recover from such errors after the compression context has been altered.
2010 */
2011static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2012{
2013 int error;
2014
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002015 if (!b_size(&h2c->dbuf))
2016 return NULL; // empty buffer
2017
2018 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2019 return NULL; // incomplete frame
2020
Willy Tarreau1915ca22019-01-24 11:49:37 +01002021 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002022
Willy Tarreau25919232019-01-03 14:48:18 +01002023 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002024 if (h2c->st0 >= H2_CS_ERROR)
2025 return NULL;
2026
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002027 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2028 /* RFC7540#5.1 */
2029 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2030 h2c->st0 = H2_CS_FRAME_E;
2031 return NULL;
2032 }
2033
Willy Tarreau25919232019-01-03 14:48:18 +01002034 if (error <= 0) {
2035 if (error == 0)
2036 return NULL; // missing data
2037
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002038 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002039 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002040 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002041 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002042 }
2043
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002044 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2045 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002046 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002047 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002048 }
2049
Willy Tarreau927b88b2019-03-04 08:03:25 +01002050 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002051 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002052 else if (h2s->cs && (h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002053 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002054 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002055 h2s_close(h2s);
2056
2057 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002058}
2059
Willy Tarreau454f9052017-10-26 19:40:35 +02002060/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2061 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2062 */
2063static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2064{
2065 int error;
2066
2067 /* note that empty DATA frames are perfectly valid and sometimes used
2068 * to signal an end of stream (with the ES flag).
2069 */
2070
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002071 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002072 return 0; // empty buffer
2073
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002074 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002075 return 0; // incomplete frame
2076
2077 /* now either the frame is complete or the buffer is complete */
2078
Willy Tarreau454f9052017-10-26 19:40:35 +02002079 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2080 /* RFC7540#6.1 */
2081 error = H2_ERR_STREAM_CLOSED;
2082 goto strm_err;
2083 }
2084
Willy Tarreau1915ca22019-01-24 11:49:37 +01002085 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2086 /* RFC7540#8.1.2 */
2087 error = H2_ERR_PROTOCOL_ERROR;
2088 goto strm_err;
2089 }
2090
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002091 if (!h2_frt_transfer_data(h2s))
2092 return 0;
2093
Willy Tarreau454f9052017-10-26 19:40:35 +02002094 /* call the upper layers to process the frame, then let the upper layer
2095 * notify the stream about any change.
2096 */
2097 if (!h2s->cs) {
2098 error = H2_ERR_STREAM_CLOSED;
2099 goto strm_err;
2100 }
2101
Willy Tarreau8f650c32017-11-21 19:36:21 +01002102 if (h2c->st0 >= H2_CS_ERROR)
2103 return 0;
2104
Willy Tarreau721c9742017-11-07 11:05:42 +01002105 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002106 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002107 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002108 }
2109
2110 /* check for completion : the callee will change this to FRAME_A or
2111 * FRAME_H once done.
2112 */
2113 if (h2c->st0 == H2_CS_FRAME_P)
2114 return 0;
2115
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002116 /* last frame */
2117 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002118 if (h2s->cs)
2119 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002120 if (h2s->st == H2_SS_OPEN)
2121 h2s->st = H2_SS_HREM;
2122 else
2123 h2s_close(h2s);
2124
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002125 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002126
2127 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2128 /* RFC7540#8.1.2 */
2129 error = H2_ERR_PROTOCOL_ERROR;
2130 goto strm_err;
2131 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002132 }
2133
Willy Tarreau454f9052017-10-26 19:40:35 +02002134 return 1;
2135
Willy Tarreau454f9052017-10-26 19:40:35 +02002136 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002137 h2s_error(h2s, error);
2138 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002139 return 0;
2140}
2141
Willy Tarreaubc933932017-10-09 16:21:43 +02002142/* process Rx frames to be demultiplexed */
2143static void h2_process_demux(struct h2c *h2c)
2144{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002145 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002146 struct h2_fh hdr;
2147 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002148
Willy Tarreau081d4722017-05-16 21:51:05 +02002149 if (h2c->st0 >= H2_CS_ERROR)
2150 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002151
2152 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2153 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002154 if (h2c->flags & H2_CF_IS_BACK)
2155 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002156 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2157 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002158 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002159 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002160 sess_log(h2c->conn->owner);
2161 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002162 goto fail;
2163 }
2164
2165 h2c->max_id = 0;
2166 h2c->st0 = H2_CS_SETTINGS1;
2167 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002168
2169 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002170 /* ensure that what is pending is a valid SETTINGS frame
2171 * without an ACK.
2172 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002173 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002174 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002175 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002176 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002177 sess_log(h2c->conn->owner);
2178 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002179 goto fail;
2180 }
2181
2182 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2183 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2184 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2185 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002186 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002187 goto fail;
2188 }
2189
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002190 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002191 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2192 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2193 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002194 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002195 goto fail;
2196 }
2197
Willy Tarreau3bf69182018-12-21 15:34:50 +01002198 /* that's OK, switch to FRAME_P to process it. This is
2199 * a SETTINGS frame whose header has already been
2200 * deleted above.
2201 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002202 padlen = 0;
2203 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002204 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002205 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002206
2207 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002208 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002209 int ret = 0;
2210
2211 if (h2c->st0 >= H2_CS_ERROR)
2212 break;
2213
2214 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002215 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002216 break;
2217
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002218 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002219 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002220 if (!h2c->nb_streams) {
2221 /* only log if no other stream can report the error */
2222 sess_log(h2c->conn->owner);
2223 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002224 break;
2225 }
2226
Willy Tarreau3bf69182018-12-21 15:34:50 +01002227 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2228 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2229 * we read the pad length and drop it from the remaining
2230 * payload (one byte + the 9 remaining ones = 10 total
2231 * removed), so we have a frame payload starting after the
2232 * pad len. Flow controlled frames (DATA) also count the
2233 * padlen in the flow control, so it must be adjusted.
2234 */
2235 if (hdr.len < 1) {
2236 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2237 sess_log(h2c->conn->owner);
2238 goto fail;
2239 }
2240 hdr.len--;
2241
2242 if (b_data(&h2c->dbuf) < 10)
2243 break; // missing padlen
2244
2245 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2246
2247 if (padlen > hdr.len) {
2248 /* RFC7540#6.1 : pad length = length of
2249 * frame payload or greater => error.
2250 */
2251 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2252 sess_log(h2c->conn->owner);
2253 goto fail;
2254 }
2255
2256 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2257 h2c->rcvd_c++;
2258 h2c->rcvd_s++;
2259 }
2260 b_del(&h2c->dbuf, 1);
2261 }
2262 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002263
2264 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002265 h2c->dfl = hdr.len;
2266 h2c->dsi = hdr.sid;
2267 h2c->dft = hdr.ft;
2268 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002269 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002270 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002271
2272 /* check for minimum basic frame format validity */
2273 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2274 if (ret != H2_ERR_NO_ERROR) {
2275 h2c_error(h2c, ret);
2276 sess_log(h2c->conn->owner);
2277 goto fail;
2278 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002279 }
2280
2281 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002282 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2283
Willy Tarreau567beb82018-12-18 16:52:44 +01002284 if (tmp_h2s != h2s && h2s && h2s->cs &&
2285 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002286 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002287 /* we may have to signal the upper layers */
2288 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002289 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002290 }
2291 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002292
Willy Tarreaud7901432017-12-29 11:34:40 +01002293 if (h2c->st0 == H2_CS_FRAME_E)
2294 goto strm_err;
2295
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002296 if (h2s->st == H2_SS_IDLE &&
2297 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2298 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2299 * this state MUST be treated as a connection error
2300 */
2301 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002302 if (!h2c->nb_streams) {
2303 /* only log if no other stream can report the error */
2304 sess_log(h2c->conn->owner);
2305 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002306 break;
2307 }
2308
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002309 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2310 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2311 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002312 * this state MUST be treated as a stream error.
2313 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2314 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002315 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002316 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2317 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2318 else
2319 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002320 goto strm_err;
2321 }
2322
Willy Tarreauab837502017-12-27 15:07:30 +01002323 /* Below the management of frames received in closed state is a
2324 * bit hackish because the spec makes strong differences between
2325 * streams closed by receiving RST, sending RST, and seeing ES
2326 * in both directions. In addition to this, the creation of a
2327 * new stream reusing the identifier of a closed one will be
2328 * detected here. Given that we cannot keep track of all closed
2329 * streams forever, we consider that unknown closed streams were
2330 * closed on RST received, which allows us to respond with an
2331 * RST without breaking the connection (eg: to abort a transfer).
2332 * Some frames have to be silently ignored as well.
2333 */
2334 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002335 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002336 /* #5.1.1: The identifier of a newly
2337 * established stream MUST be numerically
2338 * greater than all streams that the initiating
2339 * endpoint has opened or reserved. This
2340 * governs streams that are opened using a
2341 * HEADERS frame and streams that are reserved
2342 * using PUSH_PROMISE. An endpoint that
2343 * receives an unexpected stream identifier
2344 * MUST respond with a connection error.
2345 */
2346 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2347 goto strm_err;
2348 }
2349
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002350 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002351 /* RFC7540#5.1:closed: an endpoint that
2352 * receives any frame other than PRIORITY after
2353 * receiving a RST_STREAM MUST treat that as a
2354 * stream error of type STREAM_CLOSED.
2355 *
2356 * Note that old streams fall into this category
2357 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002358 *
2359 * However, we cannot generalize this to all frame types. Those
2360 * carrying compression state must still be processed before
2361 * being dropped or we'll desynchronize the decoder. This can
2362 * happen with request trailers received after sending an
2363 * RST_STREAM, or with header/trailers responses received after
2364 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002365 */
2366 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2367 h2c->st0 = H2_CS_FRAME_E;
2368 goto strm_err;
2369 }
2370
2371 /* RFC7540#5.1:closed: if this state is reached as a
2372 * result of sending a RST_STREAM frame, the peer that
2373 * receives the RST_STREAM might have already sent
2374 * frames on the stream that cannot be withdrawn. An
2375 * endpoint MUST ignore frames that it receives on
2376 * closed streams after it has sent a RST_STREAM
2377 * frame. An endpoint MAY choose to limit the period
2378 * over which it ignores frames and treat frames that
2379 * arrive after this time as being in error.
2380 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002381 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002382 /* RFC7540#5.1:closed: any frame other than
2383 * PRIO/WU/RST in this state MUST be treated as
2384 * a connection error
2385 */
2386 if (h2c->dft != H2_FT_RST_STREAM &&
2387 h2c->dft != H2_FT_PRIORITY &&
2388 h2c->dft != H2_FT_WINDOW_UPDATE) {
2389 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2390 goto strm_err;
2391 }
2392 }
2393 }
2394
Willy Tarreauc0da1962017-10-30 18:38:00 +01002395#if 0
2396 // problem below: it is not possible to completely ignore such
2397 // streams as we need to maintain the compression state as well
2398 // and for this we need to completely process these frames (eg:
2399 // HEADERS frames) as well as counting DATA frames to emit
2400 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2401 // This is a typical case of layer violation where the
2402 // transported contents are critical to the connection's
2403 // validity and must be ignored at the same time :-(
2404
2405 /* graceful shutdown, ignore streams whose ID is higher than
2406 * the one advertised in GOAWAY. RFC7540#6.8.
2407 */
2408 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002409 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2410 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002411 h2c->dfl -= ret;
2412 ret = h2c->dfl == 0;
2413 goto strm_err;
2414 }
2415#endif
2416
Willy Tarreau7e98c052017-10-10 15:56:59 +02002417 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002418 case H2_FT_SETTINGS:
2419 if (h2c->st0 == H2_CS_FRAME_P)
2420 ret = h2c_handle_settings(h2c);
2421
2422 if (h2c->st0 == H2_CS_FRAME_A)
2423 ret = h2c_ack_settings(h2c);
2424 break;
2425
Willy Tarreaucf68c782017-10-10 17:11:41 +02002426 case H2_FT_PING:
2427 if (h2c->st0 == H2_CS_FRAME_P)
2428 ret = h2c_handle_ping(h2c);
2429
2430 if (h2c->st0 == H2_CS_FRAME_A)
2431 ret = h2c_ack_ping(h2c);
2432 break;
2433
Willy Tarreau26f95952017-07-27 17:18:30 +02002434 case H2_FT_WINDOW_UPDATE:
2435 if (h2c->st0 == H2_CS_FRAME_P)
2436 ret = h2c_handle_window_update(h2c, h2s);
2437 break;
2438
Willy Tarreau61290ec2017-10-17 08:19:21 +02002439 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002440 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2441 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2442 * frames' parsers consume all following CONTINUATION
2443 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002444 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002445 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2446 sess_log(h2c->conn->owner);
2447 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002448
Willy Tarreau13278b42017-10-13 19:23:14 +02002449 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002450 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002451 if (h2c->flags & H2_CF_IS_BACK)
2452 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2453 else
2454 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002455 if (tmp_h2s) {
2456 h2s = tmp_h2s;
2457 ret = 1;
2458 }
2459 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002460 break;
2461
Willy Tarreau454f9052017-10-26 19:40:35 +02002462 case H2_FT_DATA:
2463 if (h2c->st0 == H2_CS_FRAME_P)
2464 ret = h2c_frt_handle_data(h2c, h2s);
2465
2466 if (h2c->st0 == H2_CS_FRAME_A)
2467 ret = h2c_send_strm_wu(h2c);
2468 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002469
Willy Tarreau92153fc2017-12-03 19:46:19 +01002470 case H2_FT_PRIORITY:
2471 if (h2c->st0 == H2_CS_FRAME_P)
2472 ret = h2c_handle_priority(h2c);
2473 break;
2474
Willy Tarreaucd234e92017-08-18 10:59:39 +02002475 case H2_FT_RST_STREAM:
2476 if (h2c->st0 == H2_CS_FRAME_P)
2477 ret = h2c_handle_rst_stream(h2c, h2s);
2478 break;
2479
Willy Tarreaue96b0922017-10-30 00:28:29 +01002480 case H2_FT_GOAWAY:
2481 if (h2c->st0 == H2_CS_FRAME_P)
2482 ret = h2c_handle_goaway(h2c);
2483 break;
2484
Willy Tarreau1c661982017-10-30 13:52:01 +01002485 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002486 default:
2487 /* drop frames that we ignore. They may be larger than
2488 * the buffer so we drain all of their contents until
2489 * we reach the end.
2490 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002491 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2492 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002493 h2c->dfl -= ret;
2494 ret = h2c->dfl == 0;
2495 }
2496
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002497 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002498 /* We may have to send an RST if not done yet */
2499 if (h2s->st == H2_SS_ERROR)
2500 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002501
Willy Tarreaua20a5192017-12-27 11:02:06 +01002502 if (h2c->st0 == H2_CS_FRAME_E)
2503 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002504
Willy Tarreau7e98c052017-10-10 15:56:59 +02002505 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002506 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002507 break;
2508
2509 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002510 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002511 h2c->st0 = H2_CS_FRAME_H;
2512 }
2513 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002514
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002515 if (h2c->rcvd_c > 0 &&
2516 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2517 h2c_send_conn_wu(h2c);
2518
Willy Tarreau52eed752017-09-22 15:05:09 +02002519 fail:
2520 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002521 if (h2s && h2s->cs &&
2522 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002523 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002524 /* we may have to signal the upper layers */
2525 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002526 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002527 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002528
Willy Tarreau47b515a2018-12-21 16:09:41 +01002529 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002530}
2531
2532/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2533 * the end.
2534 */
2535static int h2_process_mux(struct h2c *h2c)
2536{
Olivier Houchardd360ac62019-03-22 17:37:16 +01002537 struct h2s *h2s;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002538
Willy Tarreau01b44822018-10-03 14:26:37 +02002539 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2540 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2541 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2542 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2543 if (h2c->st0 == H2_CS_ERROR) {
2544 h2c->st0 = H2_CS_ERROR2;
2545 sess_log(h2c->conn->owner);
2546 }
2547 goto fail;
2548 }
2549 h2c->st0 = H2_CS_SETTINGS1;
2550 }
2551 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002552 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002553 return 1;
2554 }
2555
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002556 /* start by sending possibly pending window updates */
2557 if (h2c->rcvd_c > 0 &&
2558 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2559 h2c_send_conn_wu(h2c) < 0)
2560 goto fail;
2561
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002562 /* First we always process the flow control list because the streams
2563 * waiting there were already elected for immediate emission but were
2564 * blocked just on this.
2565 */
2566
Olivier Houchardd360ac62019-03-22 17:37:16 +01002567 list_for_each_entry(h2s, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002568 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2569 h2c->st0 >= H2_CS_ERROR)
2570 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002571 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2572 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002573
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002574 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002575 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2576 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002577 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002578 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002579 }
2580
Olivier Houchardd360ac62019-03-22 17:37:16 +01002581 list_for_each_entry(h2s, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002582 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2583 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002584 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2585 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002586
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002587 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002588 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2589 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002590 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002591 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002592 }
2593
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002594 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002595 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002596 if (h2c->st0 == H2_CS_ERROR) {
2597 if (h2c->max_id >= 0) {
2598 h2c_send_goaway_error(h2c, NULL);
2599 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2600 return 0;
2601 }
2602
2603 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2604 }
2605 return 1;
2606 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002607 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002608}
2609
Willy Tarreau62f52692017-10-08 23:01:42 +02002610
Willy Tarreau479998a2018-11-18 06:30:59 +01002611/* Attempt to read data, and subscribe if none available.
2612 * The function returns 1 if data has been received, otherwise zero.
2613 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002614static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002615{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002616 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002617 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002618 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002619 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002620
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002621 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002622 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002623
Willy Tarreau315d8072017-12-10 22:17:57 +01002624 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002625 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002626
Willy Tarreau44e973f2018-03-01 17:49:30 +01002627 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002628 if (!buf) {
2629 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002630 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002631 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002632
Olivier Houchard7505f942018-08-21 18:10:44 +02002633 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002634 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002635 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2636 /* HTX in use : try to pre-align the buffer like the
2637 * rxbufs will be to optimize memory copies. We'll make
2638 * sure that the frame header lands at the end of the
2639 * HTX block to alias it upon recv. We cannot use the
2640 * head because rcv_buf() will realign the buffer if
2641 * it's empty. Thus we cheat and pretend we already
2642 * have a few bytes there.
2643 */
2644 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002645 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002646 }
2647 else
2648 max = b_room(buf);
2649
Olivier Houchard7505f942018-08-21 18:10:44 +02002650 if (max)
2651 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2652 else
2653 ret = 0;
2654 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002655
Olivier Houchard53216e72018-10-10 15:46:36 +02002656 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002657 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002658
Olivier Houcharda1411e62018-08-17 18:42:48 +02002659 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002660 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002661 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002662 }
2663
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002664 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002665 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002666 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002667}
2668
Willy Tarreau479998a2018-11-18 06:30:59 +01002669/* Try to send data if possible.
2670 * The function returns 1 if data have been sent, otherwise zero.
2671 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002672static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002673{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002674 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002675 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002676 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002677
2678 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002679 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002680
Olivier Houchard7505f942018-08-21 18:10:44 +02002681
Willy Tarreaua2af5122017-10-09 11:56:46 +02002682 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2683 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002684 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002685 }
2686
Willy Tarreaubc933932017-10-09 16:21:43 +02002687 /* This loop is quite simple : it tries to fill as much as it can from
2688 * pending streams into the existing buffer until it's reportedly full
2689 * or the end of send requests is reached. Then it tries to send this
2690 * buffer's contents out, marks it not full if at least one byte could
2691 * be sent, and tries again.
2692 *
2693 * The snd_buf() function normally takes a "flags" argument which may
2694 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2695 * data immediately comes and CO_SFL_STREAMER to indicate that the
2696 * connection is streaming lots of data (used to increase TLS record
2697 * size at the expense of latency). The former can be sent any time
2698 * there's a buffer full flag, as it indicates at least one stream
2699 * attempted to send and failed so there are pending data. An
2700 * alternative would be to set it as long as there's an active stream
2701 * but that would be problematic for ACKs until we have an absolute
2702 * guarantee that all waiters have at least one byte to send. The
2703 * latter should possibly not be set for now.
2704 */
2705
2706 done = 0;
2707 while (!done) {
2708 unsigned int flags = 0;
2709
2710 /* fill as much as we can into the current buffer */
2711 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2712 done = h2_process_mux(h2c);
2713
Olivier Houchard2b094432019-01-29 18:28:36 +01002714 if (h2c->flags & H2_CF_MUX_MALLOC)
2715 break;
2716
Willy Tarreaubc933932017-10-09 16:21:43 +02002717 if (conn->flags & CO_FL_ERROR)
2718 break;
2719
2720 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2721 flags |= CO_SFL_MSG_MORE;
2722
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002723 if (b_data(&h2c->mbuf)) {
2724 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002725 if (!ret)
2726 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002727 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002728 b_del(&h2c->mbuf, ret);
2729 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002730 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002731
2732 /* wrote at least one byte, the buffer is not full anymore */
2733 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2734 }
2735
Willy Tarreaua2af5122017-10-09 11:56:46 +02002736 if (conn->flags & CO_FL_SOCK_WR_SH) {
2737 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002738 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002739 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002740 /* We're not full anymore, so we can wake any task that are waiting
2741 * for us.
2742 */
2743 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002744 struct h2s *h2s;
2745
2746 list_for_each_entry(h2s, &h2c->send_list, list) {
2747 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2748 break;
2749 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2750 continue;
2751
2752 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002753 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2754 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002755 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002756 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002757 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002758 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002759 /* We're done, no more to send */
2760 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002761 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002762schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002763 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2764 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002765 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002766}
2767
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002768/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002769static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2770{
2771 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002772 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002773
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002774 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002775 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002776 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002777 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002778 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002779 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002780 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002781}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002782
Willy Tarreau62f52692017-10-08 23:01:42 +02002783/* callback called on any event by the connection handler.
2784 * It applies changes and returns zero, or < 0 if it wants immediate
2785 * destruction of the connection (which normally doesn not happen in h2).
2786 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002787static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002788{
Olivier Houchard7505f942018-08-21 18:10:44 +02002789 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002790
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002791 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002792 h2_process_demux(h2c);
2793
2794 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002795 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002796
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002797 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002798 h2c->flags &= ~H2_CF_DEM_DFULL;
2799 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002800 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002801
Willy Tarreau0b37d652018-10-03 10:33:02 +02002802 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002803 /* frontend is stopping, reload likely in progress, let's try
2804 * to announce a graceful shutdown if not yet done. We don't
2805 * care if it fails, it will be tried again later.
2806 */
2807 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2808 if (h2c->last_sid < 0)
2809 h2c->last_sid = (1U << 31) - 1;
2810 h2c_send_goaway_error(h2c, NULL);
2811 }
2812 }
2813
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002814 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002815 * If we received early data, and the handshake is done, wake
2816 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002817 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002818 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2819 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2820 struct eb32_node *node;
2821 struct h2s *h2s;
2822
2823 h2c->flags |= H2_CF_WAIT_FOR_HS;
2824 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2825
2826 while (node) {
2827 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002828 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002829 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002830 node = eb32_next(node);
2831 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002832 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002833
Willy Tarreau26bd7612017-10-09 16:47:04 +02002834 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002835 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2836 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2837 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002838 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002839
2840 if (eb_is_empty(&h2c->streams_by_id)) {
2841 /* no more stream, kill the connection now */
2842 h2_release(conn);
2843 return -1;
2844 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002845 }
2846
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002847 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002848 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002849
Olivier Houchard53216e72018-10-10 15:46:36 +02002850 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2851 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2852 (h2c->st0 != H2_CS_ERROR &&
2853 !b_data(&h2c->mbuf) &&
2854 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2855 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002856 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002857
Willy Tarreau3f133572017-10-31 19:21:06 +01002858 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002859 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002860 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002861 task_queue(h2c->task);
2862 }
2863 else
2864 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002865 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002866
Olivier Houchard7505f942018-08-21 18:10:44 +02002867 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002868 return 0;
2869}
2870
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002871/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002872static int h2_wake(struct connection *conn)
2873{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002874 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002875
2876 return (h2_process(h2c));
2877}
2878
Willy Tarreauea392822017-10-31 10:02:25 +01002879/* Connection timeout management. The principle is that if there's no receipt
2880 * nor sending for a certain amount of time, the connection is closed. If the
2881 * MUX buffer still has lying data or is not allocatable, the connection is
2882 * immediately killed. If it's allocatable and empty, we attempt to send a
2883 * GOAWAY frame.
2884 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002885static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002886{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002887 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002888 int expired = tick_is_expired(t->expire, now_ms);
2889
Willy Tarreau0975f112018-03-29 15:22:59 +02002890 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002891 return t;
2892
Willy Tarreau0975f112018-03-29 15:22:59 +02002893 task_delete(t);
2894 task_free(t);
2895
2896 if (!h2c) {
2897 /* resources were already deleted */
2898 return NULL;
2899 }
2900
2901 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002902 h2c_error(h2c, H2_ERR_NO_ERROR);
2903 h2_wake_some_streams(h2c, 0, 0);
2904
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002905 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002906 /* don't even try to send a GOAWAY, the buffer is stuck */
2907 h2c->flags |= H2_CF_GOAWAY_FAILED;
2908 }
2909
2910 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002911 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002912 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2913 h2c->flags |= H2_CF_GOAWAY_FAILED;
2914
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002915 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2916 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002917 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002918 b_del(&h2c->mbuf, ret);
2919 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002920 }
2921 }
Willy Tarreauea392822017-10-31 10:02:25 +01002922
Willy Tarreau0975f112018-03-29 15:22:59 +02002923 /* either we can release everything now or it will be done later once
2924 * the last stream closes.
2925 */
2926 if (eb_is_empty(&h2c->streams_by_id))
2927 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002928
Willy Tarreauea392822017-10-31 10:02:25 +01002929 return NULL;
2930}
2931
2932
Willy Tarreau62f52692017-10-08 23:01:42 +02002933/*******************************************/
2934/* functions below are used by the streams */
2935/*******************************************/
2936
2937/*
2938 * Attach a new stream to a connection
2939 * (Used for outgoing connections)
2940 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002941static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002942{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002943 struct conn_stream *cs;
2944 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002945 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002946
2947 cs = cs_new(conn);
2948 if (!cs)
2949 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002950 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002951 if (!h2s) {
2952 cs_free(cs);
2953 return NULL;
2954 }
2955 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002956}
2957
Willy Tarreaufafd3982018-11-18 21:29:20 +01002958/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2959 * We have to scan because we may have some orphan streams. It might be
2960 * beneficial to scan backwards from the end to reduce the likeliness to find
2961 * orphans.
2962 */
2963static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2964{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002965 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002966 struct h2s *h2s;
2967 struct eb32_node *node;
2968
2969 node = eb32_first(&h2c->streams_by_id);
2970 while (node) {
2971 h2s = container_of(node, struct h2s, by_id);
2972 if (h2s->cs)
2973 return h2s->cs;
2974 node = eb32_next(node);
2975 }
2976 return NULL;
2977}
2978
Willy Tarreau62f52692017-10-08 23:01:42 +02002979/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002980 * Destroy the mux and the associated connection, if it is no longer used
2981 */
2982static void h2_destroy(struct connection *conn)
2983{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002984 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002985
2986 if (eb_is_empty(&h2c->streams_by_id))
2987 h2_release(h2c->conn);
2988}
2989
2990/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002991 * Detach the stream from the connection and possibly release the connection.
2992 */
2993static void h2_detach(struct conn_stream *cs)
2994{
Willy Tarreau60935142017-10-16 18:11:19 +02002995 struct h2s *h2s = cs->ctx;
2996 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002997 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002998
2999 cs->ctx = NULL;
3000 if (!h2s)
3001 return;
3002
Olivier Houchardf502aca2018-12-14 19:42:40 +01003003 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003004 h2c = h2s->h2c;
3005 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003006 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003007 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3008 !h2_frt_has_too_many_cs(h2c)) {
3009 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003010 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003011 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02003012 }
Willy Tarreau60935142017-10-16 18:11:19 +02003013
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003014 /* this stream may be blocked waiting for some data to leave (possibly
3015 * an ES or RST frame), so orphan it in this case.
3016 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003017 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003018 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003019 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003020 return;
3021
Willy Tarreau45f752e2017-10-30 15:44:59 +01003022 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3023 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3024 /* unblock the connection if it was blocked on this
3025 * stream.
3026 */
3027 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3028 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003029 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003030 }
3031
Willy Tarreau71049cc2018-03-28 13:56:39 +02003032 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003033
Olivier Houchard8a786902018-12-15 16:05:40 +01003034 if (h2c->flags & H2_CF_IS_BACK &&
3035 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003036 if (!(h2c->conn->flags &
3037 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3038 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003039 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003040 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3041 h2c->conn->owner = NULL;
3042 if (eb_is_empty(&h2c->streams_by_id)) {
3043 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3044 /* The server doesn't want it, let's kill the connection right away */
3045 h2c->conn->mux->destroy(h2c->conn);
3046 return;
3047 }
3048 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003049 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003050 if (eb_is_empty(&h2c->streams_by_id)) {
3051 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3052 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3053 return;
3054 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003055 /* Never ever allow to reuse a connection from a non-reuse backend */
3056 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3057 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003058 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003059 struct server *srv = objt_server(h2c->conn->target);
3060
3061 if (srv) {
3062 if (h2c->conn->flags & CO_FL_PRIVATE)
3063 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3064 else
3065 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3066 }
3067
3068 }
3069 }
3070 }
3071
Willy Tarreaue323f342018-03-28 13:51:45 +02003072 /* We don't want to close right now unless we're removing the
3073 * last stream, and either the connection is in error, or it
3074 * reached the ID already specified in a GOAWAY frame received
3075 * or sent (as seen by last_sid >= 0).
3076 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003077 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003078 /* no more stream will come, kill it now */
3079 h2_release(h2c->conn);
3080 }
3081 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003082 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003083 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3084 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003085 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003086 else
3087 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003088 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003089}
3090
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003091/* Performs a synchronous or asynchronous shutr().
3092 * FIXME: guess what the return code tries to indicate!
3093 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003094static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003095{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003096 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003097 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003098
Willy Tarreau721c9742017-11-07 11:05:42 +01003099 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003100 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003101
Willy Tarreau18059042019-01-31 19:12:48 +01003102 /* a connstream may require us to immediately kill the whole connection
3103 * for example because of a "tcp-request content reject" rule that is
3104 * normally used to limit abuse. In this case we schedule a goaway to
3105 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003106 */
Willy Tarreau18059042019-01-31 19:12:48 +01003107 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3108 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3109 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3110 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3111 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003112 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3113 /* Nothing was never sent for this stream, so reset with
3114 * REFUSED_STREAM error to let the client retry the
3115 * request.
3116 */
3117 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3118 }
Willy Tarreau18059042019-01-31 19:12:48 +01003119
Willy Tarreau90c32322017-11-24 08:00:30 +01003120 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003121 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003122 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003123
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003124 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003125 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003126 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003127
Olivier Houchard7a977432019-03-21 15:47:13 +01003128 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003129add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003130 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003131 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003132 if (h2s->flags & H2_SF_BLK_MFCTL) {
3133 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3134 h2s->send_wait = sw;
3135 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3136 h2s->send_wait = sw;
3137 LIST_ADDQ(&h2c->send_list, &h2s->list);
3138 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003139 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003140 /* Let the handler know we want shutr */
3141 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003142 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003143}
3144
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003145/* Performs a synchronous or asynchronous shutw().
3146 * FIXME: guess what the return code tries to indicate!
3147 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003148static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003149{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003150 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003151 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003152
Willy Tarreau721c9742017-11-07 11:05:42 +01003153 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003154 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003155
Willy Tarreau67434202017-11-06 20:20:51 +01003156 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003157 /* we can cleanly close using an empty data frame only after headers */
3158
3159 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3160 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003161 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003162
3163 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003164 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003165 else
3166 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003167 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003168 /* a connstream may require us to immediately kill the whole connection
3169 * for example because of a "tcp-request content reject" rule that is
3170 * normally used to limit abuse. In this case we schedule a goaway to
3171 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003172 */
Willy Tarreau18059042019-01-31 19:12:48 +01003173 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3174 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3175 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3176 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3177 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003178 else {
3179 /* Nothing was never sent for this stream, so reset with
3180 * REFUSED_STREAM error to let the client retry the
3181 * request.
3182 */
3183 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3184 }
Willy Tarreau18059042019-01-31 19:12:48 +01003185
Willy Tarreau90c32322017-11-24 08:00:30 +01003186 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003187 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003188 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003189
Willy Tarreau00dd0782018-03-01 16:31:34 +01003190 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003191 }
3192
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003193 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003194 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003195 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003196
3197 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003198 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003199 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003200 if (h2s->flags & H2_SF_BLK_MFCTL) {
3201 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3202 h2s->send_wait = sw;
3203 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3204 h2s->send_wait = sw;
3205 LIST_ADDQ(&h2c->send_list, &h2s->list);
3206 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003207 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003208 /* let the handler know we want to shutw */
3209 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003210 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003211}
3212
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003213/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3214 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3215 * and prevented the last frame from being emitted.
3216 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003217static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3218{
3219 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003220 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003221 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003222
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003223 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003224 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003225 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003226 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003227 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003228 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003229 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003230 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003231 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003232
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003233 if (!ret) {
3234 /* We're done trying to send, remove ourself from the send_list */
3235 h2s->send_wait->events &= ~SUB_RETRY_SEND;
3236 h2s->send_wait = NULL;
3237 LIST_DEL_INIT(&h2s->list);
3238 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003239 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003240 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003241 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003242 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003243
3244 if (h2c_is_dead(h2c))
3245 h2_release(h2c->conn);
3246 }
3247
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003248 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003249}
3250
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003251/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003252static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3253{
3254 struct h2s *h2s = cs->ctx;
3255
3256 if (!mode)
3257 return;
3258
3259 h2_do_shutr(h2s);
3260}
3261
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003262/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003263static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3264{
3265 struct h2s *h2s = cs->ctx;
3266
3267 h2_do_shutw(h2s);
3268}
3269
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003270/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003271 * HTX request or response depending on the connection's side. Returns a
3272 * positive value on success, a negative value on failure, or 0 if it couldn't
3273 * proceed. May report connection errors in h2c->errcode if the frame is
3274 * non-decodable and the connection unrecoverable. In absence of connection
3275 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003276 *
3277 * The function may fold CONTINUATION frames into the initial HEADERS frame
3278 * by removing padding and next frame header, then moving the CONTINUATION
3279 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3280 * leaving a hole between the main frame and the beginning of the next one.
3281 * The possibly remaining incomplete or next frame at the end may be moved
3282 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3283 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3284 *
3285 * A buffer at the beginning of processing may look like this :
3286 *
3287 * ,---.---------.-----.--------------.--------------.------.---.
3288 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3289 * `---^---------^-----^--------------^--------------^------^---'
3290 * | | <-----> | |
3291 * area | dpl | wrap
3292 * |<--------------> |
3293 * | dfl |
3294 * |<-------------------------------------------------->|
3295 * head data
3296 *
3297 * Padding is automatically overwritten when folding, participating to the
3298 * hole size after dfl :
3299 *
3300 * ,---.------------------------.-----.--------------.------.---.
3301 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3302 * `---^------------------------^-----^--------------^------^---'
3303 * | | <-----> | |
3304 * area | hole | wrap
3305 * |<-----------------------> |
3306 * | dfl |
3307 * |<-------------------------------------------------->|
3308 * head data
3309 *
3310 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3311 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3312 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003313 *
3314 * The <flags> field must point to either the stream's flags or to a copy of it
3315 * so that the function can update the following flags :
3316 * - H2_SF_DATA_CLEN when content-length is seen
3317 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3318 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003319 *
3320 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3321 * decoding, in order to detect if we're dealing with a headers or a trailers
3322 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003323 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003324static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len)
Willy Tarreau13278b42017-10-13 19:23:14 +02003325{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003326 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003327 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003328 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003329 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003330 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003331 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003332 int flen; // header frame len
3333 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003334 int ret = 0;
3335 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003336 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003337 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003338
Willy Tarreauea18f862018-12-22 20:19:26 +01003339next_frame:
3340 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3341 goto leave; // incomplete input frame
3342
3343 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3344 * this case, we'll try to paste it immediately after the initial
3345 * HEADERS frame payload and kill any possible padding. The initial
3346 * frame's length will be increased to represent the concatenation
3347 * of the two frames. The next frame is read from position <tlen>
3348 * and written at position <flen> (minus padding if some is present).
3349 */
3350 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3351 struct h2_fh hdr;
3352 int clen; // CONTINUATION frame's payload length
3353
3354 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3355 /* no more data, the buffer may be full, either due to
3356 * too large a frame or because of too large a hole that
3357 * we're going to compact at the end.
3358 */
3359 goto leave;
3360 }
3361
3362 if (hdr.ft != H2_FT_CONTINUATION) {
3363 /* RFC7540#6.10: frame of unexpected type */
3364 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3365 goto fail;
3366 }
3367
3368 if (hdr.sid != h2c->dsi) {
3369 /* RFC7540#6.10: frame of different stream */
3370 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3371 goto fail;
3372 }
3373
3374 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3375 /* RFC7540#4.2: invalid frame length */
3376 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3377 goto fail;
3378 }
3379
3380 /* detect when we must stop aggragating frames */
3381 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3382
3383 /* Take as much as we can of the CONTINUATION frame's payload */
3384 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3385 if (clen > hdr.len)
3386 clen = hdr.len;
3387
3388 /* Move the frame's payload over the padding, hole and frame
3389 * header. At least one of hole or dpl is null (see diagrams
3390 * above). The hole moves after the new aggragated frame.
3391 */
3392 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3393 h2c->dfl += clen - h2c->dpl;
3394 hole += h2c->dpl + 9;
3395 h2c->dpl = 0;
3396 goto next_frame;
3397 }
3398
3399 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003400
Willy Tarreau13278b42017-10-13 19:23:14 +02003401 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003402 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003403 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003404 copy = alloc_trash_chunk();
3405 if (!copy) {
3406 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3407 goto fail;
3408 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003409 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3410 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3411 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003412 }
3413
Willy Tarreau13278b42017-10-13 19:23:14 +02003414 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3415 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003416 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003417 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3418 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003419 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003420 }
3421
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003422 if (flen < 5) {
3423 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3424 goto fail;
3425 }
3426
Willy Tarreau13278b42017-10-13 19:23:14 +02003427 hdrs += 5; // stream dep = 4, weight = 1
3428 flen -= 5;
3429 }
3430
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003431 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003432 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003433 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003434 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003435
Willy Tarreau937f7602018-02-26 15:22:17 +01003436 /* we can't retry a failed decompression operation so we must be very
3437 * careful not to take any risks. In practice the output buffer is
3438 * always empty except maybe for trailers, in which case we simply have
3439 * to wait for the upper layer to finish consuming what is available.
3440 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003441
3442 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003443 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003444 if (!htx_is_empty(htx)) {
3445 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003446 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003447 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003448 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003449 if (b_data(rxbuf)) {
3450 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003451 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003452 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003453
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003454 rxbuf->head = 0;
3455 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003456 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003457
Willy Tarreau25919232019-01-03 14:48:18 +01003458 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003459 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3460 sizeof(list)/sizeof(list[0]), tmp);
3461 if (outlen < 0) {
3462 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3463 goto fail;
3464 }
3465
Willy Tarreau25919232019-01-03 14:48:18 +01003466 /* The PACK decompressor was updated, let's update the input buffer and
3467 * the parser's state to commit these changes and allow us to later
3468 * fail solely on the stream if needed.
3469 */
3470 b_del(&h2c->dbuf, h2c->dfl + hole);
3471 h2c->dfl = hole = 0;
3472 h2c->st0 = H2_CS_FRAME_H;
3473
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003474 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003475 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003476
Willy Tarreau88d138e2019-01-02 19:38:14 +01003477 if (*flags & H2_SF_HEADERS_RCVD)
3478 goto trailers;
3479
3480 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003481 if (htx) {
3482 /* HTX mode */
3483 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003484 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003485 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003486 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003487 } else {
3488 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003489 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003490 if (outlen > 0)
3491 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003492 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003493
3494 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003495 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003496 goto fail;
3497 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003498
Willy Tarreau174b06a2018-04-25 18:13:58 +02003499 if (msgf & H2_MSGF_BODY) {
3500 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003501 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003502 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003503 if (htx)
3504 htx->extra = *body_len;
3505 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003506 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003507 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003508 }
3509
Willy Tarreau88d138e2019-01-02 19:38:14 +01003510 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003511 /* indicate that a HEADERS frame was received for this stream, except
3512 * for 1xx responses. For 1xx responses, another HEADERS frame is
3513 * expected.
3514 */
3515 if (!(msgf & H2_MSGF_RSP_1XX))
3516 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003517
Christopher Faulet0b465482019-02-19 15:14:23 +01003518 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003519 /* Mark the end of message, either using EOM in HTX or with the
3520 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3521 * is not set during headers with END_STREAM.
3522 */
3523 if (htx) {
3524 if (!htx_add_endof(htx, HTX_BLK_EOM))
3525 goto fail;
3526 }
3527 else if (*flags & H2_SF_DATA_CHNK) {
3528 if (!b_putblk(rxbuf, "\r\n", 2))
3529 goto fail;
3530 }
3531 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003532
Willy Tarreau86277d42019-01-02 15:36:11 +01003533 /* success */
3534 ret = 1;
3535
Willy Tarreau68dd9852017-07-03 14:44:26 +02003536 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003537 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003538 * move the remaining data over it.
3539 */
3540 if (hole) {
3541 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3542 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3543 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3544 b_sub(&h2c->dbuf, hole);
3545 }
3546
3547 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3548 /* too large frames */
3549 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003550 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003551 }
3552
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003553 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003554 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003555 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003556 return ret;
3557
Willy Tarreau68dd9852017-07-03 14:44:26 +02003558 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003559 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003560 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003561
3562 trailers:
3563 /* This is the last HEADERS frame hence a trailer */
3564
3565 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3566 /* It's a trailer but it's missing ES flag */
3567 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3568 goto fail;
3569 }
3570
3571 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3572 * block, and when using chunks we must send the 0 CRLF marker. For
3573 * other modes, the trailers are silently dropped.
3574 */
3575 if (htx) {
3576 if (!htx_add_endof(htx, HTX_BLK_EOD))
3577 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003578 if (h2_make_htx_trailers(list, htx) <= 0)
3579 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003580 }
3581 else if (*flags & H2_SF_DATA_CHNK) {
3582 /* Legacy mode with chunked encoding : we must finalize the
3583 * data block message emit the trailing CRLF */
3584 if (!b_putblk(rxbuf, "0\r\n", 3))
3585 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003586
3587 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3588 if (outlen > 0)
3589 b_add(rxbuf, outlen);
3590 else
3591 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003592 }
3593
3594 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003595}
3596
Willy Tarreau454f9052017-10-26 19:40:35 +02003597/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3598 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3599 * in use, a new chunk is emitted for each frame. This is supposed to fit
3600 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3601 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3602 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003603 * parser state is automatically updated. Returns > 0 if it could completely
3604 * send the current frame, 0 if it couldn't complete, in which case
3605 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3606 * DATA frame can return 0 as a valid result). Stream errors are reported in
3607 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3608 * have checked the frame header and ensured that the frame was complete or the
3609 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003610 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003611static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003612{
3613 struct h2c *h2c = h2s->h2c;
3614 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003615 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003616 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003617 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003618 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003619
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003620 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003621
Olivier Houchard638b7992018-08-16 15:41:52 +02003622 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003623 if (!csbuf) {
3624 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003625 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003626 }
3627
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003628try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003629 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003630 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3631 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003632 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003633 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003634
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003635 if (flen > b_data(&h2c->dbuf)) {
3636 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003637 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003638 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003639 }
3640
Willy Tarreaua9b77962019-01-31 07:23:00 +01003641 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003642 block1 = htx_free_data_space(htx);
3643 if (!block1) {
3644 h2c->flags |= H2_CF_DEM_SFULL;
3645 goto fail;
3646 }
3647 if (flen > block1)
3648 flen = block1;
3649
3650 /* here, flen is the max we can copy into the output buffer */
3651 block1 = b_contig_data(&h2c->dbuf, 0);
3652 if (flen > block1)
3653 flen = block1;
3654
3655 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3656 h2c->flags |= H2_CF_DEM_SFULL;
3657 goto fail;
3658 }
3659
3660 b_del(&h2c->dbuf, flen);
3661 h2c->dfl -= flen;
3662 h2c->rcvd_c += flen;
3663 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003664
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003665 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003666 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003667 htx->extra = h2s->body_len;
3668 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003669 goto try_again;
3670 }
3671 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003672 /* it doesn't fit and the buffer is fragmented,
3673 * so let's defragment it and try again.
3674 */
3675 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003676 }
3677
Willy Tarreaueba10f22018-04-25 20:44:22 +02003678 /* chunked-encoding requires more room */
3679 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003680 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003681 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3682 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3683 (chklen < 1048576) ? 4 : 8;
3684 chklen += 4; // CRLF, CRLF
3685 }
3686
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003687 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003688 if (flen + chklen > b_room(csbuf)) {
3689 if (chklen >= b_room(csbuf)) {
3690 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003691 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003692 }
3693 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003694 }
3695
3696 if (h2s->flags & H2_SF_DATA_CHNK) {
3697 /* emit the chunk size */
3698 unsigned int chksz = flen;
3699 char str[10];
3700 char *beg;
3701
3702 beg = str + sizeof(str);
3703 *--beg = '\n';
3704 *--beg = '\r';
3705 do {
3706 *--beg = hextab[chksz & 0xF];
3707 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003708 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003709 }
3710
Willy Tarreau454f9052017-10-26 19:40:35 +02003711 /* Block1 is the length of the first block before the buffer wraps,
3712 * block2 is the optional second block to reach the end of the frame.
3713 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003714 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003715 if (block1 > flen)
3716 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003717 block2 = flen - block1;
3718
3719 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003720 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003721
3722 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003723 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003724
Willy Tarreaueba10f22018-04-25 20:44:22 +02003725 if (h2s->flags & H2_SF_DATA_CHNK) {
3726 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003727 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003728 }
3729
Willy Tarreau454f9052017-10-26 19:40:35 +02003730 /* now mark the input data as consumed (will be deleted from the buffer
3731 * by the caller when seeing FRAME_A after sending the window update).
3732 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003733 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003734 h2c->dfl -= flen;
3735 h2c->rcvd_c += flen;
3736 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3737
Willy Tarreau1915ca22019-01-24 11:49:37 +01003738 if (h2s->flags & H2_SF_DATA_CLEN)
3739 h2s->body_len -= flen;
3740
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003741 if (h2c->dfl > h2c->dpl) {
3742 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003743 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003744 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003745 }
3746
Willy Tarreau4a28da12018-01-04 14:41:00 +01003747 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003748 /* here we're done with the frame, all the payload (except padding) was
3749 * transferred.
3750 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003751
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003752 if (h2c->dff & H2_F_DATA_END_STREAM) {
3753 if (htx) {
3754 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3755 h2c->flags |= H2_CF_DEM_SFULL;
3756 goto fail;
3757 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003758 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003759 else if (h2s->flags & H2_SF_DATA_CHNK) {
3760 /* emit the trailing 0 CRLF CRLF */
3761 if (b_room(csbuf) < 5) {
3762 h2c->flags |= H2_CF_DEM_SFULL;
3763 goto fail;
3764 }
3765 chklen += 5;
3766 b_putblk(csbuf, "0\r\n\r\n", 5);
3767 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003768 }
3769
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003770 h2c->rcvd_c += h2c->dpl;
3771 h2c->rcvd_s += h2c->dpl;
3772 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003773 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003774 if (htx)
3775 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003776 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003777 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003778 if (htx)
3779 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003780 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003781}
3782
Willy Tarreau5dd17352018-06-14 13:33:30 +02003783/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3784 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3785 * number of bytes sent. The caller must check the stream's status to detect
3786 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003787 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003788static 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 +02003789{
3790 struct http_hdr list[MAX_HTTP_HDR];
3791 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003792 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003793 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003794 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003795 int es_now = 0;
3796 int ret = 0;
3797 int hdr;
3798
3799 if (h2c_mux_busy(h2c, h2s)) {
3800 h2s->flags |= H2_SF_BLK_MBUSY;
3801 return 0;
3802 }
3803
Willy Tarreau44e973f2018-03-01 17:49:30 +01003804 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003805 h2c->flags |= H2_CF_MUX_MALLOC;
3806 h2s->flags |= H2_SF_BLK_MROOM;
3807 return 0;
3808 }
3809
3810 /* First, try to parse the H1 response and index it into <list>.
3811 * NOTE! Since it comes from haproxy, we *know* that a response header
3812 * block does not wrap and we can safely read it this way without
3813 * having to realign the buffer.
3814 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003815 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003816 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003817 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003818 /* incomplete or invalid response, this is abnormal coming from
3819 * haproxy and may only result in a bad errorfile or bad Lua code
3820 * so that won't be fixed, raise an error now.
3821 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003822 * FIXME: we should instead add the ability to only return a
3823 * 502 bad gateway. But in theory this is not supposed to
3824 * happen.
3825 */
3826 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3827 ret = 0;
3828 goto end;
3829 }
3830
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003831 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003832
3833 /* certain statuses have no body or an empty one, regardless of
3834 * what the headers say.
3835 */
3836 if (sl.st.status >= 100 && sl.st.status < 200) {
3837 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3838 h1m->curr_len = h1m->body_len = 0;
3839 }
3840 else if (sl.st.status == 204 || sl.st.status == 304) {
3841 /* no contents, claim c-len is present and set to zero */
3842 h1m->flags &= ~H1_MF_CHNK;
3843 h1m->flags |= H1_MF_CLEN;
3844 h1m->curr_len = h1m->body_len = 0;
3845 }
3846
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003847 chunk_reset(&outbuf);
3848
3849 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003850 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003851 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003852 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003853
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003854 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003855 break;
3856 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003857 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003858 }
3859
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003860 if (outbuf.size < 9)
3861 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003862
3863 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003864 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3865 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3866 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003867
3868 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003869 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003870 /* this is an unparsable response */
3871 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3872 ret = 0;
3873 goto end;
3874 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003875
3876 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003877 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003878 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003879 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003880 }
3881
3882 /* encode all headers, stop at empty name */
3883 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003884 /* these ones do not exist in H2 and must be dropped. */
3885 if (isteq(list[hdr].n, ist("connection")) ||
3886 isteq(list[hdr].n, ist("proxy-connection")) ||
3887 isteq(list[hdr].n, ist("keep-alive")) ||
3888 isteq(list[hdr].n, ist("upgrade")) ||
3889 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003890 continue;
3891
3892 if (isteq(list[hdr].n, ist("")))
3893 break; // end
3894
3895 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3896 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003897 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003898 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003899 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003900 }
3901 }
3902
3903 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003904 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003905 es_now = 1;
3906
3907 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003908 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003909
3910 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003911 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003912
3913 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003914 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003915
3916 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003917 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003918 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003919
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003920 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003921 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003922 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003923
Willy Tarreau801250e2018-09-11 11:45:04 +02003924 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003925 h2s->flags |= H2_SF_ES_SENT;
3926 if (h2s->st == H2_SS_OPEN)
3927 h2s->st = H2_SS_HLOC;
3928 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003929 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003930 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003931 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003932 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003933 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003934 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003935 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003936 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003937 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003938
3939 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003940
3941 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003942 //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 +02003943 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003944 full:
3945 h1m_init_res(h1m);
3946 h1m->err_pos = -1; // don't care about errors on the response path
3947 h2c->flags |= H2_CF_MUX_MFULL;
3948 h2s->flags |= H2_SF_BLK_MROOM;
3949 ret = 0;
3950 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003951}
3952
Willy Tarreau5dd17352018-06-14 13:33:30 +02003953/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3954 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3955 * the number of bytes sent. The caller must check the stream's status to
3956 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003957 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003958static 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 +02003959{
3960 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003961 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003962 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003963 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003964 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003965 int es_now = 0;
3966 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003967 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003968 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003969
3970 if (h2c_mux_busy(h2c, h2s)) {
3971 h2s->flags |= H2_SF_BLK_MBUSY;
3972 goto end;
3973 }
3974
Willy Tarreau44e973f2018-03-01 17:49:30 +01003975 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003976 h2c->flags |= H2_CF_MUX_MALLOC;
3977 h2s->flags |= H2_SF_BLK_MROOM;
3978 goto end;
3979 }
3980
3981 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003982 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003983 goto end;
3984
3985 chunk_reset(&outbuf);
3986
3987 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003988 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003989 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003990 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003991
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003992 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003993 break;
3994 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003995 /* If there are pending data in the output buffer, and we have
3996 * less than 1/4 of the mbuf's size and everything fits, we'll
3997 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3998 * is full and wait, to save some slow realign calls.
3999 */
4000 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4001 h2c->flags |= H2_CF_MUX_MFULL;
4002 h2s->flags |= H2_SF_BLK_MROOM;
4003 goto end;
4004 }
4005
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004006 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004007 }
4008
4009 if (outbuf.size < 9) {
4010 h2c->flags |= H2_CF_MUX_MFULL;
4011 h2s->flags |= H2_SF_BLK_MROOM;
4012 goto end;
4013 }
4014
4015 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004016 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4017 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4018 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004019
4020 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4021 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004022 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004023 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004024 break;
4025 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004026 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004027 if ((long long)size > h1m->curr_len)
4028 size = h1m->curr_len;
4029 break;
4030 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004031 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004032 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004033 if (!ret)
4034 goto end;
4035
4036 if (ret < 0) {
4037 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004038 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004039 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4040 goto end;
4041 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004042 max -= ret;
4043 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004044 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004045 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004046 }
4047
Willy Tarreau801250e2018-09-11 11:45:04 +02004048 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004049 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004050 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004051 if (!ret)
4052 goto end;
4053
4054 if (ret < 0) {
4055 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004056 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004057 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4058 goto end;
4059 }
4060
4061 size = chunk;
4062 h1m->curr_len = chunk;
4063 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004064 max -= ret;
4065 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004066 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004067 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004068 if (!size)
4069 goto send_empty;
4070 }
4071
4072 /* in MSG_DATA state, continue below */
4073 size = h1m->curr_len;
4074 break;
4075 }
4076
4077 /* we have in <size> the exact number of bytes we need to copy from
4078 * the H1 buffer. We need to check this against the connection's and
4079 * the stream's send windows, and to ensure that this fits in the max
4080 * frame size and in the buffer's available space minus 9 bytes (for
4081 * the frame header). The connection's flow control is applied last so
4082 * that we can use a separate list of streams which are immediately
4083 * unblocked on window opening. Note: we don't implement padding.
4084 */
4085
Willy Tarreau5dd17352018-06-14 13:33:30 +02004086 if (size > max)
4087 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004088
4089 if (size > h2s->mws)
4090 size = h2s->mws;
4091
4092 if (size <= 0) {
4093 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004094 if (h2s->send_wait) {
4095 LIST_DEL(&h2s->list);
4096 LIST_INIT(&h2s->list);
4097 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004098 goto end;
4099 }
4100
4101 if (h2c->mfs && size > h2c->mfs)
4102 size = h2c->mfs;
4103
4104 if (size + 9 > outbuf.size) {
4105 /* we have an opportunity for enlarging the too small
4106 * available space, let's try.
4107 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004108 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004109 goto realign_again;
4110 size = outbuf.size - 9;
4111 }
4112
4113 if (size <= 0) {
4114 h2c->flags |= H2_CF_MUX_MFULL;
4115 h2s->flags |= H2_SF_BLK_MROOM;
4116 goto end;
4117 }
4118
4119 if (size > h2c->mws)
4120 size = h2c->mws;
4121
4122 if (size <= 0) {
4123 h2s->flags |= H2_SF_BLK_MFCTL;
4124 goto end;
4125 }
4126
4127 /* copy whatever we can */
4128 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004129 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004130 if (ret == 1)
4131 len2 = 0;
4132
4133 if (!ret || len1 + len2 < size) {
4134 /* FIXME: must normally never happen */
4135 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4136 goto end;
4137 }
4138
4139 /* limit len1/len2 to size */
4140 if (len1 + len2 > size) {
4141 int sub = len1 + len2 - size;
4142
4143 if (len2 > sub)
4144 len2 -= sub;
4145 else {
4146 sub -= len2;
4147 len2 = 0;
4148 len1 -= sub;
4149 }
4150 }
4151
4152 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004153 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004154 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004155 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004156
4157 send_empty:
4158 /* we may need to add END_STREAM */
4159 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4160 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004161 *
4162 * FIXME: what we do here is not correct because we send end_stream
4163 * before knowing if we'll have to send a HEADERS frame for the
4164 * trailers. More importantly we're not consuming the trailing CRLF
4165 * after the end of trailers, so it will be left to the caller to
4166 * eat it. The right way to do it would be to measure trailers here
4167 * and to send ES only if there are no trailers.
4168 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004169 */
4170 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004171 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004172 es_now = 1;
4173
4174 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004175 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004176
4177 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004178 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004179
4180 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004181 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004182
4183 /* consume incoming H1 response */
4184 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004185 max -= size;
4186 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004187 total += size;
4188 h1m->curr_len -= size;
4189 h2s->mws -= size;
4190 h2c->mws -= size;
4191
4192 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004193 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004194 goto new_frame;
4195 }
4196 }
4197
4198 if (es_now) {
4199 if (h2s->st == H2_SS_OPEN)
4200 h2s->st = H2_SS_HLOC;
4201 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004202 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004203
Willy Tarreau35a62702018-02-27 15:37:25 +01004204 if (!(h1m->flags & H1_MF_CHNK)) {
4205 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004206 total += max;
4207 ofs += max;
4208 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004209
Willy Tarreau801250e2018-09-11 11:45:04 +02004210 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004211 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004212
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004213 h2s->flags |= H2_SF_ES_SENT;
4214 }
4215
4216 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004217 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 +02004218 return total;
4219}
4220
Willy Tarreau115e83b2018-12-01 19:17:53 +01004221/* Try to send a HEADERS frame matching HTX response present in HTX message
4222 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4223 * must check the stream's status to detect any error which might have happened
4224 * subsequently to a successful send. The htx blocks are automatically removed
4225 * from the message. The htx message is assumed to be valid since produced from
4226 * the internal code, hence it contains a start line, an optional series of
4227 * header blocks and an end of header, otherwise an invalid frame could be
4228 * emitted and the resulting htx message could be left in an inconsistent state.
4229 */
4230static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4231{
4232 struct http_hdr list[MAX_HTTP_HDR];
4233 struct h2c *h2c = h2s->h2c;
4234 struct htx_blk *blk;
4235 struct htx_blk *blk_end;
4236 struct buffer outbuf;
4237 struct htx_sl *sl;
4238 enum htx_blk_type type;
4239 int es_now = 0;
4240 int ret = 0;
4241 int hdr;
4242 int idx;
4243
4244 if (h2c_mux_busy(h2c, h2s)) {
4245 h2s->flags |= H2_SF_BLK_MBUSY;
4246 return 0;
4247 }
4248
4249 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4250 h2c->flags |= H2_CF_MUX_MALLOC;
4251 h2s->flags |= H2_SF_BLK_MROOM;
4252 return 0;
4253 }
4254
4255 /* determine the first block which must not be deleted, blk_end may
4256 * be NULL if all blocks have to be deleted.
4257 */
4258 idx = htx_get_head(htx);
4259 blk_end = NULL;
4260 while (idx != -1) {
4261 type = htx_get_blk_type(htx_get_blk(htx, idx));
4262 idx = htx_get_next(htx, idx);
4263 if (type == HTX_BLK_EOH) {
4264 if (idx != -1)
4265 blk_end = htx_get_blk(htx, idx);
4266 break;
4267 }
4268 }
4269
4270 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004271 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004272 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004273 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004274 if (h2s->status < 100 || h2s->status > 999)
4275 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004276
4277 /* and the rest of the headers, that we dump starting at header 0 */
4278 hdr = 0;
4279
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004280 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004281 while ((idx = htx_get_next(htx, idx)) != -1) {
4282 blk = htx_get_blk(htx, idx);
4283 type = htx_get_blk_type(blk);
4284
4285 if (type == HTX_BLK_UNUSED)
4286 continue;
4287
4288 if (type != HTX_BLK_HDR)
4289 break;
4290
4291 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4292 goto fail;
4293
4294 list[hdr].n = htx_get_blk_name(htx, blk);
4295 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004296 hdr++;
4297 }
4298
4299 /* marker for end of headers */
4300 list[hdr].n = ist("");
4301
4302 if (h2s->status == 204 || h2s->status == 304) {
4303 /* no contents, claim c-len is present and set to zero */
4304 es_now = 1;
4305 }
4306
4307 chunk_reset(&outbuf);
4308
4309 while (1) {
4310 outbuf.area = b_tail(&h2c->mbuf);
4311 outbuf.size = b_contig_space(&h2c->mbuf);
4312 outbuf.data = 0;
4313
4314 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4315 break;
4316 realign_again:
4317 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4318 }
4319
4320 if (outbuf.size < 9)
4321 goto full;
4322
4323 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4324 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4325 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4326 outbuf.data = 9;
4327
4328 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004329 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004330 if (b_space_wraps(&h2c->mbuf))
4331 goto realign_again;
4332 goto full;
4333 }
4334
4335 /* encode all headers, stop at empty name */
4336 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4337 /* these ones do not exist in H2 and must be dropped. */
4338 if (isteq(list[hdr].n, ist("connection")) ||
4339 isteq(list[hdr].n, ist("proxy-connection")) ||
4340 isteq(list[hdr].n, ist("keep-alive")) ||
4341 isteq(list[hdr].n, ist("upgrade")) ||
4342 isteq(list[hdr].n, ist("transfer-encoding")))
4343 continue;
4344
4345 if (isteq(list[hdr].n, ist("")))
4346 break; // end
4347
4348 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4349 /* output full */
4350 if (b_space_wraps(&h2c->mbuf))
4351 goto realign_again;
4352 goto full;
4353 }
4354 }
4355
Christopher Faulet0b465482019-02-19 15:14:23 +01004356 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004357 * FIXME: we should also set it when we know for sure that the
4358 * content-length is zero as well as on 204/304
4359 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004360 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4361 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004362 es_now = 1;
4363
Willy Tarreau927b88b2019-03-04 08:03:25 +01004364 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004365 es_now = 1;
4366
4367 /* update the frame's size */
4368 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4369
4370 if (es_now)
4371 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4372
4373 /* commit the H2 response */
4374 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004375
4376 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4377 * 1xx responses, another HEADERS frame is expected.
4378 */
4379 if (h2s->status >= 200 || h2s->status == 101)
4380 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004381
Willy Tarreau115e83b2018-12-01 19:17:53 +01004382 if (es_now) {
4383 h2s->flags |= H2_SF_ES_SENT;
4384 if (h2s->st == H2_SS_OPEN)
4385 h2s->st = H2_SS_HLOC;
4386 else
4387 h2s_close(h2s);
4388 }
4389
4390 /* OK we could properly deliver the response */
4391
4392 /* remove all header blocks including the EOH and compute the
4393 * corresponding size.
4394 *
4395 * FIXME: We should remove everything when es_now is set.
4396 */
4397 ret = 0;
4398 idx = htx_get_head(htx);
4399 blk = htx_get_blk(htx, idx);
4400 while (blk != blk_end) {
4401 ret += htx_get_blksz(blk);
4402 blk = htx_remove_blk(htx, blk);
4403 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004404
4405 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4406 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004407 end:
4408 return ret;
4409 full:
4410 h2c->flags |= H2_CF_MUX_MFULL;
4411 h2s->flags |= H2_SF_BLK_MROOM;
4412 ret = 0;
4413 goto end;
4414 fail:
4415 /* unparsable HTX messages, too large ones to be produced in the local
4416 * list etc go here (unrecoverable errors).
4417 */
4418 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4419 ret = 0;
4420 goto end;
4421}
4422
Willy Tarreau80739692018-10-05 11:35:57 +02004423/* Try to send a HEADERS frame matching HTX request present in HTX message
4424 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4425 * must check the stream's status to detect any error which might have happened
4426 * subsequently to a successful send. The htx blocks are automatically removed
4427 * from the message. The htx message is assumed to be valid since produced from
4428 * the internal code, hence it contains a start line, an optional series of
4429 * header blocks and an end of header, otherwise an invalid frame could be
4430 * emitted and the resulting htx message could be left in an inconsistent state.
4431 */
4432static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4433{
4434 struct http_hdr list[MAX_HTTP_HDR];
4435 struct h2c *h2c = h2s->h2c;
4436 struct htx_blk *blk;
4437 struct htx_blk *blk_end;
4438 struct buffer outbuf;
4439 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004440 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004441 enum htx_blk_type type;
4442 int es_now = 0;
4443 int ret = 0;
4444 int hdr;
4445 int idx;
4446
4447 if (h2c_mux_busy(h2c, h2s)) {
4448 h2s->flags |= H2_SF_BLK_MBUSY;
4449 return 0;
4450 }
4451
4452 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4453 h2c->flags |= H2_CF_MUX_MALLOC;
4454 h2s->flags |= H2_SF_BLK_MROOM;
4455 return 0;
4456 }
4457
4458 /* determine the first block which must not be deleted, blk_end may
4459 * be NULL if all blocks have to be deleted.
4460 */
4461 idx = htx_get_head(htx);
4462 blk_end = NULL;
4463 while (idx != -1) {
4464 type = htx_get_blk_type(htx_get_blk(htx, idx));
4465 idx = htx_get_next(htx, idx);
4466 if (type == HTX_BLK_EOH) {
4467 if (idx != -1)
4468 blk_end = htx_get_blk(htx, idx);
4469 break;
4470 }
4471 }
4472
4473 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004474 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004475 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004476 meth = htx_sl_req_meth(sl);
4477 path = htx_sl_req_uri(sl);
4478
4479 /* and the rest of the headers, that we dump starting at header 0 */
4480 hdr = 0;
4481
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004482 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004483 while ((idx = htx_get_next(htx, idx)) != -1) {
4484 blk = htx_get_blk(htx, idx);
4485 type = htx_get_blk_type(blk);
4486
4487 if (type == HTX_BLK_UNUSED)
4488 continue;
4489
4490 if (type != HTX_BLK_HDR)
4491 break;
4492
4493 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4494 goto fail;
4495
4496 list[hdr].n = htx_get_blk_name(htx, blk);
4497 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004498 hdr++;
4499 }
4500
4501 /* marker for end of headers */
4502 list[hdr].n = ist("");
4503
4504 chunk_reset(&outbuf);
4505
4506 while (1) {
4507 outbuf.area = b_tail(&h2c->mbuf);
4508 outbuf.size = b_contig_space(&h2c->mbuf);
4509 outbuf.data = 0;
4510
4511 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4512 break;
4513 realign_again:
4514 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4515 }
4516
4517 if (outbuf.size < 9)
4518 goto full;
4519
4520 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4521 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4522 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4523 outbuf.data = 9;
4524
4525 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004526 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004527 if (b_space_wraps(&h2c->mbuf))
4528 goto realign_again;
4529 goto full;
4530 }
4531
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004532 /* RFC7540 #8.3: the CONNECT method must have :
4533 * - :authority set to the URI part (host:port)
4534 * - :method set to CONNECT
4535 * - :scheme and :path omitted
4536 */
4537 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4538 /* encode the scheme which is always "https" (or 0x86 for "http") */
4539 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4540 /* output full */
4541 if (b_space_wraps(&h2c->mbuf))
4542 goto realign_again;
4543 goto full;
4544 }
Willy Tarreau80739692018-10-05 11:35:57 +02004545
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004546 /* encode the path, which necessarily is the second one */
4547 if (!hpack_encode_path(&outbuf, path)) {
4548 /* output full */
4549 if (b_space_wraps(&h2c->mbuf))
4550 goto realign_again;
4551 goto full;
4552 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004553
4554 /* look for the Host header and place it in :authority */
4555 auth = ist2(NULL, 0);
4556 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4557 if (isteq(list[hdr].n, ist("")))
4558 break; // end
4559
4560 if (isteq(list[hdr].n, ist("host"))) {
4561 auth = list[hdr].v;
4562 break;
4563 }
4564 }
4565 }
4566 else {
4567 /* for CONNECT, :authority is taken from the path */
4568 auth = path;
4569 }
4570
4571 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4572 /* output full */
4573 if (b_space_wraps(&h2c->mbuf))
4574 goto realign_again;
4575 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004576 }
4577
4578 /* encode all headers, stop at empty name */
4579 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4580 /* these ones do not exist in H2 and must be dropped. */
4581 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004582 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004583 isteq(list[hdr].n, ist("proxy-connection")) ||
4584 isteq(list[hdr].n, ist("keep-alive")) ||
4585 isteq(list[hdr].n, ist("upgrade")) ||
4586 isteq(list[hdr].n, ist("transfer-encoding")))
4587 continue;
4588
4589 if (isteq(list[hdr].n, ist("")))
4590 break; // end
4591
4592 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4593 /* output full */
4594 if (b_space_wraps(&h2c->mbuf))
4595 goto realign_again;
4596 goto full;
4597 }
4598 }
4599
4600 /* we may need to add END_STREAM if we have no body :
4601 * - request already closed, or :
4602 * - no transfer-encoding, and :
4603 * - no content-length or content-length:0
4604 * Fixme: this doesn't take into account CONNECT requests.
4605 */
4606 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4607 es_now = 1;
4608
4609 if (sl->flags & HTX_SL_F_BODYLESS)
4610 es_now = 1;
4611
Willy Tarreau927b88b2019-03-04 08:03:25 +01004612 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004613 es_now = 1;
4614
4615 /* update the frame's size */
4616 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4617
4618 if (es_now)
4619 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4620
4621 /* commit the H2 response */
4622 b_add(&h2c->mbuf, outbuf.data);
4623 h2s->flags |= H2_SF_HEADERS_SENT;
4624 h2s->st = H2_SS_OPEN;
4625
Willy Tarreau80739692018-10-05 11:35:57 +02004626 if (es_now) {
4627 // trim any possibly pending data (eg: inconsistent content-length)
4628 h2s->flags |= H2_SF_ES_SENT;
4629 h2s->st = H2_SS_HLOC;
4630 }
4631
4632 /* remove all header blocks including the EOH and compute the
4633 * corresponding size.
4634 *
4635 * FIXME: We should remove everything when es_now is set.
4636 */
4637 ret = 0;
4638 idx = htx_get_head(htx);
4639 blk = htx_get_blk(htx, idx);
4640 while (blk != blk_end) {
4641 ret += htx_get_blksz(blk);
4642 blk = htx_remove_blk(htx, blk);
4643 }
4644
4645 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4646 htx_remove_blk(htx, blk_end);
4647
4648 end:
4649 return ret;
4650 full:
4651 h2c->flags |= H2_CF_MUX_MFULL;
4652 h2s->flags |= H2_SF_BLK_MROOM;
4653 ret = 0;
4654 goto end;
4655 fail:
4656 /* unparsable HTX messages, too large ones to be produced in the local
4657 * list etc go here (unrecoverable errors).
4658 */
4659 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4660 ret = 0;
4661 goto end;
4662}
4663
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004664/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004665 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4666 * caller must check the stream's status to detect any error which might have
4667 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004668 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4669 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004670static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004671{
4672 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004673 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004674 struct buffer outbuf;
4675 size_t total = 0;
4676 int es_now = 0;
4677 int bsize; /* htx block size */
4678 int fsize; /* h2 frame size */
4679 struct htx_blk *blk;
4680 enum htx_blk_type type;
4681 int idx;
4682
4683 if (h2c_mux_busy(h2c, h2s)) {
4684 h2s->flags |= H2_SF_BLK_MBUSY;
4685 goto end;
4686 }
4687
4688 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4689 h2c->flags |= H2_CF_MUX_MALLOC;
4690 h2s->flags |= H2_SF_BLK_MROOM;
4691 goto end;
4692 }
4693
Willy Tarreau98de12a2018-12-12 07:03:00 +01004694 htx = htx_from_buf(buf);
4695
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004696 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4697 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4698 * the caller to handle.
4699 */
4700
4701 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004702 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004703 goto end;
4704
4705 idx = htx_get_head(htx);
4706 blk = htx_get_blk(htx, idx);
4707 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4708 bsize = htx_get_blksz(blk);
4709 fsize = bsize;
4710
4711 if (type == HTX_BLK_EOD) {
4712 /* if we have an EOD, we're dealing with chunked data. We may
4713 * have a set of trailers after us that the caller will want to
4714 * deal with. Let's simply remove the EOD and return.
4715 */
4716 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004717 total++; // EOD counts as one byte
4718 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004719 goto end;
4720 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004721 else if (type == HTX_BLK_EOM) {
4722 if (h2s->flags & H2_SF_ES_SENT) {
4723 /* ES already sent */
4724 htx_remove_blk(htx, blk);
4725 total++; // EOM counts as one byte
4726 count--;
4727 goto end;
4728 }
4729 }
4730 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004731 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004732
4733 /* Perform some optimizations to reduce the number of buffer copies.
4734 * First, if the mux's buffer is empty and the htx area contains
4735 * exactly one data block of the same size as the requested count, and
4736 * this count fits within the frame size, the stream's window size, and
4737 * the connection's window size, then it's possible to simply swap the
4738 * caller's buffer with the mux's output buffer and adjust offsets and
4739 * length to match the entire DATA HTX block in the middle. In this
4740 * case we perform a true zero-copy operation from end-to-end. This is
4741 * the situation that happens all the time with large files. Second, if
4742 * this is not possible, but the mux's output buffer is empty, we still
4743 * have an opportunity to avoid the copy to the intermediary buffer, by
4744 * making the intermediary buffer's area point to the output buffer's
4745 * area. In this case we want to skip the HTX header to make sure that
4746 * copies remain aligned and that this operation remains possible all
4747 * the time. This goes for headers, data blocks and any data extracted
4748 * from the HTX blocks.
4749 */
4750 if (unlikely(fsize == count &&
4751 htx->used == 1 && type == HTX_BLK_DATA &&
4752 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4753 void *old_area = h2c->mbuf.area;
4754
4755 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004756 /* Too bad there are data left there. We're willing to memcpy/memmove
4757 * up to 1/4 of the buffer, which means that it's OK to copy a large
4758 * frame into a buffer containing few data if it needs to be realigned,
4759 * and that it's also OK to copy few data without realigning. Otherwise
4760 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004761 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004762 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4763 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4764 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004765 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004766
Willy Tarreau98de12a2018-12-12 07:03:00 +01004767 h2c->flags |= H2_CF_MUX_MFULL;
4768 h2s->flags |= H2_SF_BLK_MROOM;
4769 goto end;
4770 }
4771
4772 /* map an H2 frame to the HTX block so that we can put the
4773 * frame header there.
4774 */
4775 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004776 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004777 h2c->mbuf.data = fsize + 9;
4778 outbuf.area = b_head(&h2c->mbuf);
4779
4780 /* prepend an H2 DATA frame header just before the DATA block */
4781 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4782 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4783 h2_set_frame_size(outbuf.area, fsize);
4784
4785 /* update windows */
4786 h2s->mws -= fsize;
4787 h2c->mws -= fsize;
4788
4789 /* and exchange with our old area */
4790 buf->area = old_area;
4791 buf->data = buf->head = 0;
4792 total += fsize;
4793 goto end;
4794 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004795
Willy Tarreau98de12a2018-12-12 07:03:00 +01004796 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004797 /* for DATA and EOM we'll have to emit a frame, even if empty */
4798
4799 while (1) {
4800 outbuf.area = b_tail(&h2c->mbuf);
4801 outbuf.size = b_contig_space(&h2c->mbuf);
4802 outbuf.data = 0;
4803
4804 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4805 break;
4806 realign_again:
4807 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4808 }
4809
4810 if (outbuf.size < 9) {
4811 h2c->flags |= H2_CF_MUX_MFULL;
4812 h2s->flags |= H2_SF_BLK_MROOM;
4813 goto end;
4814 }
4815
4816 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4817 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4818 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4819 outbuf.data = 9;
4820
4821 /* we have in <fsize> the exact number of bytes we need to copy from
4822 * the HTX buffer. We need to check this against the connection's and
4823 * the stream's send windows, and to ensure that this fits in the max
4824 * frame size and in the buffer's available space minus 9 bytes (for
4825 * the frame header). The connection's flow control is applied last so
4826 * that we can use a separate list of streams which are immediately
4827 * unblocked on window opening. Note: we don't implement padding.
4828 */
4829
4830 /* EOM is presented with bsize==1 but would lead to the emission of an
4831 * empty frame, thus we force it to zero here.
4832 */
4833 if (type == HTX_BLK_EOM)
4834 bsize = fsize = 0;
4835
4836 if (!fsize)
4837 goto send_empty;
4838
4839 if (h2s->mws <= 0) {
4840 h2s->flags |= H2_SF_BLK_SFCTL;
4841 if (h2s->send_wait) {
4842 LIST_DEL(&h2s->list);
4843 LIST_INIT(&h2s->list);
4844 }
4845 goto end;
4846 }
4847
Willy Tarreauee573762018-12-04 15:25:57 +01004848 if (fsize > count)
4849 fsize = count;
4850
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004851 if (fsize > h2s->mws)
4852 fsize = h2s->mws; // >0
4853
4854 if (h2c->mfs && fsize > h2c->mfs)
4855 fsize = h2c->mfs; // >0
4856
4857 if (fsize + 9 > outbuf.size) {
4858 /* we have an opportunity for enlarging the too small
4859 * available space, let's try.
4860 * FIXME: is this really interesting to do? Maybe we'll
4861 * spend lots of time realigning instead of using two
4862 * frames.
4863 */
4864 if (b_space_wraps(&h2c->mbuf))
4865 goto realign_again;
4866 fsize = outbuf.size - 9;
4867
4868 if (fsize <= 0) {
4869 /* no need to send an empty frame here */
4870 h2c->flags |= H2_CF_MUX_MFULL;
4871 h2s->flags |= H2_SF_BLK_MROOM;
4872 goto end;
4873 }
4874 }
4875
4876 if (h2c->mws <= 0) {
4877 h2s->flags |= H2_SF_BLK_MFCTL;
4878 goto end;
4879 }
4880
4881 if (fsize > h2c->mws)
4882 fsize = h2c->mws;
4883
4884 /* now let's copy this this into the output buffer */
4885 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004886 h2s->mws -= fsize;
4887 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004888 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004889
4890 send_empty:
4891 /* update the frame's size */
4892 h2_set_frame_size(outbuf.area, fsize);
4893
4894 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4895 * meeting EOM. We should optimize this later.
4896 */
4897 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004898 total++; // EOM counts as one byte
4899 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004900 es_now = 1;
4901 }
4902
4903 if (es_now)
4904 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4905
4906 /* commit the H2 response */
4907 b_add(&h2c->mbuf, fsize + 9);
4908
4909 /* consume incoming HTX block, including EOM */
4910 total += fsize;
4911 if (fsize == bsize) {
4912 htx_remove_blk(htx, blk);
4913 if (fsize)
4914 goto new_frame;
4915 } else {
4916 /* we've truncated this block */
4917 htx_cut_data_blk(htx, blk, fsize);
4918 }
4919
4920 if (es_now) {
4921 if (h2s->st == H2_SS_OPEN)
4922 h2s->st = H2_SS_HLOC;
4923 else
4924 h2s_close(h2s);
4925
4926 h2s->flags |= H2_SF_ES_SENT;
4927 }
4928
4929 end:
4930 return total;
4931}
4932
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004933/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4934 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4935 * processed. The caller must check the stream's status to detect any error
4936 * which might have happened subsequently to a successful send. The htx blocks
4937 * are automatically removed from the message. The htx message is assumed to be
4938 * valid since produced from the internal code. Processing stops when meeting
4939 * the EOM, which is also removed. All trailers are processed at once and sent
4940 * as a single frame. The ES flag is always set.
4941 */
4942static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4943{
4944 struct http_hdr list[MAX_HTTP_HDR];
4945 struct h2c *h2c = h2s->h2c;
4946 struct htx_blk *blk;
4947 struct htx_blk *blk_end;
4948 struct buffer outbuf;
4949 struct h1m h1m;
4950 enum htx_blk_type type;
4951 uint32_t size;
4952 int ret = 0;
4953 int hdr;
4954 int idx;
4955 void *start;
4956
4957 if (h2c_mux_busy(h2c, h2s)) {
4958 h2s->flags |= H2_SF_BLK_MBUSY;
4959 goto end;
4960 }
4961
4962 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4963 h2c->flags |= H2_CF_MUX_MALLOC;
4964 h2s->flags |= H2_SF_BLK_MROOM;
4965 goto end;
4966 }
4967
4968 /* The principle is that we parse each and every trailers block using
4969 * the H1 headers parser, and append it to the list. We don't proceed
4970 * until EOM is met. blk_end will point to the EOM block.
4971 */
4972 hdr = 0;
4973 memset(list, 0, sizeof(list));
4974 blk_end = NULL;
4975
4976 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4977 blk = htx_get_blk(htx, idx);
4978 type = htx_get_blk_type(blk);
4979
4980 if (type == HTX_BLK_UNUSED)
4981 continue;
4982
4983 if (type != HTX_BLK_TLR) {
4984 if (type == HTX_BLK_EOM)
4985 blk_end = blk;
4986 break;
4987 }
4988
4989 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4990 goto fail;
4991
4992 size = htx_get_blksz(blk);
4993 start = htx_get_blk_ptr(htx, blk);
4994
4995 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4996 h1m.err_pos = 0;
4997 ret = h1_headers_to_hdr_list(start, start + size,
4998 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4999 &h1m, NULL);
5000 if (ret < 0)
5001 goto fail;
5002
5003 /* ret == 0 if an incomplete trailers block was found (missing
5004 * empty line), or > 0 if it was found. We have to continue on
5005 * incomplete messages because the trailers block might be
5006 * incomplete.
5007 */
5008
5009 /* search the new end */
5010 while (hdr <= sizeof(list)/sizeof(list[0])) {
5011 if (!list[hdr].n.len)
5012 break;
5013 hdr++;
5014 }
5015 }
5016
5017 if (!blk_end)
5018 goto end; // end not found yet
5019
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005020 chunk_reset(&outbuf);
5021
5022 while (1) {
5023 outbuf.area = b_tail(&h2c->mbuf);
5024 outbuf.size = b_contig_space(&h2c->mbuf);
5025 outbuf.data = 0;
5026
5027 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5028 break;
5029 realign_again:
5030 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5031 }
5032
5033 if (outbuf.size < 9)
5034 goto full;
5035
5036 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5037 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5038 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5039 outbuf.data = 9;
5040
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005041 /* encode all headers */
5042 for (idx = 0; idx < hdr; idx++) {
5043 /* these ones do not exist in H2 or must not appear in
5044 * trailers and must be dropped.
5045 */
5046 if (isteq(list[idx].n, ist("host")) ||
5047 isteq(list[idx].n, ist("content-length")) ||
5048 isteq(list[idx].n, ist("connection")) ||
5049 isteq(list[idx].n, ist("proxy-connection")) ||
5050 isteq(list[idx].n, ist("keep-alive")) ||
5051 isteq(list[idx].n, ist("upgrade")) ||
5052 isteq(list[idx].n, ist("te")) ||
5053 isteq(list[idx].n, ist("transfer-encoding")))
5054 continue;
5055
5056 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5057 /* output full */
5058 if (b_space_wraps(&h2c->mbuf))
5059 goto realign_again;
5060 goto full;
5061 }
5062 }
5063
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005064 if (!hdr) {
5065 /* here we have a problem, we've received an empty trailers
5066 * block followed by an EOM. Because of this we can't send a
5067 * HEADERS frame, so we have to cheat and instead send an empty
5068 * DATA frame conveying the ES flag.
5069 */
5070 outbuf.area[3] = H2_FT_DATA;
5071 outbuf.area[4] = H2_F_DATA_END_STREAM;
5072 }
5073
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005074 /* update the frame's size */
5075 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5076
5077 /* commit the H2 response */
5078 b_add(&h2c->mbuf, outbuf.data);
5079 h2s->flags |= H2_SF_ES_SENT;
5080
5081 if (h2s->st == H2_SS_OPEN)
5082 h2s->st = H2_SS_HLOC;
5083 else
5084 h2s_close(h2s);
5085
5086 /* OK we could properly deliver the response */
5087 done:
5088 /* remove all header blocks including EOM and compute the corresponding size. */
5089 ret = 0;
5090 idx = htx_get_head(htx);
5091 blk = htx_get_blk(htx, idx);
5092 while (blk != blk_end) {
5093 ret += htx_get_blksz(blk);
5094 blk = htx_remove_blk(htx, blk);
5095 }
5096 blk = htx_remove_blk(htx, blk);
5097 end:
5098 return ret;
5099 full:
5100 h2c->flags |= H2_CF_MUX_MFULL;
5101 h2s->flags |= H2_SF_BLK_MROOM;
5102 ret = 0;
5103 goto end;
5104 fail:
5105 /* unparsable HTX messages, too large ones to be produced in the local
5106 * list etc go here (unrecoverable errors).
5107 */
5108 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5109 ret = 0;
5110 goto end;
5111}
5112
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005113/* Called from the upper layer, to subscribe to events, such as being able to send.
5114 * The <param> argument here is supposed to be a pointer to a wait_event struct
5115 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5116 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5117 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5118 * returns 0 except for the error above.
5119 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005120static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5121{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005122 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005123 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005124 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005125
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005126 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005127 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005128 if (!(sw->events & SUB_RETRY_RECV)) {
5129 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005130 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005131 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005132 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005133 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005134 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005135 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005136 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005137 if (!(sw->events & SUB_RETRY_SEND)) {
5138 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005139 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005140 h2s->send_wait = sw;
5141 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5142 if (h2s->flags & H2_SF_BLK_MFCTL)
5143 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5144 else
5145 LIST_ADDQ(&h2c->send_list, &h2s->list);
5146 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005147 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005148 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005149 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005150 if (event_type != 0)
5151 return -1;
5152 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005153}
5154
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005155/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5156 * The <param> argument here is supposed to be a pointer to the same wait_event
5157 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5158 * It always returns zero.
5159 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005160static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5161{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005162 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005163 struct h2s *h2s = cs->ctx;
5164
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005165 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005166 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005167 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005168 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005169 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005170 }
5171 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005172 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005173 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005174 if (h2s->send_wait == sw) {
5175 LIST_DEL(&h2s->list);
5176 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005177 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005178 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005179 }
5180 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005181 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5182 sw = param;
5183 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005184 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard3ea35132019-03-25 14:10:42 +01005185 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005186 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005187 LIST_DEL(&h2s->list);
5188 LIST_INIT(&h2s->list);
Olivier Houchard3ea35132019-03-25 14:10:42 +01005189 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005190 }
5191 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005192 return 0;
5193}
5194
5195
Olivier Houchard511efea2018-08-16 15:30:32 +02005196/* Called from the upper layer, to receive data */
5197static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5198{
Olivier Houchard638b7992018-08-16 15:41:52 +02005199 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005200 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005201 struct htx *h2s_htx = NULL;
5202 struct htx *buf_htx = NULL;
5203 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005204 size_t ret = 0;
5205
5206 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005207 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5208 /* in HTX mode we ignore the count argument */
5209 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005210 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005211 /* Here htx_to_buf() will set buffer data to 0 because
5212 * the HTX is empty.
5213 */
5214 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005215 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005216 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005217
5218 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005219 count = htx_free_data_space(buf_htx);
5220 if (flags & CO_RFL_KEEP_RSV) {
5221 if (count <= global.tune.maxrewrite)
5222 goto end;
5223 count -= global.tune.maxrewrite;
5224 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005225
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005226 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005227
5228 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5229 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5230
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005231 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005232 htx_to_buf(buf_htx, buf);
5233 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005234 ret = htx_ret.ret;
5235 }
5236 else {
5237 ret = b_xfer(buf, &h2s->rxbuf, count);
5238 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005239
Christopher Faulet37070b22019-02-14 15:12:14 +01005240 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005241 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005242 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005243 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005244 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005245 if (cs->flags & CS_FL_REOS)
5246 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005247 if (cs->flags & CS_FL_ERR_PENDING)
5248 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005249 if (b_size(&h2s->rxbuf)) {
5250 b_free(&h2s->rxbuf);
5251 offer_buffers(NULL, tasks_run_queue);
5252 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005253 }
5254
Willy Tarreau082f5592018-11-25 08:03:32 +01005255 if (ret && h2c->dsi == h2s->id) {
5256 /* demux is blocking on this stream's buffer */
5257 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005258 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005259 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005260
Olivier Houchard511efea2018-08-16 15:30:32 +02005261 return ret;
5262}
5263
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005264/* stops all senders of this connection for example when the mux buffer is full.
5265 * They are moved from the sending_list to either fctl_list or send_list.
5266 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005267static void h2_stop_senders(struct h2c *h2c)
5268{
5269 struct h2s *h2s, *h2s_back;
5270
Olivier Houchardd360ac62019-03-22 17:37:16 +01005271 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5272 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005273 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005274 h2s->send_wait->events |= SUB_RETRY_SEND;
5275 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005276 }
5277}
5278
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005279/* Called from the upper layer, to send data from buffer <buf> for no more than
5280 * <count> bytes. Returns the number of bytes effectively sent. Some status
5281 * flags may be updated on the conn_stream.
5282 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005283static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005284{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005285 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005286 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005287 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005288 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005289 struct htx *htx;
5290 struct htx_blk *blk;
5291 enum htx_blk_type btype;
5292 uint32_t bsize;
5293 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005294
Olivier Houchardd360ac62019-03-22 17:37:16 +01005295 /* If we were not just woken because we wanted to send but couldn't,
5296 * and there's somebody else that is waiting to send, do nothing,
5297 * we will subscribe later and be put at the end of the list
5298 */
5299 LIST_DEL_INIT(&h2s->sending_list);
5300 if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) &&
5301 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5302 return 0;
5303
Olivier Houchardd846c262018-10-19 17:24:29 +02005304 if (h2s->send_wait) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005305 /* We want to stay in the send_list, so prepare ourself to be
5306 * eventually recalled if needed, and only remove ourself from
5307 * the list if we managed to send anything.
5308 */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005309 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01005310 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005311 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005312 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5313 return 0;
5314
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005315 /* htx will be enough to decide if we're using HTX or legacy */
5316 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5317
Willy Tarreau0bad0432018-06-14 16:54:01 +02005318 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005319 h2s->flags |= H2_SF_OUTGOING_DATA;
5320
Willy Tarreau751f2d02018-10-05 09:35:00 +02005321 if (h2s->id == 0) {
5322 int32_t id = h2c_get_next_sid(h2s->h2c);
5323
5324 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005325 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005326 return 0;
5327 }
5328
5329 eb32_delete(&h2s->by_id);
5330 h2s->by_id.key = h2s->id = id;
5331 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005332 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005333 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5334 }
5335
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005336 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005337 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5338 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005339 idx = htx_get_head(htx);
5340 blk = htx_get_blk(htx, idx);
5341 btype = htx_get_blk_type(blk);
5342 bsize = htx_get_blksz(blk);
5343
5344 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005345 case HTX_BLK_REQ_SL:
5346 /* start-line before headers */
5347 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5348 if (ret > 0) {
5349 total += ret;
5350 count -= ret;
5351 if (ret < bsize)
5352 goto done;
5353 }
5354 break;
5355
Willy Tarreau115e83b2018-12-01 19:17:53 +01005356 case HTX_BLK_RES_SL:
5357 /* start-line before headers */
5358 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5359 if (ret > 0) {
5360 total += ret;
5361 count -= ret;
5362 if (ret < bsize)
5363 goto done;
5364 }
5365 break;
5366
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005367 case HTX_BLK_DATA:
5368 case HTX_BLK_EOD:
5369 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005370 /* all these cause the emission of a DATA frame (possibly empty).
5371 * This EOM necessarily is one before trailers, as the EOM following
5372 * trailers would have been consumed by the trailers parser.
5373 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005374 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005375 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005376 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005377 total += ret;
5378 count -= ret;
5379 if (ret < bsize)
5380 goto done;
5381 }
5382 break;
5383
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005384 case HTX_BLK_TLR:
5385 /* This is the first trailers block, all the subsequent ones AND
5386 * the EOM will be swallowed by the parser.
5387 */
5388 ret = h2s_htx_make_trailers(h2s, htx);
5389 if (ret > 0) {
5390 total += ret;
5391 count -= ret;
5392 if (ret < bsize)
5393 goto done;
5394 }
5395 break;
5396
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005397 default:
5398 htx_remove_blk(htx, blk);
5399 total += bsize;
5400 count -= bsize;
5401 break;
5402 }
5403 }
5404 goto done;
5405 }
5406
5407 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005408 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005409 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005410 if (h2s->h2c->flags & H2_CF_IS_BACK)
5411 ret = -1;
5412 else
5413 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005414 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005415 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005416 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005417 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005418 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005419 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005420 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005421
Willy Tarreau5dd17352018-06-14 13:33:30 +02005422 if (unlikely((int)ret <= 0)) {
5423 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005424 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5425 break;
5426 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005427 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005428 total += count;
5429 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005430 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005431 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005432 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005433 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005434 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005435 break;
5436 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005437
5438 total += ret;
5439 count -= ret;
5440
5441 if (h2s->st >= H2_SS_ERROR)
5442 break;
5443
5444 if (h2s->flags & H2_SF_BLK_ANY)
5445 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005446 }
5447
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005448 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005449 if (h2s->st >= H2_SS_ERROR) {
5450 /* trim any possibly pending data after we close (extra CR-LF,
5451 * unprocessed trailers, abnormal extra data, ...)
5452 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005453 total += count;
5454 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005455 }
5456
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005457 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005458 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005459 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005460 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005461 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005462 }
5463
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005464 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005465 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005466 } else {
5467 b_del(buf, total);
5468 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005469
5470 /* The mux is full, cancel the pending tasks */
5471 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5472 (h2s->flags & H2_SF_BLK_MBUSY))
5473 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005474
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005475 /* If we're running HTX, and we read the whole buffer, then pretend
5476 * we read exactly what the caller specified, as with HTX the caller
5477 * will always give the buffer size, instead of the amount of data
5478 * available.
5479 */
5480 if (htx && !b_data(buf))
5481 total = orig_count;
5482
Olivier Houchard7505f942018-08-21 18:10:44 +02005483 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005484 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005485 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005486
Olivier Houchard7505f942018-08-21 18:10:44 +02005487 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005488 /* If we're waiting for flow control, and we got a shutr on the
5489 * connection, we will never be unlocked, so add an error on
5490 * the conn_stream.
5491 */
5492 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5493 !b_data(&h2s->h2c->dbuf) &&
5494 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5495 if (cs->flags & CS_FL_EOS)
5496 cs->flags |= CS_FL_ERROR;
5497 else
5498 cs->flags |= CS_FL_ERR_PENDING;
5499 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01005500 if (total > 0 && h2s->send_wait) {
5501 /* Ok we managed to send something, leave the send_list */
5502 h2s->send_wait->events &= ~SUB_RETRY_SEND;
5503 h2s->send_wait = NULL;
5504 LIST_DEL_INIT(&h2s->list);
5505 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005506 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005507}
5508
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005509/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005510static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005511{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005512 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005513 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005514 struct eb32_node *node;
5515 int fctl_cnt = 0;
5516 int send_cnt = 0;
5517 int tree_cnt = 0;
5518 int orph_cnt = 0;
5519
5520 if (!h2c)
5521 return;
5522
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005523 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005524 fctl_cnt++;
5525
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005526 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005527 send_cnt++;
5528
Willy Tarreau3af37712018-12-18 14:34:41 +01005529 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005530 node = eb32_first(&h2c->streams_by_id);
5531 while (node) {
5532 h2s = container_of(node, struct h2s, by_id);
5533 tree_cnt++;
5534 if (!h2s->cs)
5535 orph_cnt++;
5536 node = eb32_next(node);
5537 }
5538
Willy Tarreau987c0632018-12-18 10:32:05 +01005539 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5540 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5541 " .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 +02005542 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5543 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005544 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005545 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5546 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5547 h2c->msi,
5548 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5549 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5550
5551 if (h2s) {
5552 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5553 h2s, h2s->id, h2s->flags,
5554 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5555 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5556 h2s->cs);
5557 if (h2s->cs)
5558 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5559 h2s->cs->flags, h2s->cs->data);
5560 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005561}
Willy Tarreau62f52692017-10-08 23:01:42 +02005562
5563/*******************************************************/
5564/* functions below are dedicated to the config parsers */
5565/*******************************************************/
5566
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005567/* config parser for global "tune.h2.header-table-size" */
5568static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5569 struct proxy *defpx, const char *file, int line,
5570 char **err)
5571{
5572 if (too_many_args(1, args, err, NULL))
5573 return -1;
5574
5575 h2_settings_header_table_size = atoi(args[1]);
5576 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5577 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5578 return -1;
5579 }
5580 return 0;
5581}
Willy Tarreau62f52692017-10-08 23:01:42 +02005582
Willy Tarreaue6baec02017-07-27 11:45:11 +02005583/* config parser for global "tune.h2.initial-window-size" */
5584static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5585 struct proxy *defpx, const char *file, int line,
5586 char **err)
5587{
5588 if (too_many_args(1, args, err, NULL))
5589 return -1;
5590
5591 h2_settings_initial_window_size = atoi(args[1]);
5592 if (h2_settings_initial_window_size < 0) {
5593 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5594 return -1;
5595 }
5596 return 0;
5597}
5598
Willy Tarreau5242ef82017-07-27 11:47:28 +02005599/* config parser for global "tune.h2.max-concurrent-streams" */
5600static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5601 struct proxy *defpx, const char *file, int line,
5602 char **err)
5603{
5604 if (too_many_args(1, args, err, NULL))
5605 return -1;
5606
5607 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005608 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005609 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5610 return -1;
5611 }
5612 return 0;
5613}
5614
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005615/* config parser for global "tune.h2.max-frame-size" */
5616static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5617 struct proxy *defpx, const char *file, int line,
5618 char **err)
5619{
5620 if (too_many_args(1, args, err, NULL))
5621 return -1;
5622
5623 h2_settings_max_frame_size = atoi(args[1]);
5624 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5625 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5626 return -1;
5627 }
5628 return 0;
5629}
5630
Willy Tarreau62f52692017-10-08 23:01:42 +02005631
5632/****************************************/
5633/* MUX initialization and instanciation */
5634/***************************************/
5635
5636/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005637static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005638 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005639 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005640 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005641 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005642 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005643 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005644 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005645 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005646 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005647 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005648 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005649 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005650 .shutr = h2_shutr,
5651 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005652 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005653 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005654 .name = "H2",
5655};
5656
Christopher Faulet32f61c02018-04-10 14:33:41 +02005657/* PROTO selection : this mux registers PROTO token "h2" */
5658static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005659 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005660
Willy Tarreau0108d902018-11-25 19:14:37 +01005661INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5662
Willy Tarreauf8957272018-10-03 10:25:20 +02005663static struct mux_proto_list mux_proto_h2_htx =
5664 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5665
5666INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5667
Willy Tarreau62f52692017-10-08 23:01:42 +02005668/* config keyword parsers */
5669static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005670 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005671 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005672 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005673 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005674 { 0, NULL, NULL }
5675}};
5676
Willy Tarreau0108d902018-11-25 19:14:37 +01005677INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);