blob: 6fb21dd2fd6377d1b17bd7f6d4a668052d905c5d [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
147/* HTTP/2 stream flags (32 bit), in h2s->flags */
148#define H2_SF_NONE 0x00000000
149#define H2_SF_ES_RCVD 0x00000001
150#define H2_SF_ES_SENT 0x00000002
151
152#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
153#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
154
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200155/* stream flags indicating the reason the stream is blocked */
156#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
157#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
158#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
159#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
160#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
161
Willy Tarreau454f9052017-10-26 19:40:35 +0200162/* stream flags indicating how data is supposed to be sent */
163#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
164#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
165
166/* step we're currently in when sending chunks. This is needed because we may
167 * have to transfer chunks as large as a full buffer so there's no room left
168 * for size nor crlf around.
169 */
170#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
171#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
172#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
173
174#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
175
Willy Tarreau67434202017-11-06 20:20:51 +0100176#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100177#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100178
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100179#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
180
Willy Tarreau18312642017-10-11 07:57:07 +0200181/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
182 * it is being processed in the internal HTTP representation (H1 for now).
183 */
184struct h2s {
185 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100186 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200187 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200188 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200190 int32_t id; /* stream ID */
191 uint32_t flags; /* H2_SF_* */
192 int mws; /* mux window size for this stream */
193 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
194 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200195 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100196 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200197 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200198 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100199 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
200 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100202 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200203};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200204
Willy Tarreauc6405142017-09-21 20:23:50 +0200205/* descriptor for an h2 frame header */
206struct h2_fh {
207 uint32_t len; /* length, host order, 24 bits */
208 uint32_t sid; /* stream id, host order, 31 bits */
209 uint8_t ft; /* frame type */
210 uint8_t ff; /* frame flags */
211};
212
Willy Tarreau8ceae722018-11-26 11:58:30 +0100213/* the h2c connection pool */
214DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
215
216/* the h2s stream pool */
217DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
218
Willy Tarreaudc572362018-12-12 08:08:05 +0100219/* The default connection window size is 65535, it may only be enlarged using
220 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
221 * we'll pretend we already received the difference between the two to send
222 * an equivalent window update to enlarge it to 2G-1.
223 */
224#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
225
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226/* a few settings from the global section */
227static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200228static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100229static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100230static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau2a856182017-05-16 15:20:39 +0200232/* a dmumy closed stream */
233static const struct h2s *h2_closed_stream = &(const struct h2s){
234 .cs = NULL,
235 .h2c = NULL,
236 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100237 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100238 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200239 .id = 0,
240};
241
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100242/* a dmumy closed stream returning a PROTOCOL_ERROR error */
243static const struct h2s *h2_error_stream = &(const struct h2s){
244 .cs = NULL,
245 .h2c = NULL,
246 .st = H2_SS_CLOSED,
247 .errcode = H2_ERR_PROTOCOL_ERROR,
248 .flags = 0,
249 .id = 0,
250};
251
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100252/* a dmumy closed stream returning a REFUSED_STREAM error */
253static const struct h2s *h2_refused_stream = &(const struct h2s){
254 .cs = NULL,
255 .h2c = NULL,
256 .st = H2_SS_CLOSED,
257 .errcode = H2_ERR_REFUSED_STREAM,
258 .flags = 0,
259 .id = 0,
260};
261
Willy Tarreau2a856182017-05-16 15:20:39 +0200262/* and a dummy idle stream for use with any unannounced stream */
263static const struct h2s *h2_idle_stream = &(const struct h2s){
264 .cs = NULL,
265 .h2c = NULL,
266 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100267 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200268 .id = 0,
269};
270
Olivier Houchard9f6af332018-05-25 14:04:04 +0200271static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200272static int h2_send(struct h2c *h2c);
273static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200274static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200275static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100276static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100277static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100278static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200279static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100280static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100281static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200282
Olivier Houchard7a977432019-03-21 15:47:13 +0100283static __inline int
284h2c_is_dead(struct h2c *h2c)
285{
286 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
287 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
288 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
289 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
290 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
291 (conn_xprt_read0_pending(h2c->conn) ||
292 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
293 return 1;
294
295 return 0;
296
297}
298
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200299/*****************************************************/
300/* functions below are for dynamic buffer management */
301/*****************************************************/
302
Willy Tarreau315d8072017-12-10 22:17:57 +0100303/* indicates whether or not the we may call the h2_recv() function to attempt
304 * to receive data into the buffer and/or demux pending data. The condition is
305 * a bit complex due to some API limits for now. The rules are the following :
306 * - if an error or a shutdown was detected on the connection and the buffer
307 * is empty, we must not attempt to receive
308 * - if the demux buf failed to be allocated, we must not try to receive and
309 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100310 * - if no flag indicates a blocking condition, we may attempt to receive,
311 * regardless of whether the demux buffer is full or not, so that only
312 * de demux part decides whether or not to block. This is needed because
313 * the connection API indeed prevents us from re-enabling receipt that is
314 * already enabled in a polled state, so we must always immediately stop
315 * as soon as the demux can't proceed so as never to hit an end of read
316 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100317 * - otherwise must may not attempt
318 */
319static inline int h2_recv_allowed(const struct h2c *h2c)
320{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100322 (h2c->st0 >= H2_CS_ERROR ||
323 h2c->conn->flags & CO_FL_ERROR ||
324 conn_xprt_read0_pending(h2c->conn)))
325 return 0;
326
327 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100328 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100329 return 1;
330
331 return 0;
332}
333
Willy Tarreau47b515a2018-12-21 16:09:41 +0100334/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200335static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100336{
337 if (!h2_recv_allowed(h2c))
338 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200339 if ((!consider_buffer || !b_data(&h2c->dbuf))
340 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100341 return;
342 tasklet_wakeup(h2c->wait_event.task);
343}
344
345
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100346/* returns true if the front connection has too many conn_streams attached */
347static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200348{
Willy Tarreaua8754662018-12-23 20:43:58 +0100349 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200350}
351
Willy Tarreau44e973f2018-03-01 17:49:30 +0100352/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
353 * flags are used to figure what buffer was requested. It returns 1 if the
354 * allocation succeeds, in which case the connection is woken up, or 0 if it's
355 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200356 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100357static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200358{
359 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100360 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200361
Willy Tarreau44e973f2018-03-01 17:49:30 +0100362 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200363 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200364 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200365 return 1;
366 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200367
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
369 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200370
371 if (h2c->flags & H2_CF_DEM_MROOM) {
372 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200373 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200374 }
Willy Tarreau14398122017-09-22 14:26:04 +0200375 return 1;
376 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100377
378 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
379 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200380 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100381 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200382 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100383 return 1;
384 }
385
Willy Tarreau14398122017-09-22 14:26:04 +0200386 return 0;
387}
388
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200389static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200390{
391 struct buffer *buf = NULL;
392
Willy Tarreau44e973f2018-03-01 17:49:30 +0100393 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
394 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
395 h2c->buf_wait.target = h2c;
396 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100397 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100398 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100399 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200400 __conn_xprt_stop_recv(h2c->conn);
401 }
402 return buf;
403}
404
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200405static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200406{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200407 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100408 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200409 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200410 }
411}
412
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100413/* returns the number of allocatable outgoing streams for the connection taking
414 * the last_sid and the reserved ones into account.
415 */
416static inline int h2_streams_left(const struct h2c *h2c)
417{
418 int ret;
419
420 /* consider the number of outgoing streams we're allowed to create before
421 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
422 * nb_reserved is the number of streams which don't yet have an ID.
423 */
424 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
425 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
426 if (ret < 0)
427 ret = 0;
428 return ret;
429}
430
Willy Tarreau00f18a32019-01-26 12:19:01 +0100431/* returns the number of streams in use on a connection to figure if it's
432 * idle or not. We check nb_cs and not nb_streams as the caller will want
433 * to know if it was the last one after a detach().
434 */
435static int h2_used_streams(struct connection *conn)
436{
437 struct h2c *h2c = conn->ctx;
438
439 return h2c->nb_cs;
440}
441
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100442/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100443static int h2_avail_streams(struct connection *conn)
444{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100445 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100446 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100447 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100448
Willy Tarreau6afec462019-01-28 06:40:19 +0100449 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
450 * streams on the connection.
451 */
452 if (h2c->last_sid >= 0)
453 return 0;
454
Willy Tarreau86949782019-01-31 10:42:05 +0100455 /* note: may be negative if a SETTINGS frame changes the limit */
456 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100457
458 /* we must also consider the limit imposed by stream IDs */
459 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100460 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100461 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100462 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
463 ret1 = MIN(ret1, ret2);
464 }
465 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100466}
467
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200468
Willy Tarreau62f52692017-10-08 23:01:42 +0200469/*****************************************************************/
470/* functions below are dedicated to the mux setup and management */
471/*****************************************************************/
472
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200473/* Initialize the mux once it's attached. For outgoing connections, the context
474 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200475 * connections from the fact that the context is still NULL (even during mux
476 * upgrades). <input> is always used as Input buffer and may contain data. It is
477 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200478 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200479static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
480 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481{
482 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100483 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200484
Willy Tarreaubafbe012017-11-24 17:34:44 +0100485 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200486 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200487 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200488
Christopher Faulete9b70722019-04-08 10:46:02 +0200489 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200490 h2c->flags = H2_CF_IS_BACK;
491 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
492 if (tick_isset(prx->timeout.serverfin))
493 h2c->shut_timeout = prx->timeout.serverfin;
494 } else {
495 h2c->flags = H2_CF_NONE;
496 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
497 if (tick_isset(prx->timeout.clientfin))
498 h2c->shut_timeout = prx->timeout.clientfin;
499 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100500
Willy Tarreau0b37d652018-10-03 10:33:02 +0200501 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100502 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100503 if (tick_isset(h2c->timeout)) {
504 t = task_new(tid_bit);
505 if (!t)
506 goto fail;
507
508 h2c->task = t;
509 t->process = h2_timeout_task;
510 t->context = h2c;
511 t->expire = tick_add(now_ms, h2c->timeout);
512 }
Willy Tarreauea392822017-10-31 10:02:25 +0100513
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200514 h2c->wait_event.task = tasklet_new();
515 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200516 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200517 h2c->wait_event.task->process = h2_io_cb;
518 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100519 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200520
Willy Tarreau32218eb2017-09-22 08:07:25 +0200521 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
522 if (!h2c->ddht)
523 goto fail;
524
525 /* Initialise the context. */
526 h2c->st0 = H2_CS_PREFACE;
527 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100528 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 h2c->max_id = -1;
530 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100531 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200532 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100533 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200534 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100535 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100536 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200537
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200538 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539 h2c->dsi = -1;
540 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100541
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 h2c->last_sid = -1;
543
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200544 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200545 h2c->miw = 65535; /* mux initial window size */
546 h2c->mws = 65535; /* mux window size */
547 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200548 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200549 LIST_INIT(&h2c->send_list);
550 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200551 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100552 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553
Willy Tarreau3f133572017-10-31 19:21:06 +0100554 if (t)
555 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100556
Willy Tarreau01b44822018-10-03 14:26:37 +0200557 if (h2c->flags & H2_CF_IS_BACK) {
558 /* FIXME: this is temporary, for outgoing connections we need
559 * to immediately allocate a stream until the code is modified
560 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100561 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200562 */
563 struct h2s *h2s;
564
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100565 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200566 if (!h2s)
567 goto fail_stream;
568 }
569
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100570 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200571
Willy Tarreau0f383582018-10-03 14:22:21 +0200572 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200573 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200574 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200575 fail_stream:
576 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200577 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100578 if (t)
Olivier Houchard3f795f72019-04-17 22:51:06 +0200579 task_destroy(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200580 if (h2c->wait_event.task)
581 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100582 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200583 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200584 return -1;
585}
586
Willy Tarreau751f2d02018-10-05 09:35:00 +0200587/* returns the next allocatable outgoing stream ID for the H2 connection, or
588 * -1 if no more is allocatable.
589 */
590static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
591{
592 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100593
594 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200595 id = -1;
596 return id;
597}
598
Willy Tarreau2373acc2017-10-12 17:35:14 +0200599/* returns the stream associated with id <id> or NULL if not found */
600static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
601{
602 struct eb32_node *node;
603
Willy Tarreau751f2d02018-10-05 09:35:00 +0200604 if (id == 0)
605 return (struct h2s *)h2_closed_stream;
606
Willy Tarreau2a856182017-05-16 15:20:39 +0200607 if (id > h2c->max_id)
608 return (struct h2s *)h2_idle_stream;
609
Willy Tarreau2373acc2017-10-12 17:35:14 +0200610 node = eb32_lookup(&h2c->streams_by_id, id);
611 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200612 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200613
614 return container_of(node, struct h2s, by_id);
615}
616
Christopher Faulet73c12072019-04-08 11:23:22 +0200617/* release function. This one should be called to free all resources allocated
618 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200619 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200620static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200621{
Christopher Faulet61840e72019-04-15 09:33:32 +0200622 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200623
Willy Tarreau32218eb2017-09-22 08:07:25 +0200624 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200625 /* The connection must be aattached to this mux to be released */
626 if (h2c->conn && h2c->conn->ctx == h2c)
627 conn = h2c->conn;
628
Willy Tarreau32218eb2017-09-22 08:07:25 +0200629 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200630
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100631 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100632 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100633 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200634
Willy Tarreau44e973f2018-03-01 17:49:30 +0100635 h2_release_buf(h2c, &h2c->dbuf);
636 h2_release_buf(h2c, &h2c->mbuf);
637
Willy Tarreauea392822017-10-31 10:02:25 +0100638 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200639 h2c->task->context = NULL;
640 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100641 h2c->task = NULL;
642 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200643 if (h2c->wait_event.task)
644 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200645 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100646 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200647 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100648
Willy Tarreaubafbe012017-11-24 17:34:44 +0100649 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200650 }
651
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200652 if (conn) {
653 conn->mux = NULL;
654 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200655
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200656 conn_stop_tracking(conn);
657 conn_full_close(conn);
658 if (conn->destroy_cb)
659 conn->destroy_cb(conn);
660 conn_free(conn);
661 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200662}
663
664
Willy Tarreau71681172017-10-23 14:39:06 +0200665/******************************************************/
666/* functions below are for the H2 protocol processing */
667/******************************************************/
668
669/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100670static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200671{
672 return h2s ? h2s->id : 0;
673}
674
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200675/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100676static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200677{
678 if (h2c->msi < 0)
679 return 0;
680
681 if (h2c->msi == h2s_id(h2s))
682 return 0;
683
684 return 1;
685}
686
Willy Tarreau741d6df2017-10-17 08:00:59 +0200687/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100688static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200689{
690 h2c->errcode = err;
691 h2c->st0 = H2_CS_ERROR;
692}
693
Willy Tarreau175cebb2019-01-24 10:02:24 +0100694/* marks an error on the stream. It may also update an already closed stream
695 * (e.g. to report an error after an RST was received).
696 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100697static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200698{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100699 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200700 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100701 if (h2s->st < H2_SS_ERROR)
702 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100703 if (h2s->cs)
704 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200705 }
706}
707
Willy Tarreau7e094452018-12-19 18:08:52 +0100708/* attempt to notify the data layer of recv availability */
709static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
710{
711 struct wait_event *sw;
712
713 if (h2s->recv_wait) {
714 sw = h2s->recv_wait;
715 sw->events &= ~SUB_RETRY_RECV;
716 tasklet_wakeup(sw->task);
717 h2s->recv_wait = NULL;
718 }
719}
720
721/* attempt to notify the data layer of send availability */
722static void __maybe_unused h2s_notify_send(struct h2s *h2s)
723{
724 struct wait_event *sw;
725
Olivier Houchard998410a2019-04-15 19:23:37 +0200726 if (h2s->send_wait && LIST_ISEMPTY(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100727 sw = h2s->send_wait;
728 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100729 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100730 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100731 }
732}
733
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100734/* alerts the data layer, trying to wake it up by all means, following
735 * this sequence :
736 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
737 * - if its subscribed to send, then it's woken up for send
738 * - if it was subscribed to neither, its ->wake() callback is called
739 * It is safe to call this function with a closed stream which doesn't have a
740 * conn_stream anymore.
741 */
742static void __maybe_unused h2s_alert(struct h2s *h2s)
743{
744 if (h2s->recv_wait || h2s->send_wait) {
745 h2s_notify_recv(h2s);
746 h2s_notify_send(h2s);
747 }
748 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
749 h2s->cs->data_cb->wake(h2s->cs);
750}
751
Willy Tarreaue4820742017-07-27 13:37:23 +0200752/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100753static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200754{
755 uint8_t *out = frame;
756
757 *out = len >> 16;
758 write_n16(out + 1, len);
759}
760
Willy Tarreau54c15062017-10-10 17:10:03 +0200761/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
762 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
763 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200764 * available in the buffer's input prior to calling this function. The buffer
765 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200766 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100767static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200768 const struct buffer *b, int o)
769{
Willy Tarreau591d4452018-06-15 17:21:00 +0200770 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 +0200771}
772
Willy Tarreau1f094672017-11-20 21:27:45 +0100773static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200774{
Willy Tarreau591d4452018-06-15 17:21:00 +0200775 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200776}
777
Willy Tarreau1f094672017-11-20 21:27:45 +0100778static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200779{
Willy Tarreau591d4452018-06-15 17:21:00 +0200780 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200781}
782
Willy Tarreau1f094672017-11-20 21:27:45 +0100783static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200784{
Willy Tarreau591d4452018-06-15 17:21:00 +0200785 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200786}
787
788
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100789/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
790 * The algorithm is not obvious. It turns out that H2 headers are neither
791 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
792 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200793 *
794 * b0 b1 b2 b3 b4 b5..b8
795 * +----------+---------+--------+----+----+----------------------+
796 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
797 * +----------+---------+--------+----+----+----------------------+
798 *
799 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
800 * we get the sid properly aligned and ordered, and 16 bits of len properly
801 * ordered as well. The type and flags can be extracted using bit shifts from
802 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200803 * Returns zero if some bytes are missing, otherwise non-zero on success. The
804 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200805 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100806static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200807{
808 uint64_t w;
809
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100810 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200811 return 0;
812
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100813 w = h2_get_n64(b, o + 1);
814 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200815 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
816 h->ff = w >> 32;
817 h->ft = w >> 40;
818 h->len += w >> 48;
819 return 1;
820}
821
822/* skip the next 9 bytes corresponding to the frame header possibly parsed by
823 * h2_peek_frame_hdr() above.
824 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100825static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200826{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200827 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200828}
829
830/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100831static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200832{
833 int ret;
834
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100835 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200836 if (ret > 0)
837 h2_skip_frame_hdr(b);
838 return ret;
839}
840
Willy Tarreau00dd0782018-03-01 16:31:34 +0100841/* marks stream <h2s> as CLOSED and decrement the number of active streams for
842 * its connection if the stream was not yet closed. Please use this exclusively
843 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100844 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100845static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100846{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100847 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100848 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100849 if (!h2s->id)
850 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100851 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100852 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100853 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
854 h2s_notify_recv(h2s);
855 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100856 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100857 h2s->st = H2_SS_CLOSED;
858}
859
Willy Tarreau71049cc2018-03-28 13:56:39 +0200860/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
861static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100862{
863 h2s_close(h2s);
864 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200865 if (b_size(&h2s->rxbuf)) {
866 b_free(&h2s->rxbuf);
867 offer_buffers(NULL, tasks_run_queue);
868 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200869 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100870 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200871 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100872 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800873 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200874 * reference left would be in the h2c send_list/fctl_list, and if
875 * we're in it, we're getting out anyway
876 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100877 LIST_DEL_INIT(&h2s->list);
Olivier Houchard998410a2019-04-15 19:23:37 +0200878 if (!LIST_ISEMPTY(&h2s->sending_list)) {
879 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
880 LIST_DEL_INIT(&h2s->sending_list);
881 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200882 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100883 pool_free(pool_head_h2s, h2s);
884}
885
Willy Tarreaua8e49542018-10-03 18:53:55 +0200886/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
887 * stream tree. In case of error, nothing is added and NULL is returned. The
888 * causes of errors can be any failed memory allocation. The caller is
889 * responsible for checking if the connection may support an extra stream
890 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200891 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200892static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200893{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200894 struct h2s *h2s;
895
Willy Tarreaubafbe012017-11-24 17:34:44 +0100896 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200897 if (!h2s)
898 goto out;
899
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200900 h2s->wait_event.task = tasklet_new();
901 if (!h2s->wait_event.task) {
902 pool_free(pool_head_h2s, h2s);
903 goto out;
904 }
905 h2s->send_wait = NULL;
906 h2s->recv_wait = NULL;
907 h2s->wait_event.task->process = h2_deferred_shut;
908 h2s->wait_event.task->context = h2s;
909 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100910 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200911 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100912 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200913 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200914 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200915 h2s->mws = h2c->miw;
916 h2s->flags = H2_SF_NONE;
917 h2s->errcode = H2_ERR_NO_ERROR;
918 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200919 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100920 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200921 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200922
923 if (h2c->flags & H2_CF_IS_BACK) {
924 h1m_init_req(&h2s->h1m);
925 h2s->h1m.err_pos = -1; // don't care about errors on the request path
926 h2s->h1m.flags |= H1_MF_TOLOWER;
927 } else {
928 h1m_init_res(&h2s->h1m);
929 h2s->h1m.err_pos = -1; // don't care about errors on the response path
930 h2s->h1m.flags |= H1_MF_TOLOWER;
931 }
932
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200933 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200934 if (id > 0)
935 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100936 else
937 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200938
939 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100940 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100941 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200942
943 return h2s;
944
945 out_free_h2s:
946 pool_free(pool_head_h2s, h2s);
947 out:
948 return NULL;
949}
950
951/* creates a new stream <id> on the h2c connection and returns it, or NULL in
952 * case of memory allocation error.
953 */
954static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
955{
956 struct session *sess = h2c->conn->owner;
957 struct conn_stream *cs;
958 struct h2s *h2s;
959
960 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
961 goto out;
962
963 h2s = h2s_new(h2c, id);
964 if (!h2s)
965 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200966
967 cs = cs_new(h2c->conn);
968 if (!cs)
969 goto out_close;
970
Olivier Houchard746fb772018-12-15 19:42:00 +0100971 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200972 h2s->cs = cs;
973 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200974 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200975
976 if (stream_create_from_cs(cs) < 0)
977 goto out_free_cs;
978
Willy Tarreau590a0512018-09-05 11:56:48 +0200979 /* We want the accept date presented to the next stream to be the one
980 * we have now, the handshake time to be null (since the next stream
981 * is not delayed by a handshake), and the idle time to count since
982 * right now.
983 */
984 sess->accept_date = date;
985 sess->tv_accept = now;
986 sess->t_handshake = 0;
987
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200988 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100989 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200990 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200991 return h2s;
992
993 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200994 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200995 cs_free(cs);
996 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200997 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200998 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200999 sess_log(sess);
1000 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001001}
1002
Willy Tarreau751f2d02018-10-05 09:35:00 +02001003/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1004 * and returns it, or NULL in case of memory allocation error or if the highest
1005 * possible stream ID was reached.
1006 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001007static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001008{
1009 struct h2s *h2s = NULL;
1010
Willy Tarreau86949782019-01-31 10:42:05 +01001011 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001012 goto out;
1013
Willy Tarreaua80dca82019-01-24 17:08:28 +01001014 if (h2_streams_left(h2c) < 1)
1015 goto out;
1016
Willy Tarreau751f2d02018-10-05 09:35:00 +02001017 /* Defer choosing the ID until we send the first message to create the stream */
1018 h2s = h2s_new(h2c, 0);
1019 if (!h2s)
1020 goto out;
1021
1022 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001023 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001024 cs->ctx = h2s;
1025 h2c->nb_cs++;
1026
Willy Tarreau751f2d02018-10-05 09:35:00 +02001027 out:
1028 return h2s;
1029}
1030
Willy Tarreaube5b7152017-09-25 16:25:39 +02001031/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1032 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1033 * the various settings codes.
1034 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001035static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001036{
1037 struct buffer *res;
1038 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001039 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001040 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001041 int ret;
1042
1043 if (h2c_mux_busy(h2c, NULL)) {
1044 h2c->flags |= H2_CF_DEM_MBUSY;
1045 return 0;
1046 }
1047
Willy Tarreau44e973f2018-03-01 17:49:30 +01001048 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001049 if (!res) {
1050 h2c->flags |= H2_CF_MUX_MALLOC;
1051 h2c->flags |= H2_CF_DEM_MROOM;
1052 return 0;
1053 }
1054
1055 chunk_init(&buf, buf_data, sizeof(buf_data));
1056 chunk_memcpy(&buf,
1057 "\x00\x00\x00" /* length : 0 for now */
1058 "\x04\x00" /* type : 4 (settings), flags : 0 */
1059 "\x00\x00\x00\x00", /* stream ID : 0 */
1060 9);
1061
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001062 if (h2c->flags & H2_CF_IS_BACK) {
1063 /* send settings_enable_push=0 */
1064 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1065 }
1066
Willy Tarreaube5b7152017-09-25 16:25:39 +02001067 if (h2_settings_header_table_size != 4096) {
1068 char str[6] = "\x00\x01"; /* header_table_size */
1069
1070 write_n32(str + 2, h2_settings_header_table_size);
1071 chunk_memcat(&buf, str, 6);
1072 }
1073
1074 if (h2_settings_initial_window_size != 65535) {
1075 char str[6] = "\x00\x04"; /* initial_window_size */
1076
1077 write_n32(str + 2, h2_settings_initial_window_size);
1078 chunk_memcat(&buf, str, 6);
1079 }
1080
1081 if (h2_settings_max_concurrent_streams != 0) {
1082 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1083
1084 /* Note: 0 means "unlimited" for haproxy's config but not for
1085 * the protocol, so never send this value!
1086 */
1087 write_n32(str + 2, h2_settings_max_concurrent_streams);
1088 chunk_memcat(&buf, str, 6);
1089 }
1090
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001091 mfs = h2_settings_max_frame_size;
1092 if (mfs > global.tune.bufsize)
1093 mfs = global.tune.bufsize;
1094
1095 if (!mfs)
1096 mfs = global.tune.bufsize;
1097
1098 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001099 char str[6] = "\x00\x05"; /* max_frame_size */
1100
1101 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1102 * match bufsize - rewrite size, but at the moment it seems
1103 * that clients don't take care of it.
1104 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001105 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001106 chunk_memcat(&buf, str, 6);
1107 }
1108
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001109 h2_set_frame_size(buf.area, buf.data - 9);
1110 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001111 if (unlikely(ret <= 0)) {
1112 if (!ret) {
1113 h2c->flags |= H2_CF_MUX_MFULL;
1114 h2c->flags |= H2_CF_DEM_MROOM;
1115 return 0;
1116 }
1117 else {
1118 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1119 return 0;
1120 }
1121 }
1122 return ret;
1123}
1124
Willy Tarreau52eed752017-09-22 15:05:09 +02001125/* Try to receive a connection preface, then upon success try to send our
1126 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1127 * missing data. It may return an error in h2c.
1128 */
1129static int h2c_frt_recv_preface(struct h2c *h2c)
1130{
1131 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001132 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001133
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001134 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001135
1136 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001137 if (ret1 < 0)
1138 sess_log(h2c->conn->owner);
1139
Willy Tarreau52eed752017-09-22 15:05:09 +02001140 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1141 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1142 return 0;
1143 }
1144
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001145 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001146 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001147 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001148
Willy Tarreaube5b7152017-09-25 16:25:39 +02001149 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001150}
1151
Willy Tarreau01b44822018-10-03 14:26:37 +02001152/* Try to send a connection preface, then upon success try to send our
1153 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1154 * missing data. It may return an error in h2c.
1155 */
1156static int h2c_bck_send_preface(struct h2c *h2c)
1157{
1158 struct buffer *res;
1159
1160 if (h2c_mux_busy(h2c, NULL)) {
1161 h2c->flags |= H2_CF_DEM_MBUSY;
1162 return 0;
1163 }
1164
1165 res = h2_get_buf(h2c, &h2c->mbuf);
1166 if (!res) {
1167 h2c->flags |= H2_CF_MUX_MALLOC;
1168 h2c->flags |= H2_CF_DEM_MROOM;
1169 return 0;
1170 }
1171
1172 if (!b_data(res)) {
1173 /* preface not yet sent */
1174 b_istput(res, ist(H2_CONN_PREFACE));
1175 }
1176
1177 return h2c_send_settings(h2c);
1178}
1179
Willy Tarreau081d4722017-05-16 21:51:05 +02001180/* try to send a GOAWAY frame on the connection to report an error or a graceful
1181 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1182 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1183 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1184 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1185 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1186 * on unrecoverable failure. It will not attempt to send one again in this last
1187 * case so that it is safe to use h2c_error() to report such errors.
1188 */
1189static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1190{
1191 struct buffer *res;
1192 char str[17];
1193 int ret;
1194
1195 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1196 return 1; // claim that it worked
1197
1198 if (h2c_mux_busy(h2c, h2s)) {
1199 if (h2s)
1200 h2s->flags |= H2_SF_BLK_MBUSY;
1201 else
1202 h2c->flags |= H2_CF_DEM_MBUSY;
1203 return 0;
1204 }
1205
Willy Tarreau44e973f2018-03-01 17:49:30 +01001206 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001207 if (!res) {
1208 h2c->flags |= H2_CF_MUX_MALLOC;
1209 if (h2s)
1210 h2s->flags |= H2_SF_BLK_MROOM;
1211 else
1212 h2c->flags |= H2_CF_DEM_MROOM;
1213 return 0;
1214 }
1215
1216 /* len: 8, type: 7, flags: none, sid: 0 */
1217 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1218
1219 if (h2c->last_sid < 0)
1220 h2c->last_sid = h2c->max_id;
1221
1222 write_n32(str + 9, h2c->last_sid);
1223 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001224 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001225 if (unlikely(ret <= 0)) {
1226 if (!ret) {
1227 h2c->flags |= H2_CF_MUX_MFULL;
1228 if (h2s)
1229 h2s->flags |= H2_SF_BLK_MROOM;
1230 else
1231 h2c->flags |= H2_CF_DEM_MROOM;
1232 return 0;
1233 }
1234 else {
1235 /* we cannot report this error using GOAWAY, so we mark
1236 * it and claim a success.
1237 */
1238 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1239 h2c->flags |= H2_CF_GOAWAY_FAILED;
1240 return 1;
1241 }
1242 }
1243 h2c->flags |= H2_CF_GOAWAY_SENT;
1244 return ret;
1245}
1246
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001247/* Try to send an RST_STREAM frame on the connection for the indicated stream
1248 * during mux operations. This stream must be valid and cannot be closed
1249 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1250 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1251 * not yet.
1252 *
1253 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1254 * to write the message, it subscribes the stream to future notifications.
1255 */
1256static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1257{
1258 struct buffer *res;
1259 char str[13];
1260 int ret;
1261
1262 if (!h2s || h2s->st == H2_SS_CLOSED)
1263 return 1;
1264
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001265 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1266 * RST_STREAM in response to a RST_STREAM frame.
1267 */
1268 if (h2c->dft == H2_FT_RST_STREAM) {
1269 ret = 1;
1270 goto ignore;
1271 }
1272
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001273 if (h2c_mux_busy(h2c, h2s)) {
1274 h2s->flags |= H2_SF_BLK_MBUSY;
1275 return 0;
1276 }
1277
Willy Tarreau44e973f2018-03-01 17:49:30 +01001278 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001279 if (!res) {
1280 h2c->flags |= H2_CF_MUX_MALLOC;
1281 h2s->flags |= H2_SF_BLK_MROOM;
1282 return 0;
1283 }
1284
1285 /* len: 4, type: 3, flags: none */
1286 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1287 write_n32(str + 5, h2s->id);
1288 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001289 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001290
1291 if (unlikely(ret <= 0)) {
1292 if (!ret) {
1293 h2c->flags |= H2_CF_MUX_MFULL;
1294 h2s->flags |= H2_SF_BLK_MROOM;
1295 return 0;
1296 }
1297 else {
1298 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1299 return 0;
1300 }
1301 }
1302
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001303 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001304 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001305 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001306 return ret;
1307}
1308
1309/* Try to send an RST_STREAM frame on the connection for the stream being
1310 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001311 * error code, even if the stream is one of the dummy ones, and will update
1312 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001313 *
1314 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1315 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001316 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001317 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001318 */
1319static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1320{
1321 struct buffer *res;
1322 char str[13];
1323 int ret;
1324
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001325 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1326 * RST_STREAM in response to a RST_STREAM frame.
1327 */
1328 if (h2c->dft == H2_FT_RST_STREAM) {
1329 ret = 1;
1330 goto ignore;
1331 }
1332
Willy Tarreau27a84c92017-10-17 08:10:17 +02001333 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001334 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001335 return 0;
1336 }
1337
Willy Tarreau44e973f2018-03-01 17:49:30 +01001338 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001339 if (!res) {
1340 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001341 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001342 return 0;
1343 }
1344
1345 /* len: 4, type: 3, flags: none */
1346 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001347
Willy Tarreau27a84c92017-10-17 08:10:17 +02001348 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001349 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001350 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001351
Willy Tarreau27a84c92017-10-17 08:10:17 +02001352 if (unlikely(ret <= 0)) {
1353 if (!ret) {
1354 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001355 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001356 return 0;
1357 }
1358 else {
1359 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1360 return 0;
1361 }
1362 }
1363
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001364 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001365 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001366 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001367 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001368 }
1369
Willy Tarreau27a84c92017-10-17 08:10:17 +02001370 return ret;
1371}
1372
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001373/* try to send an empty DATA frame with the ES flag set to notify about the
1374 * end of stream and match a shutdown(write). If an ES was already sent as
1375 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1376 * on success or zero if nothing was done. In case of lack of room to write the
1377 * message, it subscribes the requesting stream to future notifications.
1378 */
1379static int h2_send_empty_data_es(struct h2s *h2s)
1380{
1381 struct h2c *h2c = h2s->h2c;
1382 struct buffer *res;
1383 char str[9];
1384 int ret;
1385
Willy Tarreau721c9742017-11-07 11:05:42 +01001386 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001387 return 1;
1388
1389 if (h2c_mux_busy(h2c, h2s)) {
1390 h2s->flags |= H2_SF_BLK_MBUSY;
1391 return 0;
1392 }
1393
Willy Tarreau44e973f2018-03-01 17:49:30 +01001394 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001395 if (!res) {
1396 h2c->flags |= H2_CF_MUX_MALLOC;
1397 h2s->flags |= H2_SF_BLK_MROOM;
1398 return 0;
1399 }
1400
1401 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1402 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1403 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001404 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001405 if (likely(ret > 0)) {
1406 h2s->flags |= H2_SF_ES_SENT;
1407 }
1408 else if (!ret) {
1409 h2c->flags |= H2_CF_MUX_MFULL;
1410 h2s->flags |= H2_SF_BLK_MROOM;
1411 return 0;
1412 }
1413 else {
1414 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1415 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001416 }
1417 return ret;
1418}
1419
Christopher Fauletf02ca002019-03-07 16:21:34 +01001420/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1421 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1422 * connection. The stream's state is automatically updated accordingly. If the
1423 * stream is orphaned, it is destroyed.
1424 */
1425static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1426{
1427 if (!h2s->cs) {
1428 /* this stream was already orphaned */
1429 h2s_destroy(h2s);
1430 return;
1431 }
1432
1433 h2s->cs->flags |= flags;
1434 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1435 h2s->cs->flags |= CS_FL_ERROR;
1436
1437 h2s_alert(h2s);
1438
1439 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1440 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001441 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001442 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001443 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001444 h2s_close(h2s);
1445}
1446
1447/* wake the streams attached to the connection, whose id is greater than <last>
1448 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001449 */
1450static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1451{
1452 struct eb32_node *node;
1453 struct h2s *h2s;
1454
1455 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001456 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001457
1458 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001459 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001460
Christopher Fauletf02ca002019-03-07 16:21:34 +01001461 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001462 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1463 while (node) {
1464 h2s = container_of(node, struct h2s, by_id);
1465 if (h2s->id <= last)
1466 break;
1467 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001468 h2s_wake_one_stream(h2s, flags);
1469 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001470
Christopher Fauletf02ca002019-03-07 16:21:34 +01001471 /* Wake all streams with unassigned ID (ID == 0) */
1472 node = eb32_lookup(&h2c->streams_by_id, 0);
1473 while (node) {
1474 h2s = container_of(node, struct h2s, by_id);
1475 if (h2s->id > 0)
1476 break;
1477 node = eb32_next(node);
1478 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001479 }
1480}
1481
Willy Tarreau3421aba2017-07-27 15:41:03 +02001482/* Increase all streams' outgoing window size by the difference passed in
1483 * argument. This is needed upon receipt of the settings frame if the initial
1484 * window size is different. The difference may be negative and the resulting
1485 * window size as well, for the time it takes to receive some window updates.
1486 */
1487static void h2c_update_all_ws(struct h2c *h2c, int diff)
1488{
1489 struct h2s *h2s;
1490 struct eb32_node *node;
1491
1492 if (!diff)
1493 return;
1494
1495 node = eb32_first(&h2c->streams_by_id);
1496 while (node) {
1497 h2s = container_of(node, struct h2s, by_id);
1498 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001499
1500 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1501 h2s->flags &= ~H2_SF_BLK_SFCTL;
1502 if (h2s->send_wait)
1503 LIST_ADDQ(&h2c->send_list, &h2s->list);
1504
1505 }
1506
Willy Tarreau3421aba2017-07-27 15:41:03 +02001507 node = eb32_next(node);
1508 }
1509}
1510
1511/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1512 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001513 * return an error in h2c. The caller must have already verified frame length
1514 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001515 */
1516static int h2c_handle_settings(struct h2c *h2c)
1517{
1518 unsigned int offset;
1519 int error;
1520
1521 if (h2c->dff & H2_F_SETTINGS_ACK) {
1522 if (h2c->dfl) {
1523 error = H2_ERR_FRAME_SIZE_ERROR;
1524 goto fail;
1525 }
1526 return 1;
1527 }
1528
Willy Tarreau3421aba2017-07-27 15:41:03 +02001529 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001530 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001531 return 0;
1532
1533 /* parse the frame */
1534 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001535 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1536 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001537
1538 switch (type) {
1539 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1540 /* we need to update all existing streams with the
1541 * difference from the previous iws.
1542 */
1543 if (arg < 0) { // RFC7540#6.5.2
1544 error = H2_ERR_FLOW_CONTROL_ERROR;
1545 goto fail;
1546 }
1547 h2c_update_all_ws(h2c, arg - h2c->miw);
1548 h2c->miw = arg;
1549 break;
1550 case H2_SETTINGS_MAX_FRAME_SIZE:
1551 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1552 error = H2_ERR_PROTOCOL_ERROR;
1553 goto fail;
1554 }
1555 h2c->mfs = arg;
1556 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001557 case H2_SETTINGS_ENABLE_PUSH:
1558 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1559 error = H2_ERR_PROTOCOL_ERROR;
1560 goto fail;
1561 }
1562 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001563 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1564 if (h2c->flags & H2_CF_IS_BACK) {
1565 /* the limit is only for the backend; for the frontend it is our limit */
1566 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1567 arg = h2_settings_max_concurrent_streams;
1568 h2c->streams_limit = arg;
1569 }
1570 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001571 }
1572 }
1573
1574 /* need to ACK this frame now */
1575 h2c->st0 = H2_CS_FRAME_A;
1576 return 1;
1577 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001578 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001579 h2c_error(h2c, error);
1580 return 0;
1581}
1582
1583/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1584 * success or one of the h2_status values.
1585 */
1586static int h2c_ack_settings(struct h2c *h2c)
1587{
1588 struct buffer *res;
1589 char str[9];
1590 int ret = -1;
1591
1592 if (h2c_mux_busy(h2c, NULL)) {
1593 h2c->flags |= H2_CF_DEM_MBUSY;
1594 return 0;
1595 }
1596
Willy Tarreau44e973f2018-03-01 17:49:30 +01001597 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001598 if (!res) {
1599 h2c->flags |= H2_CF_MUX_MALLOC;
1600 h2c->flags |= H2_CF_DEM_MROOM;
1601 return 0;
1602 }
1603
1604 memcpy(str,
1605 "\x00\x00\x00" /* length : 0 (no data) */
1606 "\x04" "\x01" /* type : 4, flags : ACK */
1607 "\x00\x00\x00\x00" /* stream ID */, 9);
1608
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001609 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001610 if (unlikely(ret <= 0)) {
1611 if (!ret) {
1612 h2c->flags |= H2_CF_MUX_MFULL;
1613 h2c->flags |= H2_CF_DEM_MROOM;
1614 return 0;
1615 }
1616 else {
1617 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1618 return 0;
1619 }
1620 }
1621 return ret;
1622}
1623
Willy Tarreaucf68c782017-10-10 17:11:41 +02001624/* processes a PING frame and schedules an ACK if needed. The caller must pass
1625 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001626 * missing data. The caller must have already verified frame length
1627 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001628 */
1629static int h2c_handle_ping(struct h2c *h2c)
1630{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001631 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001632 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001633 h2c->st0 = H2_CS_FRAME_A;
1634 return 1;
1635}
1636
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001637/* Try to send a window update for stream id <sid> and value <increment>.
1638 * Returns > 0 on success or zero on missing room or failure. It may return an
1639 * error in h2c.
1640 */
1641static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1642{
1643 struct buffer *res;
1644 char str[13];
1645 int ret = -1;
1646
1647 if (h2c_mux_busy(h2c, NULL)) {
1648 h2c->flags |= H2_CF_DEM_MBUSY;
1649 return 0;
1650 }
1651
Willy Tarreau44e973f2018-03-01 17:49:30 +01001652 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001653 if (!res) {
1654 h2c->flags |= H2_CF_MUX_MALLOC;
1655 h2c->flags |= H2_CF_DEM_MROOM;
1656 return 0;
1657 }
1658
1659 /* length: 4, type: 8, flags: none */
1660 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1661 write_n32(str + 5, sid);
1662 write_n32(str + 9, increment);
1663
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001664 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001665
1666 if (unlikely(ret <= 0)) {
1667 if (!ret) {
1668 h2c->flags |= H2_CF_MUX_MFULL;
1669 h2c->flags |= H2_CF_DEM_MROOM;
1670 return 0;
1671 }
1672 else {
1673 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1674 return 0;
1675 }
1676 }
1677 return ret;
1678}
1679
1680/* try to send pending window update for the connection. It's safe to call it
1681 * with no pending updates. Returns > 0 on success or zero on missing room or
1682 * failure. It may return an error in h2c.
1683 */
1684static int h2c_send_conn_wu(struct h2c *h2c)
1685{
1686 int ret = 1;
1687
1688 if (h2c->rcvd_c <= 0)
1689 return 1;
1690
Willy Tarreau97aaa672018-12-23 09:49:04 +01001691 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1692 /* increase the advertised connection window to 2G on
1693 * first update.
1694 */
1695 h2c->flags |= H2_CF_WINDOW_OPENED;
1696 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1697 }
1698
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001699 /* send WU for the connection */
1700 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1701 if (ret > 0)
1702 h2c->rcvd_c = 0;
1703
1704 return ret;
1705}
1706
1707/* try to send pending window update for the current dmux stream. It's safe to
1708 * call it with no pending updates. Returns > 0 on success or zero on missing
1709 * room or failure. It may return an error in h2c.
1710 */
1711static int h2c_send_strm_wu(struct h2c *h2c)
1712{
1713 int ret = 1;
1714
1715 if (h2c->rcvd_s <= 0)
1716 return 1;
1717
1718 /* send WU for the stream */
1719 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1720 if (ret > 0)
1721 h2c->rcvd_s = 0;
1722
1723 return ret;
1724}
1725
Willy Tarreaucf68c782017-10-10 17:11:41 +02001726/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1727 * success, 0 on missing data or one of the h2_status values.
1728 */
1729static int h2c_ack_ping(struct h2c *h2c)
1730{
1731 struct buffer *res;
1732 char str[17];
1733 int ret = -1;
1734
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001735 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001736 return 0;
1737
1738 if (h2c_mux_busy(h2c, NULL)) {
1739 h2c->flags |= H2_CF_DEM_MBUSY;
1740 return 0;
1741 }
1742
Willy Tarreau44e973f2018-03-01 17:49:30 +01001743 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001744 if (!res) {
1745 h2c->flags |= H2_CF_MUX_MALLOC;
1746 h2c->flags |= H2_CF_DEM_MROOM;
1747 return 0;
1748 }
1749
1750 memcpy(str,
1751 "\x00\x00\x08" /* length : 8 (same payload) */
1752 "\x06" "\x01" /* type : 6, flags : ACK */
1753 "\x00\x00\x00\x00" /* stream ID */, 9);
1754
1755 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001756 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001757
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001758 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001759 if (unlikely(ret <= 0)) {
1760 if (!ret) {
1761 h2c->flags |= H2_CF_MUX_MFULL;
1762 h2c->flags |= H2_CF_DEM_MROOM;
1763 return 0;
1764 }
1765 else {
1766 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1767 return 0;
1768 }
1769 }
1770 return ret;
1771}
1772
Willy Tarreau26f95952017-07-27 17:18:30 +02001773/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1774 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001775 * h2c or h2s. The caller must have already verified frame length and stream ID
1776 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001777 */
1778static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1779{
1780 int32_t inc;
1781 int error;
1782
Willy Tarreau26f95952017-07-27 17:18:30 +02001783 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001784 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001785 return 0;
1786
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001787 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001788
1789 if (h2c->dsi != 0) {
1790 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001791
1792 /* it's not an error to receive WU on a closed stream */
1793 if (h2s->st == H2_SS_CLOSED)
1794 return 1;
1795
1796 if (!inc) {
1797 error = H2_ERR_PROTOCOL_ERROR;
1798 goto strm_err;
1799 }
1800
1801 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1802 error = H2_ERR_FLOW_CONTROL_ERROR;
1803 goto strm_err;
1804 }
1805
1806 h2s->mws += inc;
1807 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1808 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001809 if (h2s->send_wait)
1810 LIST_ADDQ(&h2c->send_list, &h2s->list);
1811
Willy Tarreau26f95952017-07-27 17:18:30 +02001812 }
1813 }
1814 else {
1815 /* connection window update */
1816 if (!inc) {
1817 error = H2_ERR_PROTOCOL_ERROR;
1818 goto conn_err;
1819 }
1820
1821 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1822 error = H2_ERR_FLOW_CONTROL_ERROR;
1823 goto conn_err;
1824 }
1825
1826 h2c->mws += inc;
1827 }
1828
1829 return 1;
1830
1831 conn_err:
1832 h2c_error(h2c, error);
1833 return 0;
1834
1835 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001836 h2s_error(h2s, error);
1837 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001838 return 0;
1839}
1840
Willy Tarreaue96b0922017-10-30 00:28:29 +01001841/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001842 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1843 * have already verified frame length and stream ID validity. Described in
1844 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001845 */
1846static int h2c_handle_goaway(struct h2c *h2c)
1847{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001848 int last;
1849
Willy Tarreaue96b0922017-10-30 00:28:29 +01001850 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001851 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001852 return 0;
1853
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001854 last = h2_get_n32(&h2c->dbuf, 0);
1855 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001856 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001857 if (h2c->last_sid < 0)
1858 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001859 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001860}
1861
Willy Tarreau92153fc2017-12-03 19:46:19 +01001862/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001863 * invalid. Returns > 0 on success or zero on missing data. It may return an
1864 * error in h2c. The caller must have already verified frame length and stream
1865 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001866 */
1867static int h2c_handle_priority(struct h2c *h2c)
1868{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001869 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001870 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001871 return 0;
1872
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001873 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001874 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001875 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1876 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001877 }
1878 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001879}
1880
Willy Tarreaucd234e92017-08-18 10:59:39 +02001881/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001882 * Returns > 0 on success or zero on missing data. The caller must have already
1883 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001884 */
1885static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1886{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001887 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001888 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001889 return 0;
1890
1891 /* late RST, already handled */
1892 if (h2s->st == H2_SS_CLOSED)
1893 return 1;
1894
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001895 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001896 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001897
1898 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001899 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001900 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001901 }
1902
1903 h2s->flags |= H2_SF_RST_RCVD;
1904 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001905}
1906
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001907/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1908 * It may return an error in h2c or h2s. The caller must consider that the
1909 * return value is the new h2s in case one was allocated (most common case).
1910 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001911 * errors here are reported as connection errors since it's impossible to
1912 * recover from such errors after the compression context has been altered.
1913 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001914static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001915{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001916 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001917 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001918 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001919 int error;
1920
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001921 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001922 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001923
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001924 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001925 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001926
1927 /* now either the frame is complete or the buffer is complete */
1928 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001929 /* The stream exists/existed, this must be a trailers frame */
1930 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02001931 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
1932 /* unrecoverable error ? */
1933 if (h2c->st0 >= H2_CS_ERROR)
1934 goto out;
1935
1936 if (error == 0)
1937 goto out; // missing data
1938
1939 if (error < 0) {
1940 /* Failed to decode this frame (e.g. too large request)
1941 * but the HPACK decompressor is still synchronized.
1942 */
1943 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1944 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01001945 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02001946 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01001947 goto done;
1948 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001949 /* the connection was already killed by an RST, let's consume
1950 * the data and send another RST.
1951 */
1952 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1953 h2s = (struct h2s*)h2_error_stream;
1954 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001955 }
1956 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1957 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1958 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001959 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001960 goto conn_err;
1961 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001962 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1963 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001964
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001965 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001966
Willy Tarreau25919232019-01-03 14:48:18 +01001967 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001968 if (h2c->st0 >= H2_CS_ERROR)
1969 goto out;
1970
Willy Tarreau25919232019-01-03 14:48:18 +01001971 if (error <= 0) {
1972 if (error == 0)
1973 goto out; // missing data
1974
1975 /* Failed to decode this stream (e.g. too large request)
1976 * but the HPACK decompressor is still synchronized.
1977 */
1978 h2s = (struct h2s*)h2_error_stream;
1979 goto send_rst;
1980 }
1981
Willy Tarreau22de8d32018-09-05 19:55:58 +02001982 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001983 * positively from h2c_frt_stream_new(), the stream will report the error,
1984 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001985 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001986 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001987 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001988 h2s = (struct h2s*)h2_refused_stream;
1989 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001990 }
1991
1992 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001993 h2s->rxbuf = rxbuf;
1994 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001995 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001996
Willy Tarreau88d138e2019-01-02 19:38:14 +01001997 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001998 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001999 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002000
2001 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002002 if (h2s->cs)
2003 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002004 if (h2s->st == H2_SS_OPEN)
2005 h2s->st = H2_SS_HREM;
2006 else
2007 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002008 }
2009
Willy Tarreau3a429f02019-01-03 11:41:50 +01002010 /* update the max stream ID if the request is being processed */
2011 if (h2s->id > h2c->max_id)
2012 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002013
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002014 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002015
2016 conn_err:
2017 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002018 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002019
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002020 out:
2021 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002022 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002023
2024 send_rst:
2025 /* make the demux send an RST for the current stream. We may only
2026 * do this if we're certain that the HEADERS frame was properly
2027 * decompressed so that the HPACK decoder is still kept up to date.
2028 */
2029 h2_release_buf(h2c, &rxbuf);
2030 h2c->st0 = H2_CS_FRAME_E;
2031 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002032}
2033
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002034/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2035 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2036 * errors here are reported as connection errors since it's impossible to
2037 * recover from such errors after the compression context has been altered.
2038 */
2039static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2040{
2041 int error;
2042
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002043 if (!b_size(&h2c->dbuf))
2044 return NULL; // empty buffer
2045
2046 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2047 return NULL; // incomplete frame
2048
Willy Tarreau1915ca22019-01-24 11:49:37 +01002049 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002050
Willy Tarreau25919232019-01-03 14:48:18 +01002051 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002052 if (h2c->st0 >= H2_CS_ERROR)
2053 return NULL;
2054
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002055 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2056 /* RFC7540#5.1 */
2057 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2058 h2c->st0 = H2_CS_FRAME_E;
2059 return NULL;
2060 }
2061
Willy Tarreau25919232019-01-03 14:48:18 +01002062 if (error <= 0) {
2063 if (error == 0)
2064 return NULL; // missing data
2065
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002066 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002067 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002068 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002069 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002070 }
2071
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002072 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2073 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002074 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002075 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002076 }
2077
Willy Tarreau927b88b2019-03-04 08:03:25 +01002078 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002079 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002080 else if (h2s->cs && (h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002081 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002082 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002083 h2s_close(h2s);
2084
2085 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002086}
2087
Willy Tarreau454f9052017-10-26 19:40:35 +02002088/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2089 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2090 */
2091static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2092{
2093 int error;
2094
2095 /* note that empty DATA frames are perfectly valid and sometimes used
2096 * to signal an end of stream (with the ES flag).
2097 */
2098
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002099 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002100 return 0; // empty buffer
2101
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002102 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002103 return 0; // incomplete frame
2104
2105 /* now either the frame is complete or the buffer is complete */
2106
Willy Tarreau454f9052017-10-26 19:40:35 +02002107 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2108 /* RFC7540#6.1 */
2109 error = H2_ERR_STREAM_CLOSED;
2110 goto strm_err;
2111 }
2112
Willy Tarreau1915ca22019-01-24 11:49:37 +01002113 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2114 /* RFC7540#8.1.2 */
2115 error = H2_ERR_PROTOCOL_ERROR;
2116 goto strm_err;
2117 }
2118
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002119 if (!h2_frt_transfer_data(h2s))
2120 return 0;
2121
Willy Tarreau454f9052017-10-26 19:40:35 +02002122 /* call the upper layers to process the frame, then let the upper layer
2123 * notify the stream about any change.
2124 */
2125 if (!h2s->cs) {
2126 error = H2_ERR_STREAM_CLOSED;
2127 goto strm_err;
2128 }
2129
Willy Tarreau8f650c32017-11-21 19:36:21 +01002130 if (h2c->st0 >= H2_CS_ERROR)
2131 return 0;
2132
Willy Tarreau721c9742017-11-07 11:05:42 +01002133 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002134 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002135 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002136 }
2137
2138 /* check for completion : the callee will change this to FRAME_A or
2139 * FRAME_H once done.
2140 */
2141 if (h2c->st0 == H2_CS_FRAME_P)
2142 return 0;
2143
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002144 /* last frame */
2145 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002146 if (h2s->cs)
2147 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002148 if (h2s->st == H2_SS_OPEN)
2149 h2s->st = H2_SS_HREM;
2150 else
2151 h2s_close(h2s);
2152
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002153 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002154
2155 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2156 /* RFC7540#8.1.2 */
2157 error = H2_ERR_PROTOCOL_ERROR;
2158 goto strm_err;
2159 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002160 }
2161
Willy Tarreau454f9052017-10-26 19:40:35 +02002162 return 1;
2163
Willy Tarreau454f9052017-10-26 19:40:35 +02002164 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002165 h2s_error(h2s, error);
2166 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002167 return 0;
2168}
2169
Willy Tarreaubc933932017-10-09 16:21:43 +02002170/* process Rx frames to be demultiplexed */
2171static void h2_process_demux(struct h2c *h2c)
2172{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002173 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002174 struct h2_fh hdr;
2175 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002176
Willy Tarreau081d4722017-05-16 21:51:05 +02002177 if (h2c->st0 >= H2_CS_ERROR)
2178 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002179
2180 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2181 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002182 if (h2c->flags & H2_CF_IS_BACK)
2183 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002184 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2185 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002186 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002187 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002188 sess_log(h2c->conn->owner);
2189 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002190 goto fail;
2191 }
2192
2193 h2c->max_id = 0;
2194 h2c->st0 = H2_CS_SETTINGS1;
2195 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002196
2197 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002198 /* ensure that what is pending is a valid SETTINGS frame
2199 * without an ACK.
2200 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002201 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002202 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002203 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002204 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002205 sess_log(h2c->conn->owner);
2206 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002207 goto fail;
2208 }
2209
2210 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2211 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2212 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2213 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002214 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002215 goto fail;
2216 }
2217
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002218 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002219 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2220 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2221 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002222 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002223 goto fail;
2224 }
2225
Willy Tarreau3bf69182018-12-21 15:34:50 +01002226 /* that's OK, switch to FRAME_P to process it. This is
2227 * a SETTINGS frame whose header has already been
2228 * deleted above.
2229 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002230 padlen = 0;
2231 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002232 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002233 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002234
2235 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002236 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002237 int ret = 0;
2238
2239 if (h2c->st0 >= H2_CS_ERROR)
2240 break;
2241
2242 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002243 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002244 break;
2245
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002246 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002247 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002248 if (!h2c->nb_streams) {
2249 /* only log if no other stream can report the error */
2250 sess_log(h2c->conn->owner);
2251 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002252 break;
2253 }
2254
Willy Tarreau3bf69182018-12-21 15:34:50 +01002255 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2256 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2257 * we read the pad length and drop it from the remaining
2258 * payload (one byte + the 9 remaining ones = 10 total
2259 * removed), so we have a frame payload starting after the
2260 * pad len. Flow controlled frames (DATA) also count the
2261 * padlen in the flow control, so it must be adjusted.
2262 */
2263 if (hdr.len < 1) {
2264 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2265 sess_log(h2c->conn->owner);
2266 goto fail;
2267 }
2268 hdr.len--;
2269
2270 if (b_data(&h2c->dbuf) < 10)
2271 break; // missing padlen
2272
2273 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2274
2275 if (padlen > hdr.len) {
2276 /* RFC7540#6.1 : pad length = length of
2277 * frame payload or greater => error.
2278 */
2279 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2280 sess_log(h2c->conn->owner);
2281 goto fail;
2282 }
2283
2284 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2285 h2c->rcvd_c++;
2286 h2c->rcvd_s++;
2287 }
2288 b_del(&h2c->dbuf, 1);
2289 }
2290 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002291
2292 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002293 h2c->dfl = hdr.len;
2294 h2c->dsi = hdr.sid;
2295 h2c->dft = hdr.ft;
2296 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002297 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002298 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002299
2300 /* check for minimum basic frame format validity */
2301 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2302 if (ret != H2_ERR_NO_ERROR) {
2303 h2c_error(h2c, ret);
2304 sess_log(h2c->conn->owner);
2305 goto fail;
2306 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002307 }
2308
2309 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002310 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2311
Willy Tarreau567beb82018-12-18 16:52:44 +01002312 if (tmp_h2s != h2s && h2s && h2s->cs &&
2313 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002314 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002315 /* we may have to signal the upper layers */
2316 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002317 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002318 }
2319 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002320
Willy Tarreaud7901432017-12-29 11:34:40 +01002321 if (h2c->st0 == H2_CS_FRAME_E)
2322 goto strm_err;
2323
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002324 if (h2s->st == H2_SS_IDLE &&
2325 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2326 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2327 * this state MUST be treated as a connection error
2328 */
2329 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002330 if (!h2c->nb_streams) {
2331 /* only log if no other stream can report the error */
2332 sess_log(h2c->conn->owner);
2333 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002334 break;
2335 }
2336
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002337 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2338 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2339 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002340 * this state MUST be treated as a stream error.
2341 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2342 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002343 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002344 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2345 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2346 else
2347 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002348 goto strm_err;
2349 }
2350
Willy Tarreauab837502017-12-27 15:07:30 +01002351 /* Below the management of frames received in closed state is a
2352 * bit hackish because the spec makes strong differences between
2353 * streams closed by receiving RST, sending RST, and seeing ES
2354 * in both directions. In addition to this, the creation of a
2355 * new stream reusing the identifier of a closed one will be
2356 * detected here. Given that we cannot keep track of all closed
2357 * streams forever, we consider that unknown closed streams were
2358 * closed on RST received, which allows us to respond with an
2359 * RST without breaking the connection (eg: to abort a transfer).
2360 * Some frames have to be silently ignored as well.
2361 */
2362 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002363 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002364 /* #5.1.1: The identifier of a newly
2365 * established stream MUST be numerically
2366 * greater than all streams that the initiating
2367 * endpoint has opened or reserved. This
2368 * governs streams that are opened using a
2369 * HEADERS frame and streams that are reserved
2370 * using PUSH_PROMISE. An endpoint that
2371 * receives an unexpected stream identifier
2372 * MUST respond with a connection error.
2373 */
2374 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2375 goto strm_err;
2376 }
2377
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002378 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002379 /* RFC7540#5.1:closed: an endpoint that
2380 * receives any frame other than PRIORITY after
2381 * receiving a RST_STREAM MUST treat that as a
2382 * stream error of type STREAM_CLOSED.
2383 *
2384 * Note that old streams fall into this category
2385 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002386 *
2387 * However, we cannot generalize this to all frame types. Those
2388 * carrying compression state must still be processed before
2389 * being dropped or we'll desynchronize the decoder. This can
2390 * happen with request trailers received after sending an
2391 * RST_STREAM, or with header/trailers responses received after
2392 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002393 */
2394 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2395 h2c->st0 = H2_CS_FRAME_E;
2396 goto strm_err;
2397 }
2398
2399 /* RFC7540#5.1:closed: if this state is reached as a
2400 * result of sending a RST_STREAM frame, the peer that
2401 * receives the RST_STREAM might have already sent
2402 * frames on the stream that cannot be withdrawn. An
2403 * endpoint MUST ignore frames that it receives on
2404 * closed streams after it has sent a RST_STREAM
2405 * frame. An endpoint MAY choose to limit the period
2406 * over which it ignores frames and treat frames that
2407 * arrive after this time as being in error.
2408 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002409 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002410 /* RFC7540#5.1:closed: any frame other than
2411 * PRIO/WU/RST in this state MUST be treated as
2412 * a connection error
2413 */
2414 if (h2c->dft != H2_FT_RST_STREAM &&
2415 h2c->dft != H2_FT_PRIORITY &&
2416 h2c->dft != H2_FT_WINDOW_UPDATE) {
2417 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2418 goto strm_err;
2419 }
2420 }
2421 }
2422
Willy Tarreauc0da1962017-10-30 18:38:00 +01002423#if 0
2424 // problem below: it is not possible to completely ignore such
2425 // streams as we need to maintain the compression state as well
2426 // and for this we need to completely process these frames (eg:
2427 // HEADERS frames) as well as counting DATA frames to emit
2428 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2429 // This is a typical case of layer violation where the
2430 // transported contents are critical to the connection's
2431 // validity and must be ignored at the same time :-(
2432
2433 /* graceful shutdown, ignore streams whose ID is higher than
2434 * the one advertised in GOAWAY. RFC7540#6.8.
2435 */
2436 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002437 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2438 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002439 h2c->dfl -= ret;
2440 ret = h2c->dfl == 0;
2441 goto strm_err;
2442 }
2443#endif
2444
Willy Tarreau7e98c052017-10-10 15:56:59 +02002445 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002446 case H2_FT_SETTINGS:
2447 if (h2c->st0 == H2_CS_FRAME_P)
2448 ret = h2c_handle_settings(h2c);
2449
2450 if (h2c->st0 == H2_CS_FRAME_A)
2451 ret = h2c_ack_settings(h2c);
2452 break;
2453
Willy Tarreaucf68c782017-10-10 17:11:41 +02002454 case H2_FT_PING:
2455 if (h2c->st0 == H2_CS_FRAME_P)
2456 ret = h2c_handle_ping(h2c);
2457
2458 if (h2c->st0 == H2_CS_FRAME_A)
2459 ret = h2c_ack_ping(h2c);
2460 break;
2461
Willy Tarreau26f95952017-07-27 17:18:30 +02002462 case H2_FT_WINDOW_UPDATE:
2463 if (h2c->st0 == H2_CS_FRAME_P)
2464 ret = h2c_handle_window_update(h2c, h2s);
2465 break;
2466
Willy Tarreau61290ec2017-10-17 08:19:21 +02002467 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002468 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2469 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2470 * frames' parsers consume all following CONTINUATION
2471 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002472 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002473 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2474 sess_log(h2c->conn->owner);
2475 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002476
Willy Tarreau13278b42017-10-13 19:23:14 +02002477 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002478 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002479 if (h2c->flags & H2_CF_IS_BACK)
2480 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2481 else
2482 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002483 if (tmp_h2s) {
2484 h2s = tmp_h2s;
2485 ret = 1;
2486 }
2487 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002488 break;
2489
Willy Tarreau454f9052017-10-26 19:40:35 +02002490 case H2_FT_DATA:
2491 if (h2c->st0 == H2_CS_FRAME_P)
2492 ret = h2c_frt_handle_data(h2c, h2s);
2493
2494 if (h2c->st0 == H2_CS_FRAME_A)
2495 ret = h2c_send_strm_wu(h2c);
2496 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002497
Willy Tarreau92153fc2017-12-03 19:46:19 +01002498 case H2_FT_PRIORITY:
2499 if (h2c->st0 == H2_CS_FRAME_P)
2500 ret = h2c_handle_priority(h2c);
2501 break;
2502
Willy Tarreaucd234e92017-08-18 10:59:39 +02002503 case H2_FT_RST_STREAM:
2504 if (h2c->st0 == H2_CS_FRAME_P)
2505 ret = h2c_handle_rst_stream(h2c, h2s);
2506 break;
2507
Willy Tarreaue96b0922017-10-30 00:28:29 +01002508 case H2_FT_GOAWAY:
2509 if (h2c->st0 == H2_CS_FRAME_P)
2510 ret = h2c_handle_goaway(h2c);
2511 break;
2512
Willy Tarreau1c661982017-10-30 13:52:01 +01002513 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002514 default:
2515 /* drop frames that we ignore. They may be larger than
2516 * the buffer so we drain all of their contents until
2517 * we reach the end.
2518 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002519 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2520 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002521 h2c->dfl -= ret;
2522 ret = h2c->dfl == 0;
2523 }
2524
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002525 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002526 /* We may have to send an RST if not done yet */
2527 if (h2s->st == H2_SS_ERROR)
2528 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002529
Willy Tarreaua20a5192017-12-27 11:02:06 +01002530 if (h2c->st0 == H2_CS_FRAME_E)
2531 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002532
Willy Tarreau7e98c052017-10-10 15:56:59 +02002533 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002534 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002535 break;
2536
2537 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002538 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002539 h2c->st0 = H2_CS_FRAME_H;
2540 }
2541 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002542
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002543 if (h2c->rcvd_c > 0 &&
2544 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2545 h2c_send_conn_wu(h2c);
2546
Willy Tarreau52eed752017-09-22 15:05:09 +02002547 fail:
2548 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002549 if (h2s && h2s->cs &&
2550 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002551 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002552 /* we may have to signal the upper layers */
2553 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002554 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002555 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002556
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002557 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002558}
2559
2560/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2561 * the end.
2562 */
2563static int h2_process_mux(struct h2c *h2c)
2564{
Olivier Houchard998410a2019-04-15 19:23:37 +02002565 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002566
Willy Tarreau01b44822018-10-03 14:26:37 +02002567 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2568 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2569 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2570 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2571 if (h2c->st0 == H2_CS_ERROR) {
2572 h2c->st0 = H2_CS_ERROR2;
2573 sess_log(h2c->conn->owner);
2574 }
2575 goto fail;
2576 }
2577 h2c->st0 = H2_CS_SETTINGS1;
2578 }
2579 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002580 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002581 return 1;
2582 }
2583
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002584 /* start by sending possibly pending window updates */
2585 if (h2c->rcvd_c > 0 &&
2586 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2587 h2c_send_conn_wu(h2c) < 0)
2588 goto fail;
2589
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002590 /* First we always process the flow control list because the streams
2591 * waiting there were already elected for immediate emission but were
2592 * blocked just on this.
2593 */
2594
Olivier Houchard998410a2019-04-15 19:23:37 +02002595 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002596 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2597 h2c->st0 >= H2_CS_ERROR)
2598 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002599
2600 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002601 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002602
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002603 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002604 /* For some reason, the upper layer failed to subsribe again,
2605 * so remove it from the send_list
2606 */
2607 if (!h2s->send_wait) {
2608 LIST_DEL_INIT(&h2s->list);
2609 continue;
2610 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002611 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002612 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002613 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002614 }
2615
Olivier Houchard998410a2019-04-15 19:23:37 +02002616 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002617 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2618 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002619
2620 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002621 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002622
Olivier Houchard998410a2019-04-15 19:23:37 +02002623 /* For some reason, the upper layer failed to subsribe again,
2624 * so remove it from the send_list
2625 */
2626 if (!h2s->send_wait) {
2627 LIST_DEL_INIT(&h2s->list);
2628 continue;
2629 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002630 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002631 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002632 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002633 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002634 }
2635
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002636 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002637 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002638 if (h2c->st0 == H2_CS_ERROR) {
2639 if (h2c->max_id >= 0) {
2640 h2c_send_goaway_error(h2c, NULL);
2641 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2642 return 0;
2643 }
2644
2645 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2646 }
2647 return 1;
2648 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002649 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002650}
2651
Willy Tarreau62f52692017-10-08 23:01:42 +02002652
Willy Tarreau479998a2018-11-18 06:30:59 +01002653/* Attempt to read data, and subscribe if none available.
2654 * The function returns 1 if data has been received, otherwise zero.
2655 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002656static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002657{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002658 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002659 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002660 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002661 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002662
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002663 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002664 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002665
Willy Tarreau315d8072017-12-10 22:17:57 +01002666 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002667 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002668
Willy Tarreau44e973f2018-03-01 17:49:30 +01002669 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002670 if (!buf) {
2671 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002672 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002673 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002674
Olivier Houchard7505f942018-08-21 18:10:44 +02002675 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002676 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002677 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2678 /* HTX in use : try to pre-align the buffer like the
2679 * rxbufs will be to optimize memory copies. We'll make
2680 * sure that the frame header lands at the end of the
2681 * HTX block to alias it upon recv. We cannot use the
2682 * head because rcv_buf() will realign the buffer if
2683 * it's empty. Thus we cheat and pretend we already
2684 * have a few bytes there.
2685 */
2686 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002687 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002688 }
2689 else
2690 max = b_room(buf);
2691
Olivier Houchard7505f942018-08-21 18:10:44 +02002692 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002693 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002694 else
2695 ret = 0;
2696 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002697
Olivier Houchard53216e72018-10-10 15:46:36 +02002698 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002699 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002700
Olivier Houcharda1411e62018-08-17 18:42:48 +02002701 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002702 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002703 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002704 }
2705
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002706 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002707 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002708 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002709}
2710
Willy Tarreau479998a2018-11-18 06:30:59 +01002711/* Try to send data if possible.
2712 * The function returns 1 if data have been sent, otherwise zero.
2713 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002714static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002715{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002716 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002717 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002718 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002719
2720 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002721 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002722
Olivier Houchard7505f942018-08-21 18:10:44 +02002723
Willy Tarreaua2af5122017-10-09 11:56:46 +02002724 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2725 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002726 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002727 }
2728
Willy Tarreaubc933932017-10-09 16:21:43 +02002729 /* This loop is quite simple : it tries to fill as much as it can from
2730 * pending streams into the existing buffer until it's reportedly full
2731 * or the end of send requests is reached. Then it tries to send this
2732 * buffer's contents out, marks it not full if at least one byte could
2733 * be sent, and tries again.
2734 *
2735 * The snd_buf() function normally takes a "flags" argument which may
2736 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2737 * data immediately comes and CO_SFL_STREAMER to indicate that the
2738 * connection is streaming lots of data (used to increase TLS record
2739 * size at the expense of latency). The former can be sent any time
2740 * there's a buffer full flag, as it indicates at least one stream
2741 * attempted to send and failed so there are pending data. An
2742 * alternative would be to set it as long as there's an active stream
2743 * but that would be problematic for ACKs until we have an absolute
2744 * guarantee that all waiters have at least one byte to send. The
2745 * latter should possibly not be set for now.
2746 */
2747
2748 done = 0;
2749 while (!done) {
2750 unsigned int flags = 0;
2751
2752 /* fill as much as we can into the current buffer */
2753 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2754 done = h2_process_mux(h2c);
2755
Olivier Houchard2b094432019-01-29 18:28:36 +01002756 if (h2c->flags & H2_CF_MUX_MALLOC)
2757 break;
2758
Willy Tarreaubc933932017-10-09 16:21:43 +02002759 if (conn->flags & CO_FL_ERROR)
2760 break;
2761
2762 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2763 flags |= CO_SFL_MSG_MORE;
2764
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002765 if (b_data(&h2c->mbuf)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002766 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002767 if (!ret)
2768 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002769 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002770 b_del(&h2c->mbuf, ret);
2771 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002772 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002773
2774 /* wrote at least one byte, the buffer is not full anymore */
2775 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2776 }
2777
Willy Tarreaua2af5122017-10-09 11:56:46 +02002778 if (conn->flags & CO_FL_SOCK_WR_SH) {
2779 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002780 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002781 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002782 /* We're not full anymore, so we can wake any task that are waiting
2783 * for us.
2784 */
2785 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002786 struct h2s *h2s;
2787
2788 list_for_each_entry(h2s, &h2c->send_list, list) {
2789 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2790 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002791
2792 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002793 continue;
2794
Olivier Houchard998410a2019-04-15 19:23:37 +02002795 /* For some reason, the upper layer failed to subsribe again,
2796 * so remove it from the send_list
2797 */
2798 if (!h2s->send_wait) {
2799 LIST_DEL_INIT(&h2s->list);
2800 continue;
2801 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002802 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002803 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002804 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002805 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002806 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002807 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002808 /* We're done, no more to send */
2809 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002810 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002811schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002812 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002813 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002814 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002815}
2816
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002817/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002818static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2819{
2820 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002821 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002822
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002823 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002824 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002825 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002826 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002827 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002828 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002829 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002830}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002831
Willy Tarreau62f52692017-10-08 23:01:42 +02002832/* callback called on any event by the connection handler.
2833 * It applies changes and returns zero, or < 0 if it wants immediate
2834 * destruction of the connection (which normally doesn not happen in h2).
2835 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002836static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002837{
Olivier Houchard7505f942018-08-21 18:10:44 +02002838 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002839
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002840 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002841 h2_process_demux(h2c);
2842
2843 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002844 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002845
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002846 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002847 h2c->flags &= ~H2_CF_DEM_DFULL;
2848 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002849 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002850
Willy Tarreau0b37d652018-10-03 10:33:02 +02002851 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002852 /* frontend is stopping, reload likely in progress, let's try
2853 * to announce a graceful shutdown if not yet done. We don't
2854 * care if it fails, it will be tried again later.
2855 */
2856 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2857 if (h2c->last_sid < 0)
2858 h2c->last_sid = (1U << 31) - 1;
2859 h2c_send_goaway_error(h2c, NULL);
2860 }
2861 }
2862
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002863 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002864 * If we received early data, and the handshake is done, wake
2865 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002866 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002867 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2868 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2869 struct eb32_node *node;
2870 struct h2s *h2s;
2871
2872 h2c->flags |= H2_CF_WAIT_FOR_HS;
2873 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2874
2875 while (node) {
2876 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002877 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002878 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002879 node = eb32_next(node);
2880 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002881 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002882
Willy Tarreau26bd7612017-10-09 16:47:04 +02002883 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002884 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2885 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2886 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002887 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002888
2889 if (eb_is_empty(&h2c->streams_by_id)) {
2890 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002891 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002892 return -1;
2893 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002894 }
2895
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002896 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002897 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002898
Olivier Houchard53216e72018-10-10 15:46:36 +02002899 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2900 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2901 (h2c->st0 != H2_CS_ERROR &&
2902 !b_data(&h2c->mbuf) &&
2903 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2904 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002905 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002906
Willy Tarreau3f133572017-10-31 19:21:06 +01002907 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002908 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002909 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002910 task_queue(h2c->task);
2911 }
2912 else
2913 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002914 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002915
Olivier Houchard7505f942018-08-21 18:10:44 +02002916 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002917 return 0;
2918}
2919
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002920/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002921static int h2_wake(struct connection *conn)
2922{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002923 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002924
2925 return (h2_process(h2c));
2926}
2927
Willy Tarreauea392822017-10-31 10:02:25 +01002928/* Connection timeout management. The principle is that if there's no receipt
2929 * nor sending for a certain amount of time, the connection is closed. If the
2930 * MUX buffer still has lying data or is not allocatable, the connection is
2931 * immediately killed. If it's allocatable and empty, we attempt to send a
2932 * GOAWAY frame.
2933 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002934static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002935{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002936 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002937 int expired = tick_is_expired(t->expire, now_ms);
2938
Willy Tarreau0975f112018-03-29 15:22:59 +02002939 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002940 return t;
2941
Olivier Houchard3f795f72019-04-17 22:51:06 +02002942 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02002943
2944 if (!h2c) {
2945 /* resources were already deleted */
2946 return NULL;
2947 }
2948
2949 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002950 h2c_error(h2c, H2_ERR_NO_ERROR);
2951 h2_wake_some_streams(h2c, 0, 0);
2952
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002953 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002954 /* don't even try to send a GOAWAY, the buffer is stuck */
2955 h2c->flags |= H2_CF_GOAWAY_FAILED;
2956 }
2957
2958 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002959 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002960 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2961 h2c->flags |= H2_CF_GOAWAY_FAILED;
2962
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002963 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002964 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002965 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002966 b_del(&h2c->mbuf, ret);
2967 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002968 }
2969 }
Willy Tarreauea392822017-10-31 10:02:25 +01002970
Willy Tarreau0975f112018-03-29 15:22:59 +02002971 /* either we can release everything now or it will be done later once
2972 * the last stream closes.
2973 */
2974 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002975 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002976
Willy Tarreauea392822017-10-31 10:02:25 +01002977 return NULL;
2978}
2979
2980
Willy Tarreau62f52692017-10-08 23:01:42 +02002981/*******************************************/
2982/* functions below are used by the streams */
2983/*******************************************/
2984
2985/*
2986 * Attach a new stream to a connection
2987 * (Used for outgoing connections)
2988 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002989static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002990{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002991 struct conn_stream *cs;
2992 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002993 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002994
2995 cs = cs_new(conn);
2996 if (!cs)
2997 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002998 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002999 if (!h2s) {
3000 cs_free(cs);
3001 return NULL;
3002 }
3003 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003004}
3005
Willy Tarreaufafd3982018-11-18 21:29:20 +01003006/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3007 * We have to scan because we may have some orphan streams. It might be
3008 * beneficial to scan backwards from the end to reduce the likeliness to find
3009 * orphans.
3010 */
3011static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3012{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003013 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003014 struct h2s *h2s;
3015 struct eb32_node *node;
3016
3017 node = eb32_first(&h2c->streams_by_id);
3018 while (node) {
3019 h2s = container_of(node, struct h2s, by_id);
3020 if (h2s->cs)
3021 return h2s->cs;
3022 node = eb32_next(node);
3023 }
3024 return NULL;
3025}
3026
Willy Tarreau62f52692017-10-08 23:01:42 +02003027/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003028 * Destroy the mux and the associated connection, if it is no longer used
3029 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003030static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003031{
Christopher Faulet73c12072019-04-08 11:23:22 +02003032 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003033
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003034 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003035 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003036}
3037
3038/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003039 * Detach the stream from the connection and possibly release the connection.
3040 */
3041static void h2_detach(struct conn_stream *cs)
3042{
Willy Tarreau60935142017-10-16 18:11:19 +02003043 struct h2s *h2s = cs->ctx;
3044 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003045 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003046
3047 cs->ctx = NULL;
3048 if (!h2s)
3049 return;
3050
Olivier Houchard998410a2019-04-15 19:23:37 +02003051 /* The stream is about to die, so no need to attempt to run its task */
3052 if (!LIST_ISEMPTY(&h2s->sending_list) &&
3053 h2s->send_wait != &h2s->wait_event) {
3054 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3055 LIST_DEL_INIT(&h2s->sending_list);
3056 }
3057
Olivier Houchardf502aca2018-12-14 19:42:40 +01003058 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003059 h2c = h2s->h2c;
3060 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003061 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003062 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3063 !h2_frt_has_too_many_cs(h2c)) {
3064 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003065 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003066 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003067 }
Willy Tarreau60935142017-10-16 18:11:19 +02003068
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003069 /* this stream may be blocked waiting for some data to leave (possibly
3070 * an ES or RST frame), so orphan it in this case.
3071 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003072 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003073 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003074 (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 +01003075 return;
3076
Willy Tarreau45f752e2017-10-30 15:44:59 +01003077 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3078 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3079 /* unblock the connection if it was blocked on this
3080 * stream.
3081 */
3082 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3083 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003084 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003085 }
3086
Willy Tarreau71049cc2018-03-28 13:56:39 +02003087 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003088
Olivier Houchard8a786902018-12-15 16:05:40 +01003089 if (h2c->flags & H2_CF_IS_BACK &&
3090 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003091 if (!(h2c->conn->flags &
3092 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3093 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003094 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003095 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3096 h2c->conn->owner = NULL;
3097 if (eb_is_empty(&h2c->streams_by_id)) {
3098 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3099 /* The server doesn't want it, let's kill the connection right away */
3100 h2c->conn->mux->destroy(h2c->conn);
3101 return;
3102 }
3103 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003104 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003105 if (eb_is_empty(&h2c->streams_by_id)) {
3106 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3107 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3108 return;
3109 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003110 /* Never ever allow to reuse a connection from a non-reuse backend */
3111 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3112 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003113 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003114 struct server *srv = objt_server(h2c->conn->target);
3115
3116 if (srv) {
3117 if (h2c->conn->flags & CO_FL_PRIVATE)
3118 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3119 else
3120 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3121 }
3122
3123 }
3124 }
3125 }
3126
Willy Tarreaue323f342018-03-28 13:51:45 +02003127 /* We don't want to close right now unless we're removing the
3128 * last stream, and either the connection is in error, or it
3129 * reached the ID already specified in a GOAWAY frame received
3130 * or sent (as seen by last_sid >= 0).
3131 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003132 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003133 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003134 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003135 }
3136 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003137 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003138 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3139 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003140 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003141 else
3142 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003143 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003144}
3145
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003146/* Performs a synchronous or asynchronous shutr().
3147 * FIXME: guess what the return code tries to indicate!
3148 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003149static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003150{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003151 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003152 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003153
Willy Tarreau721c9742017-11-07 11:05:42 +01003154 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003155 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003156
Willy Tarreau18059042019-01-31 19:12:48 +01003157 /* a connstream may require us to immediately kill the whole connection
3158 * for example because of a "tcp-request content reject" rule that is
3159 * normally used to limit abuse. In this case we schedule a goaway to
3160 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003161 */
Willy Tarreau18059042019-01-31 19:12:48 +01003162 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3163 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3164 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3165 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3166 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003167 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3168 /* Nothing was never sent for this stream, so reset with
3169 * REFUSED_STREAM error to let the client retry the
3170 * request.
3171 */
3172 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3173 }
Willy Tarreau18059042019-01-31 19:12:48 +01003174
Willy Tarreau90c32322017-11-24 08:00:30 +01003175 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003176 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003177 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003178
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003179 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003180 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003181 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003182
Olivier Houchard7a977432019-03-21 15:47:13 +01003183 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003184add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003185 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003186 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003187 if (h2s->flags & H2_SF_BLK_MFCTL) {
3188 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3189 h2s->send_wait = sw;
3190 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3191 h2s->send_wait = sw;
3192 LIST_ADDQ(&h2c->send_list, &h2s->list);
3193 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003194 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003195 /* Let the handler know we want shutr */
3196 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003197 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003198}
3199
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003200/* Performs a synchronous or asynchronous shutw().
3201 * FIXME: guess what the return code tries to indicate!
3202 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003203static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003204{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003205 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003206 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003207
Willy Tarreau721c9742017-11-07 11:05:42 +01003208 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003209 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003210
Willy Tarreau67434202017-11-06 20:20:51 +01003211 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003212 /* we can cleanly close using an empty data frame only after headers */
3213
3214 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3215 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003216 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003217
3218 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003219 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003220 else
3221 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003222 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003223 /* a connstream may require us to immediately kill the whole connection
3224 * for example because of a "tcp-request content reject" rule that is
3225 * normally used to limit abuse. In this case we schedule a goaway to
3226 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003227 */
Willy Tarreau18059042019-01-31 19:12:48 +01003228 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3229 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3230 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3231 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3232 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003233 else {
3234 /* Nothing was never sent for this stream, so reset with
3235 * REFUSED_STREAM error to let the client retry the
3236 * request.
3237 */
3238 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3239 }
Willy Tarreau18059042019-01-31 19:12:48 +01003240
Willy Tarreau90c32322017-11-24 08:00:30 +01003241 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003242 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003243 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003244
Willy Tarreau00dd0782018-03-01 16:31:34 +01003245 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003246 }
3247
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003248 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003249 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003250 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003251
3252 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003253 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003254 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003255 if (h2s->flags & H2_SF_BLK_MFCTL) {
3256 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3257 h2s->send_wait = sw;
3258 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3259 h2s->send_wait = sw;
3260 LIST_ADDQ(&h2c->send_list, &h2s->list);
3261 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003262 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003263 /* let the handler know we want to shutw */
3264 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003265 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003266}
3267
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003268/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3269 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3270 * and prevented the last frame from being emitted.
3271 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003272static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3273{
3274 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003275 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003276 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003277
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003278 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003279 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003280 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003281 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003282 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003283
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003284 if (!ret) {
3285 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003286 LIST_DEL_INIT(&h2s->list);
3287 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003288 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003289 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003290 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003291 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003292
3293 if (h2c_is_dead(h2c))
Christopher Faulet73c12072019-04-08 11:23:22 +02003294 h2_release(h2c);
Olivier Houchard7a977432019-03-21 15:47:13 +01003295 }
3296
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003297 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003298}
3299
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003300/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003301static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3302{
3303 struct h2s *h2s = cs->ctx;
3304
3305 if (!mode)
3306 return;
3307
3308 h2_do_shutr(h2s);
3309}
3310
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003311/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003312static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3313{
3314 struct h2s *h2s = cs->ctx;
3315
3316 h2_do_shutw(h2s);
3317}
3318
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003319/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003320 * HTX request or response depending on the connection's side. Returns a
3321 * positive value on success, a negative value on failure, or 0 if it couldn't
3322 * proceed. May report connection errors in h2c->errcode if the frame is
3323 * non-decodable and the connection unrecoverable. In absence of connection
3324 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003325 *
3326 * The function may fold CONTINUATION frames into the initial HEADERS frame
3327 * by removing padding and next frame header, then moving the CONTINUATION
3328 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3329 * leaving a hole between the main frame and the beginning of the next one.
3330 * The possibly remaining incomplete or next frame at the end may be moved
3331 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3332 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3333 *
3334 * A buffer at the beginning of processing may look like this :
3335 *
3336 * ,---.---------.-----.--------------.--------------.------.---.
3337 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3338 * `---^---------^-----^--------------^--------------^------^---'
3339 * | | <-----> | |
3340 * area | dpl | wrap
3341 * |<--------------> |
3342 * | dfl |
3343 * |<-------------------------------------------------->|
3344 * head data
3345 *
3346 * Padding is automatically overwritten when folding, participating to the
3347 * hole size after dfl :
3348 *
3349 * ,---.------------------------.-----.--------------.------.---.
3350 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3351 * `---^------------------------^-----^--------------^------^---'
3352 * | | <-----> | |
3353 * area | hole | wrap
3354 * |<-----------------------> |
3355 * | dfl |
3356 * |<-------------------------------------------------->|
3357 * head data
3358 *
3359 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3360 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3361 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003362 *
3363 * The <flags> field must point to either the stream's flags or to a copy of it
3364 * so that the function can update the following flags :
3365 * - H2_SF_DATA_CLEN when content-length is seen
3366 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3367 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003368 *
3369 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3370 * decoding, in order to detect if we're dealing with a headers or a trailers
3371 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003372 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003373static 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 +02003374{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003375 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003376 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003377 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003378 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003379 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003380 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003381 int flen; // header frame len
3382 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003383 int ret = 0;
3384 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003385 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003386 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003387
Willy Tarreauea18f862018-12-22 20:19:26 +01003388next_frame:
3389 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3390 goto leave; // incomplete input frame
3391
3392 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3393 * this case, we'll try to paste it immediately after the initial
3394 * HEADERS frame payload and kill any possible padding. The initial
3395 * frame's length will be increased to represent the concatenation
3396 * of the two frames. The next frame is read from position <tlen>
3397 * and written at position <flen> (minus padding if some is present).
3398 */
3399 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3400 struct h2_fh hdr;
3401 int clen; // CONTINUATION frame's payload length
3402
3403 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3404 /* no more data, the buffer may be full, either due to
3405 * too large a frame or because of too large a hole that
3406 * we're going to compact at the end.
3407 */
3408 goto leave;
3409 }
3410
3411 if (hdr.ft != H2_FT_CONTINUATION) {
3412 /* RFC7540#6.10: frame of unexpected type */
3413 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3414 goto fail;
3415 }
3416
3417 if (hdr.sid != h2c->dsi) {
3418 /* RFC7540#6.10: frame of different stream */
3419 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3420 goto fail;
3421 }
3422
3423 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3424 /* RFC7540#4.2: invalid frame length */
3425 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3426 goto fail;
3427 }
3428
3429 /* detect when we must stop aggragating frames */
3430 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3431
3432 /* Take as much as we can of the CONTINUATION frame's payload */
3433 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3434 if (clen > hdr.len)
3435 clen = hdr.len;
3436
3437 /* Move the frame's payload over the padding, hole and frame
3438 * header. At least one of hole or dpl is null (see diagrams
3439 * above). The hole moves after the new aggragated frame.
3440 */
3441 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3442 h2c->dfl += clen - h2c->dpl;
3443 hole += h2c->dpl + 9;
3444 h2c->dpl = 0;
3445 goto next_frame;
3446 }
3447
3448 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003449
Willy Tarreau13278b42017-10-13 19:23:14 +02003450 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003451 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003452 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003453 copy = alloc_trash_chunk();
3454 if (!copy) {
3455 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3456 goto fail;
3457 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003458 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3459 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3460 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003461 }
3462
Willy Tarreau13278b42017-10-13 19:23:14 +02003463 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3464 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003465 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003466 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3467 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003468 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003469 }
3470
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003471 if (flen < 5) {
3472 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3473 goto fail;
3474 }
3475
Willy Tarreau13278b42017-10-13 19:23:14 +02003476 hdrs += 5; // stream dep = 4, weight = 1
3477 flen -= 5;
3478 }
3479
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003480 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003481 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003482 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003483 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003484
Willy Tarreau937f7602018-02-26 15:22:17 +01003485 /* we can't retry a failed decompression operation so we must be very
3486 * careful not to take any risks. In practice the output buffer is
3487 * always empty except maybe for trailers, in which case we simply have
3488 * to wait for the upper layer to finish consuming what is available.
3489 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003490
3491 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003492 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003493 if (!htx_is_empty(htx)) {
3494 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003495 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003496 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003497 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003498 if (b_data(rxbuf)) {
3499 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003500 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003501 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003502
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003503 rxbuf->head = 0;
3504 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003505 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003506
Willy Tarreau25919232019-01-03 14:48:18 +01003507 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003508 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3509 sizeof(list)/sizeof(list[0]), tmp);
3510 if (outlen < 0) {
3511 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3512 goto fail;
3513 }
3514
Willy Tarreau25919232019-01-03 14:48:18 +01003515 /* The PACK decompressor was updated, let's update the input buffer and
3516 * the parser's state to commit these changes and allow us to later
3517 * fail solely on the stream if needed.
3518 */
3519 b_del(&h2c->dbuf, h2c->dfl + hole);
3520 h2c->dfl = hole = 0;
3521 h2c->st0 = H2_CS_FRAME_H;
3522
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003523 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003524 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003525
Willy Tarreau88d138e2019-01-02 19:38:14 +01003526 if (*flags & H2_SF_HEADERS_RCVD)
3527 goto trailers;
3528
3529 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003530 if (htx) {
3531 /* HTX mode */
3532 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003533 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003534 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003535 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003536 } else {
3537 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003538 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003539 if (outlen > 0)
3540 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003541 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003542
3543 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003544 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003545 goto fail;
3546 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003547
Willy Tarreau174b06a2018-04-25 18:13:58 +02003548 if (msgf & H2_MSGF_BODY) {
3549 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003550 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003551 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003552 if (htx)
3553 htx->extra = *body_len;
3554 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003555 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003556 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003557 }
3558
Willy Tarreau88d138e2019-01-02 19:38:14 +01003559 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003560 /* indicate that a HEADERS frame was received for this stream, except
3561 * for 1xx responses. For 1xx responses, another HEADERS frame is
3562 * expected.
3563 */
3564 if (!(msgf & H2_MSGF_RSP_1XX))
3565 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003566
Christopher Faulet0b465482019-02-19 15:14:23 +01003567 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003568 /* Mark the end of message, either using EOM in HTX or with the
3569 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003570 * is not set during headers with END_STREAM. For HTX trailers,
3571 * we must not leave an HTX trailers block not followed by an
3572 * EOM block, the two must be atomic. Thus if we fail to emit
3573 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003574 */
3575 if (htx) {
Willy Tarreau2135f912019-05-07 11:17:32 +02003576 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3577 struct htx_blk *tail = htx_get_tail_blk(htx);
3578
3579 if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR)
3580 htx_remove_blk(htx, tail);
Willy Tarreau88d138e2019-01-02 19:38:14 +01003581 goto fail;
Willy Tarreau2135f912019-05-07 11:17:32 +02003582 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01003583 }
3584 else if (*flags & H2_SF_DATA_CHNK) {
3585 if (!b_putblk(rxbuf, "\r\n", 2))
3586 goto fail;
3587 }
3588 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003589
Willy Tarreau86277d42019-01-02 15:36:11 +01003590 /* success */
3591 ret = 1;
3592
Willy Tarreau68dd9852017-07-03 14:44:26 +02003593 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003594 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003595 * move the remaining data over it.
3596 */
3597 if (hole) {
3598 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3599 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3600 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3601 b_sub(&h2c->dbuf, hole);
3602 }
3603
Willy Tarreau97215ca2019-04-29 10:20:21 +02003604 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003605 /* too large frames */
3606 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003607 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003608 }
3609
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003610 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003611 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003612 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003613 return ret;
3614
Willy Tarreau68dd9852017-07-03 14:44:26 +02003615 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003616 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003617 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003618
3619 trailers:
3620 /* This is the last HEADERS frame hence a trailer */
3621
3622 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3623 /* It's a trailer but it's missing ES flag */
3624 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3625 goto fail;
3626 }
3627
3628 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3629 * block, and when using chunks we must send the 0 CRLF marker. For
3630 * other modes, the trailers are silently dropped.
3631 */
3632 if (htx) {
3633 if (!htx_add_endof(htx, HTX_BLK_EOD))
3634 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003635 if (h2_make_htx_trailers(list, htx) <= 0)
3636 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003637 }
3638 else if (*flags & H2_SF_DATA_CHNK) {
3639 /* Legacy mode with chunked encoding : we must finalize the
3640 * data block message emit the trailing CRLF */
3641 if (!b_putblk(rxbuf, "0\r\n", 3))
3642 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003643
3644 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3645 if (outlen > 0)
3646 b_add(rxbuf, outlen);
3647 else
3648 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003649 }
3650
3651 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003652}
3653
Willy Tarreau454f9052017-10-26 19:40:35 +02003654/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3655 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3656 * in use, a new chunk is emitted for each frame. This is supposed to fit
3657 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3658 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3659 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003660 * parser state is automatically updated. Returns > 0 if it could completely
3661 * send the current frame, 0 if it couldn't complete, in which case
3662 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3663 * DATA frame can return 0 as a valid result). Stream errors are reported in
3664 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3665 * have checked the frame header and ensured that the frame was complete or the
3666 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003667 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003668static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003669{
3670 struct h2c *h2c = h2s->h2c;
3671 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003672 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003673 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003674 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003675 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003676
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003677 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003678
Olivier Houchard638b7992018-08-16 15:41:52 +02003679 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003680 if (!csbuf) {
3681 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003682 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003683 }
3684
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003685try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003686 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003687 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3688 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003689 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003690 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003691
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003692 if (flen > b_data(&h2c->dbuf)) {
3693 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003694 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003695 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003696 }
3697
Willy Tarreaua9b77962019-01-31 07:23:00 +01003698 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003699 block1 = htx_free_data_space(htx);
3700 if (!block1) {
3701 h2c->flags |= H2_CF_DEM_SFULL;
3702 goto fail;
3703 }
3704 if (flen > block1)
3705 flen = block1;
3706
3707 /* here, flen is the max we can copy into the output buffer */
3708 block1 = b_contig_data(&h2c->dbuf, 0);
3709 if (flen > block1)
3710 flen = block1;
3711
3712 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3713 h2c->flags |= H2_CF_DEM_SFULL;
3714 goto fail;
3715 }
3716
3717 b_del(&h2c->dbuf, flen);
3718 h2c->dfl -= flen;
3719 h2c->rcvd_c += flen;
3720 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003721
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003722 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003723 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003724 htx->extra = h2s->body_len;
3725 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003726 goto try_again;
3727 }
3728 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003729 /* it doesn't fit and the buffer is fragmented,
3730 * so let's defragment it and try again.
3731 */
3732 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003733 }
3734
Willy Tarreaueba10f22018-04-25 20:44:22 +02003735 /* chunked-encoding requires more room */
3736 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003737 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003738 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3739 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3740 (chklen < 1048576) ? 4 : 8;
3741 chklen += 4; // CRLF, CRLF
3742 }
3743
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003744 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003745 if (flen + chklen > b_room(csbuf)) {
3746 if (chklen >= b_room(csbuf)) {
3747 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003748 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003749 }
3750 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003751 }
3752
3753 if (h2s->flags & H2_SF_DATA_CHNK) {
3754 /* emit the chunk size */
3755 unsigned int chksz = flen;
3756 char str[10];
3757 char *beg;
3758
3759 beg = str + sizeof(str);
3760 *--beg = '\n';
3761 *--beg = '\r';
3762 do {
3763 *--beg = hextab[chksz & 0xF];
3764 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003765 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003766 }
3767
Willy Tarreau454f9052017-10-26 19:40:35 +02003768 /* Block1 is the length of the first block before the buffer wraps,
3769 * block2 is the optional second block to reach the end of the frame.
3770 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003771 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003772 if (block1 > flen)
3773 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003774 block2 = flen - block1;
3775
3776 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003777 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003778
3779 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003780 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003781
Willy Tarreaueba10f22018-04-25 20:44:22 +02003782 if (h2s->flags & H2_SF_DATA_CHNK) {
3783 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003784 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003785 }
3786
Willy Tarreau454f9052017-10-26 19:40:35 +02003787 /* now mark the input data as consumed (will be deleted from the buffer
3788 * by the caller when seeing FRAME_A after sending the window update).
3789 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003790 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003791 h2c->dfl -= flen;
3792 h2c->rcvd_c += flen;
3793 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3794
Willy Tarreau1915ca22019-01-24 11:49:37 +01003795 if (h2s->flags & H2_SF_DATA_CLEN)
3796 h2s->body_len -= flen;
3797
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003798 if (h2c->dfl > h2c->dpl) {
3799 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003800 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003801 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003802 }
3803
Willy Tarreau4a28da12018-01-04 14:41:00 +01003804 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003805 /* here we're done with the frame, all the payload (except padding) was
3806 * transferred.
3807 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003808
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003809 if (h2c->dff & H2_F_DATA_END_STREAM) {
3810 if (htx) {
3811 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3812 h2c->flags |= H2_CF_DEM_SFULL;
3813 goto fail;
3814 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003815 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003816 else if (h2s->flags & H2_SF_DATA_CHNK) {
3817 /* emit the trailing 0 CRLF CRLF */
3818 if (b_room(csbuf) < 5) {
3819 h2c->flags |= H2_CF_DEM_SFULL;
3820 goto fail;
3821 }
3822 chklen += 5;
3823 b_putblk(csbuf, "0\r\n\r\n", 5);
3824 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003825 }
3826
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003827 h2c->rcvd_c += h2c->dpl;
3828 h2c->rcvd_s += h2c->dpl;
3829 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003830 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003831 if (htx)
3832 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003833 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003834 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003835 if (htx)
3836 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003837 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003838}
3839
Willy Tarreau5dd17352018-06-14 13:33:30 +02003840/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3841 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3842 * number of bytes sent. The caller must check the stream's status to detect
3843 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003844 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003845static 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 +02003846{
3847 struct http_hdr list[MAX_HTTP_HDR];
3848 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003849 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003850 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003851 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003852 int es_now = 0;
3853 int ret = 0;
3854 int hdr;
3855
3856 if (h2c_mux_busy(h2c, h2s)) {
3857 h2s->flags |= H2_SF_BLK_MBUSY;
3858 return 0;
3859 }
3860
Willy Tarreau44e973f2018-03-01 17:49:30 +01003861 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003862 h2c->flags |= H2_CF_MUX_MALLOC;
3863 h2s->flags |= H2_SF_BLK_MROOM;
3864 return 0;
3865 }
3866
3867 /* First, try to parse the H1 response and index it into <list>.
3868 * NOTE! Since it comes from haproxy, we *know* that a response header
3869 * block does not wrap and we can safely read it this way without
3870 * having to realign the buffer.
3871 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003872 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003873 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003874 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003875 /* incomplete or invalid response, this is abnormal coming from
3876 * haproxy and may only result in a bad errorfile or bad Lua code
3877 * so that won't be fixed, raise an error now.
3878 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003879 * FIXME: we should instead add the ability to only return a
3880 * 502 bad gateway. But in theory this is not supposed to
3881 * happen.
3882 */
3883 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3884 ret = 0;
3885 goto end;
3886 }
3887
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003888 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003889
3890 /* certain statuses have no body or an empty one, regardless of
3891 * what the headers say.
3892 */
3893 if (sl.st.status >= 100 && sl.st.status < 200) {
3894 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3895 h1m->curr_len = h1m->body_len = 0;
3896 }
3897 else if (sl.st.status == 204 || sl.st.status == 304) {
3898 /* no contents, claim c-len is present and set to zero */
3899 h1m->flags &= ~H1_MF_CHNK;
3900 h1m->flags |= H1_MF_CLEN;
3901 h1m->curr_len = h1m->body_len = 0;
3902 }
3903
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003904 chunk_reset(&outbuf);
3905
3906 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003907 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003908 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003909 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003910
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003911 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003912 break;
3913 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003914 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003915 }
3916
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003917 if (outbuf.size < 9)
3918 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003919
3920 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003921 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3922 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3923 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003924
3925 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003926 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003927 /* this is an unparsable response */
3928 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3929 ret = 0;
3930 goto end;
3931 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003932
3933 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003934 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003935 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003936 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003937 }
3938
3939 /* encode all headers, stop at empty name */
3940 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003941 /* these ones do not exist in H2 and must be dropped. */
3942 if (isteq(list[hdr].n, ist("connection")) ||
3943 isteq(list[hdr].n, ist("proxy-connection")) ||
3944 isteq(list[hdr].n, ist("keep-alive")) ||
3945 isteq(list[hdr].n, ist("upgrade")) ||
3946 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003947 continue;
3948
3949 if (isteq(list[hdr].n, ist("")))
3950 break; // end
3951
3952 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3953 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003954 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003955 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003956 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003957 }
3958 }
3959
3960 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003961 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003962 es_now = 1;
3963
3964 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003965 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003966
3967 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003968 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003969
3970 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003971 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003972
3973 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003974 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003975 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003976
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003977 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003978 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003979 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003980
Willy Tarreau801250e2018-09-11 11:45:04 +02003981 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003982 h2s->flags |= H2_SF_ES_SENT;
3983 if (h2s->st == H2_SS_OPEN)
3984 h2s->st = H2_SS_HLOC;
3985 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003986 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003987 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003988 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003989 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003990 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003991 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003992 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003993 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003994 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003995
3996 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003997
3998 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003999 //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 +02004000 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004001 full:
4002 h1m_init_res(h1m);
4003 h1m->err_pos = -1; // don't care about errors on the response path
4004 h2c->flags |= H2_CF_MUX_MFULL;
4005 h2s->flags |= H2_SF_BLK_MROOM;
4006 ret = 0;
4007 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004008}
4009
Willy Tarreau5dd17352018-06-14 13:33:30 +02004010/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4011 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4012 * the number of bytes sent. The caller must check the stream's status to
4013 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004014 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004015static 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 +02004016{
4017 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004018 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004019 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004020 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004021 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004022 int es_now = 0;
4023 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004024 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004025 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004026
4027 if (h2c_mux_busy(h2c, h2s)) {
4028 h2s->flags |= H2_SF_BLK_MBUSY;
4029 goto end;
4030 }
4031
Willy Tarreau44e973f2018-03-01 17:49:30 +01004032 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004033 h2c->flags |= H2_CF_MUX_MALLOC;
4034 h2s->flags |= H2_SF_BLK_MROOM;
4035 goto end;
4036 }
4037
4038 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004039 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004040 goto end;
4041
4042 chunk_reset(&outbuf);
4043
4044 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004045 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004046 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004047 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004048
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004049 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004050 break;
4051 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004052 /* If there are pending data in the output buffer, and we have
4053 * less than 1/4 of the mbuf's size and everything fits, we'll
4054 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4055 * is full and wait, to save some slow realign calls.
4056 */
4057 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4058 h2c->flags |= H2_CF_MUX_MFULL;
4059 h2s->flags |= H2_SF_BLK_MROOM;
4060 goto end;
4061 }
4062
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004063 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004064 }
4065
4066 if (outbuf.size < 9) {
4067 h2c->flags |= H2_CF_MUX_MFULL;
4068 h2s->flags |= H2_SF_BLK_MROOM;
4069 goto end;
4070 }
4071
4072 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004073 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4074 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4075 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004076
4077 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4078 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004079 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004080 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004081 break;
4082 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004083 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004084 if ((long long)size > h1m->curr_len)
4085 size = h1m->curr_len;
4086 break;
4087 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004088 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004089 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004090 if (!ret)
4091 goto end;
4092
4093 if (ret < 0) {
4094 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004095 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004096 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4097 goto end;
4098 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004099 max -= ret;
4100 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004101 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004102 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004103 }
4104
Willy Tarreau801250e2018-09-11 11:45:04 +02004105 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004106 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004107 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004108 if (!ret)
4109 goto end;
4110
4111 if (ret < 0) {
4112 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004113 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004114 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4115 goto end;
4116 }
4117
4118 size = chunk;
4119 h1m->curr_len = chunk;
4120 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004121 max -= ret;
4122 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004123 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004124 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004125 if (!size)
4126 goto send_empty;
4127 }
4128
4129 /* in MSG_DATA state, continue below */
4130 size = h1m->curr_len;
4131 break;
4132 }
4133
4134 /* we have in <size> the exact number of bytes we need to copy from
4135 * the H1 buffer. We need to check this against the connection's and
4136 * the stream's send windows, and to ensure that this fits in the max
4137 * frame size and in the buffer's available space minus 9 bytes (for
4138 * the frame header). The connection's flow control is applied last so
4139 * that we can use a separate list of streams which are immediately
4140 * unblocked on window opening. Note: we don't implement padding.
4141 */
4142
Willy Tarreau5dd17352018-06-14 13:33:30 +02004143 if (size > max)
4144 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004145
4146 if (size > h2s->mws)
4147 size = h2s->mws;
4148
4149 if (size <= 0) {
4150 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004151 if (h2s->send_wait) {
4152 LIST_DEL(&h2s->list);
4153 LIST_INIT(&h2s->list);
4154 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004155 goto end;
4156 }
4157
4158 if (h2c->mfs && size > h2c->mfs)
4159 size = h2c->mfs;
4160
4161 if (size + 9 > outbuf.size) {
4162 /* we have an opportunity for enlarging the too small
4163 * available space, let's try.
4164 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004165 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004166 goto realign_again;
4167 size = outbuf.size - 9;
4168 }
4169
4170 if (size <= 0) {
4171 h2c->flags |= H2_CF_MUX_MFULL;
4172 h2s->flags |= H2_SF_BLK_MROOM;
4173 goto end;
4174 }
4175
4176 if (size > h2c->mws)
4177 size = h2c->mws;
4178
4179 if (size <= 0) {
4180 h2s->flags |= H2_SF_BLK_MFCTL;
4181 goto end;
4182 }
4183
4184 /* copy whatever we can */
4185 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004186 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004187 if (ret == 1)
4188 len2 = 0;
4189
4190 if (!ret || len1 + len2 < size) {
4191 /* FIXME: must normally never happen */
4192 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4193 goto end;
4194 }
4195
4196 /* limit len1/len2 to size */
4197 if (len1 + len2 > size) {
4198 int sub = len1 + len2 - size;
4199
4200 if (len2 > sub)
4201 len2 -= sub;
4202 else {
4203 sub -= len2;
4204 len2 = 0;
4205 len1 -= sub;
4206 }
4207 }
4208
4209 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004210 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004211 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004212 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004213
4214 send_empty:
4215 /* we may need to add END_STREAM */
4216 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4217 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004218 *
4219 * FIXME: what we do here is not correct because we send end_stream
4220 * before knowing if we'll have to send a HEADERS frame for the
4221 * trailers. More importantly we're not consuming the trailing CRLF
4222 * after the end of trailers, so it will be left to the caller to
4223 * eat it. The right way to do it would be to measure trailers here
4224 * and to send ES only if there are no trailers.
4225 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004226 */
4227 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004228 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004229 es_now = 1;
4230
4231 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004232 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004233
4234 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004235 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004236
4237 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004238 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004239
4240 /* consume incoming H1 response */
4241 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004242 max -= size;
4243 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004244 total += size;
4245 h1m->curr_len -= size;
4246 h2s->mws -= size;
4247 h2c->mws -= size;
4248
4249 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004250 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004251 goto new_frame;
4252 }
4253 }
4254
4255 if (es_now) {
4256 if (h2s->st == H2_SS_OPEN)
4257 h2s->st = H2_SS_HLOC;
4258 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004259 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004260
Willy Tarreau35a62702018-02-27 15:37:25 +01004261 if (!(h1m->flags & H1_MF_CHNK)) {
4262 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004263 total += max;
4264 ofs += max;
4265 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004266
Willy Tarreau801250e2018-09-11 11:45:04 +02004267 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004268 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004269
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004270 h2s->flags |= H2_SF_ES_SENT;
4271 }
4272
4273 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004274 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004275 return total;
4276}
4277
Willy Tarreau115e83b2018-12-01 19:17:53 +01004278/* Try to send a HEADERS frame matching HTX response present in HTX message
4279 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4280 * must check the stream's status to detect any error which might have happened
4281 * subsequently to a successful send. The htx blocks are automatically removed
4282 * from the message. The htx message is assumed to be valid since produced from
4283 * the internal code, hence it contains a start line, an optional series of
4284 * header blocks and an end of header, otherwise an invalid frame could be
4285 * emitted and the resulting htx message could be left in an inconsistent state.
4286 */
4287static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4288{
4289 struct http_hdr list[MAX_HTTP_HDR];
4290 struct h2c *h2c = h2s->h2c;
4291 struct htx_blk *blk;
4292 struct htx_blk *blk_end;
4293 struct buffer outbuf;
4294 struct htx_sl *sl;
4295 enum htx_blk_type type;
4296 int es_now = 0;
4297 int ret = 0;
4298 int hdr;
4299 int idx;
4300
4301 if (h2c_mux_busy(h2c, h2s)) {
4302 h2s->flags |= H2_SF_BLK_MBUSY;
4303 return 0;
4304 }
4305
4306 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4307 h2c->flags |= H2_CF_MUX_MALLOC;
4308 h2s->flags |= H2_SF_BLK_MROOM;
4309 return 0;
4310 }
4311
4312 /* determine the first block which must not be deleted, blk_end may
4313 * be NULL if all blocks have to be deleted.
4314 */
4315 idx = htx_get_head(htx);
4316 blk_end = NULL;
4317 while (idx != -1) {
4318 type = htx_get_blk_type(htx_get_blk(htx, idx));
4319 idx = htx_get_next(htx, idx);
4320 if (type == HTX_BLK_EOH) {
4321 if (idx != -1)
4322 blk_end = htx_get_blk(htx, idx);
4323 break;
4324 }
4325 }
4326
4327 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004328 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004329 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004330 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004331 if (h2s->status < 100 || h2s->status > 999)
4332 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004333
4334 /* and the rest of the headers, that we dump starting at header 0 */
4335 hdr = 0;
4336
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004337 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004338 while ((idx = htx_get_next(htx, idx)) != -1) {
4339 blk = htx_get_blk(htx, idx);
4340 type = htx_get_blk_type(blk);
4341
4342 if (type == HTX_BLK_UNUSED)
4343 continue;
4344
4345 if (type != HTX_BLK_HDR)
4346 break;
4347
4348 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4349 goto fail;
4350
4351 list[hdr].n = htx_get_blk_name(htx, blk);
4352 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004353 hdr++;
4354 }
4355
4356 /* marker for end of headers */
4357 list[hdr].n = ist("");
4358
4359 if (h2s->status == 204 || h2s->status == 304) {
4360 /* no contents, claim c-len is present and set to zero */
4361 es_now = 1;
4362 }
4363
4364 chunk_reset(&outbuf);
4365
4366 while (1) {
4367 outbuf.area = b_tail(&h2c->mbuf);
4368 outbuf.size = b_contig_space(&h2c->mbuf);
4369 outbuf.data = 0;
4370
4371 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4372 break;
4373 realign_again:
4374 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4375 }
4376
4377 if (outbuf.size < 9)
4378 goto full;
4379
4380 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4381 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4382 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4383 outbuf.data = 9;
4384
4385 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004386 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004387 if (b_space_wraps(&h2c->mbuf))
4388 goto realign_again;
4389 goto full;
4390 }
4391
4392 /* encode all headers, stop at empty name */
4393 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4394 /* these ones do not exist in H2 and must be dropped. */
4395 if (isteq(list[hdr].n, ist("connection")) ||
4396 isteq(list[hdr].n, ist("proxy-connection")) ||
4397 isteq(list[hdr].n, ist("keep-alive")) ||
4398 isteq(list[hdr].n, ist("upgrade")) ||
4399 isteq(list[hdr].n, ist("transfer-encoding")))
4400 continue;
4401
4402 if (isteq(list[hdr].n, ist("")))
4403 break; // end
4404
4405 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4406 /* output full */
4407 if (b_space_wraps(&h2c->mbuf))
4408 goto realign_again;
4409 goto full;
4410 }
4411 }
4412
Christopher Faulet0b465482019-02-19 15:14:23 +01004413 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004414 * FIXME: we should also set it when we know for sure that the
4415 * content-length is zero as well as on 204/304
4416 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004417 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4418 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004419 es_now = 1;
4420
Willy Tarreau927b88b2019-03-04 08:03:25 +01004421 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004422 es_now = 1;
4423
4424 /* update the frame's size */
4425 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4426
4427 if (es_now)
4428 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4429
4430 /* commit the H2 response */
4431 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004432
4433 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4434 * 1xx responses, another HEADERS frame is expected.
4435 */
4436 if (h2s->status >= 200 || h2s->status == 101)
4437 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004438
Willy Tarreau115e83b2018-12-01 19:17:53 +01004439 if (es_now) {
4440 h2s->flags |= H2_SF_ES_SENT;
4441 if (h2s->st == H2_SS_OPEN)
4442 h2s->st = H2_SS_HLOC;
4443 else
4444 h2s_close(h2s);
4445 }
4446
4447 /* OK we could properly deliver the response */
4448
4449 /* remove all header blocks including the EOH and compute the
4450 * corresponding size.
4451 *
4452 * FIXME: We should remove everything when es_now is set.
4453 */
4454 ret = 0;
4455 idx = htx_get_head(htx);
4456 blk = htx_get_blk(htx, idx);
4457 while (blk != blk_end) {
4458 ret += htx_get_blksz(blk);
4459 blk = htx_remove_blk(htx, blk);
4460 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004461
4462 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4463 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004464 end:
4465 return ret;
4466 full:
4467 h2c->flags |= H2_CF_MUX_MFULL;
4468 h2s->flags |= H2_SF_BLK_MROOM;
4469 ret = 0;
4470 goto end;
4471 fail:
4472 /* unparsable HTX messages, too large ones to be produced in the local
4473 * list etc go here (unrecoverable errors).
4474 */
4475 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4476 ret = 0;
4477 goto end;
4478}
4479
Willy Tarreau80739692018-10-05 11:35:57 +02004480/* Try to send a HEADERS frame matching HTX request present in HTX message
4481 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4482 * must check the stream's status to detect any error which might have happened
4483 * subsequently to a successful send. The htx blocks are automatically removed
4484 * from the message. The htx message is assumed to be valid since produced from
4485 * the internal code, hence it contains a start line, an optional series of
4486 * header blocks and an end of header, otherwise an invalid frame could be
4487 * emitted and the resulting htx message could be left in an inconsistent state.
4488 */
4489static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4490{
4491 struct http_hdr list[MAX_HTTP_HDR];
4492 struct h2c *h2c = h2s->h2c;
4493 struct htx_blk *blk;
4494 struct htx_blk *blk_end;
4495 struct buffer outbuf;
4496 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004497 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004498 enum htx_blk_type type;
4499 int es_now = 0;
4500 int ret = 0;
4501 int hdr;
4502 int idx;
4503
4504 if (h2c_mux_busy(h2c, h2s)) {
4505 h2s->flags |= H2_SF_BLK_MBUSY;
4506 return 0;
4507 }
4508
4509 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4510 h2c->flags |= H2_CF_MUX_MALLOC;
4511 h2s->flags |= H2_SF_BLK_MROOM;
4512 return 0;
4513 }
4514
4515 /* determine the first block which must not be deleted, blk_end may
4516 * be NULL if all blocks have to be deleted.
4517 */
4518 idx = htx_get_head(htx);
4519 blk_end = NULL;
4520 while (idx != -1) {
4521 type = htx_get_blk_type(htx_get_blk(htx, idx));
4522 idx = htx_get_next(htx, idx);
4523 if (type == HTX_BLK_EOH) {
4524 if (idx != -1)
4525 blk_end = htx_get_blk(htx, idx);
4526 break;
4527 }
4528 }
4529
4530 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004531 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004532 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004533 meth = htx_sl_req_meth(sl);
4534 path = htx_sl_req_uri(sl);
4535
4536 /* and the rest of the headers, that we dump starting at header 0 */
4537 hdr = 0;
4538
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004539 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004540 while ((idx = htx_get_next(htx, idx)) != -1) {
4541 blk = htx_get_blk(htx, idx);
4542 type = htx_get_blk_type(blk);
4543
4544 if (type == HTX_BLK_UNUSED)
4545 continue;
4546
4547 if (type != HTX_BLK_HDR)
4548 break;
4549
4550 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4551 goto fail;
4552
4553 list[hdr].n = htx_get_blk_name(htx, blk);
4554 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004555 hdr++;
4556 }
4557
4558 /* marker for end of headers */
4559 list[hdr].n = ist("");
4560
4561 chunk_reset(&outbuf);
4562
4563 while (1) {
4564 outbuf.area = b_tail(&h2c->mbuf);
4565 outbuf.size = b_contig_space(&h2c->mbuf);
4566 outbuf.data = 0;
4567
4568 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4569 break;
4570 realign_again:
4571 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4572 }
4573
4574 if (outbuf.size < 9)
4575 goto full;
4576
4577 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4578 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4579 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4580 outbuf.data = 9;
4581
4582 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004583 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004584 if (b_space_wraps(&h2c->mbuf))
4585 goto realign_again;
4586 goto full;
4587 }
4588
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004589 /* RFC7540 #8.3: the CONNECT method must have :
4590 * - :authority set to the URI part (host:port)
4591 * - :method set to CONNECT
4592 * - :scheme and :path omitted
4593 */
4594 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4595 /* encode the scheme which is always "https" (or 0x86 for "http") */
4596 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4597 /* output full */
4598 if (b_space_wraps(&h2c->mbuf))
4599 goto realign_again;
4600 goto full;
4601 }
Willy Tarreau80739692018-10-05 11:35:57 +02004602
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004603 /* encode the path, which necessarily is the second one */
4604 if (!hpack_encode_path(&outbuf, path)) {
4605 /* output full */
4606 if (b_space_wraps(&h2c->mbuf))
4607 goto realign_again;
4608 goto full;
4609 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004610
4611 /* look for the Host header and place it in :authority */
4612 auth = ist2(NULL, 0);
4613 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4614 if (isteq(list[hdr].n, ist("")))
4615 break; // end
4616
4617 if (isteq(list[hdr].n, ist("host"))) {
4618 auth = list[hdr].v;
4619 break;
4620 }
4621 }
4622 }
4623 else {
4624 /* for CONNECT, :authority is taken from the path */
4625 auth = path;
4626 }
4627
4628 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4629 /* output full */
4630 if (b_space_wraps(&h2c->mbuf))
4631 goto realign_again;
4632 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004633 }
4634
4635 /* encode all headers, stop at empty name */
4636 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4637 /* these ones do not exist in H2 and must be dropped. */
4638 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004639 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004640 isteq(list[hdr].n, ist("proxy-connection")) ||
4641 isteq(list[hdr].n, ist("keep-alive")) ||
4642 isteq(list[hdr].n, ist("upgrade")) ||
4643 isteq(list[hdr].n, ist("transfer-encoding")))
4644 continue;
4645
4646 if (isteq(list[hdr].n, ist("")))
4647 break; // end
4648
4649 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4650 /* output full */
4651 if (b_space_wraps(&h2c->mbuf))
4652 goto realign_again;
4653 goto full;
4654 }
4655 }
4656
4657 /* we may need to add END_STREAM if we have no body :
4658 * - request already closed, or :
4659 * - no transfer-encoding, and :
4660 * - no content-length or content-length:0
4661 * Fixme: this doesn't take into account CONNECT requests.
4662 */
4663 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4664 es_now = 1;
4665
4666 if (sl->flags & HTX_SL_F_BODYLESS)
4667 es_now = 1;
4668
Willy Tarreau927b88b2019-03-04 08:03:25 +01004669 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004670 es_now = 1;
4671
4672 /* update the frame's size */
4673 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4674
4675 if (es_now)
4676 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4677
4678 /* commit the H2 response */
4679 b_add(&h2c->mbuf, outbuf.data);
4680 h2s->flags |= H2_SF_HEADERS_SENT;
4681 h2s->st = H2_SS_OPEN;
4682
Willy Tarreau80739692018-10-05 11:35:57 +02004683 if (es_now) {
4684 // trim any possibly pending data (eg: inconsistent content-length)
4685 h2s->flags |= H2_SF_ES_SENT;
4686 h2s->st = H2_SS_HLOC;
4687 }
4688
4689 /* remove all header blocks including the EOH and compute the
4690 * corresponding size.
4691 *
4692 * FIXME: We should remove everything when es_now is set.
4693 */
4694 ret = 0;
4695 idx = htx_get_head(htx);
4696 blk = htx_get_blk(htx, idx);
4697 while (blk != blk_end) {
4698 ret += htx_get_blksz(blk);
4699 blk = htx_remove_blk(htx, blk);
4700 }
4701
4702 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4703 htx_remove_blk(htx, blk_end);
4704
4705 end:
4706 return ret;
4707 full:
4708 h2c->flags |= H2_CF_MUX_MFULL;
4709 h2s->flags |= H2_SF_BLK_MROOM;
4710 ret = 0;
4711 goto end;
4712 fail:
4713 /* unparsable HTX messages, too large ones to be produced in the local
4714 * list etc go here (unrecoverable errors).
4715 */
4716 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4717 ret = 0;
4718 goto end;
4719}
4720
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004721/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004722 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4723 * caller must check the stream's status to detect any error which might have
4724 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004725 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4726 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004727static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004728{
4729 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004730 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004731 struct buffer outbuf;
4732 size_t total = 0;
4733 int es_now = 0;
4734 int bsize; /* htx block size */
4735 int fsize; /* h2 frame size */
4736 struct htx_blk *blk;
4737 enum htx_blk_type type;
4738 int idx;
4739
4740 if (h2c_mux_busy(h2c, h2s)) {
4741 h2s->flags |= H2_SF_BLK_MBUSY;
4742 goto end;
4743 }
4744
4745 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4746 h2c->flags |= H2_CF_MUX_MALLOC;
4747 h2s->flags |= H2_SF_BLK_MROOM;
4748 goto end;
4749 }
4750
Willy Tarreau98de12a2018-12-12 07:03:00 +01004751 htx = htx_from_buf(buf);
4752
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004753 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4754 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4755 * the caller to handle.
4756 */
4757
4758 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004759 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004760 goto end;
4761
4762 idx = htx_get_head(htx);
4763 blk = htx_get_blk(htx, idx);
4764 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4765 bsize = htx_get_blksz(blk);
4766 fsize = bsize;
4767
4768 if (type == HTX_BLK_EOD) {
4769 /* if we have an EOD, we're dealing with chunked data. We may
4770 * have a set of trailers after us that the caller will want to
4771 * deal with. Let's simply remove the EOD and return.
4772 */
4773 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004774 total++; // EOD counts as one byte
4775 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004776 goto end;
4777 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004778 else if (type == HTX_BLK_EOM) {
4779 if (h2s->flags & H2_SF_ES_SENT) {
4780 /* ES already sent */
4781 htx_remove_blk(htx, blk);
4782 total++; // EOM counts as one byte
4783 count--;
4784 goto end;
4785 }
4786 }
4787 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004788 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004789
4790 /* Perform some optimizations to reduce the number of buffer copies.
4791 * First, if the mux's buffer is empty and the htx area contains
4792 * exactly one data block of the same size as the requested count, and
4793 * this count fits within the frame size, the stream's window size, and
4794 * the connection's window size, then it's possible to simply swap the
4795 * caller's buffer with the mux's output buffer and adjust offsets and
4796 * length to match the entire DATA HTX block in the middle. In this
4797 * case we perform a true zero-copy operation from end-to-end. This is
4798 * the situation that happens all the time with large files. Second, if
4799 * this is not possible, but the mux's output buffer is empty, we still
4800 * have an opportunity to avoid the copy to the intermediary buffer, by
4801 * making the intermediary buffer's area point to the output buffer's
4802 * area. In this case we want to skip the HTX header to make sure that
4803 * copies remain aligned and that this operation remains possible all
4804 * the time. This goes for headers, data blocks and any data extracted
4805 * from the HTX blocks.
4806 */
4807 if (unlikely(fsize == count &&
4808 htx->used == 1 && type == HTX_BLK_DATA &&
4809 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4810 void *old_area = h2c->mbuf.area;
4811
4812 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004813 /* Too bad there are data left there. We're willing to memcpy/memmove
4814 * up to 1/4 of the buffer, which means that it's OK to copy a large
4815 * frame into a buffer containing few data if it needs to be realigned,
4816 * and that it's also OK to copy few data without realigning. Otherwise
4817 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004818 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004819 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4820 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4821 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004822 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004823
Willy Tarreau98de12a2018-12-12 07:03:00 +01004824 h2c->flags |= H2_CF_MUX_MFULL;
4825 h2s->flags |= H2_SF_BLK_MROOM;
4826 goto end;
4827 }
4828
4829 /* map an H2 frame to the HTX block so that we can put the
4830 * frame header there.
4831 */
4832 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004833 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004834 h2c->mbuf.data = fsize + 9;
4835 outbuf.area = b_head(&h2c->mbuf);
4836
4837 /* prepend an H2 DATA frame header just before the DATA block */
4838 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4839 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4840 h2_set_frame_size(outbuf.area, fsize);
4841
4842 /* update windows */
4843 h2s->mws -= fsize;
4844 h2c->mws -= fsize;
4845
4846 /* and exchange with our old area */
4847 buf->area = old_area;
4848 buf->data = buf->head = 0;
4849 total += fsize;
4850 goto end;
4851 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004852
Willy Tarreau98de12a2018-12-12 07:03:00 +01004853 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004854 /* for DATA and EOM we'll have to emit a frame, even if empty */
4855
4856 while (1) {
4857 outbuf.area = b_tail(&h2c->mbuf);
4858 outbuf.size = b_contig_space(&h2c->mbuf);
4859 outbuf.data = 0;
4860
4861 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4862 break;
4863 realign_again:
4864 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4865 }
4866
4867 if (outbuf.size < 9) {
4868 h2c->flags |= H2_CF_MUX_MFULL;
4869 h2s->flags |= H2_SF_BLK_MROOM;
4870 goto end;
4871 }
4872
4873 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4874 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4875 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4876 outbuf.data = 9;
4877
4878 /* we have in <fsize> the exact number of bytes we need to copy from
4879 * the HTX buffer. We need to check this against the connection's and
4880 * the stream's send windows, and to ensure that this fits in the max
4881 * frame size and in the buffer's available space minus 9 bytes (for
4882 * the frame header). The connection's flow control is applied last so
4883 * that we can use a separate list of streams which are immediately
4884 * unblocked on window opening. Note: we don't implement padding.
4885 */
4886
4887 /* EOM is presented with bsize==1 but would lead to the emission of an
4888 * empty frame, thus we force it to zero here.
4889 */
4890 if (type == HTX_BLK_EOM)
4891 bsize = fsize = 0;
4892
4893 if (!fsize)
4894 goto send_empty;
4895
4896 if (h2s->mws <= 0) {
4897 h2s->flags |= H2_SF_BLK_SFCTL;
4898 if (h2s->send_wait) {
4899 LIST_DEL(&h2s->list);
4900 LIST_INIT(&h2s->list);
4901 }
4902 goto end;
4903 }
4904
Willy Tarreauee573762018-12-04 15:25:57 +01004905 if (fsize > count)
4906 fsize = count;
4907
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004908 if (fsize > h2s->mws)
4909 fsize = h2s->mws; // >0
4910
4911 if (h2c->mfs && fsize > h2c->mfs)
4912 fsize = h2c->mfs; // >0
4913
4914 if (fsize + 9 > outbuf.size) {
4915 /* we have an opportunity for enlarging the too small
4916 * available space, let's try.
4917 * FIXME: is this really interesting to do? Maybe we'll
4918 * spend lots of time realigning instead of using two
4919 * frames.
4920 */
4921 if (b_space_wraps(&h2c->mbuf))
4922 goto realign_again;
4923 fsize = outbuf.size - 9;
4924
4925 if (fsize <= 0) {
4926 /* no need to send an empty frame here */
4927 h2c->flags |= H2_CF_MUX_MFULL;
4928 h2s->flags |= H2_SF_BLK_MROOM;
4929 goto end;
4930 }
4931 }
4932
4933 if (h2c->mws <= 0) {
4934 h2s->flags |= H2_SF_BLK_MFCTL;
4935 goto end;
4936 }
4937
4938 if (fsize > h2c->mws)
4939 fsize = h2c->mws;
4940
4941 /* now let's copy this this into the output buffer */
4942 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004943 h2s->mws -= fsize;
4944 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004945 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004946
4947 send_empty:
4948 /* update the frame's size */
4949 h2_set_frame_size(outbuf.area, fsize);
4950
4951 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4952 * meeting EOM. We should optimize this later.
4953 */
4954 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004955 total++; // EOM counts as one byte
4956 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004957 es_now = 1;
4958 }
4959
4960 if (es_now)
4961 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4962
4963 /* commit the H2 response */
4964 b_add(&h2c->mbuf, fsize + 9);
4965
4966 /* consume incoming HTX block, including EOM */
4967 total += fsize;
4968 if (fsize == bsize) {
4969 htx_remove_blk(htx, blk);
4970 if (fsize)
4971 goto new_frame;
4972 } else {
4973 /* we've truncated this block */
4974 htx_cut_data_blk(htx, blk, fsize);
4975 }
4976
4977 if (es_now) {
4978 if (h2s->st == H2_SS_OPEN)
4979 h2s->st = H2_SS_HLOC;
4980 else
4981 h2s_close(h2s);
4982
4983 h2s->flags |= H2_SF_ES_SENT;
4984 }
4985
4986 end:
4987 return total;
4988}
4989
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004990/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4991 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4992 * processed. The caller must check the stream's status to detect any error
4993 * which might have happened subsequently to a successful send. The htx blocks
4994 * are automatically removed from the message. The htx message is assumed to be
4995 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02004996 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004997 * as a single frame. The ES flag is always set.
4998 */
4999static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5000{
5001 struct http_hdr list[MAX_HTTP_HDR];
5002 struct h2c *h2c = h2s->h2c;
5003 struct htx_blk *blk;
5004 struct htx_blk *blk_end;
5005 struct buffer outbuf;
5006 struct h1m h1m;
5007 enum htx_blk_type type;
5008 uint32_t size;
5009 int ret = 0;
5010 int hdr;
5011 int idx;
5012 void *start;
5013
5014 if (h2c_mux_busy(h2c, h2s)) {
5015 h2s->flags |= H2_SF_BLK_MBUSY;
5016 goto end;
5017 }
5018
5019 if (!h2_get_buf(h2c, &h2c->mbuf)) {
5020 h2c->flags |= H2_CF_MUX_MALLOC;
5021 h2s->flags |= H2_SF_BLK_MROOM;
5022 goto end;
5023 }
5024
5025 /* The principle is that we parse each and every trailers block using
5026 * the H1 headers parser, and append it to the list. We don't proceed
5027 * until EOM is met. blk_end will point to the EOM block.
5028 */
5029 hdr = 0;
5030 memset(list, 0, sizeof(list));
5031 blk_end = NULL;
5032
5033 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5034 blk = htx_get_blk(htx, idx);
5035 type = htx_get_blk_type(blk);
5036
5037 if (type == HTX_BLK_UNUSED)
5038 continue;
5039
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005040 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005041 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005042
5043 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5044 goto fail;
5045
5046 size = htx_get_blksz(blk);
5047 start = htx_get_blk_ptr(htx, blk);
5048
5049 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5050 h1m.err_pos = 0;
5051 ret = h1_headers_to_hdr_list(start, start + size,
5052 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5053 &h1m, NULL);
5054 if (ret < 0)
5055 goto fail;
5056
5057 /* ret == 0 if an incomplete trailers block was found (missing
5058 * empty line), or > 0 if it was found. We have to continue on
5059 * incomplete messages because the trailers block might be
5060 * incomplete.
5061 */
5062
5063 /* search the new end */
5064 while (hdr <= sizeof(list)/sizeof(list[0])) {
5065 if (!list[hdr].n.len)
5066 break;
5067 hdr++;
5068 }
5069 }
5070
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005071 if (list[hdr].n.len != 0)
5072 goto fail; // empty trailer not found: internal error
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005073
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005074 chunk_reset(&outbuf);
5075
5076 while (1) {
5077 outbuf.area = b_tail(&h2c->mbuf);
5078 outbuf.size = b_contig_space(&h2c->mbuf);
5079 outbuf.data = 0;
5080
5081 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5082 break;
5083 realign_again:
5084 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5085 }
5086
5087 if (outbuf.size < 9)
5088 goto full;
5089
5090 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5091 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5092 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5093 outbuf.data = 9;
5094
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005095 /* encode all headers */
5096 for (idx = 0; idx < hdr; idx++) {
5097 /* these ones do not exist in H2 or must not appear in
5098 * trailers and must be dropped.
5099 */
5100 if (isteq(list[idx].n, ist("host")) ||
5101 isteq(list[idx].n, ist("content-length")) ||
5102 isteq(list[idx].n, ist("connection")) ||
5103 isteq(list[idx].n, ist("proxy-connection")) ||
5104 isteq(list[idx].n, ist("keep-alive")) ||
5105 isteq(list[idx].n, ist("upgrade")) ||
5106 isteq(list[idx].n, ist("te")) ||
5107 isteq(list[idx].n, ist("transfer-encoding")))
5108 continue;
5109
5110 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5111 /* output full */
5112 if (b_space_wraps(&h2c->mbuf))
5113 goto realign_again;
5114 goto full;
5115 }
5116 }
5117
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005118 if (outbuf.data == 9) {
5119 /* here we have a problem, we have nothing to emit (either we
5120 * received an empty trailers block followed or we removed its
5121 * contents above). Because of this we can't send a HEADERS
5122 * frame, so we have to cheat and instead send an empty DATA
5123 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005124 */
5125 outbuf.area[3] = H2_FT_DATA;
5126 outbuf.area[4] = H2_F_DATA_END_STREAM;
5127 }
5128
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005129 /* update the frame's size */
5130 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5131
5132 /* commit the H2 response */
5133 b_add(&h2c->mbuf, outbuf.data);
5134 h2s->flags |= H2_SF_ES_SENT;
5135
5136 if (h2s->st == H2_SS_OPEN)
5137 h2s->st = H2_SS_HLOC;
5138 else
5139 h2s_close(h2s);
5140
5141 /* OK we could properly deliver the response */
5142 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005143 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005144 ret = 0;
5145 idx = htx_get_head(htx);
5146 blk = htx_get_blk(htx, idx);
5147 while (blk != blk_end) {
5148 ret += htx_get_blksz(blk);
5149 blk = htx_remove_blk(htx, blk);
5150 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005151 end:
5152 return ret;
5153 full:
5154 h2c->flags |= H2_CF_MUX_MFULL;
5155 h2s->flags |= H2_SF_BLK_MROOM;
5156 ret = 0;
5157 goto end;
5158 fail:
5159 /* unparsable HTX messages, too large ones to be produced in the local
5160 * list etc go here (unrecoverable errors).
5161 */
5162 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5163 ret = 0;
5164 goto end;
5165}
5166
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005167/* Called from the upper layer, to subscribe to events, such as being able to send.
5168 * The <param> argument here is supposed to be a pointer to a wait_event struct
5169 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5170 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5171 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5172 * returns 0 except for the error above.
5173 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005174static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5175{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005176 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005177 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005178 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005179
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005180 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005181 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005182 if (!(sw->events & SUB_RETRY_RECV)) {
5183 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005184 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005185 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005186 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005187 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005188 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005189 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005190 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005191 if (!(sw->events & SUB_RETRY_SEND)) {
5192 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005193 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005194 h2s->send_wait = sw;
Olivier Houchard9a0f5592019-04-15 19:22:24 +02005195 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5196 LIST_ISEMPTY(&h2s->list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005197 if (h2s->flags & H2_SF_BLK_MFCTL)
5198 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5199 else
5200 LIST_ADDQ(&h2c->send_list, &h2s->list);
5201 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005202 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005203 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005204 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005205 if (event_type != 0)
5206 return -1;
5207 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005208}
5209
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005210/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5211 * The <param> argument here is supposed to be a pointer to the same wait_event
5212 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5213 * It always returns zero.
5214 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005215static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5216{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005217 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005218 struct h2s *h2s = cs->ctx;
5219
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005220 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005221 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005222 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005223 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005224 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005225 }
5226 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005227 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005228 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005229 if (h2s->send_wait == sw) {
5230 LIST_DEL(&h2s->list);
5231 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005232 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard998410a2019-04-15 19:23:37 +02005233 /* We were about to send, make sure it does not happen */
5234 if (!LIST_ISEMPTY(&h2s->sending_list) &&
5235 h2s->send_wait != &h2s->wait_event) {
5236 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5237 LIST_DEL_INIT(&h2s->sending_list);
5238 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005239 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02005240
Olivier Houchardd846c262018-10-19 17:24:29 +02005241 }
5242 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005243 return 0;
5244}
5245
5246
Olivier Houchard511efea2018-08-16 15:30:32 +02005247/* Called from the upper layer, to receive data */
5248static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5249{
Olivier Houchard638b7992018-08-16 15:41:52 +02005250 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005251 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005252 struct htx *h2s_htx = NULL;
5253 struct htx *buf_htx = NULL;
5254 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005255 size_t ret = 0;
5256
5257 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005258 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5259 /* in HTX mode we ignore the count argument */
5260 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005261 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005262 /* Here htx_to_buf() will set buffer data to 0 because
5263 * the HTX is empty.
5264 */
5265 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005266 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005267 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005268
5269 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005270 count = htx_free_data_space(buf_htx);
5271 if (flags & CO_RFL_KEEP_RSV) {
5272 if (count <= global.tune.maxrewrite)
5273 goto end;
5274 count -= global.tune.maxrewrite;
5275 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005276
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005277 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005278
5279 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5280 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5281
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005282 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005283 htx_to_buf(buf_htx, buf);
5284 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005285 ret = htx_ret.ret;
5286 }
5287 else {
5288 ret = b_xfer(buf, &h2s->rxbuf, count);
5289 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005290
Christopher Faulet37070b22019-02-14 15:12:14 +01005291 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005292 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005293 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005294 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005295 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005296 if (cs->flags & CS_FL_REOS)
5297 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005298 if (cs->flags & CS_FL_ERR_PENDING)
5299 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005300 if (b_size(&h2s->rxbuf)) {
5301 b_free(&h2s->rxbuf);
5302 offer_buffers(NULL, tasks_run_queue);
5303 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005304 }
5305
Willy Tarreau082f5592018-11-25 08:03:32 +01005306 if (ret && h2c->dsi == h2s->id) {
5307 /* demux is blocking on this stream's buffer */
5308 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005309 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005310 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005311
Olivier Houchard511efea2018-08-16 15:30:32 +02005312 return ret;
5313}
5314
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005315/* stops all senders of this connection for example when the mux buffer is full.
5316 * They are moved from the sending_list to either fctl_list or send_list.
5317 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005318static void h2_stop_senders(struct h2c *h2c)
5319{
5320 struct h2s *h2s, *h2s_back;
5321
Olivier Houchardd360ac62019-03-22 17:37:16 +01005322 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5323 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005324 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005325 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005326 }
5327}
5328
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005329/* Called from the upper layer, to send data from buffer <buf> for no more than
5330 * <count> bytes. Returns the number of bytes effectively sent. Some status
5331 * flags may be updated on the conn_stream.
5332 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005333static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005334{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005335 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005336 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005337 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005338 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005339 struct htx *htx;
5340 struct htx_blk *blk;
5341 enum htx_blk_type btype;
5342 uint32_t bsize;
5343 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005344
Olivier Houchardd360ac62019-03-22 17:37:16 +01005345 /* If we were not just woken because we wanted to send but couldn't,
5346 * and there's somebody else that is waiting to send, do nothing,
5347 * we will subscribe later and be put at the end of the list
5348 */
Olivier Houchard998410a2019-04-15 19:23:37 +02005349 if (LIST_ISEMPTY(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005350 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5351 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005352 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005353
Olivier Houchard998410a2019-04-15 19:23:37 +02005354 /* We couldn't set it to NULL before, because we needed it in case
5355 * we had to cancel the tasklet
5356 */
5357 h2s->send_wait = NULL;
5358
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005359 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5360 return 0;
5361
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005362 /* htx will be enough to decide if we're using HTX or legacy */
5363 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5364
Willy Tarreau0bad0432018-06-14 16:54:01 +02005365 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005366 h2s->flags |= H2_SF_OUTGOING_DATA;
5367
Willy Tarreau751f2d02018-10-05 09:35:00 +02005368 if (h2s->id == 0) {
5369 int32_t id = h2c_get_next_sid(h2s->h2c);
5370
5371 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005372 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005373 return 0;
5374 }
5375
5376 eb32_delete(&h2s->by_id);
5377 h2s->by_id.key = h2s->id = id;
5378 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005379 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005380 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5381 }
5382
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005383 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005384 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005385 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005386 idx = htx_get_head(htx);
5387 blk = htx_get_blk(htx, idx);
5388 btype = htx_get_blk_type(blk);
5389 bsize = htx_get_blksz(blk);
5390
5391 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005392 case HTX_BLK_REQ_SL:
5393 /* start-line before headers */
5394 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5395 if (ret > 0) {
5396 total += ret;
5397 count -= ret;
5398 if (ret < bsize)
5399 goto done;
5400 }
5401 break;
5402
Willy Tarreau115e83b2018-12-01 19:17:53 +01005403 case HTX_BLK_RES_SL:
5404 /* start-line before headers */
5405 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5406 if (ret > 0) {
5407 total += ret;
5408 count -= ret;
5409 if (ret < bsize)
5410 goto done;
5411 }
5412 break;
5413
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005414 case HTX_BLK_DATA:
5415 case HTX_BLK_EOD:
5416 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005417 /* all these cause the emission of a DATA frame (possibly empty).
5418 * This EOM necessarily is one before trailers, as the EOM following
5419 * trailers would have been consumed by the trailers parser.
5420 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005421 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005422 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005423 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005424 total += ret;
5425 count -= ret;
5426 if (ret < bsize)
5427 goto done;
5428 }
5429 break;
5430
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005431 case HTX_BLK_TLR:
5432 /* This is the first trailers block, all the subsequent ones AND
5433 * the EOM will be swallowed by the parser.
5434 */
5435 ret = h2s_htx_make_trailers(h2s, htx);
5436 if (ret > 0) {
5437 total += ret;
5438 count -= ret;
5439 if (ret < bsize)
5440 goto done;
5441 }
5442 break;
5443
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005444 default:
5445 htx_remove_blk(htx, blk);
5446 total += bsize;
5447 count -= bsize;
5448 break;
5449 }
5450 }
5451 goto done;
5452 }
5453
5454 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005455 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005456 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005457 if (h2s->h2c->flags & H2_CF_IS_BACK)
5458 ret = -1;
5459 else
5460 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005461 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005462 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005463 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005464 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005465 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005466 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005467 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005468
Willy Tarreau5dd17352018-06-14 13:33:30 +02005469 if (unlikely((int)ret <= 0)) {
5470 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005471 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5472 break;
5473 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005474 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005475 total += count;
5476 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005477 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005478 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005479 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005480 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005481 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005482 break;
5483 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005484
5485 total += ret;
5486 count -= ret;
5487
Willy Tarreau2b778482019-05-06 15:00:22 +02005488 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005489 break;
5490
5491 if (h2s->flags & H2_SF_BLK_ANY)
5492 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005493 }
5494
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005495 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005496 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005497 /* trim any possibly pending data after we close (extra CR-LF,
5498 * unprocessed trailers, abnormal extra data, ...)
5499 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005500 total += count;
5501 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005502 }
5503
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005504 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005505 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005506 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005507 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005508 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005509 }
5510
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005511 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005512 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005513 } else {
5514 b_del(buf, total);
5515 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005516
5517 /* The mux is full, cancel the pending tasks */
5518 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5519 (h2s->flags & H2_SF_BLK_MBUSY))
5520 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005521
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005522 /* If we're running HTX, and we read the whole buffer, then pretend
5523 * we read exactly what the caller specified, as with HTX the caller
5524 * will always give the buffer size, instead of the amount of data
5525 * available.
5526 */
5527 if (htx && !b_data(buf))
5528 total = orig_count;
5529
Olivier Houchard7505f942018-08-21 18:10:44 +02005530 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005531 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005532 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005533
Olivier Houchard7505f942018-08-21 18:10:44 +02005534 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005535 /* If we're waiting for flow control, and we got a shutr on the
5536 * connection, we will never be unlocked, so add an error on
5537 * the conn_stream.
5538 */
5539 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5540 !b_data(&h2s->h2c->dbuf) &&
5541 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5542 if (cs->flags & CS_FL_EOS)
5543 cs->flags |= CS_FL_ERROR;
5544 else
5545 cs->flags |= CS_FL_ERR_PENDING;
5546 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005547 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005548 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005549 LIST_DEL_INIT(&h2s->list);
5550 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005551 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005552}
5553
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005554/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005555static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005556{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005557 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005558 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005559 struct eb32_node *node;
5560 int fctl_cnt = 0;
5561 int send_cnt = 0;
5562 int tree_cnt = 0;
5563 int orph_cnt = 0;
5564
5565 if (!h2c)
5566 return;
5567
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005568 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005569 fctl_cnt++;
5570
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005571 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005572 send_cnt++;
5573
Willy Tarreau3af37712018-12-18 14:34:41 +01005574 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005575 node = eb32_first(&h2c->streams_by_id);
5576 while (node) {
5577 h2s = container_of(node, struct h2s, by_id);
5578 tree_cnt++;
5579 if (!h2s->cs)
5580 orph_cnt++;
5581 node = eb32_next(node);
5582 }
5583
Willy Tarreau987c0632018-12-18 10:32:05 +01005584 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5585 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5586 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u",
Willy Tarreau616ac812018-07-24 14:12:42 +02005587 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5588 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005589 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005590 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5591 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5592 h2c->msi,
5593 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5594 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5595
5596 if (h2s) {
5597 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5598 h2s, h2s->id, h2s->flags,
5599 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5600 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5601 h2s->cs);
5602 if (h2s->cs)
5603 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5604 h2s->cs->flags, h2s->cs->data);
5605 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005606}
Willy Tarreau62f52692017-10-08 23:01:42 +02005607
5608/*******************************************************/
5609/* functions below are dedicated to the config parsers */
5610/*******************************************************/
5611
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005612/* config parser for global "tune.h2.header-table-size" */
5613static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5614 struct proxy *defpx, const char *file, int line,
5615 char **err)
5616{
5617 if (too_many_args(1, args, err, NULL))
5618 return -1;
5619
5620 h2_settings_header_table_size = atoi(args[1]);
5621 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5622 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5623 return -1;
5624 }
5625 return 0;
5626}
Willy Tarreau62f52692017-10-08 23:01:42 +02005627
Willy Tarreaue6baec02017-07-27 11:45:11 +02005628/* config parser for global "tune.h2.initial-window-size" */
5629static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5630 struct proxy *defpx, const char *file, int line,
5631 char **err)
5632{
5633 if (too_many_args(1, args, err, NULL))
5634 return -1;
5635
5636 h2_settings_initial_window_size = atoi(args[1]);
5637 if (h2_settings_initial_window_size < 0) {
5638 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5639 return -1;
5640 }
5641 return 0;
5642}
5643
Willy Tarreau5242ef82017-07-27 11:47:28 +02005644/* config parser for global "tune.h2.max-concurrent-streams" */
5645static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5646 struct proxy *defpx, const char *file, int line,
5647 char **err)
5648{
5649 if (too_many_args(1, args, err, NULL))
5650 return -1;
5651
5652 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005653 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005654 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5655 return -1;
5656 }
5657 return 0;
5658}
5659
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005660/* config parser for global "tune.h2.max-frame-size" */
5661static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5662 struct proxy *defpx, const char *file, int line,
5663 char **err)
5664{
5665 if (too_many_args(1, args, err, NULL))
5666 return -1;
5667
5668 h2_settings_max_frame_size = atoi(args[1]);
5669 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5670 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5671 return -1;
5672 }
5673 return 0;
5674}
5675
Willy Tarreau62f52692017-10-08 23:01:42 +02005676
5677/****************************************/
5678/* MUX initialization and instanciation */
5679/***************************************/
5680
5681/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005682static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005683 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005684 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005685 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005686 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005687 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005688 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005689 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005690 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005691 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005692 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005693 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005694 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005695 .shutr = h2_shutr,
5696 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005697 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005698 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005699 .name = "H2",
5700};
5701
Christopher Faulet32f61c02018-04-10 14:33:41 +02005702/* PROTO selection : this mux registers PROTO token "h2" */
5703static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005704 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005705
Willy Tarreau0108d902018-11-25 19:14:37 +01005706INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5707
Christopher Faulet9b579102019-04-11 22:52:25 +02005708
5709/* The mux operations */
5710static const struct mux_ops h2_htx_ops = {
5711 .init = h2_init,
5712 .wake = h2_wake,
5713 .snd_buf = h2_snd_buf,
5714 .rcv_buf = h2_rcv_buf,
5715 .subscribe = h2_subscribe,
5716 .unsubscribe = h2_unsubscribe,
5717 .attach = h2_attach,
5718 .get_first_cs = h2_get_first_cs,
5719 .detach = h2_detach,
5720 .destroy = h2_destroy,
5721 .avail_streams = h2_avail_streams,
5722 .used_streams = h2_used_streams,
5723 .shutr = h2_shutr,
5724 .shutw = h2_shutw,
5725 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005726 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005727 .name = "H2",
5728};
5729
Willy Tarreauf8957272018-10-03 10:25:20 +02005730static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005731 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005732
5733INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5734
Willy Tarreau62f52692017-10-08 23:01:42 +02005735/* config keyword parsers */
5736static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005737 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005738 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005739 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005740 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005741 { 0, NULL, NULL }
5742}};
5743
Willy Tarreau0108d902018-11-25 19:14:37 +01005744INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);