blob: bedbcaa4faf7e98d607720491af2024c2e65ae03 [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 Tarreau3b94d982021-01-20 10:53:13 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
67#define H2_CF_RCVD_SHUT 0x00020000 // a recv() attempt already failed on a shutdown
68#define H2_CF_END_REACHED 0x00040000 // pending data too short with RCVD_SHUT present
Willy Tarreau081d4722017-05-16 21:51:05 +020069
Willy Tarreau5ab6b572017-09-22 08:05:00 +020070/* H2 connection state, in h2c->st0 */
71enum h2_cs {
72 H2_CS_PREFACE, // init done, waiting for connection preface
73 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
74 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
75 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010076 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
77 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020078 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
79 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
80 H2_CS_ENTRIES // must be last
81} __attribute__((packed));
82
Willy Tarreau51330962019-05-26 09:38:07 +020083
Willy Tarreau9c218e72019-05-26 10:08:28 +020084/* 32 buffers: one for the ring's root, rest for the mbuf itself */
85#define H2C_MBUF_CNT 32
Willy Tarreau51330962019-05-26 09:38:07 +020086
Willy Tarreau5ab6b572017-09-22 08:05:00 +020087/* H2 connection descriptor */
88struct h2c {
89 struct connection *conn;
90
91 enum h2_cs st0; /* mux state */
92 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
93
94 /* 16 bit hole here */
95 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010096 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020097 int32_t max_id; /* highest ID known on this connection, <0 before preface */
98 uint32_t rcvd_c; /* newly received data to ACK for the connection */
99 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
100
101 /* states for the demux direction */
102 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200103 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200104
105 int32_t dsi; /* demux stream ID (<0 = idle) */
106 int32_t dfl; /* demux frame length (if dsi >= 0) */
107 int8_t dft; /* demux frame type (if dsi >= 0) */
108 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100109 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
110 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200111 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
112
113 /* states for the mux direction */
Willy Tarreau51330962019-05-26 09:38:07 +0200114 struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200115 int32_t msi; /* mux stream ID (<0 = idle) */
116 int32_t mfl; /* mux frame length (if dsi >= 0) */
117 int8_t mft; /* mux frame type (if dsi >= 0) */
118 int8_t mff; /* mux frame flags (if dsi >= 0) */
119 /* 16 bit hole here */
120 int32_t miw; /* mux initial window size for all new streams */
121 int32_t mws; /* mux window size. Can be negative. */
122 int32_t mfs; /* mux's max frame size */
123
Willy Tarreauea392822017-10-31 10:02:25 +0100124 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100125 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100126 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200127 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100128 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100129 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200130 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100131 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132 struct eb_root streams_by_id; /* all active streams by their ID */
133 struct list send_list; /* list of blocked streams requesting to send */
134 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200135 struct list blocked_list; /* list of streams blocked for other reasons (e.g. sfctl, dep) */
Olivier Houchardd846c262018-10-19 17:24:29 +0200136 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100137 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200138 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200139};
140
Willy Tarreau18312642017-10-11 07:57:07 +0200141/* H2 stream state, in h2s->st */
142enum h2_ss {
143 H2_SS_IDLE = 0, // idle
144 H2_SS_RLOC, // reserved(local)
145 H2_SS_RREM, // reserved(remote)
146 H2_SS_OPEN, // open
147 H2_SS_HREM, // half-closed(remote)
148 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200149 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200150 H2_SS_CLOSED, // closed
151 H2_SS_ENTRIES // must be last
152} __attribute__((packed));
153
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200154#define H2_SS_MASK(state) (1UL << (state))
155#define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE)
156#define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC)
157#define H2_SS_RREM_BIT (1UL << H2_SS_RREM)
158#define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN)
159#define H2_SS_HREM_BIT (1UL << H2_SS_HREM)
160#define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC)
161#define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR)
162#define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED)
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200163
Willy Tarreau18312642017-10-11 07:57:07 +0200164/* HTTP/2 stream flags (32 bit), in h2s->flags */
165#define H2_SF_NONE 0x00000000
166#define H2_SF_ES_RCVD 0x00000001
167#define H2_SF_ES_SENT 0x00000002
168
169#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
170#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
171
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200172/* stream flags indicating the reason the stream is blocked */
173#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200174#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux (must be in send list)
175#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl (must be in fctl list)
176#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl (must be in blocked list)
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200177#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
178
Willy Tarreau454f9052017-10-26 19:40:35 +0200179/* stream flags indicating how data is supposed to be sent */
180#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
181#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
182
183/* step we're currently in when sending chunks. This is needed because we may
184 * have to transfer chunks as large as a full buffer so there's no room left
185 * for size nor crlf around.
186 */
187#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
188#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
189#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
190
191#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
192
Willy Tarreau67434202017-11-06 20:20:51 +0100193#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100194#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100195
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100196#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
197
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200198#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
199#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200200#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200201
202
Willy Tarreau18312642017-10-11 07:57:07 +0200203/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
204 * it is being processed in the internal HTTP representation (H1 for now).
205 */
206struct h2s {
207 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100208 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200209 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200210 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200211 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200212 int32_t id; /* stream ID */
213 uint32_t flags; /* H2_SF_* */
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200214 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200215 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
216 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200217 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100218 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200219 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200220 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 +0100221 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
222 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 +0200223 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100224 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200225};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200226
Willy Tarreauc6405142017-09-21 20:23:50 +0200227/* descriptor for an h2 frame header */
228struct h2_fh {
229 uint32_t len; /* length, host order, 24 bits */
230 uint32_t sid; /* stream id, host order, 31 bits */
231 uint8_t ft; /* frame type */
232 uint8_t ff; /* frame flags */
233};
234
Willy Tarreau8ceae722018-11-26 11:58:30 +0100235/* the h2c connection pool */
236DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
237
238/* the h2s stream pool */
239DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
240
Willy Tarreaudc572362018-12-12 08:08:05 +0100241/* The default connection window size is 65535, it may only be enlarged using
242 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
243 * we'll pretend we already received the difference between the two to send
244 * an equivalent window update to enlarge it to 2G-1.
245 */
246#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
247
Willy Tarreau455d5682019-05-24 19:42:18 +0200248/* maximum amount of data we're OK with re-aligning for buffer optimizations */
249#define MAX_DATA_REALIGN 1024
250
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200251/* a few settings from the global section */
252static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200253static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100254static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100255static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200256
Willy Tarreau2a856182017-05-16 15:20:39 +0200257/* a dmumy closed stream */
258static const struct h2s *h2_closed_stream = &(const struct h2s){
259 .cs = NULL,
260 .h2c = NULL,
261 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100262 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100263 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200264 .id = 0,
265};
266
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100267/* a dmumy closed stream returning a PROTOCOL_ERROR error */
268static const struct h2s *h2_error_stream = &(const struct h2s){
269 .cs = NULL,
270 .h2c = NULL,
271 .st = H2_SS_CLOSED,
272 .errcode = H2_ERR_PROTOCOL_ERROR,
273 .flags = 0,
274 .id = 0,
275};
276
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100277/* a dmumy closed stream returning a REFUSED_STREAM error */
278static const struct h2s *h2_refused_stream = &(const struct h2s){
279 .cs = NULL,
280 .h2c = NULL,
281 .st = H2_SS_CLOSED,
282 .errcode = H2_ERR_REFUSED_STREAM,
283 .flags = 0,
284 .id = 0,
285};
286
Willy Tarreau2a856182017-05-16 15:20:39 +0200287/* and a dummy idle stream for use with any unannounced stream */
288static const struct h2s *h2_idle_stream = &(const struct h2s){
289 .cs = NULL,
290 .h2c = NULL,
291 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100292 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200293 .id = 0,
294};
295
Olivier Houchard9f6af332018-05-25 14:04:04 +0200296static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200297static int h2_send(struct h2c *h2c);
298static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200299static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200300static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100301static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100302static 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 +0100303static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200304static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100305static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100306static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200307
Christopher Faulet11b10532020-10-08 15:38:41 +0200308
Willy Tarreau3b94d982021-01-20 10:53:13 +0100309/* Detect a pending read0 for a H2 connection. It happens if a read0 was
310 * already reported on a previous xprt->rcvbuf() AND a frame parser failed
311 * to parse pending data, confirming no more progress is possible because
312 * we're facing a truncated frame. The function returns 1 to report a read0
313 * or 0 otherwise.
Christopher Faulet11b10532020-10-08 15:38:41 +0200314 */
Willy Tarreau3b94d982021-01-20 10:53:13 +0100315static inline int h2c_read0_pending(struct h2c *h2c)
Christopher Faulet11b10532020-10-08 15:38:41 +0200316{
Willy Tarreau3b94d982021-01-20 10:53:13 +0100317 return !!(h2c->flags & H2_CF_END_REACHED);
Christopher Faulet11b10532020-10-08 15:38:41 +0200318}
319
Willy Tarreau534d8f92019-10-01 10:12:00 +0200320/* returns true if the connection is allowed to expire, false otherwise. A
321 * connection may expire when:
322 * - it has no stream
323 * - it has data in the mux buffer
324 * - it has streams in the blocked list
325 * - it has streams in the fctl list
326 * - it has streams in the send list
327 * Otherwise it means some streams are waiting in the data layer and it should
328 * not expire.
329 */
330static inline int h2c_may_expire(const struct h2c *h2c)
331{
332 return eb_is_empty(&h2c->streams_by_id) ||
333 br_data(h2c->mbuf) ||
334 !LIST_ISEMPTY(&h2c->blocked_list) ||
335 !LIST_ISEMPTY(&h2c->fctl_list) ||
336 !LIST_ISEMPTY(&h2c->send_list) ||
337 !LIST_ISEMPTY(&h2c->sending_list);
338}
339
Olivier Houchard7a977432019-03-21 15:47:13 +0100340static __inline int
Willy Tarreau534d8f92019-10-01 10:12:00 +0200341h2c_is_dead(const struct h2c *h2c)
Olivier Houchard7a977432019-03-21 15:47:13 +0100342{
343 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
344 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
345 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
346 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200347 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100348 (conn_xprt_read0_pending(h2c->conn) ||
349 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
350 return 1;
351
352 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100353}
354
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200355/*****************************************************/
356/* functions below are for dynamic buffer management */
357/*****************************************************/
358
Willy Tarreau315d8072017-12-10 22:17:57 +0100359/* indicates whether or not the we may call the h2_recv() function to attempt
360 * to receive data into the buffer and/or demux pending data. The condition is
361 * a bit complex due to some API limits for now. The rules are the following :
362 * - if an error or a shutdown was detected on the connection and the buffer
363 * is empty, we must not attempt to receive
364 * - if the demux buf failed to be allocated, we must not try to receive and
365 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100366 * - if no flag indicates a blocking condition, we may attempt to receive,
367 * regardless of whether the demux buffer is full or not, so that only
368 * de demux part decides whether or not to block. This is needed because
369 * the connection API indeed prevents us from re-enabling receipt that is
370 * already enabled in a polled state, so we must always immediately stop
371 * as soon as the demux can't proceed so as never to hit an end of read
372 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100373 * - otherwise must may not attempt
374 */
375static inline int h2_recv_allowed(const struct h2c *h2c)
376{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200377 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100378 (h2c->st0 >= H2_CS_ERROR ||
379 h2c->conn->flags & CO_FL_ERROR ||
380 conn_xprt_read0_pending(h2c->conn)))
381 return 0;
382
383 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100384 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100385 return 1;
386
387 return 0;
388}
389
Willy Tarreau47b515a2018-12-21 16:09:41 +0100390/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200391static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100392{
393 if (!h2_recv_allowed(h2c))
394 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200395 if ((!consider_buffer || !b_data(&h2c->dbuf))
396 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100397 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200398 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100399}
400
401
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100402/* returns true if the front connection has too many conn_streams attached */
403static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200404{
Willy Tarreaua8754662018-12-23 20:43:58 +0100405 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200406}
407
Willy Tarreau44e973f2018-03-01 17:49:30 +0100408/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
409 * flags are used to figure what buffer was requested. It returns 1 if the
410 * allocation succeeds, in which case the connection is woken up, or 0 if it's
411 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200412 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100413static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200414{
415 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100416 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200417
Willy Tarreau44e973f2018-03-01 17:49:30 +0100418 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200419 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200420 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200421 return 1;
422 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200423
Willy Tarreau51330962019-05-26 09:38:07 +0200424 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100425 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200426
427 if (h2c->flags & H2_CF_DEM_MROOM) {
428 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200429 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200430 }
Willy Tarreau14398122017-09-22 14:26:04 +0200431 return 1;
432 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100433
434 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
435 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200436 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100437 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200438 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100439 return 1;
440 }
441
Willy Tarreau14398122017-09-22 14:26:04 +0200442 return 0;
443}
444
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200445static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200446{
447 struct buffer *buf = NULL;
448
Willy Tarreauc234ae32019-05-13 17:56:11 +0200449 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100450 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
451 h2c->buf_wait.target = h2c;
452 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100453 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100454 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100455 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200456 __conn_xprt_stop_recv(h2c->conn);
457 }
458 return buf;
459}
460
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200461static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200462{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200463 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100464 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200465 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200466 }
467}
468
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200469static inline void h2_release_mbuf(struct h2c *h2c)
470{
471 struct buffer *buf;
472 unsigned int count = 0;
473
474 while (b_size(buf = br_head_pick(h2c->mbuf))) {
475 b_free(buf);
476 count++;
477 }
478 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200479 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200480}
481
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100482/* returns the number of allocatable outgoing streams for the connection taking
483 * the last_sid and the reserved ones into account.
484 */
485static inline int h2_streams_left(const struct h2c *h2c)
486{
487 int ret;
488
489 /* consider the number of outgoing streams we're allowed to create before
490 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
491 * nb_reserved is the number of streams which don't yet have an ID.
492 */
493 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
494 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
495 if (ret < 0)
496 ret = 0;
497 return ret;
498}
499
Willy Tarreau00f18a32019-01-26 12:19:01 +0100500/* returns the number of streams in use on a connection to figure if it's
501 * idle or not. We check nb_cs and not nb_streams as the caller will want
502 * to know if it was the last one after a detach().
503 */
504static int h2_used_streams(struct connection *conn)
505{
506 struct h2c *h2c = conn->ctx;
507
508 return h2c->nb_cs;
509}
510
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100511/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100512static int h2_avail_streams(struct connection *conn)
513{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100514 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100515 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100516 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100517
Willy Tarreau6afec462019-01-28 06:40:19 +0100518 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
519 * streams on the connection.
520 */
521 if (h2c->last_sid >= 0)
522 return 0;
523
Willy Tarreaud63b85d2019-10-31 15:10:03 +0100524 if (h2c->st0 >= H2_CS_ERROR)
525 return 0;
526
Willy Tarreau86949782019-01-31 10:42:05 +0100527 /* note: may be negative if a SETTINGS frame changes the limit */
528 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100529
530 /* we must also consider the limit imposed by stream IDs */
531 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100532 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100533 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100534 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
535 ret1 = MIN(ret1, ret2);
536 }
537 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100538}
539
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200540
Willy Tarreau62f52692017-10-08 23:01:42 +0200541/*****************************************************************/
542/* functions below are dedicated to the mux setup and management */
543/*****************************************************************/
544
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200545/* Initialize the mux once it's attached. For outgoing connections, the context
546 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200547 * connections from the fact that the context is still NULL (even during mux
548 * upgrades). <input> is always used as Input buffer and may contain data. It is
549 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200550 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200551static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
552 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553{
554 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100555 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200556
Willy Tarreaubafbe012017-11-24 17:34:44 +0100557 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200558 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200559 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200560
Christopher Faulete9b70722019-04-08 10:46:02 +0200561 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200562 h2c->flags = H2_CF_IS_BACK;
563 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
564 if (tick_isset(prx->timeout.serverfin))
565 h2c->shut_timeout = prx->timeout.serverfin;
566 } else {
567 h2c->flags = H2_CF_NONE;
568 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
569 if (tick_isset(prx->timeout.clientfin))
570 h2c->shut_timeout = prx->timeout.clientfin;
571 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100572
Willy Tarreau0b37d652018-10-03 10:33:02 +0200573 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100574 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100575 if (tick_isset(h2c->timeout)) {
576 t = task_new(tid_bit);
577 if (!t)
578 goto fail;
579
580 h2c->task = t;
581 t->process = h2_timeout_task;
582 t->context = h2c;
583 t->expire = tick_add(now_ms, h2c->timeout);
584 }
Willy Tarreauea392822017-10-31 10:02:25 +0100585
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200586 h2c->wait_event.tasklet = tasklet_new();
587 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200588 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200589 h2c->wait_event.tasklet->process = h2_io_cb;
590 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100591 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200592
Willy Tarreau32218eb2017-09-22 08:07:25 +0200593 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
594 if (!h2c->ddht)
595 goto fail;
596
597 /* Initialise the context. */
598 h2c->st0 = H2_CS_PREFACE;
599 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100600 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200601 h2c->max_id = -1;
602 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100603 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200604 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100605 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200606 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100607 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100608 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200609
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200610 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200611 h2c->dsi = -1;
612 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100613
Willy Tarreau32218eb2017-09-22 08:07:25 +0200614 h2c->last_sid = -1;
615
Willy Tarreau51330962019-05-26 09:38:07 +0200616 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200617 h2c->miw = 65535; /* mux initial window size */
618 h2c->mws = 65535; /* mux window size */
619 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200620 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200621 LIST_INIT(&h2c->send_list);
622 LIST_INIT(&h2c->fctl_list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200623 LIST_INIT(&h2c->blocked_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200624 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100625 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200626
Willy Tarreau3f133572017-10-31 19:21:06 +0100627 if (t)
628 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100629
Willy Tarreau01b44822018-10-03 14:26:37 +0200630 if (h2c->flags & H2_CF_IS_BACK) {
631 /* FIXME: this is temporary, for outgoing connections we need
632 * to immediately allocate a stream until the code is modified
633 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100634 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200635 */
636 struct h2s *h2s;
637
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100638 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200639 if (!h2s)
640 goto fail_stream;
641 }
642
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100643 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200644
Willy Tarreau0f383582018-10-03 14:22:21 +0200645 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200646 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200647 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200648 fail_stream:
649 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200650 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200651 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200652 if (h2c->wait_event.tasklet)
653 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100654 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200655 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200656 return -1;
657}
658
Willy Tarreau751f2d02018-10-05 09:35:00 +0200659/* returns the next allocatable outgoing stream ID for the H2 connection, or
660 * -1 if no more is allocatable.
661 */
662static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
663{
664 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100665
666 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200667 id = -1;
668 return id;
669}
670
Willy Tarreau2373acc2017-10-12 17:35:14 +0200671/* returns the stream associated with id <id> or NULL if not found */
672static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
673{
674 struct eb32_node *node;
675
Willy Tarreau751f2d02018-10-05 09:35:00 +0200676 if (id == 0)
677 return (struct h2s *)h2_closed_stream;
678
Willy Tarreau2a856182017-05-16 15:20:39 +0200679 if (id > h2c->max_id)
680 return (struct h2s *)h2_idle_stream;
681
Willy Tarreau2373acc2017-10-12 17:35:14 +0200682 node = eb32_lookup(&h2c->streams_by_id, id);
683 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200684 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200685
686 return container_of(node, struct h2s, by_id);
687}
688
Christopher Faulet73c12072019-04-08 11:23:22 +0200689/* release function. This one should be called to free all resources allocated
690 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200691 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200692static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200693{
Christopher Faulet61840e72019-04-15 09:33:32 +0200694 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200695
Willy Tarreau32218eb2017-09-22 08:07:25 +0200696 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200697 /* The connection must be aattached to this mux to be released */
698 if (h2c->conn && h2c->conn->ctx == h2c)
699 conn = h2c->conn;
700
Willy Tarreau32218eb2017-09-22 08:07:25 +0200701 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200702
Willy Tarreau186e96e2019-05-28 17:21:18 +0200703 if (LIST_ADDED(&h2c->buf_wait.list)) {
704 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
705 LIST_DEL(&h2c->buf_wait.list);
706 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
707 }
Willy Tarreau14398122017-09-22 14:26:04 +0200708
Willy Tarreau44e973f2018-03-01 17:49:30 +0100709 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200710 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100711
Willy Tarreauea392822017-10-31 10:02:25 +0100712 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200713 h2c->task->context = NULL;
714 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100715 h2c->task = NULL;
716 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200717 if (h2c->wait_event.tasklet)
718 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet0e012562019-09-18 11:07:20 +0200719 if (conn && h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100720 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Christopher Faulet0e012562019-09-18 11:07:20 +0200721 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100722
Willy Tarreaubafbe012017-11-24 17:34:44 +0100723 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200724 }
725
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200726 if (conn) {
727 conn->mux = NULL;
728 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200729
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200730 conn_stop_tracking(conn);
731 conn_full_close(conn);
732 if (conn->destroy_cb)
733 conn->destroy_cb(conn);
734 conn_free(conn);
735 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200736}
737
738
Willy Tarreau71681172017-10-23 14:39:06 +0200739/******************************************************/
740/* functions below are for the H2 protocol processing */
741/******************************************************/
742
743/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100744static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200745{
746 return h2s ? h2s->id : 0;
747}
748
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200749/* returns the sum of the stream's own window size and the mux's initial
750 * window, which together form the stream's effective window size.
751 */
752static inline int h2s_mws(const struct h2s *h2s)
753{
754 return h2s->sws + h2s->h2c->miw;
755}
756
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200757/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100758static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200759{
760 if (h2c->msi < 0)
761 return 0;
762
763 if (h2c->msi == h2s_id(h2s))
764 return 0;
765
766 return 1;
767}
768
Willy Tarreau741d6df2017-10-17 08:00:59 +0200769/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100770static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200771{
772 h2c->errcode = err;
773 h2c->st0 = H2_CS_ERROR;
774}
775
Willy Tarreau175cebb2019-01-24 10:02:24 +0100776/* marks an error on the stream. It may also update an already closed stream
777 * (e.g. to report an error after an RST was received).
778 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100779static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200780{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100781 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200782 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100783 if (h2s->st < H2_SS_ERROR)
784 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100785 if (h2s->cs)
786 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200787 }
788}
789
Willy Tarreau7e094452018-12-19 18:08:52 +0100790/* attempt to notify the data layer of recv availability */
791static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
792{
793 struct wait_event *sw;
794
795 if (h2s->recv_wait) {
796 sw = h2s->recv_wait;
797 sw->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200798 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100799 h2s->recv_wait = NULL;
800 }
801}
802
803/* attempt to notify the data layer of send availability */
804static void __maybe_unused h2s_notify_send(struct h2s *h2s)
805{
806 struct wait_event *sw;
807
Willy Tarreauc234ae32019-05-13 17:56:11 +0200808 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100809 sw = h2s->send_wait;
810 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100811 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200812 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100813 }
814}
815
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100816/* alerts the data layer, trying to wake it up by all means, following
817 * this sequence :
818 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
819 * - if its subscribed to send, then it's woken up for send
820 * - if it was subscribed to neither, its ->wake() callback is called
821 * It is safe to call this function with a closed stream which doesn't have a
822 * conn_stream anymore.
823 */
824static void __maybe_unused h2s_alert(struct h2s *h2s)
825{
826 if (h2s->recv_wait || h2s->send_wait) {
827 h2s_notify_recv(h2s);
828 h2s_notify_send(h2s);
829 }
830 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
831 h2s->cs->data_cb->wake(h2s->cs);
832}
833
Willy Tarreaue4820742017-07-27 13:37:23 +0200834/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100835static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200836{
837 uint8_t *out = frame;
838
839 *out = len >> 16;
840 write_n16(out + 1, len);
841}
842
Willy Tarreau54c15062017-10-10 17:10:03 +0200843/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
844 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
845 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200846 * available in the buffer's input prior to calling this function. The buffer
847 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200848 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100849static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200850 const struct buffer *b, int o)
851{
Willy Tarreau591d4452018-06-15 17:21:00 +0200852 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 +0200853}
854
Willy Tarreau1f094672017-11-20 21:27:45 +0100855static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200856{
Willy Tarreau591d4452018-06-15 17:21:00 +0200857 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200858}
859
Willy Tarreau1f094672017-11-20 21:27:45 +0100860static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200861{
Willy Tarreau591d4452018-06-15 17:21:00 +0200862 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200863}
864
Willy Tarreau1f094672017-11-20 21:27:45 +0100865static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200866{
Willy Tarreau591d4452018-06-15 17:21:00 +0200867 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200868}
869
870
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100871/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
872 * The algorithm is not obvious. It turns out that H2 headers are neither
873 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
874 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200875 *
876 * b0 b1 b2 b3 b4 b5..b8
877 * +----------+---------+--------+----+----+----------------------+
878 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
879 * +----------+---------+--------+----+----+----------------------+
880 *
881 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
882 * we get the sid properly aligned and ordered, and 16 bits of len properly
883 * ordered as well. The type and flags can be extracted using bit shifts from
884 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200885 * Returns zero if some bytes are missing, otherwise non-zero on success. The
886 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200887 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100888static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200889{
890 uint64_t w;
891
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100892 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200893 return 0;
894
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100895 w = h2_get_n64(b, o + 1);
896 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200897 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
898 h->ff = w >> 32;
899 h->ft = w >> 40;
900 h->len += w >> 48;
901 return 1;
902}
903
904/* skip the next 9 bytes corresponding to the frame header possibly parsed by
905 * h2_peek_frame_hdr() above.
906 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100907static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200908{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200909 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200910}
911
912/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100913static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200914{
915 int ret;
916
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100917 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200918 if (ret > 0)
919 h2_skip_frame_hdr(b);
920 return ret;
921}
922
Willy Tarreau00dd0782018-03-01 16:31:34 +0100923/* marks stream <h2s> as CLOSED and decrement the number of active streams for
924 * its connection if the stream was not yet closed. Please use this exclusively
925 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100926 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100927static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100928{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100929 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100930 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100931 if (!h2s->id)
932 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100933 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +0100934 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
935 h2s_notify_recv(h2s);
936 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100937 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100938 h2s->st = H2_SS_CLOSED;
939}
940
Willy Tarreau71049cc2018-03-28 13:56:39 +0200941/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
942static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100943{
944 h2s_close(h2s);
945 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200946 if (b_size(&h2s->rxbuf)) {
947 b_free(&h2s->rxbuf);
948 offer_buffers(NULL, tasks_run_queue);
949 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200950 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100951 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200952 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100953 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800954 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200955 * reference left would be in the h2c send_list/fctl_list, and if
956 * we're in it, we're getting out anyway
957 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100958 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200959 if (LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau86eded62019-06-14 14:47:49 +0200960 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +0200961 LIST_DEL_INIT(&h2s->sending_list);
962 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200963 tasklet_free(h2s->wait_event.tasklet);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100964 pool_free(pool_head_h2s, h2s);
965}
966
Willy Tarreaua8e49542018-10-03 18:53:55 +0200967/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
968 * stream tree. In case of error, nothing is added and NULL is returned. The
969 * causes of errors can be any failed memory allocation. The caller is
970 * responsible for checking if the connection may support an extra stream
971 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200972 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200973static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200974{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200975 struct h2s *h2s;
976
Willy Tarreaubafbe012017-11-24 17:34:44 +0100977 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200978 if (!h2s)
979 goto out;
980
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200981 h2s->wait_event.tasklet = tasklet_new();
982 if (!h2s->wait_event.tasklet) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200983 pool_free(pool_head_h2s, h2s);
984 goto out;
985 }
986 h2s->send_wait = NULL;
987 h2s->recv_wait = NULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200988 h2s->wait_event.tasklet->process = h2_deferred_shut;
989 h2s->wait_event.tasklet->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100990 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200991 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100992 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200993 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200994 h2s->cs = NULL;
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200995 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200996 h2s->flags = H2_SF_NONE;
997 h2s->errcode = H2_ERR_NO_ERROR;
998 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200999 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001000 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +02001001 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001002
1003 if (h2c->flags & H2_CF_IS_BACK) {
1004 h1m_init_req(&h2s->h1m);
1005 h2s->h1m.err_pos = -1; // don't care about errors on the request path
1006 h2s->h1m.flags |= H1_MF_TOLOWER;
1007 } else {
1008 h1m_init_res(&h2s->h1m);
1009 h2s->h1m.err_pos = -1; // don't care about errors on the response path
1010 h2s->h1m.flags |= H1_MF_TOLOWER;
1011 }
1012
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001013 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001014 if (id > 0)
1015 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001016 else
1017 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001018
1019 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001020 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001021 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001022
1023 return h2s;
1024
1025 out_free_h2s:
1026 pool_free(pool_head_h2s, h2s);
1027 out:
1028 return NULL;
1029}
1030
1031/* creates a new stream <id> on the h2c connection and returns it, or NULL in
1032 * case of memory allocation error.
1033 */
1034static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
1035{
1036 struct session *sess = h2c->conn->owner;
1037 struct conn_stream *cs;
1038 struct h2s *h2s;
1039
1040 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1041 goto out;
1042
1043 h2s = h2s_new(h2c, id);
1044 if (!h2s)
1045 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001046
1047 cs = cs_new(h2c->conn);
1048 if (!cs)
1049 goto out_close;
1050
Olivier Houchard746fb772018-12-15 19:42:00 +01001051 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001052 h2s->cs = cs;
1053 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001054 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001055
1056 if (stream_create_from_cs(cs) < 0)
1057 goto out_free_cs;
1058
Willy Tarreau590a0512018-09-05 11:56:48 +02001059 /* We want the accept date presented to the next stream to be the one
1060 * we have now, the handshake time to be null (since the next stream
1061 * is not delayed by a handshake), and the idle time to count since
1062 * right now.
1063 */
1064 sess->accept_date = date;
1065 sess->tv_accept = now;
1066 sess->t_handshake = 0;
1067
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001068 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001069 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001070 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001071 return h2s;
1072
1073 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001074 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001075 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001076 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001077 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001078 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001079 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001080 sess_log(sess);
1081 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001082}
1083
Willy Tarreau751f2d02018-10-05 09:35:00 +02001084/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1085 * and returns it, or NULL in case of memory allocation error or if the highest
1086 * possible stream ID was reached.
1087 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001088static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001089{
1090 struct h2s *h2s = NULL;
1091
Willy Tarreau86949782019-01-31 10:42:05 +01001092 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001093 goto out;
1094
Willy Tarreaua80dca82019-01-24 17:08:28 +01001095 if (h2_streams_left(h2c) < 1)
1096 goto out;
1097
Willy Tarreau751f2d02018-10-05 09:35:00 +02001098 /* Defer choosing the ID until we send the first message to create the stream */
1099 h2s = h2s_new(h2c, 0);
1100 if (!h2s)
1101 goto out;
1102
1103 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001104 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001105 cs->ctx = h2s;
1106 h2c->nb_cs++;
1107
Willy Tarreau751f2d02018-10-05 09:35:00 +02001108 out:
1109 return h2s;
1110}
1111
Willy Tarreaube5b7152017-09-25 16:25:39 +02001112/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1113 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1114 * the various settings codes.
1115 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001116static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001117{
1118 struct buffer *res;
1119 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001120 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001121 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001122 int ret;
1123
1124 if (h2c_mux_busy(h2c, NULL)) {
1125 h2c->flags |= H2_CF_DEM_MBUSY;
1126 return 0;
1127 }
1128
Willy Tarreaube5b7152017-09-25 16:25:39 +02001129 chunk_init(&buf, buf_data, sizeof(buf_data));
1130 chunk_memcpy(&buf,
1131 "\x00\x00\x00" /* length : 0 for now */
1132 "\x04\x00" /* type : 4 (settings), flags : 0 */
1133 "\x00\x00\x00\x00", /* stream ID : 0 */
1134 9);
1135
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001136 if (h2c->flags & H2_CF_IS_BACK) {
1137 /* send settings_enable_push=0 */
1138 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1139 }
1140
Willy Tarreaube5b7152017-09-25 16:25:39 +02001141 if (h2_settings_header_table_size != 4096) {
1142 char str[6] = "\x00\x01"; /* header_table_size */
1143
1144 write_n32(str + 2, h2_settings_header_table_size);
1145 chunk_memcat(&buf, str, 6);
1146 }
1147
1148 if (h2_settings_initial_window_size != 65535) {
1149 char str[6] = "\x00\x04"; /* initial_window_size */
1150
1151 write_n32(str + 2, h2_settings_initial_window_size);
1152 chunk_memcat(&buf, str, 6);
1153 }
1154
1155 if (h2_settings_max_concurrent_streams != 0) {
1156 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1157
1158 /* Note: 0 means "unlimited" for haproxy's config but not for
1159 * the protocol, so never send this value!
1160 */
1161 write_n32(str + 2, h2_settings_max_concurrent_streams);
1162 chunk_memcat(&buf, str, 6);
1163 }
1164
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001165 mfs = h2_settings_max_frame_size;
1166 if (mfs > global.tune.bufsize)
1167 mfs = global.tune.bufsize;
1168
1169 if (!mfs)
1170 mfs = global.tune.bufsize;
1171
1172 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001173 char str[6] = "\x00\x05"; /* max_frame_size */
1174
1175 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1176 * match bufsize - rewrite size, but at the moment it seems
1177 * that clients don't take care of it.
1178 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001179 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001180 chunk_memcat(&buf, str, 6);
1181 }
1182
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001183 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001184
1185 res = br_tail(h2c->mbuf);
1186 retry:
1187 if (!h2_get_buf(h2c, res)) {
1188 h2c->flags |= H2_CF_MUX_MALLOC;
1189 h2c->flags |= H2_CF_DEM_MROOM;
1190 return 0;
1191 }
1192
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001193 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001194 if (unlikely(ret <= 0)) {
1195 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001196 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1197 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001198 h2c->flags |= H2_CF_MUX_MFULL;
1199 h2c->flags |= H2_CF_DEM_MROOM;
1200 return 0;
1201 }
1202 else {
1203 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1204 return 0;
1205 }
1206 }
1207 return ret;
1208}
1209
Willy Tarreau52eed752017-09-22 15:05:09 +02001210/* Try to receive a connection preface, then upon success try to send our
1211 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1212 * missing data. It may return an error in h2c.
1213 */
1214static int h2c_frt_recv_preface(struct h2c *h2c)
1215{
1216 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001217 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001218
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001219 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001220
1221 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001222 if (ret1 < 0)
1223 sess_log(h2c->conn->owner);
1224
Willy Tarreau52eed752017-09-22 15:05:09 +02001225 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1226 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1227 return 0;
1228 }
1229
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001230 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001231 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001232 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001233
Willy Tarreaube5b7152017-09-25 16:25:39 +02001234 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001235}
1236
Willy Tarreau01b44822018-10-03 14:26:37 +02001237/* Try to send a connection preface, then upon success try to send our
1238 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1239 * missing data. It may return an error in h2c.
1240 */
1241static int h2c_bck_send_preface(struct h2c *h2c)
1242{
1243 struct buffer *res;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001244 int ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001245
1246 if (h2c_mux_busy(h2c, NULL)) {
1247 h2c->flags |= H2_CF_DEM_MBUSY;
1248 return 0;
1249 }
1250
Willy Tarreaubcc45952019-05-26 10:05:50 +02001251 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001252 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001253 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001254 h2c->flags |= H2_CF_MUX_MALLOC;
1255 h2c->flags |= H2_CF_DEM_MROOM;
1256 return 0;
1257 }
1258
1259 if (!b_data(res)) {
1260 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001261 ret = b_istput(res, ist(H2_CONN_PREFACE));
1262 if (unlikely(ret <= 0)) {
1263 if (!ret) {
1264 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1265 goto retry;
1266 h2c->flags |= H2_CF_MUX_MFULL;
1267 h2c->flags |= H2_CF_DEM_MROOM;
1268 return 0;
1269 }
1270 else {
1271 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1272 return 0;
1273 }
1274 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001275 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001276 return h2c_send_settings(h2c);
1277}
1278
Willy Tarreau081d4722017-05-16 21:51:05 +02001279/* try to send a GOAWAY frame on the connection to report an error or a graceful
1280 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1281 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1282 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1283 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1284 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1285 * on unrecoverable failure. It will not attempt to send one again in this last
1286 * case so that it is safe to use h2c_error() to report such errors.
1287 */
1288static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1289{
1290 struct buffer *res;
1291 char str[17];
1292 int ret;
1293
1294 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1295 return 1; // claim that it worked
1296
1297 if (h2c_mux_busy(h2c, h2s)) {
1298 if (h2s)
1299 h2s->flags |= H2_SF_BLK_MBUSY;
1300 else
1301 h2c->flags |= H2_CF_DEM_MBUSY;
1302 return 0;
1303 }
1304
Willy Tarreau9c218e72019-05-26 10:08:28 +02001305 /* len: 8, type: 7, flags: none, sid: 0 */
1306 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1307
1308 if (h2c->last_sid < 0)
1309 h2c->last_sid = h2c->max_id;
1310
1311 write_n32(str + 9, h2c->last_sid);
1312 write_n32(str + 13, h2c->errcode);
1313
Willy Tarreaubcc45952019-05-26 10:05:50 +02001314 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001315 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001316 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001317 h2c->flags |= H2_CF_MUX_MALLOC;
1318 if (h2s)
1319 h2s->flags |= H2_SF_BLK_MROOM;
1320 else
1321 h2c->flags |= H2_CF_DEM_MROOM;
1322 return 0;
1323 }
1324
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001325 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001326 if (unlikely(ret <= 0)) {
1327 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001328 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1329 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001330 h2c->flags |= H2_CF_MUX_MFULL;
1331 if (h2s)
1332 h2s->flags |= H2_SF_BLK_MROOM;
1333 else
1334 h2c->flags |= H2_CF_DEM_MROOM;
1335 return 0;
1336 }
1337 else {
1338 /* we cannot report this error using GOAWAY, so we mark
1339 * it and claim a success.
1340 */
1341 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1342 h2c->flags |= H2_CF_GOAWAY_FAILED;
1343 return 1;
1344 }
1345 }
1346 h2c->flags |= H2_CF_GOAWAY_SENT;
1347 return ret;
1348}
1349
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001350/* Try to send an RST_STREAM frame on the connection for the indicated stream
1351 * during mux operations. This stream must be valid and cannot be closed
1352 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1353 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1354 * not yet.
1355 *
1356 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1357 * to write the message, it subscribes the stream to future notifications.
1358 */
1359static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1360{
1361 struct buffer *res;
1362 char str[13];
1363 int ret;
1364
1365 if (!h2s || h2s->st == H2_SS_CLOSED)
1366 return 1;
1367
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001368 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1369 * RST_STREAM in response to a RST_STREAM frame.
1370 */
Willy Tarreaubf9a20b2019-08-06 10:01:40 +02001371 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001372 ret = 1;
1373 goto ignore;
1374 }
1375
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001376 if (h2c_mux_busy(h2c, h2s)) {
1377 h2s->flags |= H2_SF_BLK_MBUSY;
1378 return 0;
1379 }
1380
Willy Tarreau9c218e72019-05-26 10:08:28 +02001381 /* len: 4, type: 3, flags: none */
1382 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1383 write_n32(str + 5, h2s->id);
1384 write_n32(str + 9, h2s->errcode);
1385
Willy Tarreaubcc45952019-05-26 10:05:50 +02001386 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001387 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001388 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001389 h2c->flags |= H2_CF_MUX_MALLOC;
1390 h2s->flags |= H2_SF_BLK_MROOM;
1391 return 0;
1392 }
1393
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001394 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001395 if (unlikely(ret <= 0)) {
1396 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001397 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1398 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001399 h2c->flags |= H2_CF_MUX_MFULL;
1400 h2s->flags |= H2_SF_BLK_MROOM;
1401 return 0;
1402 }
1403 else {
1404 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1405 return 0;
1406 }
1407 }
1408
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001409 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001410 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001411 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001412 return ret;
1413}
1414
1415/* Try to send an RST_STREAM frame on the connection for the stream being
1416 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001417 * error code, even if the stream is one of the dummy ones, and will update
1418 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001419 *
1420 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1421 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001422 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001423 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001424 */
1425static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1426{
1427 struct buffer *res;
1428 char str[13];
1429 int ret;
1430
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001431 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1432 * RST_STREAM in response to a RST_STREAM frame.
1433 */
1434 if (h2c->dft == H2_FT_RST_STREAM) {
1435 ret = 1;
1436 goto ignore;
1437 }
1438
Willy Tarreau27a84c92017-10-17 08:10:17 +02001439 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001440 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001441 return 0;
1442 }
1443
Willy Tarreau9c218e72019-05-26 10:08:28 +02001444 /* len: 4, type: 3, flags: none */
1445 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1446
1447 write_n32(str + 5, h2c->dsi);
1448 write_n32(str + 9, h2s->errcode);
1449
Willy Tarreaubcc45952019-05-26 10:05:50 +02001450 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001451 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001452 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001453 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001454 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001455 return 0;
1456 }
1457
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001458 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001459 if (unlikely(ret <= 0)) {
1460 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001461 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1462 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001463 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001464 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001465 return 0;
1466 }
1467 else {
1468 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1469 return 0;
1470 }
1471 }
1472
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001473 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001474 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001475 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001476 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001477 }
1478
Willy Tarreau27a84c92017-10-17 08:10:17 +02001479 return ret;
1480}
1481
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001482/* try to send an empty DATA frame with the ES flag set to notify about the
1483 * end of stream and match a shutdown(write). If an ES was already sent as
1484 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1485 * on success or zero if nothing was done. In case of lack of room to write the
1486 * message, it subscribes the requesting stream to future notifications.
1487 */
1488static int h2_send_empty_data_es(struct h2s *h2s)
1489{
1490 struct h2c *h2c = h2s->h2c;
1491 struct buffer *res;
1492 char str[9];
1493 int ret;
1494
Willy Tarreau721c9742017-11-07 11:05:42 +01001495 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001496 return 1;
1497
1498 if (h2c_mux_busy(h2c, h2s)) {
1499 h2s->flags |= H2_SF_BLK_MBUSY;
1500 return 0;
1501 }
1502
Willy Tarreau9c218e72019-05-26 10:08:28 +02001503 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1504 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1505 write_n32(str + 5, h2s->id);
1506
Willy Tarreaubcc45952019-05-26 10:05:50 +02001507 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001508 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001509 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001510 h2c->flags |= H2_CF_MUX_MALLOC;
1511 h2s->flags |= H2_SF_BLK_MROOM;
1512 return 0;
1513 }
1514
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001515 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001516 if (likely(ret > 0)) {
1517 h2s->flags |= H2_SF_ES_SENT;
1518 }
1519 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001520 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1521 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001522 h2c->flags |= H2_CF_MUX_MFULL;
1523 h2s->flags |= H2_SF_BLK_MROOM;
1524 return 0;
1525 }
1526 else {
1527 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1528 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001529 }
1530 return ret;
1531}
1532
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001533/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001534 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001535 * is automatically updated accordingly. If the stream is orphaned, it is
1536 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001537 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001538static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001539{
1540 if (!h2s->cs) {
1541 /* this stream was already orphaned */
1542 h2s_destroy(h2s);
1543 return;
1544 }
1545
Christopher Faulet11b10532020-10-08 15:38:41 +02001546 if (h2c_read0_pending(h2s->h2c)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001547 if (h2s->st == H2_SS_OPEN)
1548 h2s->st = H2_SS_HREM;
1549 else if (h2s->st == H2_SS_HLOC)
1550 h2s_close(h2s);
1551 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001552
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001553 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1554 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1555 h2s->cs->flags |= CS_FL_ERR_PENDING;
1556 if (h2s->cs->flags & CS_FL_EOS)
1557 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001558
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001559 if (h2s->st < H2_SS_ERROR)
1560 h2s->st = H2_SS_ERROR;
1561 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001562
1563 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001564}
1565
1566/* wake the streams attached to the connection, whose id is greater than <last>
1567 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001568 */
Willy Tarreau23482912019-05-07 15:23:14 +02001569static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001570{
1571 struct eb32_node *node;
1572 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001573
Christopher Fauletf02ca002019-03-07 16:21:34 +01001574 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001575 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1576 while (node) {
1577 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001578 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001579 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001580 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001581
Christopher Fauletf02ca002019-03-07 16:21:34 +01001582 /* Wake all streams with unassigned ID (ID == 0) */
1583 node = eb32_lookup(&h2c->streams_by_id, 0);
1584 while (node) {
1585 h2s = container_of(node, struct h2s, by_id);
1586 if (h2s->id > 0)
1587 break;
1588 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001589 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001590 }
1591}
1592
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001593/* Wake up all blocked streams whose window size has become positive after the
1594 * mux's initial window was adjusted. This should be done after having processed
1595 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001596 */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001597static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001598{
1599 struct h2s *h2s;
1600 struct eb32_node *node;
1601
Willy Tarreau3421aba2017-07-27 15:41:03 +02001602 node = eb32_first(&h2c->streams_by_id);
1603 while (node) {
1604 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001605 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001606 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreaubda92dd2019-10-02 10:49:59 +02001607 LIST_DEL_INIT(&h2s->list);
1608 if (h2s->send_wait)
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001609 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001610 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001611 node = eb32_next(node);
1612 }
1613}
1614
1615/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1616 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001617 * return an error in h2c. The caller must have already verified frame length
1618 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001619 */
1620static int h2c_handle_settings(struct h2c *h2c)
1621{
1622 unsigned int offset;
1623 int error;
1624
1625 if (h2c->dff & H2_F_SETTINGS_ACK) {
1626 if (h2c->dfl) {
1627 error = H2_ERR_FRAME_SIZE_ERROR;
1628 goto fail;
1629 }
1630 return 1;
1631 }
1632
Willy Tarreau3421aba2017-07-27 15:41:03 +02001633 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001634 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001635 return 0;
1636
1637 /* parse the frame */
1638 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001639 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1640 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001641
1642 switch (type) {
1643 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1644 /* we need to update all existing streams with the
1645 * difference from the previous iws.
1646 */
1647 if (arg < 0) { // RFC7540#6.5.2
1648 error = H2_ERR_FLOW_CONTROL_ERROR;
1649 goto fail;
1650 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001651 h2c->miw = arg;
1652 break;
1653 case H2_SETTINGS_MAX_FRAME_SIZE:
1654 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1655 error = H2_ERR_PROTOCOL_ERROR;
1656 goto fail;
1657 }
1658 h2c->mfs = arg;
1659 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001660 case H2_SETTINGS_ENABLE_PUSH:
1661 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1662 error = H2_ERR_PROTOCOL_ERROR;
1663 goto fail;
1664 }
1665 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001666 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1667 if (h2c->flags & H2_CF_IS_BACK) {
1668 /* the limit is only for the backend; for the frontend it is our limit */
1669 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1670 arg = h2_settings_max_concurrent_streams;
1671 h2c->streams_limit = arg;
1672 }
1673 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001674 }
1675 }
1676
1677 /* need to ACK this frame now */
1678 h2c->st0 = H2_CS_FRAME_A;
1679 return 1;
1680 fail:
Willy Tarreau21178a52019-10-23 11:06:35 +02001681 if (!(h2c->flags & H2_CF_IS_BACK))
1682 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001683 h2c_error(h2c, error);
1684 return 0;
1685}
1686
1687/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1688 * success or one of the h2_status values.
1689 */
1690static int h2c_ack_settings(struct h2c *h2c)
1691{
1692 struct buffer *res;
1693 char str[9];
1694 int ret = -1;
1695
1696 if (h2c_mux_busy(h2c, NULL)) {
1697 h2c->flags |= H2_CF_DEM_MBUSY;
1698 return 0;
1699 }
1700
Willy Tarreau9c218e72019-05-26 10:08:28 +02001701 memcpy(str,
1702 "\x00\x00\x00" /* length : 0 (no data) */
1703 "\x04" "\x01" /* type : 4, flags : ACK */
1704 "\x00\x00\x00\x00" /* stream ID */, 9);
1705
Willy Tarreaubcc45952019-05-26 10:05:50 +02001706 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001707 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001708 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001709 h2c->flags |= H2_CF_MUX_MALLOC;
1710 h2c->flags |= H2_CF_DEM_MROOM;
1711 return 0;
1712 }
1713
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001714 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001715 if (unlikely(ret <= 0)) {
1716 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001717 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1718 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001719 h2c->flags |= H2_CF_MUX_MFULL;
1720 h2c->flags |= H2_CF_DEM_MROOM;
1721 return 0;
1722 }
1723 else {
1724 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1725 return 0;
1726 }
1727 }
1728 return ret;
1729}
1730
Willy Tarreaucf68c782017-10-10 17:11:41 +02001731/* processes a PING frame and schedules an ACK if needed. The caller must pass
1732 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001733 * missing data. The caller must have already verified frame length
1734 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001735 */
1736static int h2c_handle_ping(struct h2c *h2c)
1737{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001738 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001739 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001740 h2c->st0 = H2_CS_FRAME_A;
1741 return 1;
1742}
1743
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001744/* Try to send a window update for stream id <sid> and value <increment>.
1745 * Returns > 0 on success or zero on missing room or failure. It may return an
1746 * error in h2c.
1747 */
1748static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1749{
1750 struct buffer *res;
1751 char str[13];
1752 int ret = -1;
1753
1754 if (h2c_mux_busy(h2c, NULL)) {
1755 h2c->flags |= H2_CF_DEM_MBUSY;
1756 return 0;
1757 }
1758
Willy Tarreau9c218e72019-05-26 10:08:28 +02001759 /* length: 4, type: 8, flags: none */
1760 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1761 write_n32(str + 5, sid);
1762 write_n32(str + 9, increment);
1763
Willy Tarreaubcc45952019-05-26 10:05:50 +02001764 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001765 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001766 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001767 h2c->flags |= H2_CF_MUX_MALLOC;
1768 h2c->flags |= H2_CF_DEM_MROOM;
1769 return 0;
1770 }
1771
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001772 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001773 if (unlikely(ret <= 0)) {
1774 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001775 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1776 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001777 h2c->flags |= H2_CF_MUX_MFULL;
1778 h2c->flags |= H2_CF_DEM_MROOM;
1779 return 0;
1780 }
1781 else {
1782 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1783 return 0;
1784 }
1785 }
1786 return ret;
1787}
1788
1789/* try to send pending window update for the connection. It's safe to call it
1790 * with no pending updates. Returns > 0 on success or zero on missing room or
1791 * failure. It may return an error in h2c.
1792 */
1793static int h2c_send_conn_wu(struct h2c *h2c)
1794{
1795 int ret = 1;
1796
1797 if (h2c->rcvd_c <= 0)
1798 return 1;
1799
Willy Tarreau97aaa672018-12-23 09:49:04 +01001800 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1801 /* increase the advertised connection window to 2G on
1802 * first update.
1803 */
1804 h2c->flags |= H2_CF_WINDOW_OPENED;
1805 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1806 }
1807
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001808 /* send WU for the connection */
1809 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1810 if (ret > 0)
1811 h2c->rcvd_c = 0;
1812
1813 return ret;
1814}
1815
1816/* try to send pending window update for the current dmux stream. It's safe to
1817 * call it with no pending updates. Returns > 0 on success or zero on missing
1818 * room or failure. It may return an error in h2c.
1819 */
1820static int h2c_send_strm_wu(struct h2c *h2c)
1821{
1822 int ret = 1;
1823
1824 if (h2c->rcvd_s <= 0)
1825 return 1;
1826
1827 /* send WU for the stream */
1828 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1829 if (ret > 0)
1830 h2c->rcvd_s = 0;
1831
1832 return ret;
1833}
1834
Willy Tarreaucf68c782017-10-10 17:11:41 +02001835/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1836 * success, 0 on missing data or one of the h2_status values.
1837 */
1838static int h2c_ack_ping(struct h2c *h2c)
1839{
1840 struct buffer *res;
1841 char str[17];
1842 int ret = -1;
1843
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001844 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001845 return 0;
1846
1847 if (h2c_mux_busy(h2c, NULL)) {
1848 h2c->flags |= H2_CF_DEM_MBUSY;
1849 return 0;
1850 }
1851
Willy Tarreaucf68c782017-10-10 17:11:41 +02001852 memcpy(str,
1853 "\x00\x00\x08" /* length : 8 (same payload) */
1854 "\x06" "\x01" /* type : 6, flags : ACK */
1855 "\x00\x00\x00\x00" /* stream ID */, 9);
1856
1857 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001858 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001859
Willy Tarreau9c218e72019-05-26 10:08:28 +02001860 res = br_tail(h2c->mbuf);
1861 retry:
1862 if (!h2_get_buf(h2c, res)) {
1863 h2c->flags |= H2_CF_MUX_MALLOC;
1864 h2c->flags |= H2_CF_DEM_MROOM;
1865 return 0;
1866 }
1867
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001868 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001869 if (unlikely(ret <= 0)) {
1870 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001871 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1872 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02001873 h2c->flags |= H2_CF_MUX_MFULL;
1874 h2c->flags |= H2_CF_DEM_MROOM;
1875 return 0;
1876 }
1877 else {
1878 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1879 return 0;
1880 }
1881 }
1882 return ret;
1883}
1884
Willy Tarreau26f95952017-07-27 17:18:30 +02001885/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1886 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001887 * h2c or h2s. The caller must have already verified frame length and stream ID
1888 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001889 */
1890static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1891{
1892 int32_t inc;
1893 int error;
1894
Willy Tarreau26f95952017-07-27 17:18:30 +02001895 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001896 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001897 return 0;
1898
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001899 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001900
1901 if (h2c->dsi != 0) {
1902 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001903
1904 /* it's not an error to receive WU on a closed stream */
1905 if (h2s->st == H2_SS_CLOSED)
1906 return 1;
1907
1908 if (!inc) {
1909 error = H2_ERR_PROTOCOL_ERROR;
1910 goto strm_err;
1911 }
1912
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001913 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001914 error = H2_ERR_FLOW_CONTROL_ERROR;
1915 goto strm_err;
1916 }
1917
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001918 h2s->sws += inc;
1919 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001920 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreaubda92dd2019-10-02 10:49:59 +02001921 LIST_DEL_INIT(&h2s->list);
1922 if (h2s->send_wait)
Olivier Houcharddddfe312018-10-10 18:51:00 +02001923 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001924 }
1925 }
1926 else {
1927 /* connection window update */
1928 if (!inc) {
1929 error = H2_ERR_PROTOCOL_ERROR;
1930 goto conn_err;
1931 }
1932
1933 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1934 error = H2_ERR_FLOW_CONTROL_ERROR;
1935 goto conn_err;
1936 }
1937
1938 h2c->mws += inc;
1939 }
1940
1941 return 1;
1942
1943 conn_err:
1944 h2c_error(h2c, error);
1945 return 0;
1946
1947 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001948 h2s_error(h2s, error);
1949 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001950 return 0;
1951}
1952
Willy Tarreaue96b0922017-10-30 00:28:29 +01001953/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001954 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1955 * have already verified frame length and stream ID validity. Described in
1956 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001957 */
1958static int h2c_handle_goaway(struct h2c *h2c)
1959{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001960 int last;
1961
Willy Tarreaue96b0922017-10-30 00:28:29 +01001962 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001963 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001964 return 0;
1965
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001966 last = h2_get_n32(&h2c->dbuf, 0);
1967 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001968 if (h2c->last_sid < 0)
1969 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001970 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001971 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001972}
1973
Willy Tarreau92153fc2017-12-03 19:46:19 +01001974/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001975 * invalid. Returns > 0 on success or zero on missing data. It may return an
1976 * error in h2c. The caller must have already verified frame length and stream
1977 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001978 */
1979static int h2c_handle_priority(struct h2c *h2c)
1980{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001981 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001982 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001983 return 0;
1984
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001985 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001986 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001987 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1988 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001989 }
1990 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001991}
1992
Willy Tarreaucd234e92017-08-18 10:59:39 +02001993/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001994 * Returns > 0 on success or zero on missing data. The caller must have already
1995 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001996 */
1997static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1998{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001999 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002000 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02002001 return 0;
2002
2003 /* late RST, already handled */
2004 if (h2s->st == H2_SS_CLOSED)
2005 return 1;
2006
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002007 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002008 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002009
2010 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01002011 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01002012 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002013 }
2014
2015 h2s->flags |= H2_SF_RST_RCVD;
2016 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002017}
2018
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002019/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2020 * It may return an error in h2c or h2s. The caller must consider that the
2021 * return value is the new h2s in case one was allocated (most common case).
2022 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002023 * errors here are reported as connection errors since it's impossible to
2024 * recover from such errors after the compression context has been altered.
2025 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002026static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002027{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002028 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002029 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002030 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002031 int error;
2032
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002033 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002034 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02002035
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002036 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002037 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02002038
2039 /* now either the frame is complete or the buffer is complete */
2040 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002041 /* The stream exists/existed, this must be a trailers frame */
2042 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02002043 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
2044 /* unrecoverable error ? */
2045 if (h2c->st0 >= H2_CS_ERROR)
2046 goto out;
2047
2048 if (error == 0)
2049 goto out; // missing data
2050
2051 if (error < 0) {
2052 /* Failed to decode this frame (e.g. too large request)
2053 * but the HPACK decompressor is still synchronized.
2054 */
2055 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2056 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002057 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002058 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002059 goto done;
2060 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002061 /* the connection was already killed by an RST, let's consume
2062 * the data and send another RST.
2063 */
2064 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2065 h2s = (struct h2s*)h2_error_stream;
2066 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002067 }
2068 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2069 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2070 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002071 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002072 goto conn_err;
2073 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002074 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2075 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002076
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002077 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002078
Willy Tarreau25919232019-01-03 14:48:18 +01002079 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002080 if (h2c->st0 >= H2_CS_ERROR)
2081 goto out;
2082
Willy Tarreau25919232019-01-03 14:48:18 +01002083 if (error <= 0) {
2084 if (error == 0)
2085 goto out; // missing data
2086
2087 /* Failed to decode this stream (e.g. too large request)
2088 * but the HPACK decompressor is still synchronized.
2089 */
2090 h2s = (struct h2s*)h2_error_stream;
2091 goto send_rst;
2092 }
2093
Willy Tarreau22de8d32018-09-05 19:55:58 +02002094 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002095 * positively from h2c_frt_stream_new(), the stream will report the error,
2096 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002097 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002098 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002099 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002100 h2s = (struct h2s*)h2_refused_stream;
2101 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002102 }
2103
2104 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002105 h2s->rxbuf = rxbuf;
2106 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002107 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002108
Willy Tarreau88d138e2019-01-02 19:38:14 +01002109 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002110 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002111 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002112
2113 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002114 if (h2s->st == H2_SS_OPEN)
2115 h2s->st = H2_SS_HREM;
2116 else
2117 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002118 }
2119
Willy Tarreau3a429f02019-01-03 11:41:50 +01002120 /* update the max stream ID if the request is being processed */
2121 if (h2s->id > h2c->max_id)
2122 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002123
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002124 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002125
2126 conn_err:
2127 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002128 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002129
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002130 out:
2131 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002132 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002133
2134 send_rst:
2135 /* make the demux send an RST for the current stream. We may only
2136 * do this if we're certain that the HEADERS frame was properly
2137 * decompressed so that the HPACK decoder is still kept up to date.
2138 */
2139 h2_release_buf(h2c, &rxbuf);
2140 h2c->st0 = H2_CS_FRAME_E;
2141 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002142}
2143
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002144/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2145 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2146 * errors here are reported as connection errors since it's impossible to
2147 * recover from such errors after the compression context has been altered.
2148 */
2149static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2150{
Christopher Faulet96b88f22019-09-23 15:28:20 +02002151 struct buffer rxbuf = BUF_NULL;
2152 unsigned long long body_len = 0;
2153 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002154 int error;
2155
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002156 if (!b_size(&h2c->dbuf))
2157 return NULL; // empty buffer
2158
2159 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2160 return NULL; // incomplete frame
2161
Christopher Faulet96b88f22019-09-23 15:28:20 +02002162 if (h2s->st != H2_SS_CLOSED) {
2163 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
2164 }
2165 else {
2166 /* the connection was already killed by an RST, let's consume
2167 * the data and send another RST.
2168 */
2169 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Christopher Faulet03427052019-09-26 16:19:13 +02002170 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet96b88f22019-09-23 15:28:20 +02002171 h2c->st0 = H2_CS_FRAME_E;
2172 goto send_rst;
2173 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002174
Willy Tarreau25919232019-01-03 14:48:18 +01002175 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002176 if (h2c->st0 >= H2_CS_ERROR)
2177 return NULL;
2178
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002179 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2180 /* RFC7540#5.1 */
2181 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2182 h2c->st0 = H2_CS_FRAME_E;
2183 return NULL;
2184 }
2185
Willy Tarreau25919232019-01-03 14:48:18 +01002186 if (error <= 0) {
2187 if (error == 0)
2188 return NULL; // missing data
2189
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002190 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002191 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002192 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002193 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002194 }
2195
Christopher Fauletfa922f02019-05-07 10:55:17 +02002196 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002197 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002198
Willy Tarreau927b88b2019-03-04 08:03:25 +01002199 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002200 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002201 else if (h2s->flags & H2_SF_ES_RCVD) {
2202 if (h2s->st == H2_SS_OPEN)
2203 h2s->st = H2_SS_HREM;
2204 else if (h2s->st == H2_SS_HLOC)
2205 h2s_close(h2s);
2206 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002207
2208 return h2s;
Christopher Faulet96b88f22019-09-23 15:28:20 +02002209
2210 send_rst:
2211 /* make the demux send an RST for the current stream. We may only
2212 * do this if we're certain that the HEADERS frame was properly
2213 * decompressed so that the HPACK decoder is still kept up to date.
2214 */
2215 h2_release_buf(h2c, &rxbuf);
2216 h2c->st0 = H2_CS_FRAME_E;
2217 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002218}
2219
Willy Tarreau454f9052017-10-26 19:40:35 +02002220/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2221 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2222 */
2223static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2224{
2225 int error;
2226
2227 /* note that empty DATA frames are perfectly valid and sometimes used
2228 * to signal an end of stream (with the ES flag).
2229 */
2230
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002231 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002232 return 0; // empty buffer
2233
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002234 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002235 return 0; // incomplete frame
2236
2237 /* now either the frame is complete or the buffer is complete */
2238
Willy Tarreau454f9052017-10-26 19:40:35 +02002239 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2240 /* RFC7540#6.1 */
2241 error = H2_ERR_STREAM_CLOSED;
2242 goto strm_err;
2243 }
2244
Christopher Faulet4fb65f42019-06-19 09:25:58 +02002245 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002246 /* RFC7540#8.1.2 */
2247 error = H2_ERR_PROTOCOL_ERROR;
2248 goto strm_err;
2249 }
2250
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002251 if (!h2_frt_transfer_data(h2s))
2252 return 0;
2253
Willy Tarreau454f9052017-10-26 19:40:35 +02002254 /* call the upper layers to process the frame, then let the upper layer
2255 * notify the stream about any change.
2256 */
2257 if (!h2s->cs) {
Willy Tarreauc1a29652019-08-06 10:11:02 +02002258 /* The upper layer has already closed, this may happen on
2259 * 4xx/redirects during POST, or when receiving a response
2260 * from an H2 server after the client has aborted.
2261 */
2262 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002263 goto strm_err;
2264 }
2265
Willy Tarreau8f650c32017-11-21 19:36:21 +01002266 if (h2c->st0 >= H2_CS_ERROR)
2267 return 0;
2268
Willy Tarreau721c9742017-11-07 11:05:42 +01002269 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002270 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002271 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002272 }
2273
2274 /* check for completion : the callee will change this to FRAME_A or
2275 * FRAME_H once done.
2276 */
2277 if (h2c->st0 == H2_CS_FRAME_P)
2278 return 0;
2279
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002280 /* last frame */
2281 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002282 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002283 if (h2s->st == H2_SS_OPEN)
2284 h2s->st = H2_SS_HREM;
2285 else
2286 h2s_close(h2s);
2287
Willy Tarreau1915ca22019-01-24 11:49:37 +01002288 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2289 /* RFC7540#8.1.2 */
2290 error = H2_ERR_PROTOCOL_ERROR;
2291 goto strm_err;
2292 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002293 }
2294
Willy Tarreau454f9052017-10-26 19:40:35 +02002295 return 1;
2296
Willy Tarreau454f9052017-10-26 19:40:35 +02002297 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002298 h2s_error(h2s, error);
2299 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002300 return 0;
2301}
2302
Willy Tarreaubc933932017-10-09 16:21:43 +02002303/* process Rx frames to be demultiplexed */
2304static void h2_process_demux(struct h2c *h2c)
2305{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002306 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002307 struct h2_fh hdr;
2308 unsigned int padlen = 0;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002309 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002310
Willy Tarreau081d4722017-05-16 21:51:05 +02002311 if (h2c->st0 >= H2_CS_ERROR)
2312 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002313
2314 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2315 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002316 if (h2c->flags & H2_CF_IS_BACK)
2317 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002318 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2319 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002320 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002321 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002322 sess_log(h2c->conn->owner);
2323 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002324 goto fail;
2325 }
2326
2327 h2c->max_id = 0;
2328 h2c->st0 = H2_CS_SETTINGS1;
2329 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002330
2331 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002332 /* ensure that what is pending is a valid SETTINGS frame
2333 * without an ACK.
2334 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002335 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002336 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002337 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002338 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002339 if (!(h2c->flags & H2_CF_IS_BACK))
2340 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002341 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002342 goto fail;
2343 }
2344
2345 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2346 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2347 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2348 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002349 if (!(h2c->flags & H2_CF_IS_BACK))
2350 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002351 goto fail;
2352 }
2353
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002354 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002355 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2356 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2357 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002358 if (!(h2c->flags & H2_CF_IS_BACK))
2359 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002360 goto fail;
2361 }
2362
Willy Tarreau3bf69182018-12-21 15:34:50 +01002363 /* that's OK, switch to FRAME_P to process it. This is
2364 * a SETTINGS frame whose header has already been
2365 * deleted above.
2366 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002367 padlen = 0;
2368 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002369 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002370 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002371
2372 /* process as many incoming frames as possible below */
Christopher Faulet8f304a62021-02-12 13:12:06 +01002373 while (1) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002374 int ret = 0;
2375
Willy Tarreaudf203d82021-02-05 12:16:01 +01002376 if (!b_data(&h2c->dbuf))
2377 goto dbuf_empty;
2378
Willy Tarreau7e98c052017-10-10 15:56:59 +02002379 if (h2c->st0 >= H2_CS_ERROR)
2380 break;
2381
2382 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreauab3ebbc2019-08-06 15:49:51 +02002383 h2c->rcvd_s = 0;
2384
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002385 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002386 break;
2387
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002388 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002389 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002390 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02002391 /* only log if no other stream can report the error */
2392 sess_log(h2c->conn->owner);
2393 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002394 break;
2395 }
2396
Christopher Faulet3d574a52019-06-18 12:22:38 +02002397 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002398 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2399 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2400 * we read the pad length and drop it from the remaining
2401 * payload (one byte + the 9 remaining ones = 10 total
2402 * removed), so we have a frame payload starting after the
2403 * pad len. Flow controlled frames (DATA) also count the
2404 * padlen in the flow control, so it must be adjusted.
2405 */
2406 if (hdr.len < 1) {
2407 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002408 if (!(h2c->flags & H2_CF_IS_BACK))
2409 sess_log(h2c->conn->owner);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002410 goto fail;
2411 }
2412 hdr.len--;
2413
2414 if (b_data(&h2c->dbuf) < 10)
2415 break; // missing padlen
2416
2417 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2418
2419 if (padlen > hdr.len) {
2420 /* RFC7540#6.1 : pad length = length of
2421 * frame payload or greater => error.
2422 */
2423 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002424 if (!(h2c->flags & H2_CF_IS_BACK))
2425 sess_log(h2c->conn->owner);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002426 goto fail;
2427 }
2428
2429 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2430 h2c->rcvd_c++;
2431 h2c->rcvd_s++;
2432 }
2433 b_del(&h2c->dbuf, 1);
2434 }
2435 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002436
2437 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002438 h2c->dfl = hdr.len;
2439 h2c->dsi = hdr.sid;
2440 h2c->dft = hdr.ft;
2441 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002442 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002443 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002444
2445 /* check for minimum basic frame format validity */
2446 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2447 if (ret != H2_ERR_NO_ERROR) {
2448 h2c_error(h2c, ret);
Willy Tarreau21178a52019-10-23 11:06:35 +02002449 if (!(h2c->flags & H2_CF_IS_BACK))
2450 sess_log(h2c->conn->owner);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002451 goto fail;
2452 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002453 }
2454
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002455 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
2456 * H2_CS_FRAME_P indicates an incomplete previous operation
2457 * (most often the first attempt) and requires some validity
2458 * checks for the frame and the current state. The two other
2459 * ones are set after completion (or abortion) and must skip
2460 * validity checks.
2461 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002462 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2463
Willy Tarreau567beb82018-12-18 16:52:44 +01002464 if (tmp_h2s != h2s && h2s && h2s->cs &&
2465 (b_data(&h2s->rxbuf) ||
Christopher Faulet11b10532020-10-08 15:38:41 +02002466 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002467 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002468 (h2s->flags & H2_SF_ES_RCVD) ||
2469 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002470 /* we may have to signal the upper layers */
2471 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002472 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002473 }
2474 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002475
Willy Tarreaud7901432017-12-29 11:34:40 +01002476 if (h2c->st0 == H2_CS_FRAME_E)
2477 goto strm_err;
2478
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002479 if (h2c->st0 == H2_CS_FRAME_A)
2480 goto valid_frame_type;
2481
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002482 if (h2s->st == H2_SS_IDLE &&
2483 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2484 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2485 * this state MUST be treated as a connection error
2486 */
2487 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002488 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02002489 /* only log if no other stream can report the error */
2490 sess_log(h2c->conn->owner);
2491 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002492 break;
2493 }
2494
Willy Tarreau85ee6e82019-11-24 14:57:53 +01002495 if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) {
2496 /* only PUSH_PROMISE would be permitted here */
2497 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2498 break;
2499 }
2500
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002501 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2502 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2503 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002504 * this state MUST be treated as a stream error.
2505 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2506 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002507 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002508 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2509 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2510 else
2511 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002512 goto strm_err;
2513 }
2514
Willy Tarreauab837502017-12-27 15:07:30 +01002515 /* Below the management of frames received in closed state is a
2516 * bit hackish because the spec makes strong differences between
2517 * streams closed by receiving RST, sending RST, and seeing ES
2518 * in both directions. In addition to this, the creation of a
2519 * new stream reusing the identifier of a closed one will be
2520 * detected here. Given that we cannot keep track of all closed
2521 * streams forever, we consider that unknown closed streams were
2522 * closed on RST received, which allows us to respond with an
2523 * RST without breaking the connection (eg: to abort a transfer).
2524 * Some frames have to be silently ignored as well.
2525 */
2526 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002527 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002528 /* #5.1.1: The identifier of a newly
2529 * established stream MUST be numerically
2530 * greater than all streams that the initiating
2531 * endpoint has opened or reserved. This
2532 * governs streams that are opened using a
2533 * HEADERS frame and streams that are reserved
2534 * using PUSH_PROMISE. An endpoint that
2535 * receives an unexpected stream identifier
2536 * MUST respond with a connection error.
2537 */
2538 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2539 goto strm_err;
2540 }
2541
Willy Tarreaueb5be9d2019-09-26 08:47:15 +02002542 if (h2s->flags & H2_SF_RST_RCVD &&
2543 !(h2_ft_bit(h2c->dft) & (H2_FT_HDR_MASK | H2_FT_RST_STREAM_BIT | H2_FT_PRIORITY_BIT | H2_FT_WINDOW_UPDATE_BIT))) {
Willy Tarreauab837502017-12-27 15:07:30 +01002544 /* RFC7540#5.1:closed: an endpoint that
2545 * receives any frame other than PRIORITY after
2546 * receiving a RST_STREAM MUST treat that as a
2547 * stream error of type STREAM_CLOSED.
2548 *
2549 * Note that old streams fall into this category
2550 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002551 *
2552 * However, we cannot generalize this to all frame types. Those
2553 * carrying compression state must still be processed before
2554 * being dropped or we'll desynchronize the decoder. This can
2555 * happen with request trailers received after sending an
2556 * RST_STREAM, or with header/trailers responses received after
2557 * sending RST_STREAM (aborted stream).
Willy Tarreaueb5be9d2019-09-26 08:47:15 +02002558 *
2559 * In addition, since our CLOSED streams always carry the
2560 * RST_RCVD bit, we don't want to accidently catch valid
2561 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreauab837502017-12-27 15:07:30 +01002562 */
2563 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2564 h2c->st0 = H2_CS_FRAME_E;
2565 goto strm_err;
2566 }
2567
2568 /* RFC7540#5.1:closed: if this state is reached as a
2569 * result of sending a RST_STREAM frame, the peer that
2570 * receives the RST_STREAM might have already sent
2571 * frames on the stream that cannot be withdrawn. An
2572 * endpoint MUST ignore frames that it receives on
2573 * closed streams after it has sent a RST_STREAM
2574 * frame. An endpoint MAY choose to limit the period
2575 * over which it ignores frames and treat frames that
2576 * arrive after this time as being in error.
2577 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002578 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002579 /* RFC7540#5.1:closed: any frame other than
2580 * PRIO/WU/RST in this state MUST be treated as
2581 * a connection error
2582 */
2583 if (h2c->dft != H2_FT_RST_STREAM &&
2584 h2c->dft != H2_FT_PRIORITY &&
2585 h2c->dft != H2_FT_WINDOW_UPDATE) {
2586 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2587 goto strm_err;
2588 }
2589 }
2590 }
2591
Willy Tarreauc0da1962017-10-30 18:38:00 +01002592#if 0
2593 // problem below: it is not possible to completely ignore such
2594 // streams as we need to maintain the compression state as well
2595 // and for this we need to completely process these frames (eg:
2596 // HEADERS frames) as well as counting DATA frames to emit
2597 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2598 // This is a typical case of layer violation where the
2599 // transported contents are critical to the connection's
2600 // validity and must be ignored at the same time :-(
2601
2602 /* graceful shutdown, ignore streams whose ID is higher than
2603 * the one advertised in GOAWAY. RFC7540#6.8.
2604 */
2605 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002606 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2607 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002608 h2c->dfl -= ret;
2609 ret = h2c->dfl == 0;
2610 goto strm_err;
2611 }
2612#endif
2613
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002614 valid_frame_type:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002615 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002616 case H2_FT_SETTINGS:
2617 if (h2c->st0 == H2_CS_FRAME_P)
2618 ret = h2c_handle_settings(h2c);
2619
2620 if (h2c->st0 == H2_CS_FRAME_A)
2621 ret = h2c_ack_settings(h2c);
2622 break;
2623
Willy Tarreaucf68c782017-10-10 17:11:41 +02002624 case H2_FT_PING:
2625 if (h2c->st0 == H2_CS_FRAME_P)
2626 ret = h2c_handle_ping(h2c);
2627
2628 if (h2c->st0 == H2_CS_FRAME_A)
2629 ret = h2c_ack_ping(h2c);
2630 break;
2631
Willy Tarreau26f95952017-07-27 17:18:30 +02002632 case H2_FT_WINDOW_UPDATE:
2633 if (h2c->st0 == H2_CS_FRAME_P)
2634 ret = h2c_handle_window_update(h2c, h2s);
2635 break;
2636
Willy Tarreau61290ec2017-10-17 08:19:21 +02002637 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002638 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2639 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2640 * frames' parsers consume all following CONTINUATION
2641 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002642 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002643 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002644 if (!(h2c->flags & H2_CF_IS_BACK))
2645 sess_log(h2c->conn->owner);
Willy Tarreauea18f862018-12-22 20:19:26 +01002646 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002647
Willy Tarreau13278b42017-10-13 19:23:14 +02002648 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002649 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002650 if (h2c->flags & H2_CF_IS_BACK)
2651 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2652 else
2653 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002654 if (tmp_h2s) {
2655 h2s = tmp_h2s;
2656 ret = 1;
2657 }
2658 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002659 break;
2660
Willy Tarreau454f9052017-10-26 19:40:35 +02002661 case H2_FT_DATA:
2662 if (h2c->st0 == H2_CS_FRAME_P)
2663 ret = h2c_frt_handle_data(h2c, h2s);
2664
2665 if (h2c->st0 == H2_CS_FRAME_A)
2666 ret = h2c_send_strm_wu(h2c);
2667 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002668
Willy Tarreau92153fc2017-12-03 19:46:19 +01002669 case H2_FT_PRIORITY:
2670 if (h2c->st0 == H2_CS_FRAME_P)
2671 ret = h2c_handle_priority(h2c);
2672 break;
2673
Willy Tarreaucd234e92017-08-18 10:59:39 +02002674 case H2_FT_RST_STREAM:
2675 if (h2c->st0 == H2_CS_FRAME_P)
2676 ret = h2c_handle_rst_stream(h2c, h2s);
2677 break;
2678
Willy Tarreaue96b0922017-10-30 00:28:29 +01002679 case H2_FT_GOAWAY:
2680 if (h2c->st0 == H2_CS_FRAME_P)
2681 ret = h2c_handle_goaway(h2c);
2682 break;
2683
Willy Tarreau1c661982017-10-30 13:52:01 +01002684 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002685 default:
2686 /* drop frames that we ignore. They may be larger than
2687 * the buffer so we drain all of their contents until
2688 * we reach the end.
2689 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002690 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2691 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002692 h2c->dfl -= ret;
2693 ret = h2c->dfl == 0;
2694 }
2695
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002696 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002697 /* We may have to send an RST if not done yet */
2698 if (h2s->st == H2_SS_ERROR)
2699 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002700
Willy Tarreaua20a5192017-12-27 11:02:06 +01002701 if (h2c->st0 == H2_CS_FRAME_E)
2702 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002703
Willy Tarreaudf203d82021-02-05 12:16:01 +01002704 dbuf_empty:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002705 /* error or missing data condition met above ? */
Willy Tarreau3b94d982021-01-20 10:53:13 +01002706 if (ret <= 0) {
2707 if (h2c->flags & H2_CF_RCVD_SHUT)
2708 h2c->flags |= H2_CF_END_REACHED;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002709 break;
Willy Tarreau3b94d982021-01-20 10:53:13 +01002710 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002711
2712 if (h2c->st0 != H2_CS_FRAME_H) {
Christopher Faulete12a26f2019-09-26 16:38:28 +02002713 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2714 b_del(&h2c->dbuf, ret);
2715 h2c->dfl -= ret;
2716 if (!h2c->dfl)
2717 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002718 }
2719 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002720
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002721 if (h2c->rcvd_c > 0 &&
2722 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2723 h2c_send_conn_wu(h2c);
2724
Willy Tarreau3b94d982021-01-20 10:53:13 +01002725 done:
Willy Tarreau567beb82018-12-18 16:52:44 +01002726 if (h2s && h2s->cs &&
2727 (b_data(&h2s->rxbuf) ||
Christopher Faulet11b10532020-10-08 15:38:41 +02002728 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002729 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002730 (h2s->flags & H2_SF_ES_RCVD) ||
2731 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002732 /* we may have to signal the upper layers */
2733 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002734 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002735 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002736
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002737 if (old_iw != h2c->miw)
2738 h2c_unblock_sfctl(h2c);
2739
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002740 h2c_restart_reading(h2c, 0);
Willy Tarreau3b94d982021-01-20 10:53:13 +01002741 return;
2742
2743 fail:
2744 /* we can go here on missing data, blocked response or error, but we
2745 * need to check if we've met a short read condition.
2746 */
2747 if (h2c->flags & H2_CF_RCVD_SHUT)
2748 h2c->flags |= H2_CF_END_REACHED;
2749 goto done;
Willy Tarreaubc933932017-10-09 16:21:43 +02002750}
2751
Willy Tarreaufa9a0402020-01-10 17:01:29 +01002752/* resume each h2s eligible for sending in list head <head> */
2753static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head)
2754{
2755 struct h2s *h2s, *h2s_back;
2756
2757 list_for_each_entry_safe(h2s, h2s_back, head, list) {
2758 if (h2c->mws <= 0 ||
2759 h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2760 h2c->st0 >= H2_CS_ERROR)
2761 break;
2762
2763 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreaud4e72e22020-01-10 18:20:15 +01002764
2765 if (LIST_ADDED(&h2s->sending_list))
2766 continue;
2767
Willy Tarreaufa9a0402020-01-10 17:01:29 +01002768 /* For some reason, the upper layer failed to subscribe again,
2769 * so remove it from the send_list
2770 */
2771 if (!h2s->send_wait) {
2772 LIST_DEL_INIT(&h2s->list);
2773 continue;
2774 }
2775
2776 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2777 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
2778 tasklet_wakeup(h2s->send_wait->tasklet);
2779 }
2780}
2781
Willy Tarreaubc933932017-10-09 16:21:43 +02002782/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2783 * the end.
2784 */
2785static int h2_process_mux(struct h2c *h2c)
2786{
Willy Tarreau01b44822018-10-03 14:26:37 +02002787 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2788 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2789 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2790 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau21178a52019-10-23 11:06:35 +02002791 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02002792 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02002793 goto fail;
2794 }
2795 h2c->st0 = H2_CS_SETTINGS1;
2796 }
2797 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002798 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002799 return 1;
2800 }
2801
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002802 /* start by sending possibly pending window updates */
Willy Tarreau9c497c22019-08-06 15:39:32 +02002803 if (h2c->rcvd_s > 0 &&
2804 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2805 h2c_send_strm_wu(h2c) < 0)
2806 goto fail;
2807
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002808 if (h2c->rcvd_c > 0 &&
2809 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2810 h2c_send_conn_wu(h2c) < 0)
2811 goto fail;
2812
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002813 /* First we always process the flow control list because the streams
2814 * waiting there were already elected for immediate emission but were
2815 * blocked just on this.
2816 */
Willy Tarreaufa9a0402020-01-10 17:01:29 +01002817 h2_resume_each_sending_h2s(h2c, &h2c->fctl_list);
2818 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002819
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002820 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002821 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002822 if (h2c->st0 == H2_CS_ERROR) {
2823 if (h2c->max_id >= 0) {
2824 h2c_send_goaway_error(h2c, NULL);
2825 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2826 return 0;
2827 }
2828
2829 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2830 }
2831 return 1;
2832 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002833 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002834}
2835
Willy Tarreau62f52692017-10-08 23:01:42 +02002836
Willy Tarreau479998a2018-11-18 06:30:59 +01002837/* Attempt to read data, and subscribe if none available.
2838 * The function returns 1 if data has been received, otherwise zero.
2839 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002840static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002841{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002842 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002843 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002844 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002845 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002846
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002847 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002848 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002849
Willy Tarreau315d8072017-12-10 22:17:57 +01002850 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002851 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002852
Willy Tarreau44e973f2018-03-01 17:49:30 +01002853 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002854 if (!buf) {
2855 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002856 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002857 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002858
Willy Tarreau3b94d982021-01-20 10:53:13 +01002859 if (h2c->flags & H2_CF_RCVD_SHUT)
2860 return 0;
2861
Olivier Houchard7505f942018-08-21 18:10:44 +02002862 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002863 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002864 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2865 /* HTX in use : try to pre-align the buffer like the
2866 * rxbufs will be to optimize memory copies. We'll make
2867 * sure that the frame header lands at the end of the
2868 * HTX block to alias it upon recv. We cannot use the
2869 * head because rcv_buf() will realign the buffer if
2870 * it's empty. Thus we cheat and pretend we already
2871 * have a few bytes there.
2872 */
2873 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002874 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002875 }
2876 else
2877 max = b_room(buf);
2878
Olivier Houchard7505f942018-08-21 18:10:44 +02002879 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002880 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002881 else
2882 ret = 0;
2883 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002884
Willy Tarreaue70050e2021-02-05 11:41:46 +01002885 if (max && !ret) {
Willy Tarreau3b94d982021-01-20 10:53:13 +01002886 if (conn_xprt_read0_pending(h2c->conn))
2887 h2c->flags |= H2_CF_RCVD_SHUT;
Willy Tarreaue70050e2021-02-05 11:41:46 +01002888 else if (h2_recv_allowed(h2c))
Willy Tarreau3b94d982021-01-20 10:53:13 +01002889 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
2890 }
Olivier Houchard81a15af2018-10-19 17:26:49 +02002891
Olivier Houcharda1411e62018-08-17 18:42:48 +02002892 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002893 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002894 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002895 }
2896
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002897 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002898 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002899 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002900}
2901
Willy Tarreau479998a2018-11-18 06:30:59 +01002902/* Try to send data if possible.
2903 * The function returns 1 if data have been sent, otherwise zero.
2904 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002905static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002906{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002907 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002908 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002909 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002910
2911 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002912 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002913
Olivier Houchard7505f942018-08-21 18:10:44 +02002914
Willy Tarreaua2af5122017-10-09 11:56:46 +02002915 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2916 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002917 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002918 }
2919
Willy Tarreaubc933932017-10-09 16:21:43 +02002920 /* This loop is quite simple : it tries to fill as much as it can from
2921 * pending streams into the existing buffer until it's reportedly full
2922 * or the end of send requests is reached. Then it tries to send this
2923 * buffer's contents out, marks it not full if at least one byte could
2924 * be sent, and tries again.
2925 *
2926 * The snd_buf() function normally takes a "flags" argument which may
2927 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2928 * data immediately comes and CO_SFL_STREAMER to indicate that the
2929 * connection is streaming lots of data (used to increase TLS record
2930 * size at the expense of latency). The former can be sent any time
2931 * there's a buffer full flag, as it indicates at least one stream
2932 * attempted to send and failed so there are pending data. An
2933 * alternative would be to set it as long as there's an active stream
2934 * but that would be problematic for ACKs until we have an absolute
2935 * guarantee that all waiters have at least one byte to send. The
2936 * latter should possibly not be set for now.
2937 */
2938
2939 done = 0;
2940 while (!done) {
2941 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002942 unsigned int released = 0;
2943 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02002944
2945 /* fill as much as we can into the current buffer */
2946 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2947 done = h2_process_mux(h2c);
2948
Olivier Houchard2b094432019-01-29 18:28:36 +01002949 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002950 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01002951
Christopher Faulet71633512020-10-22 16:24:58 +02002952 if ((conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)) ||
2953 (h2c->st0 == H2_CS_ERROR2) || (h2c->flags & H2_CF_GOAWAY_FAILED))
Willy Tarreaubc933932017-10-09 16:21:43 +02002954 break;
2955
2956 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2957 flags |= CO_SFL_MSG_MORE;
2958
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002959 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2960 if (b_data(buf)) {
2961 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002962 if (!ret) {
2963 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002964 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002965 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002966 sent = 1;
2967 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002968 if (b_data(buf)) {
2969 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002970 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002971 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002972 }
2973 b_free(buf);
2974 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002975 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002976
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002977 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002978 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002979
Willy Tarreaubc933932017-10-09 16:21:43 +02002980 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet07423082019-10-24 10:31:01 +02002981 if (sent)
2982 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02002983 }
2984
Willy Tarreaua2af5122017-10-09 11:56:46 +02002985 if (conn->flags & CO_FL_SOCK_WR_SH) {
2986 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002987 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002988 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002989 /* We're not full anymore, so we can wake any task that are waiting
2990 * for us.
2991 */
Willy Tarreaufa9a0402020-01-10 17:01:29 +01002992 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H)
2993 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Olivier Houchard998410a2019-04-15 19:23:37 +02002994
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002995 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002996 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002997 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002998schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002999 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003000 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003001
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003002 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003003}
3004
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003005/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003006static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
3007{
3008 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02003009 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003010
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003011 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02003012 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003013 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02003014 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01003015 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02003016 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003017 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003018}
Willy Tarreaua2af5122017-10-09 11:56:46 +02003019
Willy Tarreau62f52692017-10-08 23:01:42 +02003020/* callback called on any event by the connection handler.
3021 * It applies changes and returns zero, or < 0 if it wants immediate
3022 * destruction of the connection (which normally doesn not happen in h2).
3023 */
Olivier Houchard7505f942018-08-21 18:10:44 +02003024static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003025{
Olivier Houchard7505f942018-08-21 18:10:44 +02003026 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003027
Willy Tarreaue70050e2021-02-05 11:41:46 +01003028 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY) &&
3029 (b_data(&h2c->dbuf) || (h2c->flags & H2_CF_RCVD_SHUT))) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01003030 h2_process_demux(h2c);
3031
3032 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003033 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003034
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003035 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01003036 h2c->flags &= ~H2_CF_DEM_DFULL;
3037 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003038 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003039
Willy Tarreau318822b2020-10-13 18:09:15 +02003040 if (unlikely(h2c->proxy->state == PR_STSTOPPED) && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01003041 /* frontend is stopping, reload likely in progress, let's try
3042 * to announce a graceful shutdown if not yet done. We don't
3043 * care if it fails, it will be tried again later.
3044 */
3045 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3046 if (h2c->last_sid < 0)
3047 h2c->last_sid = (1U << 31) - 1;
3048 h2c_send_goaway_error(h2c, NULL);
3049 }
3050 }
3051
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003052 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003053 * If we received early data, and the handshake is done, wake
3054 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003055 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003056 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
3057 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
3058 struct eb32_node *node;
3059 struct h2s *h2s;
3060
3061 h2c->flags |= H2_CF_WAIT_FOR_HS;
3062 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3063
3064 while (node) {
3065 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003066 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003067 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003068 node = eb32_next(node);
3069 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003070 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003071
Christopher Faulet11b10532020-10-08 15:38:41 +02003072 if (conn->flags & CO_FL_ERROR || h2c_read0_pending(h2c) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003073 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3074 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3075 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003076 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003077
3078 if (eb_is_empty(&h2c->streams_by_id)) {
3079 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003080 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003081 return -1;
3082 }
Willy Tarreauab66e152019-10-31 15:36:30 +01003083
3084 /* connections in error must be removed from the idle lists */
3085 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3086 LIST_DEL_LOCKED(&conn->list);
3087 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3088 }
3089 else if (h2c->st0 == H2_CS_ERROR) {
3090 /* connections in error must be removed from the idle lists */
3091 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3092 LIST_DEL_LOCKED(&conn->list);
3093 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003094 }
3095
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003096 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003097 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003098
Olivier Houchard53216e72018-10-10 15:46:36 +02003099 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3100 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3101 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003102 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003103 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3104 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003105 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003106
Willy Tarreau3f133572017-10-31 19:21:06 +01003107 if (h2c->task) {
Willy Tarreau534d8f92019-10-01 10:12:00 +02003108 if (h2c_may_expire(h2c))
3109 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3110 else
3111 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003112 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003113 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003114
Olivier Houchard7505f942018-08-21 18:10:44 +02003115 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02003116 return 0;
3117}
3118
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003119/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003120static int h2_wake(struct connection *conn)
3121{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003122 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003123
3124 return (h2_process(h2c));
3125}
3126
Willy Tarreauea392822017-10-31 10:02:25 +01003127/* Connection timeout management. The principle is that if there's no receipt
3128 * nor sending for a certain amount of time, the connection is closed. If the
3129 * MUX buffer still has lying data or is not allocatable, the connection is
3130 * immediately killed. If it's allocatable and empty, we attempt to send a
3131 * GOAWAY frame.
3132 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003133static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003134{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003135 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003136 int expired = tick_is_expired(t->expire, now_ms);
3137
Willy Tarreau0975f112018-03-29 15:22:59 +02003138 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003139 return t;
3140
Willy Tarreau534d8f92019-10-01 10:12:00 +02003141 if (h2c && !h2c_may_expire(h2c)) {
3142 /* we do still have streams but all of them are idle, waiting
3143 * for the data layer, so we must not enforce the timeout here.
3144 */
3145 t->expire = TICK_ETERNITY;
3146 return t;
3147 }
3148
Olivier Houchard3f795f72019-04-17 22:51:06 +02003149 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003150
3151 if (!h2c) {
3152 /* resources were already deleted */
3153 return NULL;
3154 }
3155
3156 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003157 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003158 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003159
Willy Tarreau662fafc2019-05-26 09:43:07 +02003160 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003161 /* don't even try to send a GOAWAY, the buffer is stuck */
3162 h2c->flags |= H2_CF_GOAWAY_FAILED;
3163 }
3164
3165 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003166 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003167 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3168 h2c->flags |= H2_CF_GOAWAY_FAILED;
3169
Willy Tarreau662fafc2019-05-26 09:43:07 +02003170 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003171 unsigned int released = 0;
3172 struct buffer *buf;
3173
3174 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3175 if (b_data(buf)) {
3176 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3177 if (!ret)
3178 break;
3179 b_del(buf, ret);
3180 if (b_data(buf))
3181 break;
3182 b_free(buf);
3183 released++;
3184 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003185 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003186
3187 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003188 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003189 }
Willy Tarreauea392822017-10-31 10:02:25 +01003190
Willy Tarreauab66e152019-10-31 15:36:30 +01003191 /* in any case this connection must not be considered idle anymore */
3192 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3193 LIST_DEL_LOCKED(&h2c->conn->list);
3194 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3195
Willy Tarreau0975f112018-03-29 15:22:59 +02003196 /* either we can release everything now or it will be done later once
3197 * the last stream closes.
3198 */
3199 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003200 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003201
Willy Tarreauea392822017-10-31 10:02:25 +01003202 return NULL;
3203}
3204
3205
Willy Tarreau62f52692017-10-08 23:01:42 +02003206/*******************************************/
3207/* functions below are used by the streams */
3208/*******************************************/
3209
3210/*
3211 * Attach a new stream to a connection
3212 * (Used for outgoing connections)
3213 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003214static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003215{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003216 struct conn_stream *cs;
3217 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003218 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003219
3220 cs = cs_new(conn);
3221 if (!cs)
3222 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003223 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003224 if (!h2s) {
3225 cs_free(cs);
3226 return NULL;
3227 }
3228 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003229}
3230
Willy Tarreaufafd3982018-11-18 21:29:20 +01003231/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3232 * We have to scan because we may have some orphan streams. It might be
3233 * beneficial to scan backwards from the end to reduce the likeliness to find
3234 * orphans.
3235 */
3236static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3237{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003238 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003239 struct h2s *h2s;
3240 struct eb32_node *node;
3241
3242 node = eb32_first(&h2c->streams_by_id);
3243 while (node) {
3244 h2s = container_of(node, struct h2s, by_id);
3245 if (h2s->cs)
3246 return h2s->cs;
3247 node = eb32_next(node);
3248 }
3249 return NULL;
3250}
3251
Olivier Houchard198d1292019-10-25 16:19:26 +02003252static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3253{
3254 int ret = 0;
3255 struct h2c *h2c = conn->ctx;
3256
3257 switch (mux_ctl) {
3258 case MUX_STATUS:
3259 /* Only consider the mux to be ready if we're done with
3260 * the preface and settings, and we had no error.
3261 */
3262 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
3263 ret |= MUX_STATUS_READY;
3264 return ret;
3265 default:
3266 return -1;
3267 }
3268}
3269
Willy Tarreau62f52692017-10-08 23:01:42 +02003270/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003271 * Destroy the mux and the associated connection, if it is no longer used
3272 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003273static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003274{
Christopher Faulet73c12072019-04-08 11:23:22 +02003275 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003276
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003277 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003278 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003279}
3280
3281/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003282 * Detach the stream from the connection and possibly release the connection.
3283 */
3284static void h2_detach(struct conn_stream *cs)
3285{
Willy Tarreau60935142017-10-16 18:11:19 +02003286 struct h2s *h2s = cs->ctx;
3287 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003288 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003289
3290 cs->ctx = NULL;
3291 if (!h2s)
3292 return;
3293
Olivier Houchard998410a2019-04-15 19:23:37 +02003294 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003295 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003296 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003297 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003298 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003299 /*
3300 * At this point, the stream_interface is supposed to have called
3301 * h2_unsubscribe(), so the only way there's still a
3302 * subscription that came from the stream_interface (as we
3303 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3304 * without the stream_interface involved) is that we subscribed
3305 * for sending, we woke the tasklet up and removed the
3306 * SUB_RETRY_SEND flag, so the stream_interface would not
3307 * know it has to unsubscribe for send, but the tasklet hasn't
3308 * run yet. Make sure to handle that by explicitely setting
3309 * send_wait to NULL, as nothing else will do it for us.
3310 */
3311 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003312 }
3313
Olivier Houchardf502aca2018-12-14 19:42:40 +01003314 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003315 h2c = h2s->h2c;
3316 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003317 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003318 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3319 !h2_frt_has_too_many_cs(h2c)) {
3320 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003321 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003322 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003323 }
Willy Tarreau60935142017-10-16 18:11:19 +02003324
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003325 /* this stream may be blocked waiting for some data to leave (possibly
3326 * an ES or RST frame), so orphan it in this case.
3327 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003328 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003329 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003330 (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 +01003331 return;
3332
Willy Tarreau45f752e2017-10-30 15:44:59 +01003333 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3334 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3335 /* unblock the connection if it was blocked on this
3336 * stream.
3337 */
3338 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3339 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003340 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003341 }
3342
Willy Tarreau71049cc2018-03-28 13:56:39 +02003343 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003344
Olivier Houchard8a786902018-12-15 16:05:40 +01003345 if (h2c->flags & H2_CF_IS_BACK &&
3346 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003347 if (!(h2c->conn->flags &
3348 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3349 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003350 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003351 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3352 h2c->conn->owner = NULL;
3353 if (eb_is_empty(&h2c->streams_by_id)) {
3354 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3355 /* The server doesn't want it, let's kill the connection right away */
Olivier Houchard109ba132020-02-14 13:23:45 +01003356 h2c->conn->mux->destroy(h2c);
Olivier Houchard351411f2018-12-27 17:20:54 +01003357 return;
3358 }
3359 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003360 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003361 if (eb_is_empty(&h2c->streams_by_id)) {
3362 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3363 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3364 return;
3365 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003366 /* Never ever allow to reuse a connection from a non-reuse backend */
3367 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3368 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003369 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003370 struct server *srv = objt_server(h2c->conn->target);
3371
3372 if (srv) {
3373 if (h2c->conn->flags & CO_FL_PRIVATE)
3374 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3375 else
3376 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3377 }
3378
3379 }
3380 }
3381 }
3382
Willy Tarreaue323f342018-03-28 13:51:45 +02003383 /* We don't want to close right now unless we're removing the
3384 * last stream, and either the connection is in error, or it
3385 * reached the ID already specified in a GOAWAY frame received
3386 * or sent (as seen by last_sid >= 0).
3387 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003388 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003389 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003390 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003391 }
3392 else if (h2c->task) {
Willy Tarreau534d8f92019-10-01 10:12:00 +02003393 if (h2c_may_expire(h2c))
3394 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3395 else
3396 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003397 task_queue(h2c->task);
Willy Tarreau60935142017-10-16 18:11:19 +02003398 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003399}
3400
Willy Tarreau88bdba32019-05-13 18:17:53 +02003401/* Performs a synchronous or asynchronous shutr(). */
3402static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003403{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003404 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003405 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003406
Willy Tarreauf983d002019-05-14 10:40:21 +02003407 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003408 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003409
Willy Tarreau18059042019-01-31 19:12:48 +01003410 /* a connstream may require us to immediately kill the whole connection
3411 * for example because of a "tcp-request content reject" rule that is
3412 * normally used to limit abuse. In this case we schedule a goaway to
3413 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003414 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003415 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003416 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3417 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3418 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3419 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003420 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3421 /* Nothing was never sent for this stream, so reset with
3422 * REFUSED_STREAM error to let the client retry the
3423 * request.
3424 */
3425 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3426 }
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003427 else {
3428 /* a final response was already provided, we don't want this
3429 * stream anymore. This may happen when the server responds
3430 * before the end of an upload and closes quickly (redirect,
3431 * deny, ...)
3432 */
3433 h2s_error(h2s, H2_ERR_CANCEL);
3434 }
Willy Tarreau18059042019-01-31 19:12:48 +01003435
Willy Tarreau90c32322017-11-24 08:00:30 +01003436 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003437 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003438 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003439
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003440 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003441 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003442 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003443 done:
3444 h2s->flags &= ~H2_SF_WANT_SHUTR;
3445 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003446add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003447 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003448 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003449 if (h2s->flags & H2_SF_BLK_MFCTL) {
3450 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3451 h2s->send_wait = sw;
3452 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3453 h2s->send_wait = sw;
3454 LIST_ADDQ(&h2c->send_list, &h2s->list);
3455 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003456 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003457 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003458 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003459 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003460}
3461
Willy Tarreau88bdba32019-05-13 18:17:53 +02003462/* Performs a synchronous or asynchronous shutw(). */
3463static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003464{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003465 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003466 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003467
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003468 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003469 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003470
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003471 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003472 /* we can cleanly close using an empty data frame only after headers */
3473
3474 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3475 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003476 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003477
3478 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003479 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003480 else
3481 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003482 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003483 /* a connstream may require us to immediately kill the whole connection
3484 * for example because of a "tcp-request content reject" rule that is
3485 * normally used to limit abuse. In this case we schedule a goaway to
3486 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003487 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003488 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003489 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3490 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3491 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3492 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003493 else {
3494 /* Nothing was never sent for this stream, so reset with
3495 * REFUSED_STREAM error to let the client retry the
3496 * request.
3497 */
3498 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3499 }
Willy Tarreau18059042019-01-31 19:12:48 +01003500
Willy Tarreau90c32322017-11-24 08:00:30 +01003501 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003502 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003503 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003504
Willy Tarreau00dd0782018-03-01 16:31:34 +01003505 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003506 }
3507
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003508 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003509 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003510 done:
3511 h2s->flags &= ~H2_SF_WANT_SHUTW;
3512 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003513
3514 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003515 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003516 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003517 if (h2s->flags & H2_SF_BLK_MFCTL) {
3518 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3519 h2s->send_wait = sw;
3520 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3521 h2s->send_wait = sw;
3522 LIST_ADDQ(&h2c->send_list, &h2s->list);
3523 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003524 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003525 /* let the handler know we want to shutw */
3526 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003527 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003528}
3529
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003530/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003531 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3532 * and prevented the last frame from being emitted.
3533 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003534static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3535{
3536 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003537 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003538
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003539 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003540 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003541 h2_do_shutw(h2s);
3542
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003543 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003544 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003545
Willy Tarreau88bdba32019-05-13 18:17:53 +02003546 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003547 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003548 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003549
Willy Tarreau88bdba32019-05-13 18:17:53 +02003550 if (!h2s->cs) {
3551 h2s_destroy(h2s);
3552 if (h2c_is_dead(h2c))
3553 h2_release(h2c);
3554 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003555 }
3556
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003557 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003558}
3559
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003560/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003561static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3562{
3563 struct h2s *h2s = cs->ctx;
3564
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003565 if (cs->flags & CS_FL_KILL_CONN)
3566 h2s->flags |= H2_SF_KILL_CONN;
3567
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003568 if (!mode)
3569 return;
3570
3571 h2_do_shutr(h2s);
3572}
3573
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003574/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003575static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3576{
3577 struct h2s *h2s = cs->ctx;
3578
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003579 if (cs->flags & CS_FL_KILL_CONN)
3580 h2s->flags |= H2_SF_KILL_CONN;
3581
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003582 h2_do_shutw(h2s);
3583}
3584
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003585/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003586 * HTX request or response depending on the connection's side. Returns a
3587 * positive value on success, a negative value on failure, or 0 if it couldn't
3588 * proceed. May report connection errors in h2c->errcode if the frame is
3589 * non-decodable and the connection unrecoverable. In absence of connection
3590 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003591 *
3592 * The function may fold CONTINUATION frames into the initial HEADERS frame
3593 * by removing padding and next frame header, then moving the CONTINUATION
3594 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3595 * leaving a hole between the main frame and the beginning of the next one.
3596 * The possibly remaining incomplete or next frame at the end may be moved
3597 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3598 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3599 *
3600 * A buffer at the beginning of processing may look like this :
3601 *
3602 * ,---.---------.-----.--------------.--------------.------.---.
3603 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3604 * `---^---------^-----^--------------^--------------^------^---'
3605 * | | <-----> | |
3606 * area | dpl | wrap
3607 * |<--------------> |
3608 * | dfl |
3609 * |<-------------------------------------------------->|
3610 * head data
3611 *
3612 * Padding is automatically overwritten when folding, participating to the
3613 * hole size after dfl :
3614 *
3615 * ,---.------------------------.-----.--------------.------.---.
3616 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3617 * `---^------------------------^-----^--------------^------^---'
3618 * | | <-----> | |
3619 * area | hole | wrap
3620 * |<-----------------------> |
3621 * | dfl |
3622 * |<-------------------------------------------------->|
3623 * head data
3624 *
3625 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3626 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3627 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003628 *
3629 * The <flags> field must point to either the stream's flags or to a copy of it
3630 * so that the function can update the following flags :
3631 * - H2_SF_DATA_CLEN when content-length is seen
3632 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3633 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003634 *
3635 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3636 * decoding, in order to detect if we're dealing with a headers or a trailers
3637 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003638 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003639static 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 +02003640{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003641 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003642 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003643 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003644 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003645 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003646 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003647 int flen; // header frame len
3648 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003649 int ret = 0;
3650 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003651 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003652 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003653
Willy Tarreauea18f862018-12-22 20:19:26 +01003654next_frame:
3655 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3656 goto leave; // incomplete input frame
3657
3658 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3659 * this case, we'll try to paste it immediately after the initial
3660 * HEADERS frame payload and kill any possible padding. The initial
3661 * frame's length will be increased to represent the concatenation
3662 * of the two frames. The next frame is read from position <tlen>
3663 * and written at position <flen> (minus padding if some is present).
3664 */
3665 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3666 struct h2_fh hdr;
3667 int clen; // CONTINUATION frame's payload length
3668
3669 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3670 /* no more data, the buffer may be full, either due to
3671 * too large a frame or because of too large a hole that
3672 * we're going to compact at the end.
3673 */
3674 goto leave;
3675 }
3676
3677 if (hdr.ft != H2_FT_CONTINUATION) {
3678 /* RFC7540#6.10: frame of unexpected type */
3679 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3680 goto fail;
3681 }
3682
3683 if (hdr.sid != h2c->dsi) {
3684 /* RFC7540#6.10: frame of different stream */
3685 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3686 goto fail;
3687 }
3688
3689 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3690 /* RFC7540#4.2: invalid frame length */
3691 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3692 goto fail;
3693 }
3694
3695 /* detect when we must stop aggragating frames */
3696 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3697
3698 /* Take as much as we can of the CONTINUATION frame's payload */
3699 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3700 if (clen > hdr.len)
3701 clen = hdr.len;
3702
3703 /* Move the frame's payload over the padding, hole and frame
3704 * header. At least one of hole or dpl is null (see diagrams
3705 * above). The hole moves after the new aggragated frame.
3706 */
3707 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3708 h2c->dfl += clen - h2c->dpl;
3709 hole += h2c->dpl + 9;
3710 h2c->dpl = 0;
3711 goto next_frame;
3712 }
3713
3714 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003715
Willy Tarreau13278b42017-10-13 19:23:14 +02003716 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003717 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003718 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003719 copy = alloc_trash_chunk();
3720 if (!copy) {
3721 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3722 goto fail;
3723 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003724 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3725 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3726 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003727 }
3728
Willy Tarreau13278b42017-10-13 19:23:14 +02003729 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3730 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003731 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003732 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3733 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003734 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003735 }
3736
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003737 if (flen < 5) {
3738 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3739 goto fail;
3740 }
3741
Willy Tarreau13278b42017-10-13 19:23:14 +02003742 hdrs += 5; // stream dep = 4, weight = 1
3743 flen -= 5;
3744 }
3745
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003746 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003747 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003748 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003749 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003750
Willy Tarreau937f7602018-02-26 15:22:17 +01003751 /* we can't retry a failed decompression operation so we must be very
3752 * careful not to take any risks. In practice the output buffer is
3753 * always empty except maybe for trailers, in which case we simply have
3754 * to wait for the upper layer to finish consuming what is available.
3755 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003756
3757 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003758 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003759 if (!htx_is_empty(htx)) {
3760 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003761 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003762 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003763 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003764 if (b_data(rxbuf)) {
3765 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003766 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003767 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003768
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003769 rxbuf->head = 0;
3770 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003771 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003772
Willy Tarreau25919232019-01-03 14:48:18 +01003773 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003774 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3775 sizeof(list)/sizeof(list[0]), tmp);
3776 if (outlen < 0) {
3777 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3778 goto fail;
3779 }
3780
Willy Tarreau25919232019-01-03 14:48:18 +01003781 /* The PACK decompressor was updated, let's update the input buffer and
3782 * the parser's state to commit these changes and allow us to later
3783 * fail solely on the stream if needed.
3784 */
3785 b_del(&h2c->dbuf, h2c->dfl + hole);
3786 h2c->dfl = hole = 0;
3787 h2c->st0 = H2_CS_FRAME_H;
3788
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003789 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003790 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003791
Willy Tarreau88d138e2019-01-02 19:38:14 +01003792 if (*flags & H2_SF_HEADERS_RCVD)
3793 goto trailers;
3794
3795 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003796 if (htx) {
3797 /* HTX mode */
3798 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003799 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003800 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003801 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003802 } else {
3803 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003804 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003805 if (outlen > 0)
3806 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003807 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003808
3809 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003810 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003811 goto fail;
3812 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003813
Willy Tarreau174b06a2018-04-25 18:13:58 +02003814 if (msgf & H2_MSGF_BODY) {
3815 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003816 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003817 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003818 if (htx)
3819 htx->extra = *body_len;
3820 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003821 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003822 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003823 }
3824
Willy Tarreau88d138e2019-01-02 19:38:14 +01003825 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003826 /* indicate that a HEADERS frame was received for this stream, except
3827 * for 1xx responses. For 1xx responses, another HEADERS frame is
3828 * expected.
3829 */
3830 if (!(msgf & H2_MSGF_RSP_1XX))
3831 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003832
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003833 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003834 /* Mark the end of message, either using EOM in HTX or with the
3835 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003836 * is not set during headers with END_STREAM. For HTX trailers,
3837 * we must not leave an HTX trailers block not followed by an
3838 * EOM block, the two must be atomic. Thus if we fail to emit
3839 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003840 */
3841 if (htx) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02003842 if (!htx_add_endof(htx, HTX_BLK_EOM))
Willy Tarreau88d138e2019-01-02 19:38:14 +01003843 goto fail;
3844 }
3845 else if (*flags & H2_SF_DATA_CHNK) {
3846 if (!b_putblk(rxbuf, "\r\n", 2))
3847 goto fail;
3848 }
3849 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003850
Willy Tarreau86277d42019-01-02 15:36:11 +01003851 /* success */
3852 ret = 1;
3853
Willy Tarreau68dd9852017-07-03 14:44:26 +02003854 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003855 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003856 * move the remaining data over it.
3857 */
3858 if (hole) {
3859 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3860 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3861 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3862 b_sub(&h2c->dbuf, hole);
3863 }
3864
Christopher Faulete4254872021-04-21 10:39:53 +02003865 if (b_full(&h2c->dbuf) && h2c->dfl) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003866 /* too large frames */
3867 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003868 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003869 }
3870
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003871 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003872 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003873 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003874 return ret;
3875
Willy Tarreau68dd9852017-07-03 14:44:26 +02003876 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003877 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003878 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003879
3880 trailers:
3881 /* This is the last HEADERS frame hence a trailer */
3882
3883 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3884 /* It's a trailer but it's missing ES flag */
3885 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3886 goto fail;
3887 }
3888
Christopher Faulet54b5e212019-06-04 10:08:28 +02003889 /* Trailers terminate a DATA sequence. In HTX we always handle them. In
3890 * legacy, when using chunks, we have to emit the 0 CRLF marker first
3891 * and then handle the trailers. For other modes, the trailers are
3892 * silently dropped.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003893 */
3894 if (htx) {
Willy Tarreau5255f282019-01-03 18:41:05 +01003895 if (h2_make_htx_trailers(list, htx) <= 0)
3896 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003897 }
3898 else if (*flags & H2_SF_DATA_CHNK) {
3899 /* Legacy mode with chunked encoding : we must finalize the
3900 * data block message emit the trailing CRLF */
3901 if (!b_putblk(rxbuf, "0\r\n", 3))
3902 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003903
3904 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3905 if (outlen > 0)
3906 b_add(rxbuf, outlen);
3907 else
3908 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003909 }
3910
3911 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003912}
3913
Willy Tarreau454f9052017-10-26 19:40:35 +02003914/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3915 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3916 * in use, a new chunk is emitted for each frame. This is supposed to fit
3917 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3918 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3919 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003920 * parser state is automatically updated. Returns > 0 if it could completely
3921 * send the current frame, 0 if it couldn't complete, in which case
3922 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3923 * DATA frame can return 0 as a valid result). Stream errors are reported in
3924 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3925 * have checked the frame header and ensured that the frame was complete or the
3926 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003927 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003928static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003929{
3930 struct h2c *h2c = h2s->h2c;
3931 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003932 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003933 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003934 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003935 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003936
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003937 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003938
Olivier Houchard638b7992018-08-16 15:41:52 +02003939 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003940 if (!csbuf) {
3941 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003942 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003943 }
3944
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003945try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003946 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003947 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3948 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003949 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003950 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003951
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003952 if (flen > b_data(&h2c->dbuf)) {
3953 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003954 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003955 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003956 }
3957
Willy Tarreaua9b77962019-01-31 07:23:00 +01003958 if (htx) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003959 unsigned int sent;
3960
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003961 block1 = htx_free_data_space(htx);
3962 if (!block1) {
3963 h2c->flags |= H2_CF_DEM_SFULL;
3964 goto fail;
3965 }
3966 if (flen > block1)
3967 flen = block1;
3968
3969 /* here, flen is the max we can copy into the output buffer */
3970 block1 = b_contig_data(&h2c->dbuf, 0);
3971 if (flen > block1)
3972 flen = block1;
3973
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003974 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003975
Willy Tarreaub6563f42019-06-15 11:34:41 +02003976 b_del(&h2c->dbuf, sent);
3977 h2c->dfl -= sent;
3978 h2c->rcvd_c += sent;
3979 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003980
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003981 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreaub6563f42019-06-15 11:34:41 +02003982 h2s->body_len -= sent;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003983 htx->extra = h2s->body_len;
3984 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003985
3986 if (sent < flen) {
3987 h2c->flags |= H2_CF_DEM_SFULL;
3988 goto fail;
3989 }
3990
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003991 goto try_again;
3992 }
Willy Tarreau455d5682019-05-24 19:42:18 +02003993 else if (unlikely(b_space_wraps(csbuf) &&
3994 flen + chklen <= b_room(csbuf) &&
3995 b_data(csbuf) <= MAX_DATA_REALIGN)) {
3996 /* it doesn't fit in a single block and the buffer is fragmented, if there are
3997 * not too many data in the buffer, let's defragment it and try
3998 * again.
Willy Tarreaud755ea62018-02-26 15:44:54 +01003999 */
4000 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02004001 }
4002
Willy Tarreaueba10f22018-04-25 20:44:22 +02004003 /* chunked-encoding requires more room */
4004 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01004005 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02004006 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
4007 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
4008 (chklen < 1048576) ? 4 : 8;
4009 chklen += 4; // CRLF, CRLF
4010 }
4011
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004012 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004013 if (flen + chklen > b_room(csbuf)) {
4014 if (chklen >= b_room(csbuf)) {
4015 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004016 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004017 }
4018 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004019 }
4020
4021 if (h2s->flags & H2_SF_DATA_CHNK) {
4022 /* emit the chunk size */
4023 unsigned int chksz = flen;
4024 char str[10];
4025 char *beg;
4026
4027 beg = str + sizeof(str);
4028 *--beg = '\n';
4029 *--beg = '\r';
4030 do {
4031 *--beg = hextab[chksz & 0xF];
4032 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004033 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004034 }
4035
Willy Tarreau454f9052017-10-26 19:40:35 +02004036 /* Block1 is the length of the first block before the buffer wraps,
4037 * block2 is the optional second block to reach the end of the frame.
4038 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004039 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004040 if (block1 > flen)
4041 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02004042 block2 = flen - block1;
4043
4044 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01004045 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02004046
4047 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01004048 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02004049
Willy Tarreaueba10f22018-04-25 20:44:22 +02004050 if (h2s->flags & H2_SF_DATA_CHNK) {
4051 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004052 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02004053 }
4054
Willy Tarreau454f9052017-10-26 19:40:35 +02004055 /* now mark the input data as consumed (will be deleted from the buffer
4056 * by the caller when seeing FRAME_A after sending the window update).
4057 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004058 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004059 h2c->dfl -= flen;
4060 h2c->rcvd_c += flen;
4061 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
4062
Willy Tarreau1915ca22019-01-24 11:49:37 +01004063 if (h2s->flags & H2_SF_DATA_CLEN)
4064 h2s->body_len -= flen;
4065
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004066 if (h2c->dfl > h2c->dpl) {
4067 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004068 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004069 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004070 }
4071
Willy Tarreau4a28da12018-01-04 14:41:00 +01004072 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004073 /* here we're done with the frame, all the payload (except padding) was
4074 * transferred.
4075 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02004076
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004077 if (h2c->dff & H2_F_DATA_END_STREAM) {
4078 if (htx) {
4079 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
4080 h2c->flags |= H2_CF_DEM_SFULL;
4081 goto fail;
4082 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01004083 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004084 else if (h2s->flags & H2_SF_DATA_CHNK) {
4085 /* emit the trailing 0 CRLF CRLF */
4086 if (b_room(csbuf) < 5) {
4087 h2c->flags |= H2_CF_DEM_SFULL;
4088 goto fail;
4089 }
4090 chklen += 5;
4091 b_putblk(csbuf, "0\r\n\r\n", 5);
4092 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02004093 }
4094
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004095 h2c->rcvd_c += h2c->dpl;
4096 h2c->rcvd_s += h2c->dpl;
4097 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004098 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004099 if (htx)
4100 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004101 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004102 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004103 if (htx)
4104 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004105 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004106}
4107
Willy Tarreau5dd17352018-06-14 13:33:30 +02004108/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
4109 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
4110 * number of bytes sent. The caller must check the stream's status to detect
4111 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004112 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004113static 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 +02004114{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004115 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004116 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004117 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004118 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004119 struct buffer *mbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004120 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004121 int es_now = 0;
4122 int ret = 0;
4123 int hdr;
4124
4125 if (h2c_mux_busy(h2c, h2s)) {
4126 h2s->flags |= H2_SF_BLK_MBUSY;
4127 return 0;
4128 }
4129
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004130 /* First, try to parse the H1 response and index it into <list>.
4131 * NOTE! Since it comes from haproxy, we *know* that a response header
4132 * block does not wrap and we can safely read it this way without
4133 * having to realign the buffer.
4134 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004135 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004136 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004137 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01004138 /* incomplete or invalid response, this is abnormal coming from
4139 * haproxy and may only result in a bad errorfile or bad Lua code
4140 * so that won't be fixed, raise an error now.
4141 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004142 * FIXME: we should instead add the ability to only return a
4143 * 502 bad gateway. But in theory this is not supposed to
4144 * happen.
4145 */
4146 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4147 ret = 0;
4148 goto end;
4149 }
4150
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004151 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02004152
4153 /* certain statuses have no body or an empty one, regardless of
4154 * what the headers say.
4155 */
4156 if (sl.st.status >= 100 && sl.st.status < 200) {
4157 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
4158 h1m->curr_len = h1m->body_len = 0;
4159 }
4160 else if (sl.st.status == 204 || sl.st.status == 304) {
4161 /* no contents, claim c-len is present and set to zero */
4162 h1m->flags &= ~H1_MF_CHNK;
4163 h1m->flags |= H1_MF_CLEN;
4164 h1m->curr_len = h1m->body_len = 0;
4165 }
4166
Willy Tarreaubcc45952019-05-26 10:05:50 +02004167 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004168 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004169 if (!h2_get_buf(h2c, mbuf)) {
4170 h2c->flags |= H2_CF_MUX_MALLOC;
4171 h2s->flags |= H2_SF_BLK_MROOM;
4172 return 0;
4173 }
4174
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004175 chunk_reset(&outbuf);
4176
4177 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004178 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4179 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004180 break;
4181 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004182 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004183 }
4184
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004185 if (outbuf.size < 9)
4186 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004187
4188 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004189 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4190 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4191 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004192
4193 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004194 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01004195 /* this is an unparsable response */
4196 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4197 ret = 0;
4198 goto end;
4199 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01004200
4201 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004202 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004203 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004204 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004205 }
4206
4207 /* encode all headers, stop at empty name */
4208 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01004209 /* these ones do not exist in H2 and must be dropped. */
4210 if (isteq(list[hdr].n, ist("connection")) ||
4211 isteq(list[hdr].n, ist("proxy-connection")) ||
4212 isteq(list[hdr].n, ist("keep-alive")) ||
4213 isteq(list[hdr].n, ist("upgrade")) ||
4214 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004215 continue;
4216
4217 if (isteq(list[hdr].n, ist("")))
4218 break; // end
4219
4220 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4221 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004222 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004223 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004224 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004225 }
4226 }
4227
4228 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01004229 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004230 es_now = 1;
4231
4232 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004233 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004234
4235 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004236 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004237
4238 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004239 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004240
4241 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004242 b_add(mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01004243 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004244
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004245 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004246 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004247 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004248
Willy Tarreau801250e2018-09-11 11:45:04 +02004249 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004250 h2s->flags |= H2_SF_ES_SENT;
4251 if (h2s->st == H2_SS_OPEN)
4252 h2s->st = H2_SS_HLOC;
4253 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004254 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004255 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004256 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004257 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004258 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004259 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004260 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004261 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004262 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004263
4264 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004265
4266 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004267 //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 +02004268 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004269 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004270 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4271 goto retry;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004272 h1m_init_res(h1m);
4273 h1m->err_pos = -1; // don't care about errors on the response path
4274 h2c->flags |= H2_CF_MUX_MFULL;
4275 h2s->flags |= H2_SF_BLK_MROOM;
4276 ret = 0;
4277 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004278}
4279
Willy Tarreau5dd17352018-06-14 13:33:30 +02004280/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4281 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4282 * the number of bytes sent. The caller must check the stream's status to
4283 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004284 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004285static 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 +02004286{
4287 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004288 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004289 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004290 struct buffer *mbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004291 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004292 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004293 int es_now = 0;
4294 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004295 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004296 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004297
4298 if (h2c_mux_busy(h2c, h2s)) {
4299 h2s->flags |= H2_SF_BLK_MBUSY;
4300 goto end;
4301 }
4302
Willy Tarreaubcc45952019-05-26 10:05:50 +02004303 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004304 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004305 if (!h2_get_buf(h2c, mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004306 h2c->flags |= H2_CF_MUX_MALLOC;
4307 h2s->flags |= H2_SF_BLK_MROOM;
4308 goto end;
4309 }
4310
4311 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004312 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004313 goto end;
4314
4315 chunk_reset(&outbuf);
4316
4317 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004318 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4319 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004320 break;
4321 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004322 /* If there are pending data in the output buffer, and we have
4323 * less than 1/4 of the mbuf's size and everything fits, we'll
4324 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4325 * is full and wait, to save some slow realign calls.
4326 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004327 if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004328 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4329 goto retry;
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004330 h2c->flags |= H2_CF_MUX_MFULL;
4331 h2s->flags |= H2_SF_BLK_MROOM;
4332 goto end;
4333 }
4334
Willy Tarreaubcc45952019-05-26 10:05:50 +02004335 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004336 }
4337
4338 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004339 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4340 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004341 h2c->flags |= H2_CF_MUX_MFULL;
4342 h2s->flags |= H2_SF_BLK_MROOM;
4343 goto end;
4344 }
4345
4346 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004347 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4348 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4349 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004350
4351 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4352 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004353 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004354 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004355 break;
4356 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004357 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004358 if ((long long)size > h1m->curr_len)
4359 size = h1m->curr_len;
4360 break;
4361 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004362 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004363 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02004364 if (ret <= 0) {
Christopher Faulet307f31e2020-08-05 14:37:13 +02004365 /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ?
4366 * It should not happen except if opposite connection was closed.
4367 * Note: check there is at least something to parse to be sure the chunk crlf
4368 * is truncated.
4369 */
4370 if (max) {
4371 h1m->err_pos = ofs + max + ret;
4372 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4373 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004374 goto end;
4375 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004376 max -= ret;
4377 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004378 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004379 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004380 }
4381
Willy Tarreau801250e2018-09-11 11:45:04 +02004382 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004383 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004384 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02004385 if (ret <= 0) {
Christopher Faulet307f31e2020-08-05 14:37:13 +02004386 /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ?
4387 * It should not happen except if opposite connection was closed.
4388 * Note: check there is at least something to parse to be sure the chunke size
4389 * is truncated.
4390 */
4391 if (max) {
4392 h1m->err_pos = ofs + max + ret;
4393 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4394 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004395 goto end;
4396 }
4397
4398 size = chunk;
4399 h1m->curr_len = chunk;
4400 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004401 max -= ret;
4402 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004403 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004404 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004405 if (!size)
4406 goto send_empty;
4407 }
4408
4409 /* in MSG_DATA state, continue below */
4410 size = h1m->curr_len;
4411 break;
4412 }
4413
4414 /* we have in <size> the exact number of bytes we need to copy from
4415 * the H1 buffer. We need to check this against the connection's and
4416 * the stream's send windows, and to ensure that this fits in the max
4417 * frame size and in the buffer's available space minus 9 bytes (for
4418 * the frame header). The connection's flow control is applied last so
4419 * that we can use a separate list of streams which are immediately
4420 * unblocked on window opening. Note: we don't implement padding.
4421 */
4422
Willy Tarreau5dd17352018-06-14 13:33:30 +02004423 if (size > max)
4424 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004425
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004426 if (size > h2s_mws(h2s))
4427 size = h2s_mws(h2s);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004428
4429 if (size <= 0) {
4430 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004431 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004432 LIST_DEL_INIT(&h2s->list);
Willy Tarreau55dc0842019-10-22 10:04:39 +02004433 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004434 goto end;
4435 }
4436
4437 if (h2c->mfs && size > h2c->mfs)
4438 size = h2c->mfs;
4439
4440 if (size + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004441 /* It doesn't fit at once. If it at least fits once split and
4442 * the amount of data to move is low, let's defragment the
4443 * buffer now.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004444 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004445 if (b_space_wraps(mbuf) &&
4446 (size + 9 <= b_room(mbuf)) &&
4447 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004448 goto realign_again;
4449 size = outbuf.size - 9;
4450 }
4451
4452 if (size <= 0) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004453 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4454 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004455 h2c->flags |= H2_CF_MUX_MFULL;
4456 h2s->flags |= H2_SF_BLK_MROOM;
4457 goto end;
4458 }
4459
4460 if (size > h2c->mws)
4461 size = h2c->mws;
4462
4463 if (size <= 0) {
4464 h2s->flags |= H2_SF_BLK_MFCTL;
4465 goto end;
4466 }
4467
4468 /* copy whatever we can */
4469 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004470 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004471 if (ret == 1)
4472 len2 = 0;
4473
4474 if (!ret || len1 + len2 < size) {
4475 /* FIXME: must normally never happen */
4476 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4477 goto end;
4478 }
4479
4480 /* limit len1/len2 to size */
4481 if (len1 + len2 > size) {
4482 int sub = len1 + len2 - size;
4483
4484 if (len2 > sub)
4485 len2 -= sub;
4486 else {
4487 sub -= len2;
4488 len2 = 0;
4489 len1 -= sub;
4490 }
4491 }
4492
4493 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004494 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004495 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004496 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004497
4498 send_empty:
4499 /* we may need to add END_STREAM */
4500 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4501 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004502 *
4503 * FIXME: what we do here is not correct because we send end_stream
4504 * before knowing if we'll have to send a HEADERS frame for the
4505 * trailers. More importantly we're not consuming the trailing CRLF
4506 * after the end of trailers, so it will be left to the caller to
4507 * eat it. The right way to do it would be to measure trailers here
4508 * and to send ES only if there are no trailers.
4509 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004510 */
4511 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004512 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004513 es_now = 1;
4514
4515 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004516 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004517
4518 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004519 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004520
4521 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004522 b_add(mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004523
4524 /* consume incoming H1 response */
4525 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004526 max -= size;
4527 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004528 total += size;
4529 h1m->curr_len -= size;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004530 h2s->sws -= size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004531 h2c->mws -= size;
4532
4533 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004534 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004535 goto new_frame;
4536 }
4537 }
4538
4539 if (es_now) {
4540 if (h2s->st == H2_SS_OPEN)
4541 h2s->st = H2_SS_HLOC;
4542 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004543 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004544
Willy Tarreau35a62702018-02-27 15:37:25 +01004545 if (!(h1m->flags & H1_MF_CHNK)) {
4546 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004547 total += max;
4548 ofs += max;
4549 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004550
Willy Tarreau801250e2018-09-11 11:45:04 +02004551 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004552 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004553
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004554 h2s->flags |= H2_SF_ES_SENT;
4555 }
4556
4557 end:
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004558 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(h2s), (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004559 return total;
4560}
4561
Willy Tarreau115e83b2018-12-01 19:17:53 +01004562/* Try to send a HEADERS frame matching HTX response present in HTX message
4563 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4564 * must check the stream's status to detect any error which might have happened
4565 * subsequently to a successful send. The htx blocks are automatically removed
4566 * from the message. The htx message is assumed to be valid since produced from
4567 * the internal code, hence it contains a start line, an optional series of
4568 * header blocks and an end of header, otherwise an invalid frame could be
4569 * emitted and the resulting htx message could be left in an inconsistent state.
4570 */
4571static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4572{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004573 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004574 struct h2c *h2c = h2s->h2c;
4575 struct htx_blk *blk;
4576 struct htx_blk *blk_end;
4577 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004578 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004579 struct htx_sl *sl;
4580 enum htx_blk_type type;
4581 int es_now = 0;
4582 int ret = 0;
4583 int hdr;
4584 int idx;
4585
4586 if (h2c_mux_busy(h2c, h2s)) {
4587 h2s->flags |= H2_SF_BLK_MBUSY;
4588 return 0;
4589 }
4590
Willy Tarreau115e83b2018-12-01 19:17:53 +01004591 /* determine the first block which must not be deleted, blk_end may
4592 * be NULL if all blocks have to be deleted.
4593 */
4594 idx = htx_get_head(htx);
4595 blk_end = NULL;
4596 while (idx != -1) {
4597 type = htx_get_blk_type(htx_get_blk(htx, idx));
4598 idx = htx_get_next(htx, idx);
4599 if (type == HTX_BLK_EOH) {
4600 if (idx != -1)
4601 blk_end = htx_get_blk(htx, idx);
4602 break;
4603 }
4604 }
4605
4606 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004607 blk = htx_get_head_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01004608 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_RES_SL);
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004609 ALREADY_CHECKED(blk);
4610 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004611 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004612 if (h2s->status < 100 || h2s->status > 999)
4613 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004614
4615 /* and the rest of the headers, that we dump starting at header 0 */
4616 hdr = 0;
4617
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004618 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004619 while ((idx = htx_get_next(htx, idx)) != -1) {
4620 blk = htx_get_blk(htx, idx);
4621 type = htx_get_blk_type(blk);
4622
4623 if (type == HTX_BLK_UNUSED)
4624 continue;
4625
4626 if (type != HTX_BLK_HDR)
4627 break;
4628
4629 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4630 goto fail;
4631
4632 list[hdr].n = htx_get_blk_name(htx, blk);
4633 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004634 hdr++;
4635 }
4636
4637 /* marker for end of headers */
4638 list[hdr].n = ist("");
4639
4640 if (h2s->status == 204 || h2s->status == 304) {
4641 /* no contents, claim c-len is present and set to zero */
4642 es_now = 1;
4643 }
4644
Willy Tarreau9c218e72019-05-26 10:08:28 +02004645 mbuf = br_tail(h2c->mbuf);
4646 retry:
4647 if (!h2_get_buf(h2c, mbuf)) {
4648 h2c->flags |= H2_CF_MUX_MALLOC;
4649 h2s->flags |= H2_SF_BLK_MROOM;
4650 return 0;
4651 }
4652
Willy Tarreau115e83b2018-12-01 19:17:53 +01004653 chunk_reset(&outbuf);
4654
4655 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004656 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4657 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004658 break;
4659 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004660 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004661 }
4662
4663 if (outbuf.size < 9)
4664 goto full;
4665
4666 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4667 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4668 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4669 outbuf.data = 9;
4670
4671 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004672 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004673 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004674 goto realign_again;
4675 goto full;
4676 }
4677
4678 /* encode all headers, stop at empty name */
4679 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4680 /* these ones do not exist in H2 and must be dropped. */
4681 if (isteq(list[hdr].n, ist("connection")) ||
4682 isteq(list[hdr].n, ist("proxy-connection")) ||
4683 isteq(list[hdr].n, ist("keep-alive")) ||
4684 isteq(list[hdr].n, ist("upgrade")) ||
4685 isteq(list[hdr].n, ist("transfer-encoding")))
4686 continue;
4687
4688 if (isteq(list[hdr].n, ist("")))
4689 break; // end
4690
4691 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4692 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004693 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004694 goto realign_again;
4695 goto full;
4696 }
4697 }
4698
Christopher Faulet0b465482019-02-19 15:14:23 +01004699 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004700 * FIXME: we should also set it when we know for sure that the
4701 * content-length is zero as well as on 204/304
4702 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004703 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4704 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004705 es_now = 1;
4706
Willy Tarreau927b88b2019-03-04 08:03:25 +01004707 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004708 es_now = 1;
4709
4710 /* update the frame's size */
4711 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4712
4713 if (es_now)
4714 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4715
4716 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004717 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004718
4719 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4720 * 1xx responses, another HEADERS frame is expected.
4721 */
4722 if (h2s->status >= 200 || h2s->status == 101)
4723 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004724
Willy Tarreau115e83b2018-12-01 19:17:53 +01004725 if (es_now) {
4726 h2s->flags |= H2_SF_ES_SENT;
4727 if (h2s->st == H2_SS_OPEN)
4728 h2s->st = H2_SS_HLOC;
4729 else
4730 h2s_close(h2s);
4731 }
4732
4733 /* OK we could properly deliver the response */
4734
4735 /* remove all header blocks including the EOH and compute the
4736 * corresponding size.
4737 *
4738 * FIXME: We should remove everything when es_now is set.
4739 */
4740 ret = 0;
4741 idx = htx_get_head(htx);
4742 blk = htx_get_blk(htx, idx);
4743 while (blk != blk_end) {
4744 ret += htx_get_blksz(blk);
4745 blk = htx_remove_blk(htx, blk);
4746 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004747
Christopher Faulet316934d2019-05-15 14:22:48 +02004748 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4749 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004750 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004751 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004752 end:
4753 return ret;
4754 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004755 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4756 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004757 h2c->flags |= H2_CF_MUX_MFULL;
4758 h2s->flags |= H2_SF_BLK_MROOM;
4759 ret = 0;
4760 goto end;
4761 fail:
4762 /* unparsable HTX messages, too large ones to be produced in the local
4763 * list etc go here (unrecoverable errors).
4764 */
4765 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4766 ret = 0;
4767 goto end;
4768}
4769
Willy Tarreau80739692018-10-05 11:35:57 +02004770/* Try to send a HEADERS frame matching HTX request present in HTX message
4771 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4772 * must check the stream's status to detect any error which might have happened
4773 * subsequently to a successful send. The htx blocks are automatically removed
4774 * from the message. The htx message is assumed to be valid since produced from
4775 * the internal code, hence it contains a start line, an optional series of
4776 * header blocks and an end of header, otherwise an invalid frame could be
4777 * emitted and the resulting htx message could be left in an inconsistent state.
4778 */
4779static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4780{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004781 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004782 struct h2c *h2c = h2s->h2c;
4783 struct htx_blk *blk;
4784 struct htx_blk *blk_end;
4785 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004786 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004787 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004788 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004789 enum htx_blk_type type;
4790 int es_now = 0;
4791 int ret = 0;
4792 int hdr;
4793 int idx;
4794
4795 if (h2c_mux_busy(h2c, h2s)) {
4796 h2s->flags |= H2_SF_BLK_MBUSY;
4797 return 0;
4798 }
4799
Willy Tarreau80739692018-10-05 11:35:57 +02004800 /* determine the first block which must not be deleted, blk_end may
4801 * be NULL if all blocks have to be deleted.
4802 */
4803 idx = htx_get_head(htx);
4804 blk_end = NULL;
4805 while (idx != -1) {
4806 type = htx_get_blk_type(htx_get_blk(htx, idx));
4807 idx = htx_get_next(htx, idx);
4808 if (type == HTX_BLK_EOH) {
4809 if (idx != -1)
4810 blk_end = htx_get_blk(htx, idx);
4811 break;
4812 }
4813 }
4814
4815 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004816 blk = htx_get_head_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01004817 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004818 ALREADY_CHECKED(blk);
4819 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004820 meth = htx_sl_req_meth(sl);
4821 path = htx_sl_req_uri(sl);
4822
4823 /* and the rest of the headers, that we dump starting at header 0 */
4824 hdr = 0;
4825
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004826 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004827 while ((idx = htx_get_next(htx, idx)) != -1) {
4828 blk = htx_get_blk(htx, idx);
4829 type = htx_get_blk_type(blk);
4830
4831 if (type == HTX_BLK_UNUSED)
4832 continue;
4833
4834 if (type != HTX_BLK_HDR)
4835 break;
4836
4837 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4838 goto fail;
4839
4840 list[hdr].n = htx_get_blk_name(htx, blk);
4841 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004842 hdr++;
4843 }
4844
4845 /* marker for end of headers */
4846 list[hdr].n = ist("");
4847
Willy Tarreau9c218e72019-05-26 10:08:28 +02004848 mbuf = br_tail(h2c->mbuf);
4849 retry:
4850 if (!h2_get_buf(h2c, mbuf)) {
4851 h2c->flags |= H2_CF_MUX_MALLOC;
4852 h2s->flags |= H2_SF_BLK_MROOM;
4853 return 0;
4854 }
4855
Willy Tarreau80739692018-10-05 11:35:57 +02004856 chunk_reset(&outbuf);
4857
4858 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004859 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4860 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004861 break;
4862 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004863 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004864 }
4865
4866 if (outbuf.size < 9)
4867 goto full;
4868
4869 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4870 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4871 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4872 outbuf.data = 9;
4873
4874 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004875 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004876 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004877 goto realign_again;
4878 goto full;
4879 }
4880
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004881 /* RFC7540 #8.3: the CONNECT method must have :
4882 * - :authority set to the URI part (host:port)
4883 * - :method set to CONNECT
4884 * - :scheme and :path omitted
4885 */
4886 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4887 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004888 struct ist scheme;
4889
4890 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4891 scheme = ist("http");
4892 else
4893 scheme = ist("https");
4894
4895 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004896 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004897 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004898 goto realign_again;
4899 goto full;
4900 }
Willy Tarreau80739692018-10-05 11:35:57 +02004901
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004902 /* encode the path, which necessarily is the second one */
4903 if (!hpack_encode_path(&outbuf, path)) {
4904 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004905 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004906 goto realign_again;
4907 goto full;
4908 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004909
4910 /* look for the Host header and place it in :authority */
4911 auth = ist2(NULL, 0);
4912 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4913 if (isteq(list[hdr].n, ist("")))
4914 break; // end
4915
4916 if (isteq(list[hdr].n, ist("host"))) {
4917 auth = list[hdr].v;
4918 break;
4919 }
4920 }
4921 }
4922 else {
4923 /* for CONNECT, :authority is taken from the path */
4924 auth = path;
4925 }
4926
4927 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4928 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004929 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004930 goto realign_again;
4931 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004932 }
4933
4934 /* encode all headers, stop at empty name */
4935 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004936 struct ist n = list[hdr].n;
4937 struct ist v = list[hdr].v;
4938
Willy Tarreau80739692018-10-05 11:35:57 +02004939 /* these ones do not exist in H2 and must be dropped. */
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004940 if (isteq(n, ist("connection")) ||
4941 isteq(n, ist("host")) ||
4942 isteq(n, ist("proxy-connection")) ||
4943 isteq(n, ist("keep-alive")) ||
4944 isteq(n, ist("upgrade")) ||
4945 isteq(n, ist("transfer-encoding")))
Willy Tarreau80739692018-10-05 11:35:57 +02004946 continue;
4947
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004948 if (isteq(n, ist("te"))) {
4949 /* "te" may only be sent with "trailers" if this value
4950 * is present, otherwise it must be deleted.
4951 */
4952 v = istist(v, ist("trailers"));
4953 if (!v.ptr || (v.len > 8 && v.ptr[8] != ','))
4954 continue;
4955 v = ist("trailers");
4956 }
4957
4958 if (isteq(n, ist("")))
Willy Tarreau80739692018-10-05 11:35:57 +02004959 break; // end
4960
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004961 if (!hpack_encode_header(&outbuf, n, v)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004962 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004963 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004964 goto realign_again;
4965 goto full;
4966 }
4967 }
4968
4969 /* we may need to add END_STREAM if we have no body :
4970 * - request already closed, or :
4971 * - no transfer-encoding, and :
4972 * - no content-length or content-length:0
4973 * Fixme: this doesn't take into account CONNECT requests.
4974 */
4975 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4976 es_now = 1;
4977
4978 if (sl->flags & HTX_SL_F_BODYLESS)
4979 es_now = 1;
4980
Willy Tarreau927b88b2019-03-04 08:03:25 +01004981 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004982 es_now = 1;
4983
4984 /* update the frame's size */
4985 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4986
4987 if (es_now)
4988 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4989
4990 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004991 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004992 h2s->flags |= H2_SF_HEADERS_SENT;
4993 h2s->st = H2_SS_OPEN;
4994
Willy Tarreau80739692018-10-05 11:35:57 +02004995 if (es_now) {
4996 // trim any possibly pending data (eg: inconsistent content-length)
4997 h2s->flags |= H2_SF_ES_SENT;
4998 h2s->st = H2_SS_HLOC;
4999 }
5000
5001 /* remove all header blocks including the EOH and compute the
5002 * corresponding size.
5003 *
5004 * FIXME: We should remove everything when es_now is set.
5005 */
5006 ret = 0;
5007 idx = htx_get_head(htx);
5008 blk = htx_get_blk(htx, idx);
5009 while (blk != blk_end) {
5010 ret += htx_get_blksz(blk);
5011 blk = htx_remove_blk(htx, blk);
5012 }
5013
Christopher Faulet316934d2019-05-15 14:22:48 +02005014 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5015 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02005016 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02005017 }
Willy Tarreau80739692018-10-05 11:35:57 +02005018
5019 end:
5020 return ret;
5021 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005022 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5023 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02005024 h2c->flags |= H2_CF_MUX_MFULL;
5025 h2s->flags |= H2_SF_BLK_MROOM;
5026 ret = 0;
5027 goto end;
5028 fail:
5029 /* unparsable HTX messages, too large ones to be produced in the local
5030 * list etc go here (unrecoverable errors).
5031 */
5032 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5033 ret = 0;
5034 goto end;
5035}
5036
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005037/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01005038 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
5039 * caller must check the stream's status to detect any error which might have
5040 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02005041 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005042 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005043static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005044{
5045 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005046 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005047 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005048 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005049 size_t total = 0;
5050 int es_now = 0;
5051 int bsize; /* htx block size */
5052 int fsize; /* h2 frame size */
5053 struct htx_blk *blk;
5054 enum htx_blk_type type;
5055 int idx;
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005056 int trunc_out; /* non-zero if truncated on out buf */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005057
5058 if (h2c_mux_busy(h2c, h2s)) {
5059 h2s->flags |= H2_SF_BLK_MBUSY;
5060 goto end;
5061 }
5062
Willy Tarreau98de12a2018-12-12 07:03:00 +01005063 htx = htx_from_buf(buf);
5064
Christopher Faulet54b5e212019-06-04 10:08:28 +02005065 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
5066 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
5067 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005068 */
5069
5070 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01005071 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005072 goto end;
5073
5074 idx = htx_get_head(htx);
5075 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02005076 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005077 bsize = htx_get_blksz(blk);
5078 fsize = bsize;
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005079 trunc_out = 0;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005080
Christopher Faulet54b5e212019-06-04 10:08:28 +02005081 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01005082 if (h2s->flags & H2_SF_ES_SENT) {
5083 /* ES already sent */
5084 htx_remove_blk(htx, blk);
5085 total++; // EOM counts as one byte
5086 count--;
5087 goto end;
5088 }
5089 }
5090 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005091 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005092
Willy Tarreau9c218e72019-05-26 10:08:28 +02005093 mbuf = br_tail(h2c->mbuf);
5094 retry:
5095 if (!h2_get_buf(h2c, mbuf)) {
5096 h2c->flags |= H2_CF_MUX_MALLOC;
5097 h2s->flags |= H2_SF_BLK_MROOM;
5098 goto end;
5099 }
5100
Willy Tarreau98de12a2018-12-12 07:03:00 +01005101 /* Perform some optimizations to reduce the number of buffer copies.
5102 * First, if the mux's buffer is empty and the htx area contains
5103 * exactly one data block of the same size as the requested count, and
5104 * this count fits within the frame size, the stream's window size, and
5105 * the connection's window size, then it's possible to simply swap the
5106 * caller's buffer with the mux's output buffer and adjust offsets and
5107 * length to match the entire DATA HTX block in the middle. In this
5108 * case we perform a true zero-copy operation from end-to-end. This is
5109 * the situation that happens all the time with large files. Second, if
5110 * this is not possible, but the mux's output buffer is empty, we still
5111 * have an opportunity to avoid the copy to the intermediary buffer, by
5112 * making the intermediary buffer's area point to the output buffer's
5113 * area. In this case we want to skip the HTX header to make sure that
5114 * copies remain aligned and that this operation remains possible all
5115 * the time. This goes for headers, data blocks and any data extracted
5116 * from the HTX blocks.
5117 */
5118 if (unlikely(fsize == count &&
5119 htx->used == 1 && type == HTX_BLK_DATA &&
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005120 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005121 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005122
Willy Tarreaubcc45952019-05-26 10:05:50 +02005123 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005124 /* Too bad there are data left there. We're willing to memcpy/memmove
5125 * up to 1/4 of the buffer, which means that it's OK to copy a large
5126 * frame into a buffer containing few data if it needs to be realigned,
5127 * and that it's also OK to copy few data without realigning. Otherwise
5128 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005129 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005130 if (fsize + 9 <= b_room(mbuf) &&
5131 (b_data(mbuf) <= b_size(mbuf) / 4 ||
5132 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01005133 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005134
Willy Tarreau9c218e72019-05-26 10:08:28 +02005135 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5136 goto retry;
5137
Willy Tarreau98de12a2018-12-12 07:03:00 +01005138 h2c->flags |= H2_CF_MUX_MFULL;
5139 h2s->flags |= H2_SF_BLK_MROOM;
5140 goto end;
5141 }
5142
5143 /* map an H2 frame to the HTX block so that we can put the
5144 * frame header there.
5145 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005146 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5147 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005148
5149 /* prepend an H2 DATA frame header just before the DATA block */
5150 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5151 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5152 h2_set_frame_size(outbuf.area, fsize);
5153
5154 /* update windows */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005155 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005156 h2c->mws -= fsize;
5157
5158 /* and exchange with our old area */
5159 buf->area = old_area;
5160 buf->data = buf->head = 0;
5161 total += fsize;
5162 goto end;
5163 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005164
Willy Tarreau98de12a2018-12-12 07:03:00 +01005165 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005166 /* for DATA and EOM we'll have to emit a frame, even if empty */
5167
5168 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005169 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5170 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005171 break;
5172 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005173 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005174 }
5175
5176 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005177 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5178 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005179 h2c->flags |= H2_CF_MUX_MFULL;
5180 h2s->flags |= H2_SF_BLK_MROOM;
5181 goto end;
5182 }
5183
5184 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5185 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5186 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5187 outbuf.data = 9;
5188
5189 /* we have in <fsize> the exact number of bytes we need to copy from
5190 * the HTX buffer. We need to check this against the connection's and
5191 * the stream's send windows, and to ensure that this fits in the max
5192 * frame size and in the buffer's available space minus 9 bytes (for
5193 * the frame header). The connection's flow control is applied last so
5194 * that we can use a separate list of streams which are immediately
5195 * unblocked on window opening. Note: we don't implement padding.
5196 */
5197
5198 /* EOM is presented with bsize==1 but would lead to the emission of an
5199 * empty frame, thus we force it to zero here.
5200 */
5201 if (type == HTX_BLK_EOM)
5202 bsize = fsize = 0;
5203
5204 if (!fsize)
5205 goto send_empty;
5206
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005207 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005208 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005209 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005210 LIST_DEL_INIT(&h2s->list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005211 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005212 goto end;
5213 }
5214
Willy Tarreauee573762018-12-04 15:25:57 +01005215 if (fsize > count)
5216 fsize = count;
5217
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005218 if (fsize > h2s_mws(h2s))
5219 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005220
5221 if (h2c->mfs && fsize > h2c->mfs)
5222 fsize = h2c->mfs; // >0
5223
5224 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005225 /* It doesn't fit at once. If it at least fits once split and
5226 * the amount of data to move is low, let's defragment the
5227 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005228 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005229 if (b_space_wraps(mbuf) &&
5230 (fsize + 9 <= b_room(mbuf)) &&
5231 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005232 goto realign_again;
5233 fsize = outbuf.size - 9;
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005234 trunc_out = 1;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005235
5236 if (fsize <= 0) {
5237 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005238 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5239 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005240 h2c->flags |= H2_CF_MUX_MFULL;
5241 h2s->flags |= H2_SF_BLK_MROOM;
5242 goto end;
5243 }
5244 }
5245
5246 if (h2c->mws <= 0) {
5247 h2s->flags |= H2_SF_BLK_MFCTL;
5248 goto end;
5249 }
5250
5251 if (fsize > h2c->mws)
5252 fsize = h2c->mws;
5253
5254 /* now let's copy this this into the output buffer */
5255 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005256 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005257 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005258 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005259
5260 send_empty:
5261 /* update the frame's size */
5262 h2_set_frame_size(outbuf.area, fsize);
5263
5264 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5265 * meeting EOM. We should optimize this later.
5266 */
5267 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005268 total++; // EOM counts as one byte
5269 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005270 es_now = 1;
5271 }
5272
5273 if (es_now)
5274 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5275
5276 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005277 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005278
5279 /* consume incoming HTX block, including EOM */
5280 total += fsize;
5281 if (fsize == bsize) {
5282 htx_remove_blk(htx, blk);
5283 if (fsize)
5284 goto new_frame;
5285 } else {
5286 /* we've truncated this block */
5287 htx_cut_data_blk(htx, blk, fsize);
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005288 if (trunc_out)
5289 goto new_frame;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005290 }
5291
5292 if (es_now) {
5293 if (h2s->st == H2_SS_OPEN)
5294 h2s->st = H2_SS_HLOC;
5295 else
5296 h2s_close(h2s);
5297
5298 h2s->flags |= H2_SF_ES_SENT;
5299 }
5300
5301 end:
5302 return total;
5303}
5304
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005305/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5306 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5307 * processed. The caller must check the stream's status to detect any error
5308 * which might have happened subsequently to a successful send. The htx blocks
5309 * are automatically removed from the message. The htx message is assumed to be
5310 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005311 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005312 * as a single frame. The ES flag is always set.
5313 */
5314static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5315{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005316 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005317 struct h2c *h2c = h2s->h2c;
5318 struct htx_blk *blk;
5319 struct htx_blk *blk_end;
5320 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005321 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005322 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005323 int ret = 0;
5324 int hdr;
5325 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005326
5327 if (h2c_mux_busy(h2c, h2s)) {
5328 h2s->flags |= H2_SF_BLK_MBUSY;
5329 goto end;
5330 }
5331
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005332 /* determine the first block which must not be deleted, blk_end may
5333 * be NULL if all blocks have to be deleted. also get trailers.
5334 */
5335 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005336 blk_end = NULL;
5337
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005338 hdr = 0;
5339 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005340 blk = htx_get_blk(htx, idx);
5341 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005342 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005343 if (type == HTX_BLK_UNUSED)
5344 continue;
5345
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005346 if (type == HTX_BLK_EOT) {
5347 if (idx != -1)
5348 blk_end = blk;
5349 break;
5350 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005351 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005352 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005353
5354 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5355 goto fail;
5356
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005357 list[hdr].n = htx_get_blk_name(htx, blk);
5358 list[hdr].v = htx_get_blk_value(htx, blk);
5359 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005360 }
5361
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005362 /* marker for end of trailers */
5363 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005364
Willy Tarreau9c218e72019-05-26 10:08:28 +02005365 mbuf = br_tail(h2c->mbuf);
5366 retry:
5367 if (!h2_get_buf(h2c, mbuf)) {
5368 h2c->flags |= H2_CF_MUX_MALLOC;
5369 h2s->flags |= H2_SF_BLK_MROOM;
5370 goto end;
5371 }
5372
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005373 chunk_reset(&outbuf);
5374
5375 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005376 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5377 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005378 break;
5379 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005380 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005381 }
5382
5383 if (outbuf.size < 9)
5384 goto full;
5385
5386 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5387 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5388 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5389 outbuf.data = 9;
5390
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005391 /* encode all headers */
5392 for (idx = 0; idx < hdr; idx++) {
5393 /* these ones do not exist in H2 or must not appear in
5394 * trailers and must be dropped.
5395 */
5396 if (isteq(list[idx].n, ist("host")) ||
5397 isteq(list[idx].n, ist("content-length")) ||
5398 isteq(list[idx].n, ist("connection")) ||
5399 isteq(list[idx].n, ist("proxy-connection")) ||
5400 isteq(list[idx].n, ist("keep-alive")) ||
5401 isteq(list[idx].n, ist("upgrade")) ||
5402 isteq(list[idx].n, ist("te")) ||
5403 isteq(list[idx].n, ist("transfer-encoding")))
5404 continue;
5405
5406 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5407 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005408 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005409 goto realign_again;
5410 goto full;
5411 }
5412 }
5413
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005414 if (outbuf.data == 9) {
5415 /* here we have a problem, we have nothing to emit (either we
5416 * received an empty trailers block followed or we removed its
5417 * contents above). Because of this we can't send a HEADERS
5418 * frame, so we have to cheat and instead send an empty DATA
5419 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005420 */
5421 outbuf.area[3] = H2_FT_DATA;
5422 outbuf.area[4] = H2_F_DATA_END_STREAM;
5423 }
5424
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005425 /* update the frame's size */
5426 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5427
5428 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005429 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005430 h2s->flags |= H2_SF_ES_SENT;
5431
5432 if (h2s->st == H2_SS_OPEN)
5433 h2s->st = H2_SS_HLOC;
5434 else
5435 h2s_close(h2s);
5436
5437 /* OK we could properly deliver the response */
5438 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005439 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005440 ret = 0;
5441 idx = htx_get_head(htx);
5442 blk = htx_get_blk(htx, idx);
5443 while (blk != blk_end) {
5444 ret += htx_get_blksz(blk);
5445 blk = htx_remove_blk(htx, blk);
5446 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005447
5448 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5449 ret += htx_get_blksz(blk_end);
5450 htx_remove_blk(htx, blk_end);
5451 }
5452
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005453 end:
5454 return ret;
5455 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005456 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5457 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005458 h2c->flags |= H2_CF_MUX_MFULL;
5459 h2s->flags |= H2_SF_BLK_MROOM;
5460 ret = 0;
5461 goto end;
5462 fail:
5463 /* unparsable HTX messages, too large ones to be produced in the local
5464 * list etc go here (unrecoverable errors).
5465 */
5466 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5467 ret = 0;
5468 goto end;
5469}
5470
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005471/* Called from the upper layer, to subscribe to events, such as being able to send.
5472 * The <param> argument here is supposed to be a pointer to a wait_event struct
5473 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5474 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5475 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5476 * returns 0 except for the error above.
5477 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005478static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5479{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005480 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005481 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005482 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005483
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005484 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005485 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005486 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5487 sw->events |= SUB_RETRY_RECV;
5488 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005489 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005490 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005491 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005492 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005493 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5494 sw->events |= SUB_RETRY_SEND;
5495 h2s->send_wait = sw;
5496 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5497 !LIST_ADDED(&h2s->list)) {
5498 if (h2s->flags & H2_SF_BLK_MFCTL)
5499 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5500 else
5501 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005502 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005503 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005504 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005505 if (event_type != 0)
5506 return -1;
5507 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005508}
5509
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005510/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5511 * The <param> argument here is supposed to be a pointer to the same wait_event
5512 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5513 * It always returns zero.
5514 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005515static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5516{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005517 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005518 struct h2s *h2s = cs->ctx;
5519
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005520 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005521 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005522 BUG_ON(h2s->recv_wait != sw);
5523 sw->events &= ~SUB_RETRY_RECV;
5524 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005525 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005526 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005527 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005528 BUG_ON(h2s->send_wait != sw);
5529 LIST_DEL(&h2s->list);
5530 LIST_INIT(&h2s->list);
5531 sw->events &= ~SUB_RETRY_SEND;
5532 /* We were about to send, make sure it does not happen */
5533 if (LIST_ADDED(&h2s->sending_list) &&
5534 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02005535 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02005536 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005537 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005538 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005539 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005540 return 0;
5541}
5542
5543
Olivier Houchard511efea2018-08-16 15:30:32 +02005544/* Called from the upper layer, to receive data */
5545static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5546{
Olivier Houchard638b7992018-08-16 15:41:52 +02005547 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005548 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005549 struct htx *h2s_htx = NULL;
5550 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005551 size_t ret = 0;
5552
5553 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005554 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau86724e22018-12-01 23:19:43 +01005555 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005556 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005557 /* Here htx_to_buf() will set buffer data to 0 because
5558 * the HTX is empty.
5559 */
5560 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005561 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005562 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005563
Christopher Faulet156852b2019-05-16 11:29:13 +02005564 ret = h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005565 buf_htx = htx_from_buf(buf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005566
Christopher Faulet2f6edc82019-05-15 10:07:59 +02005567 /* <buf> is empty and the message is small enough, swap the
5568 * buffers. */
5569 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
5570 htx_to_buf(buf_htx, buf);
5571 htx_to_buf(h2s_htx, &h2s->rxbuf);
5572 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5573 goto end;
5574 }
5575
Christopher Faulet156852b2019-05-16 11:29:13 +02005576 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005577
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005578 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
Willy Tarreau7196dd62019-03-05 10:51:11 +01005579 buf_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005580 if (htx_is_empty(buf_htx))
5581 cs->flags |= CS_FL_EOI;
5582 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005583
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005584 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005585 htx_to_buf(buf_htx, buf);
5586 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet156852b2019-05-16 11:29:13 +02005587 ret -= h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005588 }
5589 else {
5590 ret = b_xfer(buf, &h2s->rxbuf, count);
5591 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005592
Christopher Faulet37070b22019-02-14 15:12:14 +01005593 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005594 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005595 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005596 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005597 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005598 if (h2s->flags & H2_SF_ES_RCVD)
5599 cs->flags |= CS_FL_EOI;
Christopher Faulet11b10532020-10-08 15:38:41 +02005600 if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005601 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005602 if (cs->flags & CS_FL_ERR_PENDING)
5603 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005604 if (b_size(&h2s->rxbuf)) {
5605 b_free(&h2s->rxbuf);
5606 offer_buffers(NULL, tasks_run_queue);
5607 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005608 }
5609
Willy Tarreau082f5592018-11-25 08:03:32 +01005610 if (ret && h2c->dsi == h2s->id) {
5611 /* demux is blocking on this stream's buffer */
5612 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005613 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005614 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005615
Olivier Houchard511efea2018-08-16 15:30:32 +02005616 return ret;
5617}
5618
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005619/* stops all senders of this connection for example when the mux buffer is full.
5620 * They are moved from the sending_list to either fctl_list or send_list.
5621 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005622static void h2_stop_senders(struct h2c *h2c)
5623{
5624 struct h2s *h2s, *h2s_back;
5625
Olivier Houchardd360ac62019-03-22 17:37:16 +01005626 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5627 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02005628 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005629 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005630 }
5631}
5632
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005633/* Called from the upper layer, to send data from buffer <buf> for no more than
5634 * <count> bytes. Returns the number of bytes effectively sent. Some status
5635 * flags may be updated on the conn_stream.
5636 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005637static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005638{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005639 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005640 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005641 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005642 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005643 struct htx *htx;
5644 struct htx_blk *blk;
5645 enum htx_blk_type btype;
5646 uint32_t bsize;
5647 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005648
Olivier Houchardd360ac62019-03-22 17:37:16 +01005649 /* If we were not just woken because we wanted to send but couldn't,
5650 * and there's somebody else that is waiting to send, do nothing,
5651 * we will subscribe later and be put at the end of the list
5652 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005653 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005654 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5655 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005656 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005657
Olivier Houchard998410a2019-04-15 19:23:37 +02005658 /* We couldn't set it to NULL before, because we needed it in case
5659 * we had to cancel the tasklet
5660 */
5661 h2s->send_wait = NULL;
5662
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005663 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5664 return 0;
5665
Willy Tarreau902df222019-10-31 15:48:18 +01005666 if (h2s->h2c->st0 >= H2_CS_ERROR) {
5667 cs->flags |= CS_FL_ERROR;
5668 return 0;
5669 }
5670
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005671 /* htx will be enough to decide if we're using HTX or legacy */
5672 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5673
Willy Tarreau0bad0432018-06-14 16:54:01 +02005674 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005675 h2s->flags |= H2_SF_OUTGOING_DATA;
5676
Willy Tarreau751f2d02018-10-05 09:35:00 +02005677 if (h2s->id == 0) {
5678 int32_t id = h2c_get_next_sid(h2s->h2c);
5679
5680 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005681 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005682 return 0;
5683 }
5684
5685 eb32_delete(&h2s->by_id);
5686 h2s->by_id.key = h2s->id = id;
5687 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005688 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005689 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5690 }
5691
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005692 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005693 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005694 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005695 idx = htx_get_head(htx);
5696 blk = htx_get_blk(htx, idx);
5697 btype = htx_get_blk_type(blk);
5698 bsize = htx_get_blksz(blk);
5699
5700 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005701 case HTX_BLK_REQ_SL:
5702 /* start-line before headers */
5703 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5704 if (ret > 0) {
5705 total += ret;
5706 count -= ret;
5707 if (ret < bsize)
5708 goto done;
5709 }
5710 break;
5711
Willy Tarreau115e83b2018-12-01 19:17:53 +01005712 case HTX_BLK_RES_SL:
5713 /* start-line before headers */
5714 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5715 if (ret > 0) {
5716 total += ret;
5717 count -= ret;
5718 if (ret < bsize)
5719 goto done;
5720 }
5721 break;
5722
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005723 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005724 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005725 /* all these cause the emission of a DATA frame (possibly empty).
5726 * This EOM necessarily is one before trailers, as the EOM following
5727 * trailers would have been consumed by the trailers parser.
5728 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005729 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005730 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005731 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005732 total += ret;
5733 count -= ret;
5734 if (ret < bsize)
5735 goto done;
5736 }
5737 break;
5738
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005739 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005740 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005741 /* This is the first trailers block, all the subsequent ones AND
5742 * the EOM will be swallowed by the parser.
5743 */
5744 ret = h2s_htx_make_trailers(h2s, htx);
5745 if (ret > 0) {
5746 total += ret;
5747 count -= ret;
5748 if (ret < bsize)
5749 goto done;
5750 }
5751 break;
5752
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005753 default:
5754 htx_remove_blk(htx, blk);
5755 total += bsize;
5756 count -= bsize;
5757 break;
5758 }
5759 }
5760 goto done;
5761 }
5762
5763 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005764 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005765 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005766 if (h2s->h2c->flags & H2_CF_IS_BACK)
5767 ret = -1;
5768 else
5769 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005770 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005771 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005772 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005773 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005774 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005775 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005776 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005777
Willy Tarreau5dd17352018-06-14 13:33:30 +02005778 if (unlikely((int)ret <= 0)) {
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02005779 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005780 break;
5781 }
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02005782
Willy Tarreau35a62702018-02-27 15:37:25 +01005783 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005784 total += count;
5785 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005786 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005787 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005788 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005789 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005790 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005791 break;
5792 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005793
5794 total += ret;
5795 count -= ret;
5796
Willy Tarreau2b778482019-05-06 15:00:22 +02005797 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005798 break;
5799
5800 if (h2s->flags & H2_SF_BLK_ANY)
5801 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005802 }
5803
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005804 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005805 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005806 /* trim any possibly pending data after we close (extra CR-LF,
5807 * unprocessed trailers, abnormal extra data, ...)
5808 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005809 total += count;
5810 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005811 }
5812
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005813 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005814 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005815 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005816 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005817 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005818 }
5819
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005820 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005821 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005822 } else {
5823 b_del(buf, total);
5824 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005825
5826 /* The mux is full, cancel the pending tasks */
5827 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5828 (h2s->flags & H2_SF_BLK_MBUSY))
5829 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005830
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005831 /* If we're running HTX, and we read the whole buffer, then pretend
5832 * we read exactly what the caller specified, as with HTX the caller
5833 * will always give the buffer size, instead of the amount of data
5834 * available.
5835 */
5836 if (htx && !b_data(buf))
5837 total = orig_count;
5838
Olivier Houchard7505f942018-08-21 18:10:44 +02005839 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005840 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005841 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005842
Olivier Houchard7505f942018-08-21 18:10:44 +02005843 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005844 /* If we're waiting for flow control, and we got a shutr on the
5845 * connection, we will never be unlocked, so add an error on
5846 * the conn_stream.
5847 */
5848 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5849 !b_data(&h2s->h2c->dbuf) &&
5850 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5851 if (cs->flags & CS_FL_EOS)
5852 cs->flags |= CS_FL_ERROR;
5853 else
5854 cs->flags |= CS_FL_ERR_PENDING;
5855 }
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005856
5857 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) {
5858 /* Ok we managed to send something, leave the send_list if we were still there */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005859 LIST_DEL_INIT(&h2s->list);
5860 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005861 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005862}
5863
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005864/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005865static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005866{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005867 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005868 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005869 struct eb32_node *node;
5870 int fctl_cnt = 0;
5871 int send_cnt = 0;
5872 int tree_cnt = 0;
5873 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005874 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005875
5876 if (!h2c)
5877 return;
5878
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005879 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005880 fctl_cnt++;
5881
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005882 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005883 send_cnt++;
5884
Willy Tarreau3af37712018-12-18 14:34:41 +01005885 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005886 node = eb32_first(&h2c->streams_by_id);
5887 while (node) {
5888 h2s = container_of(node, struct h2s, by_id);
5889 tree_cnt++;
5890 if (!h2s->cs)
5891 orph_cnt++;
5892 node = eb32_next(node);
5893 }
5894
Willy Tarreau60f62682019-05-26 11:32:27 +02005895 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005896 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005897 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5898 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005899 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5900 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005901 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5902 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005903 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005904 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5905 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5906 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005907 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5908 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5909 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005910 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5911 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005912
5913 if (h2s) {
5914 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5915 h2s, h2s->id, h2s->flags,
5916 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5917 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5918 h2s->cs);
5919 if (h2s->cs)
5920 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5921 h2s->cs->flags, h2s->cs->data);
5922 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005923}
Willy Tarreau62f52692017-10-08 23:01:42 +02005924
5925/*******************************************************/
5926/* functions below are dedicated to the config parsers */
5927/*******************************************************/
5928
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005929/* config parser for global "tune.h2.header-table-size" */
5930static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5931 struct proxy *defpx, const char *file, int line,
5932 char **err)
5933{
5934 if (too_many_args(1, args, err, NULL))
5935 return -1;
5936
5937 h2_settings_header_table_size = atoi(args[1]);
5938 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5939 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5940 return -1;
5941 }
5942 return 0;
5943}
Willy Tarreau62f52692017-10-08 23:01:42 +02005944
Willy Tarreaue6baec02017-07-27 11:45:11 +02005945/* config parser for global "tune.h2.initial-window-size" */
5946static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5947 struct proxy *defpx, const char *file, int line,
5948 char **err)
5949{
5950 if (too_many_args(1, args, err, NULL))
5951 return -1;
5952
5953 h2_settings_initial_window_size = atoi(args[1]);
5954 if (h2_settings_initial_window_size < 0) {
5955 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5956 return -1;
5957 }
5958 return 0;
5959}
5960
Willy Tarreau5242ef82017-07-27 11:47:28 +02005961/* config parser for global "tune.h2.max-concurrent-streams" */
5962static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5963 struct proxy *defpx, const char *file, int line,
5964 char **err)
5965{
5966 if (too_many_args(1, args, err, NULL))
5967 return -1;
5968
5969 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005970 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005971 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5972 return -1;
5973 }
5974 return 0;
5975}
5976
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005977/* config parser for global "tune.h2.max-frame-size" */
5978static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5979 struct proxy *defpx, const char *file, int line,
5980 char **err)
5981{
5982 if (too_many_args(1, args, err, NULL))
5983 return -1;
5984
5985 h2_settings_max_frame_size = atoi(args[1]);
5986 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5987 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5988 return -1;
5989 }
5990 return 0;
5991}
5992
Willy Tarreau62f52692017-10-08 23:01:42 +02005993
5994/****************************************/
5995/* MUX initialization and instanciation */
5996/***************************************/
5997
5998/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005999static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02006000 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02006001 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02006002 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02006003 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02006004 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006005 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02006006 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01006007 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02006008 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01006009 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01006010 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01006011 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02006012 .shutr = h2_shutr,
6013 .shutw = h2_shutw,
Olivier Houchard198d1292019-10-25 16:19:26 +02006014 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006015 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01006016 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02006017 .name = "H2",
6018};
6019
Christopher Faulet32f61c02018-04-10 14:33:41 +02006020/* PROTO selection : this mux registers PROTO token "h2" */
6021static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02006022 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02006023
Willy Tarreau0108d902018-11-25 19:14:37 +01006024INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
6025
Christopher Faulet9b579102019-04-11 22:52:25 +02006026
6027/* The mux operations */
6028static const struct mux_ops h2_htx_ops = {
6029 .init = h2_init,
6030 .wake = h2_wake,
6031 .snd_buf = h2_snd_buf,
6032 .rcv_buf = h2_rcv_buf,
6033 .subscribe = h2_subscribe,
6034 .unsubscribe = h2_unsubscribe,
6035 .attach = h2_attach,
6036 .get_first_cs = h2_get_first_cs,
6037 .detach = h2_detach,
6038 .destroy = h2_destroy,
6039 .avail_streams = h2_avail_streams,
6040 .used_streams = h2_used_streams,
6041 .shutr = h2_shutr,
6042 .shutw = h2_shutw,
Olivier Houchard198d1292019-10-25 16:19:26 +02006043 .ctl = h2_ctl,
Christopher Faulet9b579102019-04-11 22:52:25 +02006044 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02006045 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02006046 .name = "H2",
6047};
6048
Willy Tarreauf8957272018-10-03 10:25:20 +02006049static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02006050 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02006051
6052INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
6053
Willy Tarreau62f52692017-10-08 23:01:42 +02006054/* config keyword parsers */
6055static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02006056 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02006057 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02006058 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006059 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02006060 { 0, NULL, NULL }
6061}};
6062
Willy Tarreau0108d902018-11-25 19:14:37 +01006063INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);