blob: eca85cbe216e95a4a4d050f77ee336e4f06317d0 [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 Tarreauf6562792019-05-07 19:05:35 +0200578 task_destroy(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200579 if (h2c->wait_event.task)
580 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100581 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200582 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200583 return -1;
584}
585
Willy Tarreau751f2d02018-10-05 09:35:00 +0200586/* returns the next allocatable outgoing stream ID for the H2 connection, or
587 * -1 if no more is allocatable.
588 */
589static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
590{
591 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100592
593 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200594 id = -1;
595 return id;
596}
597
Willy Tarreau2373acc2017-10-12 17:35:14 +0200598/* returns the stream associated with id <id> or NULL if not found */
599static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
600{
601 struct eb32_node *node;
602
Willy Tarreau751f2d02018-10-05 09:35:00 +0200603 if (id == 0)
604 return (struct h2s *)h2_closed_stream;
605
Willy Tarreau2a856182017-05-16 15:20:39 +0200606 if (id > h2c->max_id)
607 return (struct h2s *)h2_idle_stream;
608
Willy Tarreau2373acc2017-10-12 17:35:14 +0200609 node = eb32_lookup(&h2c->streams_by_id, id);
610 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200611 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200612
613 return container_of(node, struct h2s, by_id);
614}
615
Christopher Faulet73c12072019-04-08 11:23:22 +0200616/* release function. This one should be called to free all resources allocated
617 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200618 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200619static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200620{
Christopher Faulet61840e72019-04-15 09:33:32 +0200621 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200622
Willy Tarreau32218eb2017-09-22 08:07:25 +0200623 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200624 /* The connection must be aattached to this mux to be released */
625 if (h2c->conn && h2c->conn->ctx == h2c)
626 conn = h2c->conn;
627
Willy Tarreau32218eb2017-09-22 08:07:25 +0200628 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200629
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100630 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100631 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100632 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200633
Willy Tarreau44e973f2018-03-01 17:49:30 +0100634 h2_release_buf(h2c, &h2c->dbuf);
635 h2_release_buf(h2c, &h2c->mbuf);
636
Willy Tarreauea392822017-10-31 10:02:25 +0100637 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200638 h2c->task->context = NULL;
639 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100640 h2c->task = NULL;
641 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200642 if (h2c->wait_event.task)
643 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200644 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100645 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200646 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100647
Willy Tarreaubafbe012017-11-24 17:34:44 +0100648 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200649 }
650
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200651 if (conn) {
652 conn->mux = NULL;
653 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200654
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200655 conn_stop_tracking(conn);
656 conn_full_close(conn);
657 if (conn->destroy_cb)
658 conn->destroy_cb(conn);
659 conn_free(conn);
660 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200661}
662
663
Willy Tarreau71681172017-10-23 14:39:06 +0200664/******************************************************/
665/* functions below are for the H2 protocol processing */
666/******************************************************/
667
668/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100669static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200670{
671 return h2s ? h2s->id : 0;
672}
673
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200674/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100675static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200676{
677 if (h2c->msi < 0)
678 return 0;
679
680 if (h2c->msi == h2s_id(h2s))
681 return 0;
682
683 return 1;
684}
685
Willy Tarreau741d6df2017-10-17 08:00:59 +0200686/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100687static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200688{
689 h2c->errcode = err;
690 h2c->st0 = H2_CS_ERROR;
691}
692
Willy Tarreau175cebb2019-01-24 10:02:24 +0100693/* marks an error on the stream. It may also update an already closed stream
694 * (e.g. to report an error after an RST was received).
695 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100696static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200697{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100698 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200699 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100700 if (h2s->st < H2_SS_ERROR)
701 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100702 if (h2s->cs)
703 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200704 }
705}
706
Willy Tarreau7e094452018-12-19 18:08:52 +0100707/* attempt to notify the data layer of recv availability */
708static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
709{
710 struct wait_event *sw;
711
712 if (h2s->recv_wait) {
713 sw = h2s->recv_wait;
714 sw->events &= ~SUB_RETRY_RECV;
715 tasklet_wakeup(sw->task);
716 h2s->recv_wait = NULL;
717 }
718}
719
720/* attempt to notify the data layer of send availability */
721static void __maybe_unused h2s_notify_send(struct h2s *h2s)
722{
723 struct wait_event *sw;
724
Olivier Houchard998410a2019-04-15 19:23:37 +0200725 if (h2s->send_wait && LIST_ISEMPTY(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100726 sw = h2s->send_wait;
727 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100728 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100729 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100730 }
731}
732
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100733/* alerts the data layer, trying to wake it up by all means, following
734 * this sequence :
735 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
736 * - if its subscribed to send, then it's woken up for send
737 * - if it was subscribed to neither, its ->wake() callback is called
738 * It is safe to call this function with a closed stream which doesn't have a
739 * conn_stream anymore.
740 */
741static void __maybe_unused h2s_alert(struct h2s *h2s)
742{
743 if (h2s->recv_wait || h2s->send_wait) {
744 h2s_notify_recv(h2s);
745 h2s_notify_send(h2s);
746 }
747 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
748 h2s->cs->data_cb->wake(h2s->cs);
749}
750
Willy Tarreaue4820742017-07-27 13:37:23 +0200751/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100752static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200753{
754 uint8_t *out = frame;
755
756 *out = len >> 16;
757 write_n16(out + 1, len);
758}
759
Willy Tarreau54c15062017-10-10 17:10:03 +0200760/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
761 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
762 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200763 * available in the buffer's input prior to calling this function. The buffer
764 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200765 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100766static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200767 const struct buffer *b, int o)
768{
Willy Tarreau591d4452018-06-15 17:21:00 +0200769 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 +0200770}
771
Willy Tarreau1f094672017-11-20 21:27:45 +0100772static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200773{
Willy Tarreau591d4452018-06-15 17:21:00 +0200774 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200775}
776
Willy Tarreau1f094672017-11-20 21:27:45 +0100777static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200778{
Willy Tarreau591d4452018-06-15 17:21:00 +0200779 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200780}
781
Willy Tarreau1f094672017-11-20 21:27:45 +0100782static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200783{
Willy Tarreau591d4452018-06-15 17:21:00 +0200784 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200785}
786
787
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100788/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
789 * The algorithm is not obvious. It turns out that H2 headers are neither
790 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
791 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200792 *
793 * b0 b1 b2 b3 b4 b5..b8
794 * +----------+---------+--------+----+----+----------------------+
795 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
796 * +----------+---------+--------+----+----+----------------------+
797 *
798 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
799 * we get the sid properly aligned and ordered, and 16 bits of len properly
800 * ordered as well. The type and flags can be extracted using bit shifts from
801 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200802 * Returns zero if some bytes are missing, otherwise non-zero on success. The
803 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200804 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100805static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200806{
807 uint64_t w;
808
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100809 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200810 return 0;
811
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100812 w = h2_get_n64(b, o + 1);
813 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200814 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
815 h->ff = w >> 32;
816 h->ft = w >> 40;
817 h->len += w >> 48;
818 return 1;
819}
820
821/* skip the next 9 bytes corresponding to the frame header possibly parsed by
822 * h2_peek_frame_hdr() above.
823 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100824static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200825{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200826 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200827}
828
829/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100830static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200831{
832 int ret;
833
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100834 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200835 if (ret > 0)
836 h2_skip_frame_hdr(b);
837 return ret;
838}
839
Willy Tarreau00dd0782018-03-01 16:31:34 +0100840/* marks stream <h2s> as CLOSED and decrement the number of active streams for
841 * its connection if the stream was not yet closed. Please use this exclusively
842 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100843 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100844static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100845{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100846 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100847 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100848 if (!h2s->id)
849 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100850 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100851 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100852 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
853 h2s_notify_recv(h2s);
854 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100855 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100856 h2s->st = H2_SS_CLOSED;
857}
858
Willy Tarreau71049cc2018-03-28 13:56:39 +0200859/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
860static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100861{
862 h2s_close(h2s);
863 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200864 if (b_size(&h2s->rxbuf)) {
865 b_free(&h2s->rxbuf);
866 offer_buffers(NULL, tasks_run_queue);
867 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200868 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100869 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200870 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100871 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800872 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200873 * reference left would be in the h2c send_list/fctl_list, and if
874 * we're in it, we're getting out anyway
875 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100876 LIST_DEL_INIT(&h2s->list);
Olivier Houchard998410a2019-04-15 19:23:37 +0200877 if (!LIST_ISEMPTY(&h2s->sending_list)) {
878 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
879 LIST_DEL_INIT(&h2s->sending_list);
880 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200881 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100882 pool_free(pool_head_h2s, h2s);
883}
884
Willy Tarreaua8e49542018-10-03 18:53:55 +0200885/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
886 * stream tree. In case of error, nothing is added and NULL is returned. The
887 * causes of errors can be any failed memory allocation. The caller is
888 * responsible for checking if the connection may support an extra stream
889 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200890 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200891static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200892{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200893 struct h2s *h2s;
894
Willy Tarreaubafbe012017-11-24 17:34:44 +0100895 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200896 if (!h2s)
897 goto out;
898
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200899 h2s->wait_event.task = tasklet_new();
900 if (!h2s->wait_event.task) {
901 pool_free(pool_head_h2s, h2s);
902 goto out;
903 }
904 h2s->send_wait = NULL;
905 h2s->recv_wait = NULL;
906 h2s->wait_event.task->process = h2_deferred_shut;
907 h2s->wait_event.task->context = h2s;
908 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100909 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200910 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100911 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200912 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200913 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200914 h2s->mws = h2c->miw;
915 h2s->flags = H2_SF_NONE;
916 h2s->errcode = H2_ERR_NO_ERROR;
917 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200918 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100919 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200920 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200921
922 if (h2c->flags & H2_CF_IS_BACK) {
923 h1m_init_req(&h2s->h1m);
924 h2s->h1m.err_pos = -1; // don't care about errors on the request path
925 h2s->h1m.flags |= H1_MF_TOLOWER;
926 } else {
927 h1m_init_res(&h2s->h1m);
928 h2s->h1m.err_pos = -1; // don't care about errors on the response path
929 h2s->h1m.flags |= H1_MF_TOLOWER;
930 }
931
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200932 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200933 if (id > 0)
934 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100935 else
936 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200937
938 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100939 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100940 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200941
942 return h2s;
943
944 out_free_h2s:
945 pool_free(pool_head_h2s, h2s);
946 out:
947 return NULL;
948}
949
950/* creates a new stream <id> on the h2c connection and returns it, or NULL in
951 * case of memory allocation error.
952 */
953static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
954{
955 struct session *sess = h2c->conn->owner;
956 struct conn_stream *cs;
957 struct h2s *h2s;
958
959 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
960 goto out;
961
962 h2s = h2s_new(h2c, id);
963 if (!h2s)
964 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200965
966 cs = cs_new(h2c->conn);
967 if (!cs)
968 goto out_close;
969
Olivier Houchard746fb772018-12-15 19:42:00 +0100970 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200971 h2s->cs = cs;
972 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200973 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200974
975 if (stream_create_from_cs(cs) < 0)
976 goto out_free_cs;
977
Willy Tarreau590a0512018-09-05 11:56:48 +0200978 /* We want the accept date presented to the next stream to be the one
979 * we have now, the handshake time to be null (since the next stream
980 * is not delayed by a handshake), and the idle time to count since
981 * right now.
982 */
983 sess->accept_date = date;
984 sess->tv_accept = now;
985 sess->t_handshake = 0;
986
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200987 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100988 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200989 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200990 return h2s;
991
992 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200993 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200994 cs_free(cs);
995 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200996 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200997 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200998 sess_log(sess);
999 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001000}
1001
Willy Tarreau751f2d02018-10-05 09:35:00 +02001002/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1003 * and returns it, or NULL in case of memory allocation error or if the highest
1004 * possible stream ID was reached.
1005 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001006static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001007{
1008 struct h2s *h2s = NULL;
1009
Willy Tarreau86949782019-01-31 10:42:05 +01001010 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001011 goto out;
1012
Willy Tarreaua80dca82019-01-24 17:08:28 +01001013 if (h2_streams_left(h2c) < 1)
1014 goto out;
1015
Willy Tarreau751f2d02018-10-05 09:35:00 +02001016 /* Defer choosing the ID until we send the first message to create the stream */
1017 h2s = h2s_new(h2c, 0);
1018 if (!h2s)
1019 goto out;
1020
1021 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001022 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001023 cs->ctx = h2s;
1024 h2c->nb_cs++;
1025
Willy Tarreau751f2d02018-10-05 09:35:00 +02001026 out:
1027 return h2s;
1028}
1029
Willy Tarreaube5b7152017-09-25 16:25:39 +02001030/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1031 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1032 * the various settings codes.
1033 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001034static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001035{
1036 struct buffer *res;
1037 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001038 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001039 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001040 int ret;
1041
1042 if (h2c_mux_busy(h2c, NULL)) {
1043 h2c->flags |= H2_CF_DEM_MBUSY;
1044 return 0;
1045 }
1046
Willy Tarreau44e973f2018-03-01 17:49:30 +01001047 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001048 if (!res) {
1049 h2c->flags |= H2_CF_MUX_MALLOC;
1050 h2c->flags |= H2_CF_DEM_MROOM;
1051 return 0;
1052 }
1053
1054 chunk_init(&buf, buf_data, sizeof(buf_data));
1055 chunk_memcpy(&buf,
1056 "\x00\x00\x00" /* length : 0 for now */
1057 "\x04\x00" /* type : 4 (settings), flags : 0 */
1058 "\x00\x00\x00\x00", /* stream ID : 0 */
1059 9);
1060
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001061 if (h2c->flags & H2_CF_IS_BACK) {
1062 /* send settings_enable_push=0 */
1063 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1064 }
1065
Willy Tarreaube5b7152017-09-25 16:25:39 +02001066 if (h2_settings_header_table_size != 4096) {
1067 char str[6] = "\x00\x01"; /* header_table_size */
1068
1069 write_n32(str + 2, h2_settings_header_table_size);
1070 chunk_memcat(&buf, str, 6);
1071 }
1072
1073 if (h2_settings_initial_window_size != 65535) {
1074 char str[6] = "\x00\x04"; /* initial_window_size */
1075
1076 write_n32(str + 2, h2_settings_initial_window_size);
1077 chunk_memcat(&buf, str, 6);
1078 }
1079
1080 if (h2_settings_max_concurrent_streams != 0) {
1081 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1082
1083 /* Note: 0 means "unlimited" for haproxy's config but not for
1084 * the protocol, so never send this value!
1085 */
1086 write_n32(str + 2, h2_settings_max_concurrent_streams);
1087 chunk_memcat(&buf, str, 6);
1088 }
1089
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001090 mfs = h2_settings_max_frame_size;
1091 if (mfs > global.tune.bufsize)
1092 mfs = global.tune.bufsize;
1093
1094 if (!mfs)
1095 mfs = global.tune.bufsize;
1096
1097 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001098 char str[6] = "\x00\x05"; /* max_frame_size */
1099
1100 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1101 * match bufsize - rewrite size, but at the moment it seems
1102 * that clients don't take care of it.
1103 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001104 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001105 chunk_memcat(&buf, str, 6);
1106 }
1107
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001108 h2_set_frame_size(buf.area, buf.data - 9);
1109 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001110 if (unlikely(ret <= 0)) {
1111 if (!ret) {
1112 h2c->flags |= H2_CF_MUX_MFULL;
1113 h2c->flags |= H2_CF_DEM_MROOM;
1114 return 0;
1115 }
1116 else {
1117 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1118 return 0;
1119 }
1120 }
1121 return ret;
1122}
1123
Willy Tarreau52eed752017-09-22 15:05:09 +02001124/* Try to receive a connection preface, then upon success try to send our
1125 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1126 * missing data. It may return an error in h2c.
1127 */
1128static int h2c_frt_recv_preface(struct h2c *h2c)
1129{
1130 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001131 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001132
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001133 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001134
1135 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001136 if (ret1 < 0)
1137 sess_log(h2c->conn->owner);
1138
Willy Tarreau52eed752017-09-22 15:05:09 +02001139 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1140 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1141 return 0;
1142 }
1143
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001144 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001145 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001146 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001147
Willy Tarreaube5b7152017-09-25 16:25:39 +02001148 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001149}
1150
Willy Tarreau01b44822018-10-03 14:26:37 +02001151/* Try to send a connection preface, then upon success try to send our
1152 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1153 * missing data. It may return an error in h2c.
1154 */
1155static int h2c_bck_send_preface(struct h2c *h2c)
1156{
1157 struct buffer *res;
1158
1159 if (h2c_mux_busy(h2c, NULL)) {
1160 h2c->flags |= H2_CF_DEM_MBUSY;
1161 return 0;
1162 }
1163
1164 res = h2_get_buf(h2c, &h2c->mbuf);
1165 if (!res) {
1166 h2c->flags |= H2_CF_MUX_MALLOC;
1167 h2c->flags |= H2_CF_DEM_MROOM;
1168 return 0;
1169 }
1170
1171 if (!b_data(res)) {
1172 /* preface not yet sent */
1173 b_istput(res, ist(H2_CONN_PREFACE));
1174 }
1175
1176 return h2c_send_settings(h2c);
1177}
1178
Willy Tarreau081d4722017-05-16 21:51:05 +02001179/* try to send a GOAWAY frame on the connection to report an error or a graceful
1180 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1181 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1182 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1183 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1184 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1185 * on unrecoverable failure. It will not attempt to send one again in this last
1186 * case so that it is safe to use h2c_error() to report such errors.
1187 */
1188static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1189{
1190 struct buffer *res;
1191 char str[17];
1192 int ret;
1193
1194 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1195 return 1; // claim that it worked
1196
1197 if (h2c_mux_busy(h2c, h2s)) {
1198 if (h2s)
1199 h2s->flags |= H2_SF_BLK_MBUSY;
1200 else
1201 h2c->flags |= H2_CF_DEM_MBUSY;
1202 return 0;
1203 }
1204
Willy Tarreau44e973f2018-03-01 17:49:30 +01001205 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001206 if (!res) {
1207 h2c->flags |= H2_CF_MUX_MALLOC;
1208 if (h2s)
1209 h2s->flags |= H2_SF_BLK_MROOM;
1210 else
1211 h2c->flags |= H2_CF_DEM_MROOM;
1212 return 0;
1213 }
1214
1215 /* len: 8, type: 7, flags: none, sid: 0 */
1216 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1217
1218 if (h2c->last_sid < 0)
1219 h2c->last_sid = h2c->max_id;
1220
1221 write_n32(str + 9, h2c->last_sid);
1222 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001223 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001224 if (unlikely(ret <= 0)) {
1225 if (!ret) {
1226 h2c->flags |= H2_CF_MUX_MFULL;
1227 if (h2s)
1228 h2s->flags |= H2_SF_BLK_MROOM;
1229 else
1230 h2c->flags |= H2_CF_DEM_MROOM;
1231 return 0;
1232 }
1233 else {
1234 /* we cannot report this error using GOAWAY, so we mark
1235 * it and claim a success.
1236 */
1237 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1238 h2c->flags |= H2_CF_GOAWAY_FAILED;
1239 return 1;
1240 }
1241 }
1242 h2c->flags |= H2_CF_GOAWAY_SENT;
1243 return ret;
1244}
1245
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001246/* Try to send an RST_STREAM frame on the connection for the indicated stream
1247 * during mux operations. This stream must be valid and cannot be closed
1248 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1249 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1250 * not yet.
1251 *
1252 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1253 * to write the message, it subscribes the stream to future notifications.
1254 */
1255static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1256{
1257 struct buffer *res;
1258 char str[13];
1259 int ret;
1260
1261 if (!h2s || h2s->st == H2_SS_CLOSED)
1262 return 1;
1263
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001264 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1265 * RST_STREAM in response to a RST_STREAM frame.
1266 */
1267 if (h2c->dft == H2_FT_RST_STREAM) {
1268 ret = 1;
1269 goto ignore;
1270 }
1271
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001272 if (h2c_mux_busy(h2c, h2s)) {
1273 h2s->flags |= H2_SF_BLK_MBUSY;
1274 return 0;
1275 }
1276
Willy Tarreau44e973f2018-03-01 17:49:30 +01001277 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001278 if (!res) {
1279 h2c->flags |= H2_CF_MUX_MALLOC;
1280 h2s->flags |= H2_SF_BLK_MROOM;
1281 return 0;
1282 }
1283
1284 /* len: 4, type: 3, flags: none */
1285 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1286 write_n32(str + 5, h2s->id);
1287 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001288 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001289
1290 if (unlikely(ret <= 0)) {
1291 if (!ret) {
1292 h2c->flags |= H2_CF_MUX_MFULL;
1293 h2s->flags |= H2_SF_BLK_MROOM;
1294 return 0;
1295 }
1296 else {
1297 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1298 return 0;
1299 }
1300 }
1301
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001302 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001303 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001304 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001305 return ret;
1306}
1307
1308/* Try to send an RST_STREAM frame on the connection for the stream being
1309 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001310 * error code, even if the stream is one of the dummy ones, and will update
1311 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001312 *
1313 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1314 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001315 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001316 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001317 */
1318static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1319{
1320 struct buffer *res;
1321 char str[13];
1322 int ret;
1323
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001324 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1325 * RST_STREAM in response to a RST_STREAM frame.
1326 */
1327 if (h2c->dft == H2_FT_RST_STREAM) {
1328 ret = 1;
1329 goto ignore;
1330 }
1331
Willy Tarreau27a84c92017-10-17 08:10:17 +02001332 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001333 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001334 return 0;
1335 }
1336
Willy Tarreau44e973f2018-03-01 17:49:30 +01001337 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001338 if (!res) {
1339 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001340 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001341 return 0;
1342 }
1343
1344 /* len: 4, type: 3, flags: none */
1345 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001346
Willy Tarreau27a84c92017-10-17 08:10:17 +02001347 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001348 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001349 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001350
Willy Tarreau27a84c92017-10-17 08:10:17 +02001351 if (unlikely(ret <= 0)) {
1352 if (!ret) {
1353 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001354 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001355 return 0;
1356 }
1357 else {
1358 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1359 return 0;
1360 }
1361 }
1362
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001363 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001364 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001365 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001366 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001367 }
1368
Willy Tarreau27a84c92017-10-17 08:10:17 +02001369 return ret;
1370}
1371
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001372/* try to send an empty DATA frame with the ES flag set to notify about the
1373 * end of stream and match a shutdown(write). If an ES was already sent as
1374 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1375 * on success or zero if nothing was done. In case of lack of room to write the
1376 * message, it subscribes the requesting stream to future notifications.
1377 */
1378static int h2_send_empty_data_es(struct h2s *h2s)
1379{
1380 struct h2c *h2c = h2s->h2c;
1381 struct buffer *res;
1382 char str[9];
1383 int ret;
1384
Willy Tarreau721c9742017-11-07 11:05:42 +01001385 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001386 return 1;
1387
1388 if (h2c_mux_busy(h2c, h2s)) {
1389 h2s->flags |= H2_SF_BLK_MBUSY;
1390 return 0;
1391 }
1392
Willy Tarreau44e973f2018-03-01 17:49:30 +01001393 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001394 if (!res) {
1395 h2c->flags |= H2_CF_MUX_MALLOC;
1396 h2s->flags |= H2_SF_BLK_MROOM;
1397 return 0;
1398 }
1399
1400 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1401 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1402 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001403 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001404 if (likely(ret > 0)) {
1405 h2s->flags |= H2_SF_ES_SENT;
1406 }
1407 else if (!ret) {
1408 h2c->flags |= H2_CF_MUX_MFULL;
1409 h2s->flags |= H2_SF_BLK_MROOM;
1410 return 0;
1411 }
1412 else {
1413 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1414 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001415 }
1416 return ret;
1417}
1418
Christopher Fauletf02ca002019-03-07 16:21:34 +01001419/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1420 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1421 * connection. The stream's state is automatically updated accordingly. If the
1422 * stream is orphaned, it is destroyed.
1423 */
1424static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1425{
1426 if (!h2s->cs) {
1427 /* this stream was already orphaned */
1428 h2s_destroy(h2s);
1429 return;
1430 }
1431
1432 h2s->cs->flags |= flags;
1433 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1434 h2s->cs->flags |= CS_FL_ERROR;
1435
1436 h2s_alert(h2s);
1437
1438 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1439 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001440 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001441 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001442 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001443 h2s_close(h2s);
1444}
1445
1446/* wake the streams attached to the connection, whose id is greater than <last>
1447 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001448 */
1449static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1450{
1451 struct eb32_node *node;
1452 struct h2s *h2s;
1453
1454 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001455 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001456
1457 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001458 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001459
Christopher Fauletf02ca002019-03-07 16:21:34 +01001460 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001461 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1462 while (node) {
1463 h2s = container_of(node, struct h2s, by_id);
1464 if (h2s->id <= last)
1465 break;
1466 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001467 h2s_wake_one_stream(h2s, flags);
1468 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001469
Christopher Fauletf02ca002019-03-07 16:21:34 +01001470 /* Wake all streams with unassigned ID (ID == 0) */
1471 node = eb32_lookup(&h2c->streams_by_id, 0);
1472 while (node) {
1473 h2s = container_of(node, struct h2s, by_id);
1474 if (h2s->id > 0)
1475 break;
1476 node = eb32_next(node);
1477 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001478 }
1479}
1480
Willy Tarreau3421aba2017-07-27 15:41:03 +02001481/* Increase all streams' outgoing window size by the difference passed in
1482 * argument. This is needed upon receipt of the settings frame if the initial
1483 * window size is different. The difference may be negative and the resulting
1484 * window size as well, for the time it takes to receive some window updates.
1485 */
1486static void h2c_update_all_ws(struct h2c *h2c, int diff)
1487{
1488 struct h2s *h2s;
1489 struct eb32_node *node;
1490
1491 if (!diff)
1492 return;
1493
1494 node = eb32_first(&h2c->streams_by_id);
1495 while (node) {
1496 h2s = container_of(node, struct h2s, by_id);
1497 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001498
1499 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1500 h2s->flags &= ~H2_SF_BLK_SFCTL;
1501 if (h2s->send_wait)
1502 LIST_ADDQ(&h2c->send_list, &h2s->list);
1503
1504 }
1505
Willy Tarreau3421aba2017-07-27 15:41:03 +02001506 node = eb32_next(node);
1507 }
1508}
1509
1510/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1511 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001512 * return an error in h2c. The caller must have already verified frame length
1513 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001514 */
1515static int h2c_handle_settings(struct h2c *h2c)
1516{
1517 unsigned int offset;
1518 int error;
1519
1520 if (h2c->dff & H2_F_SETTINGS_ACK) {
1521 if (h2c->dfl) {
1522 error = H2_ERR_FRAME_SIZE_ERROR;
1523 goto fail;
1524 }
1525 return 1;
1526 }
1527
Willy Tarreau3421aba2017-07-27 15:41:03 +02001528 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001529 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001530 return 0;
1531
1532 /* parse the frame */
1533 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001534 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1535 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001536
1537 switch (type) {
1538 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1539 /* we need to update all existing streams with the
1540 * difference from the previous iws.
1541 */
1542 if (arg < 0) { // RFC7540#6.5.2
1543 error = H2_ERR_FLOW_CONTROL_ERROR;
1544 goto fail;
1545 }
1546 h2c_update_all_ws(h2c, arg - h2c->miw);
1547 h2c->miw = arg;
1548 break;
1549 case H2_SETTINGS_MAX_FRAME_SIZE:
1550 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1551 error = H2_ERR_PROTOCOL_ERROR;
1552 goto fail;
1553 }
1554 h2c->mfs = arg;
1555 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001556 case H2_SETTINGS_ENABLE_PUSH:
1557 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1558 error = H2_ERR_PROTOCOL_ERROR;
1559 goto fail;
1560 }
1561 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001562 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1563 if (h2c->flags & H2_CF_IS_BACK) {
1564 /* the limit is only for the backend; for the frontend it is our limit */
1565 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1566 arg = h2_settings_max_concurrent_streams;
1567 h2c->streams_limit = arg;
1568 }
1569 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001570 }
1571 }
1572
1573 /* need to ACK this frame now */
1574 h2c->st0 = H2_CS_FRAME_A;
1575 return 1;
1576 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001577 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001578 h2c_error(h2c, error);
1579 return 0;
1580}
1581
1582/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1583 * success or one of the h2_status values.
1584 */
1585static int h2c_ack_settings(struct h2c *h2c)
1586{
1587 struct buffer *res;
1588 char str[9];
1589 int ret = -1;
1590
1591 if (h2c_mux_busy(h2c, NULL)) {
1592 h2c->flags |= H2_CF_DEM_MBUSY;
1593 return 0;
1594 }
1595
Willy Tarreau44e973f2018-03-01 17:49:30 +01001596 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001597 if (!res) {
1598 h2c->flags |= H2_CF_MUX_MALLOC;
1599 h2c->flags |= H2_CF_DEM_MROOM;
1600 return 0;
1601 }
1602
1603 memcpy(str,
1604 "\x00\x00\x00" /* length : 0 (no data) */
1605 "\x04" "\x01" /* type : 4, flags : ACK */
1606 "\x00\x00\x00\x00" /* stream ID */, 9);
1607
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001608 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001609 if (unlikely(ret <= 0)) {
1610 if (!ret) {
1611 h2c->flags |= H2_CF_MUX_MFULL;
1612 h2c->flags |= H2_CF_DEM_MROOM;
1613 return 0;
1614 }
1615 else {
1616 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1617 return 0;
1618 }
1619 }
1620 return ret;
1621}
1622
Willy Tarreaucf68c782017-10-10 17:11:41 +02001623/* processes a PING frame and schedules an ACK if needed. The caller must pass
1624 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001625 * missing data. The caller must have already verified frame length
1626 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001627 */
1628static int h2c_handle_ping(struct h2c *h2c)
1629{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001630 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001631 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001632 h2c->st0 = H2_CS_FRAME_A;
1633 return 1;
1634}
1635
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001636/* Try to send a window update for stream id <sid> and value <increment>.
1637 * Returns > 0 on success or zero on missing room or failure. It may return an
1638 * error in h2c.
1639 */
1640static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1641{
1642 struct buffer *res;
1643 char str[13];
1644 int ret = -1;
1645
1646 if (h2c_mux_busy(h2c, NULL)) {
1647 h2c->flags |= H2_CF_DEM_MBUSY;
1648 return 0;
1649 }
1650
Willy Tarreau44e973f2018-03-01 17:49:30 +01001651 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001652 if (!res) {
1653 h2c->flags |= H2_CF_MUX_MALLOC;
1654 h2c->flags |= H2_CF_DEM_MROOM;
1655 return 0;
1656 }
1657
1658 /* length: 4, type: 8, flags: none */
1659 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1660 write_n32(str + 5, sid);
1661 write_n32(str + 9, increment);
1662
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001663 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001664
1665 if (unlikely(ret <= 0)) {
1666 if (!ret) {
1667 h2c->flags |= H2_CF_MUX_MFULL;
1668 h2c->flags |= H2_CF_DEM_MROOM;
1669 return 0;
1670 }
1671 else {
1672 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1673 return 0;
1674 }
1675 }
1676 return ret;
1677}
1678
1679/* try to send pending window update for the connection. It's safe to call it
1680 * with no pending updates. Returns > 0 on success or zero on missing room or
1681 * failure. It may return an error in h2c.
1682 */
1683static int h2c_send_conn_wu(struct h2c *h2c)
1684{
1685 int ret = 1;
1686
1687 if (h2c->rcvd_c <= 0)
1688 return 1;
1689
Willy Tarreau97aaa672018-12-23 09:49:04 +01001690 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1691 /* increase the advertised connection window to 2G on
1692 * first update.
1693 */
1694 h2c->flags |= H2_CF_WINDOW_OPENED;
1695 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1696 }
1697
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001698 /* send WU for the connection */
1699 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1700 if (ret > 0)
1701 h2c->rcvd_c = 0;
1702
1703 return ret;
1704}
1705
1706/* try to send pending window update for the current dmux stream. It's safe to
1707 * call it with no pending updates. Returns > 0 on success or zero on missing
1708 * room or failure. It may return an error in h2c.
1709 */
1710static int h2c_send_strm_wu(struct h2c *h2c)
1711{
1712 int ret = 1;
1713
1714 if (h2c->rcvd_s <= 0)
1715 return 1;
1716
1717 /* send WU for the stream */
1718 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1719 if (ret > 0)
1720 h2c->rcvd_s = 0;
1721
1722 return ret;
1723}
1724
Willy Tarreaucf68c782017-10-10 17:11:41 +02001725/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1726 * success, 0 on missing data or one of the h2_status values.
1727 */
1728static int h2c_ack_ping(struct h2c *h2c)
1729{
1730 struct buffer *res;
1731 char str[17];
1732 int ret = -1;
1733
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001734 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001735 return 0;
1736
1737 if (h2c_mux_busy(h2c, NULL)) {
1738 h2c->flags |= H2_CF_DEM_MBUSY;
1739 return 0;
1740 }
1741
Willy Tarreau44e973f2018-03-01 17:49:30 +01001742 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001743 if (!res) {
1744 h2c->flags |= H2_CF_MUX_MALLOC;
1745 h2c->flags |= H2_CF_DEM_MROOM;
1746 return 0;
1747 }
1748
1749 memcpy(str,
1750 "\x00\x00\x08" /* length : 8 (same payload) */
1751 "\x06" "\x01" /* type : 6, flags : ACK */
1752 "\x00\x00\x00\x00" /* stream ID */, 9);
1753
1754 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001755 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001756
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001757 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001758 if (unlikely(ret <= 0)) {
1759 if (!ret) {
1760 h2c->flags |= H2_CF_MUX_MFULL;
1761 h2c->flags |= H2_CF_DEM_MROOM;
1762 return 0;
1763 }
1764 else {
1765 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1766 return 0;
1767 }
1768 }
1769 return ret;
1770}
1771
Willy Tarreau26f95952017-07-27 17:18:30 +02001772/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1773 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001774 * h2c or h2s. The caller must have already verified frame length and stream ID
1775 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001776 */
1777static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1778{
1779 int32_t inc;
1780 int error;
1781
Willy Tarreau26f95952017-07-27 17:18:30 +02001782 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001783 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001784 return 0;
1785
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001786 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001787
1788 if (h2c->dsi != 0) {
1789 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001790
1791 /* it's not an error to receive WU on a closed stream */
1792 if (h2s->st == H2_SS_CLOSED)
1793 return 1;
1794
1795 if (!inc) {
1796 error = H2_ERR_PROTOCOL_ERROR;
1797 goto strm_err;
1798 }
1799
1800 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1801 error = H2_ERR_FLOW_CONTROL_ERROR;
1802 goto strm_err;
1803 }
1804
1805 h2s->mws += inc;
1806 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1807 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001808 if (h2s->send_wait)
1809 LIST_ADDQ(&h2c->send_list, &h2s->list);
1810
Willy Tarreau26f95952017-07-27 17:18:30 +02001811 }
1812 }
1813 else {
1814 /* connection window update */
1815 if (!inc) {
1816 error = H2_ERR_PROTOCOL_ERROR;
1817 goto conn_err;
1818 }
1819
1820 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1821 error = H2_ERR_FLOW_CONTROL_ERROR;
1822 goto conn_err;
1823 }
1824
1825 h2c->mws += inc;
1826 }
1827
1828 return 1;
1829
1830 conn_err:
1831 h2c_error(h2c, error);
1832 return 0;
1833
1834 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001835 h2s_error(h2s, error);
1836 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001837 return 0;
1838}
1839
Willy Tarreaue96b0922017-10-30 00:28:29 +01001840/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001841 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1842 * have already verified frame length and stream ID validity. Described in
1843 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001844 */
1845static int h2c_handle_goaway(struct h2c *h2c)
1846{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001847 int last;
1848
Willy Tarreaue96b0922017-10-30 00:28:29 +01001849 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001850 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001851 return 0;
1852
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001853 last = h2_get_n32(&h2c->dbuf, 0);
1854 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001855 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001856 if (h2c->last_sid < 0)
1857 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001858 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001859}
1860
Willy Tarreau92153fc2017-12-03 19:46:19 +01001861/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001862 * invalid. Returns > 0 on success or zero on missing data. It may return an
1863 * error in h2c. The caller must have already verified frame length and stream
1864 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001865 */
1866static int h2c_handle_priority(struct h2c *h2c)
1867{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001868 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001869 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001870 return 0;
1871
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001872 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001873 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001874 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1875 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001876 }
1877 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001878}
1879
Willy Tarreaucd234e92017-08-18 10:59:39 +02001880/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001881 * Returns > 0 on success or zero on missing data. The caller must have already
1882 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001883 */
1884static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1885{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001886 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001887 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001888 return 0;
1889
1890 /* late RST, already handled */
1891 if (h2s->st == H2_SS_CLOSED)
1892 return 1;
1893
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001894 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001895 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001896
1897 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001898 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001899 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001900 }
1901
1902 h2s->flags |= H2_SF_RST_RCVD;
1903 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001904}
1905
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001906/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1907 * It may return an error in h2c or h2s. The caller must consider that the
1908 * return value is the new h2s in case one was allocated (most common case).
1909 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001910 * errors here are reported as connection errors since it's impossible to
1911 * recover from such errors after the compression context has been altered.
1912 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001913static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001914{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001915 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001916 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001917 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001918 int error;
1919
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001920 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001921 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001922
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001923 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001924 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001925
1926 /* now either the frame is complete or the buffer is complete */
1927 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001928 /* The stream exists/existed, this must be a trailers frame */
1929 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02001930 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
1931 /* unrecoverable error ? */
1932 if (h2c->st0 >= H2_CS_ERROR)
1933 goto out;
1934
1935 if (error == 0)
1936 goto out; // missing data
1937
1938 if (error < 0) {
1939 /* Failed to decode this frame (e.g. too large request)
1940 * but the HPACK decompressor is still synchronized.
1941 */
1942 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1943 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01001944 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02001945 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01001946 goto done;
1947 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001948 /* the connection was already killed by an RST, let's consume
1949 * the data and send another RST.
1950 */
1951 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1952 h2s = (struct h2s*)h2_error_stream;
1953 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001954 }
1955 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1956 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1957 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001958 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001959 goto conn_err;
1960 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001961 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1962 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001963
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001964 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001965
Willy Tarreau25919232019-01-03 14:48:18 +01001966 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001967 if (h2c->st0 >= H2_CS_ERROR)
1968 goto out;
1969
Willy Tarreau25919232019-01-03 14:48:18 +01001970 if (error <= 0) {
1971 if (error == 0)
1972 goto out; // missing data
1973
1974 /* Failed to decode this stream (e.g. too large request)
1975 * but the HPACK decompressor is still synchronized.
1976 */
1977 h2s = (struct h2s*)h2_error_stream;
1978 goto send_rst;
1979 }
1980
Willy Tarreau22de8d32018-09-05 19:55:58 +02001981 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001982 * positively from h2c_frt_stream_new(), the stream will report the error,
1983 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001984 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001985 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001986 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001987 h2s = (struct h2s*)h2_refused_stream;
1988 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001989 }
1990
1991 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001992 h2s->rxbuf = rxbuf;
1993 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001994 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001995
Willy Tarreau88d138e2019-01-02 19:38:14 +01001996 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001997 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001998 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001999
2000 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002001 if (h2s->cs)
2002 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002003 if (h2s->st == H2_SS_OPEN)
2004 h2s->st = H2_SS_HREM;
2005 else
2006 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002007 }
2008
Willy Tarreau3a429f02019-01-03 11:41:50 +01002009 /* update the max stream ID if the request is being processed */
2010 if (h2s->id > h2c->max_id)
2011 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002012
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002013 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002014
2015 conn_err:
2016 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002017 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002018
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002019 out:
2020 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002021 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002022
2023 send_rst:
2024 /* make the demux send an RST for the current stream. We may only
2025 * do this if we're certain that the HEADERS frame was properly
2026 * decompressed so that the HPACK decoder is still kept up to date.
2027 */
2028 h2_release_buf(h2c, &rxbuf);
2029 h2c->st0 = H2_CS_FRAME_E;
2030 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002031}
2032
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002033/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2034 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2035 * errors here are reported as connection errors since it's impossible to
2036 * recover from such errors after the compression context has been altered.
2037 */
2038static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2039{
2040 int error;
2041
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002042 if (!b_size(&h2c->dbuf))
2043 return NULL; // empty buffer
2044
2045 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2046 return NULL; // incomplete frame
2047
Willy Tarreau1915ca22019-01-24 11:49:37 +01002048 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002049
Willy Tarreau25919232019-01-03 14:48:18 +01002050 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002051 if (h2c->st0 >= H2_CS_ERROR)
2052 return NULL;
2053
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002054 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2055 /* RFC7540#5.1 */
2056 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2057 h2c->st0 = H2_CS_FRAME_E;
2058 return NULL;
2059 }
2060
Willy Tarreau25919232019-01-03 14:48:18 +01002061 if (error <= 0) {
2062 if (error == 0)
2063 return NULL; // missing data
2064
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002065 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002066 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002067 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002068 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002069 }
2070
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002071 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2072 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002073 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002074 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002075 }
2076
Willy Tarreau927b88b2019-03-04 08:03:25 +01002077 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002078 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002079 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 +02002080 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002081 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002082 h2s_close(h2s);
2083
2084 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002085}
2086
Willy Tarreau454f9052017-10-26 19:40:35 +02002087/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2088 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2089 */
2090static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2091{
2092 int error;
2093
2094 /* note that empty DATA frames are perfectly valid and sometimes used
2095 * to signal an end of stream (with the ES flag).
2096 */
2097
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002098 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002099 return 0; // empty buffer
2100
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002101 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002102 return 0; // incomplete frame
2103
2104 /* now either the frame is complete or the buffer is complete */
2105
Willy Tarreau454f9052017-10-26 19:40:35 +02002106 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2107 /* RFC7540#6.1 */
2108 error = H2_ERR_STREAM_CLOSED;
2109 goto strm_err;
2110 }
2111
Willy Tarreau1915ca22019-01-24 11:49:37 +01002112 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2113 /* RFC7540#8.1.2 */
2114 error = H2_ERR_PROTOCOL_ERROR;
2115 goto strm_err;
2116 }
2117
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002118 if (!h2_frt_transfer_data(h2s))
2119 return 0;
2120
Willy Tarreau454f9052017-10-26 19:40:35 +02002121 /* call the upper layers to process the frame, then let the upper layer
2122 * notify the stream about any change.
2123 */
2124 if (!h2s->cs) {
2125 error = H2_ERR_STREAM_CLOSED;
2126 goto strm_err;
2127 }
2128
Willy Tarreau8f650c32017-11-21 19:36:21 +01002129 if (h2c->st0 >= H2_CS_ERROR)
2130 return 0;
2131
Willy Tarreau721c9742017-11-07 11:05:42 +01002132 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002133 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002134 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002135 }
2136
2137 /* check for completion : the callee will change this to FRAME_A or
2138 * FRAME_H once done.
2139 */
2140 if (h2c->st0 == H2_CS_FRAME_P)
2141 return 0;
2142
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002143 /* last frame */
2144 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002145 if (h2s->cs)
2146 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002147 if (h2s->st == H2_SS_OPEN)
2148 h2s->st = H2_SS_HREM;
2149 else
2150 h2s_close(h2s);
2151
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002152 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002153
2154 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2155 /* RFC7540#8.1.2 */
2156 error = H2_ERR_PROTOCOL_ERROR;
2157 goto strm_err;
2158 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002159 }
2160
Willy Tarreau454f9052017-10-26 19:40:35 +02002161 return 1;
2162
Willy Tarreau454f9052017-10-26 19:40:35 +02002163 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002164 h2s_error(h2s, error);
2165 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002166 return 0;
2167}
2168
Willy Tarreaubc933932017-10-09 16:21:43 +02002169/* process Rx frames to be demultiplexed */
2170static void h2_process_demux(struct h2c *h2c)
2171{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002172 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002173 struct h2_fh hdr;
2174 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002175
Willy Tarreau081d4722017-05-16 21:51:05 +02002176 if (h2c->st0 >= H2_CS_ERROR)
2177 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002178
2179 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2180 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002181 if (h2c->flags & H2_CF_IS_BACK)
2182 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002183 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2184 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002185 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002186 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002187 sess_log(h2c->conn->owner);
2188 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002189 goto fail;
2190 }
2191
2192 h2c->max_id = 0;
2193 h2c->st0 = H2_CS_SETTINGS1;
2194 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002195
2196 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002197 /* ensure that what is pending is a valid SETTINGS frame
2198 * without an ACK.
2199 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002200 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002201 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002202 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002203 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002204 sess_log(h2c->conn->owner);
2205 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002206 goto fail;
2207 }
2208
2209 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2210 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2211 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2212 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002213 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002214 goto fail;
2215 }
2216
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002217 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002218 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2219 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2220 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002221 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002222 goto fail;
2223 }
2224
Willy Tarreau3bf69182018-12-21 15:34:50 +01002225 /* that's OK, switch to FRAME_P to process it. This is
2226 * a SETTINGS frame whose header has already been
2227 * deleted above.
2228 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002229 padlen = 0;
2230 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002231 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002232 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002233
2234 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002235 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002236 int ret = 0;
2237
2238 if (h2c->st0 >= H2_CS_ERROR)
2239 break;
2240
2241 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002242 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002243 break;
2244
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002245 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002246 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002247 if (!h2c->nb_streams) {
2248 /* only log if no other stream can report the error */
2249 sess_log(h2c->conn->owner);
2250 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002251 break;
2252 }
2253
Willy Tarreau3bf69182018-12-21 15:34:50 +01002254 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2255 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2256 * we read the pad length and drop it from the remaining
2257 * payload (one byte + the 9 remaining ones = 10 total
2258 * removed), so we have a frame payload starting after the
2259 * pad len. Flow controlled frames (DATA) also count the
2260 * padlen in the flow control, so it must be adjusted.
2261 */
2262 if (hdr.len < 1) {
2263 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2264 sess_log(h2c->conn->owner);
2265 goto fail;
2266 }
2267 hdr.len--;
2268
2269 if (b_data(&h2c->dbuf) < 10)
2270 break; // missing padlen
2271
2272 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2273
2274 if (padlen > hdr.len) {
2275 /* RFC7540#6.1 : pad length = length of
2276 * frame payload or greater => error.
2277 */
2278 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2279 sess_log(h2c->conn->owner);
2280 goto fail;
2281 }
2282
2283 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2284 h2c->rcvd_c++;
2285 h2c->rcvd_s++;
2286 }
2287 b_del(&h2c->dbuf, 1);
2288 }
2289 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002290
2291 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002292 h2c->dfl = hdr.len;
2293 h2c->dsi = hdr.sid;
2294 h2c->dft = hdr.ft;
2295 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002296 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002297 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002298
2299 /* check for minimum basic frame format validity */
2300 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2301 if (ret != H2_ERR_NO_ERROR) {
2302 h2c_error(h2c, ret);
2303 sess_log(h2c->conn->owner);
2304 goto fail;
2305 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002306 }
2307
2308 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002309 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2310
Willy Tarreau567beb82018-12-18 16:52:44 +01002311 if (tmp_h2s != h2s && h2s && h2s->cs &&
2312 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002313 (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 +01002314 /* we may have to signal the upper layers */
2315 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002316 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002317 }
2318 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002319
Willy Tarreaud7901432017-12-29 11:34:40 +01002320 if (h2c->st0 == H2_CS_FRAME_E)
2321 goto strm_err;
2322
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002323 if (h2s->st == H2_SS_IDLE &&
2324 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2325 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2326 * this state MUST be treated as a connection error
2327 */
2328 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002329 if (!h2c->nb_streams) {
2330 /* only log if no other stream can report the error */
2331 sess_log(h2c->conn->owner);
2332 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002333 break;
2334 }
2335
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002336 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2337 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2338 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002339 * this state MUST be treated as a stream error.
2340 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2341 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002342 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002343 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2344 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2345 else
2346 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002347 goto strm_err;
2348 }
2349
Willy Tarreauab837502017-12-27 15:07:30 +01002350 /* Below the management of frames received in closed state is a
2351 * bit hackish because the spec makes strong differences between
2352 * streams closed by receiving RST, sending RST, and seeing ES
2353 * in both directions. In addition to this, the creation of a
2354 * new stream reusing the identifier of a closed one will be
2355 * detected here. Given that we cannot keep track of all closed
2356 * streams forever, we consider that unknown closed streams were
2357 * closed on RST received, which allows us to respond with an
2358 * RST without breaking the connection (eg: to abort a transfer).
2359 * Some frames have to be silently ignored as well.
2360 */
2361 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002362 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002363 /* #5.1.1: The identifier of a newly
2364 * established stream MUST be numerically
2365 * greater than all streams that the initiating
2366 * endpoint has opened or reserved. This
2367 * governs streams that are opened using a
2368 * HEADERS frame and streams that are reserved
2369 * using PUSH_PROMISE. An endpoint that
2370 * receives an unexpected stream identifier
2371 * MUST respond with a connection error.
2372 */
2373 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2374 goto strm_err;
2375 }
2376
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002377 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002378 /* RFC7540#5.1:closed: an endpoint that
2379 * receives any frame other than PRIORITY after
2380 * receiving a RST_STREAM MUST treat that as a
2381 * stream error of type STREAM_CLOSED.
2382 *
2383 * Note that old streams fall into this category
2384 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002385 *
2386 * However, we cannot generalize this to all frame types. Those
2387 * carrying compression state must still be processed before
2388 * being dropped or we'll desynchronize the decoder. This can
2389 * happen with request trailers received after sending an
2390 * RST_STREAM, or with header/trailers responses received after
2391 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002392 */
2393 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2394 h2c->st0 = H2_CS_FRAME_E;
2395 goto strm_err;
2396 }
2397
2398 /* RFC7540#5.1:closed: if this state is reached as a
2399 * result of sending a RST_STREAM frame, the peer that
2400 * receives the RST_STREAM might have already sent
2401 * frames on the stream that cannot be withdrawn. An
2402 * endpoint MUST ignore frames that it receives on
2403 * closed streams after it has sent a RST_STREAM
2404 * frame. An endpoint MAY choose to limit the period
2405 * over which it ignores frames and treat frames that
2406 * arrive after this time as being in error.
2407 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002408 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002409 /* RFC7540#5.1:closed: any frame other than
2410 * PRIO/WU/RST in this state MUST be treated as
2411 * a connection error
2412 */
2413 if (h2c->dft != H2_FT_RST_STREAM &&
2414 h2c->dft != H2_FT_PRIORITY &&
2415 h2c->dft != H2_FT_WINDOW_UPDATE) {
2416 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2417 goto strm_err;
2418 }
2419 }
2420 }
2421
Willy Tarreauc0da1962017-10-30 18:38:00 +01002422#if 0
2423 // problem below: it is not possible to completely ignore such
2424 // streams as we need to maintain the compression state as well
2425 // and for this we need to completely process these frames (eg:
2426 // HEADERS frames) as well as counting DATA frames to emit
2427 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2428 // This is a typical case of layer violation where the
2429 // transported contents are critical to the connection's
2430 // validity and must be ignored at the same time :-(
2431
2432 /* graceful shutdown, ignore streams whose ID is higher than
2433 * the one advertised in GOAWAY. RFC7540#6.8.
2434 */
2435 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002436 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2437 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002438 h2c->dfl -= ret;
2439 ret = h2c->dfl == 0;
2440 goto strm_err;
2441 }
2442#endif
2443
Willy Tarreau7e98c052017-10-10 15:56:59 +02002444 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002445 case H2_FT_SETTINGS:
2446 if (h2c->st0 == H2_CS_FRAME_P)
2447 ret = h2c_handle_settings(h2c);
2448
2449 if (h2c->st0 == H2_CS_FRAME_A)
2450 ret = h2c_ack_settings(h2c);
2451 break;
2452
Willy Tarreaucf68c782017-10-10 17:11:41 +02002453 case H2_FT_PING:
2454 if (h2c->st0 == H2_CS_FRAME_P)
2455 ret = h2c_handle_ping(h2c);
2456
2457 if (h2c->st0 == H2_CS_FRAME_A)
2458 ret = h2c_ack_ping(h2c);
2459 break;
2460
Willy Tarreau26f95952017-07-27 17:18:30 +02002461 case H2_FT_WINDOW_UPDATE:
2462 if (h2c->st0 == H2_CS_FRAME_P)
2463 ret = h2c_handle_window_update(h2c, h2s);
2464 break;
2465
Willy Tarreau61290ec2017-10-17 08:19:21 +02002466 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002467 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2468 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2469 * frames' parsers consume all following CONTINUATION
2470 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002471 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002472 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2473 sess_log(h2c->conn->owner);
2474 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002475
Willy Tarreau13278b42017-10-13 19:23:14 +02002476 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002477 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002478 if (h2c->flags & H2_CF_IS_BACK)
2479 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2480 else
2481 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002482 if (tmp_h2s) {
2483 h2s = tmp_h2s;
2484 ret = 1;
2485 }
2486 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002487 break;
2488
Willy Tarreau454f9052017-10-26 19:40:35 +02002489 case H2_FT_DATA:
2490 if (h2c->st0 == H2_CS_FRAME_P)
2491 ret = h2c_frt_handle_data(h2c, h2s);
2492
2493 if (h2c->st0 == H2_CS_FRAME_A)
2494 ret = h2c_send_strm_wu(h2c);
2495 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002496
Willy Tarreau92153fc2017-12-03 19:46:19 +01002497 case H2_FT_PRIORITY:
2498 if (h2c->st0 == H2_CS_FRAME_P)
2499 ret = h2c_handle_priority(h2c);
2500 break;
2501
Willy Tarreaucd234e92017-08-18 10:59:39 +02002502 case H2_FT_RST_STREAM:
2503 if (h2c->st0 == H2_CS_FRAME_P)
2504 ret = h2c_handle_rst_stream(h2c, h2s);
2505 break;
2506
Willy Tarreaue96b0922017-10-30 00:28:29 +01002507 case H2_FT_GOAWAY:
2508 if (h2c->st0 == H2_CS_FRAME_P)
2509 ret = h2c_handle_goaway(h2c);
2510 break;
2511
Willy Tarreau1c661982017-10-30 13:52:01 +01002512 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002513 default:
2514 /* drop frames that we ignore. They may be larger than
2515 * the buffer so we drain all of their contents until
2516 * we reach the end.
2517 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002518 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2519 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002520 h2c->dfl -= ret;
2521 ret = h2c->dfl == 0;
2522 }
2523
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002524 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002525 /* We may have to send an RST if not done yet */
2526 if (h2s->st == H2_SS_ERROR)
2527 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002528
Willy Tarreaua20a5192017-12-27 11:02:06 +01002529 if (h2c->st0 == H2_CS_FRAME_E)
2530 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002531
Willy Tarreau7e98c052017-10-10 15:56:59 +02002532 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002533 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002534 break;
2535
2536 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002537 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002538 h2c->st0 = H2_CS_FRAME_H;
2539 }
2540 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002541
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002542 if (h2c->rcvd_c > 0 &&
2543 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2544 h2c_send_conn_wu(h2c);
2545
Willy Tarreau52eed752017-09-22 15:05:09 +02002546 fail:
2547 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002548 if (h2s && h2s->cs &&
2549 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002550 (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 +01002551 /* we may have to signal the upper layers */
2552 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002553 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002554 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002555
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002556 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002557}
2558
2559/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2560 * the end.
2561 */
2562static int h2_process_mux(struct h2c *h2c)
2563{
Olivier Houchard998410a2019-04-15 19:23:37 +02002564 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002565
Willy Tarreau01b44822018-10-03 14:26:37 +02002566 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2567 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2568 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2569 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2570 if (h2c->st0 == H2_CS_ERROR) {
2571 h2c->st0 = H2_CS_ERROR2;
2572 sess_log(h2c->conn->owner);
2573 }
2574 goto fail;
2575 }
2576 h2c->st0 = H2_CS_SETTINGS1;
2577 }
2578 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002579 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002580 return 1;
2581 }
2582
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002583 /* start by sending possibly pending window updates */
2584 if (h2c->rcvd_c > 0 &&
2585 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2586 h2c_send_conn_wu(h2c) < 0)
2587 goto fail;
2588
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002589 /* First we always process the flow control list because the streams
2590 * waiting there were already elected for immediate emission but were
2591 * blocked just on this.
2592 */
2593
Olivier Houchard998410a2019-04-15 19:23:37 +02002594 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002595 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2596 h2c->st0 >= H2_CS_ERROR)
2597 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002598
2599 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002600 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002601
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002602 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002603 /* For some reason, the upper layer failed to subsribe again,
2604 * so remove it from the send_list
2605 */
2606 if (!h2s->send_wait) {
2607 LIST_DEL_INIT(&h2s->list);
2608 continue;
2609 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002610 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002611 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002612 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002613 }
2614
Olivier Houchard998410a2019-04-15 19:23:37 +02002615 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002616 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2617 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002618
2619 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002620 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002621
Olivier Houchard998410a2019-04-15 19:23:37 +02002622 /* For some reason, the upper layer failed to subsribe again,
2623 * so remove it from the send_list
2624 */
2625 if (!h2s->send_wait) {
2626 LIST_DEL_INIT(&h2s->list);
2627 continue;
2628 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002629 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002630 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002631 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002632 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002633 }
2634
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002635 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002636 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002637 if (h2c->st0 == H2_CS_ERROR) {
2638 if (h2c->max_id >= 0) {
2639 h2c_send_goaway_error(h2c, NULL);
2640 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2641 return 0;
2642 }
2643
2644 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2645 }
2646 return 1;
2647 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002648 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002649}
2650
Willy Tarreau62f52692017-10-08 23:01:42 +02002651
Willy Tarreau479998a2018-11-18 06:30:59 +01002652/* Attempt to read data, and subscribe if none available.
2653 * The function returns 1 if data has been received, otherwise zero.
2654 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002655static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002656{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002657 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002658 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002659 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002660 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002661
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002662 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002663 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002664
Willy Tarreau315d8072017-12-10 22:17:57 +01002665 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002666 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002667
Willy Tarreau44e973f2018-03-01 17:49:30 +01002668 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002669 if (!buf) {
2670 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002671 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002672 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002673
Olivier Houchard7505f942018-08-21 18:10:44 +02002674 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002675 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002676 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2677 /* HTX in use : try to pre-align the buffer like the
2678 * rxbufs will be to optimize memory copies. We'll make
2679 * sure that the frame header lands at the end of the
2680 * HTX block to alias it upon recv. We cannot use the
2681 * head because rcv_buf() will realign the buffer if
2682 * it's empty. Thus we cheat and pretend we already
2683 * have a few bytes there.
2684 */
2685 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002686 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002687 }
2688 else
2689 max = b_room(buf);
2690
Olivier Houchard7505f942018-08-21 18:10:44 +02002691 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002692 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002693 else
2694 ret = 0;
2695 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002696
Olivier Houchard53216e72018-10-10 15:46:36 +02002697 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002698 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002699
Olivier Houcharda1411e62018-08-17 18:42:48 +02002700 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002701 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002702 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002703 }
2704
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002705 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002706 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002707 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002708}
2709
Willy Tarreau479998a2018-11-18 06:30:59 +01002710/* Try to send data if possible.
2711 * The function returns 1 if data have been sent, otherwise zero.
2712 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002713static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002714{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002715 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002716 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002717 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002718
2719 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002720 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002721
Olivier Houchard7505f942018-08-21 18:10:44 +02002722
Willy Tarreaua2af5122017-10-09 11:56:46 +02002723 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2724 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002725 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002726 }
2727
Willy Tarreaubc933932017-10-09 16:21:43 +02002728 /* This loop is quite simple : it tries to fill as much as it can from
2729 * pending streams into the existing buffer until it's reportedly full
2730 * or the end of send requests is reached. Then it tries to send this
2731 * buffer's contents out, marks it not full if at least one byte could
2732 * be sent, and tries again.
2733 *
2734 * The snd_buf() function normally takes a "flags" argument which may
2735 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2736 * data immediately comes and CO_SFL_STREAMER to indicate that the
2737 * connection is streaming lots of data (used to increase TLS record
2738 * size at the expense of latency). The former can be sent any time
2739 * there's a buffer full flag, as it indicates at least one stream
2740 * attempted to send and failed so there are pending data. An
2741 * alternative would be to set it as long as there's an active stream
2742 * but that would be problematic for ACKs until we have an absolute
2743 * guarantee that all waiters have at least one byte to send. The
2744 * latter should possibly not be set for now.
2745 */
2746
2747 done = 0;
2748 while (!done) {
2749 unsigned int flags = 0;
2750
2751 /* fill as much as we can into the current buffer */
2752 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2753 done = h2_process_mux(h2c);
2754
Olivier Houchard2b094432019-01-29 18:28:36 +01002755 if (h2c->flags & H2_CF_MUX_MALLOC)
2756 break;
2757
Willy Tarreaubc933932017-10-09 16:21:43 +02002758 if (conn->flags & CO_FL_ERROR)
2759 break;
2760
2761 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2762 flags |= CO_SFL_MSG_MORE;
2763
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002764 if (b_data(&h2c->mbuf)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002765 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002766 if (!ret)
2767 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002768 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002769 b_del(&h2c->mbuf, ret);
2770 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002771 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002772
2773 /* wrote at least one byte, the buffer is not full anymore */
2774 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2775 }
2776
Willy Tarreaua2af5122017-10-09 11:56:46 +02002777 if (conn->flags & CO_FL_SOCK_WR_SH) {
2778 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002779 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002780 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002781 /* We're not full anymore, so we can wake any task that are waiting
2782 * for us.
2783 */
2784 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002785 struct h2s *h2s;
2786
2787 list_for_each_entry(h2s, &h2c->send_list, list) {
2788 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2789 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002790
2791 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002792 continue;
2793
Olivier Houchard998410a2019-04-15 19:23:37 +02002794 /* For some reason, the upper layer failed to subsribe again,
2795 * so remove it from the send_list
2796 */
2797 if (!h2s->send_wait) {
2798 LIST_DEL_INIT(&h2s->list);
2799 continue;
2800 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002801 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002802 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002803 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002804 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002805 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002806 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002807 /* We're done, no more to send */
2808 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002809 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002810schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002811 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002812 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002813 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002814}
2815
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002816/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002817static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2818{
2819 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002820 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002821
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002822 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002823 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002824 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002825 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002826 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002827 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002828 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002829}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002830
Willy Tarreau62f52692017-10-08 23:01:42 +02002831/* callback called on any event by the connection handler.
2832 * It applies changes and returns zero, or < 0 if it wants immediate
2833 * destruction of the connection (which normally doesn not happen in h2).
2834 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002835static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002836{
Olivier Houchard7505f942018-08-21 18:10:44 +02002837 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002838
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002839 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002840 h2_process_demux(h2c);
2841
2842 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002843 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002844
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002845 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002846 h2c->flags &= ~H2_CF_DEM_DFULL;
2847 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002848 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002849
Willy Tarreau0b37d652018-10-03 10:33:02 +02002850 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002851 /* frontend is stopping, reload likely in progress, let's try
2852 * to announce a graceful shutdown if not yet done. We don't
2853 * care if it fails, it will be tried again later.
2854 */
2855 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2856 if (h2c->last_sid < 0)
2857 h2c->last_sid = (1U << 31) - 1;
2858 h2c_send_goaway_error(h2c, NULL);
2859 }
2860 }
2861
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002862 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002863 * If we received early data, and the handshake is done, wake
2864 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002865 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002866 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2867 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2868 struct eb32_node *node;
2869 struct h2s *h2s;
2870
2871 h2c->flags |= H2_CF_WAIT_FOR_HS;
2872 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2873
2874 while (node) {
2875 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002876 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002877 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002878 node = eb32_next(node);
2879 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002880 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002881
Willy Tarreau26bd7612017-10-09 16:47:04 +02002882 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002883 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2884 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2885 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002886 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002887
2888 if (eb_is_empty(&h2c->streams_by_id)) {
2889 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002890 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002891 return -1;
2892 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002893 }
2894
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002895 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002896 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002897
Olivier Houchard53216e72018-10-10 15:46:36 +02002898 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2899 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2900 (h2c->st0 != H2_CS_ERROR &&
2901 !b_data(&h2c->mbuf) &&
2902 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2903 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002904 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002905
Willy Tarreau3f133572017-10-31 19:21:06 +01002906 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002907 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002908 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002909 task_queue(h2c->task);
2910 }
2911 else
2912 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002913 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002914
Olivier Houchard7505f942018-08-21 18:10:44 +02002915 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002916 return 0;
2917}
2918
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002919/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002920static int h2_wake(struct connection *conn)
2921{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002922 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002923
2924 return (h2_process(h2c));
2925}
2926
Willy Tarreauea392822017-10-31 10:02:25 +01002927/* Connection timeout management. The principle is that if there's no receipt
2928 * nor sending for a certain amount of time, the connection is closed. If the
2929 * MUX buffer still has lying data or is not allocatable, the connection is
2930 * immediately killed. If it's allocatable and empty, we attempt to send a
2931 * GOAWAY frame.
2932 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002933static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002934{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002935 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002936 int expired = tick_is_expired(t->expire, now_ms);
2937
Willy Tarreau0975f112018-03-29 15:22:59 +02002938 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002939 return t;
2940
Olivier Houchard3f795f72019-04-17 22:51:06 +02002941 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02002942
2943 if (!h2c) {
2944 /* resources were already deleted */
2945 return NULL;
2946 }
2947
2948 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002949 h2c_error(h2c, H2_ERR_NO_ERROR);
2950 h2_wake_some_streams(h2c, 0, 0);
2951
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002952 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002953 /* don't even try to send a GOAWAY, the buffer is stuck */
2954 h2c->flags |= H2_CF_GOAWAY_FAILED;
2955 }
2956
2957 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002958 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002959 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2960 h2c->flags |= H2_CF_GOAWAY_FAILED;
2961
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002962 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002963 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 +02002964 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002965 b_del(&h2c->mbuf, ret);
2966 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002967 }
2968 }
Willy Tarreauea392822017-10-31 10:02:25 +01002969
Willy Tarreau0975f112018-03-29 15:22:59 +02002970 /* either we can release everything now or it will be done later once
2971 * the last stream closes.
2972 */
2973 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002974 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002975
Willy Tarreauea392822017-10-31 10:02:25 +01002976 return NULL;
2977}
2978
2979
Willy Tarreau62f52692017-10-08 23:01:42 +02002980/*******************************************/
2981/* functions below are used by the streams */
2982/*******************************************/
2983
2984/*
2985 * Attach a new stream to a connection
2986 * (Used for outgoing connections)
2987 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002988static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002989{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002990 struct conn_stream *cs;
2991 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002992 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002993
2994 cs = cs_new(conn);
2995 if (!cs)
2996 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002997 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002998 if (!h2s) {
2999 cs_free(cs);
3000 return NULL;
3001 }
3002 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003003}
3004
Willy Tarreaufafd3982018-11-18 21:29:20 +01003005/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3006 * We have to scan because we may have some orphan streams. It might be
3007 * beneficial to scan backwards from the end to reduce the likeliness to find
3008 * orphans.
3009 */
3010static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3011{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003012 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003013 struct h2s *h2s;
3014 struct eb32_node *node;
3015
3016 node = eb32_first(&h2c->streams_by_id);
3017 while (node) {
3018 h2s = container_of(node, struct h2s, by_id);
3019 if (h2s->cs)
3020 return h2s->cs;
3021 node = eb32_next(node);
3022 }
3023 return NULL;
3024}
3025
Willy Tarreau62f52692017-10-08 23:01:42 +02003026/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003027 * Destroy the mux and the associated connection, if it is no longer used
3028 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003029static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003030{
Christopher Faulet73c12072019-04-08 11:23:22 +02003031 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003032
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003033 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003034 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003035}
3036
3037/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003038 * Detach the stream from the connection and possibly release the connection.
3039 */
3040static void h2_detach(struct conn_stream *cs)
3041{
Willy Tarreau60935142017-10-16 18:11:19 +02003042 struct h2s *h2s = cs->ctx;
3043 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003044 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003045
3046 cs->ctx = NULL;
3047 if (!h2s)
3048 return;
3049
Olivier Houchard998410a2019-04-15 19:23:37 +02003050 /* The stream is about to die, so no need to attempt to run its task */
3051 if (!LIST_ISEMPTY(&h2s->sending_list) &&
3052 h2s->send_wait != &h2s->wait_event) {
3053 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3054 LIST_DEL_INIT(&h2s->sending_list);
3055 }
3056
Olivier Houchardf502aca2018-12-14 19:42:40 +01003057 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003058 h2c = h2s->h2c;
3059 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003060 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003061 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3062 !h2_frt_has_too_many_cs(h2c)) {
3063 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003064 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003065 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003066 }
Willy Tarreau60935142017-10-16 18:11:19 +02003067
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003068 /* this stream may be blocked waiting for some data to leave (possibly
3069 * an ES or RST frame), so orphan it in this case.
3070 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003071 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003072 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003073 (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 +01003074 return;
3075
Willy Tarreau45f752e2017-10-30 15:44:59 +01003076 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3077 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3078 /* unblock the connection if it was blocked on this
3079 * stream.
3080 */
3081 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3082 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003083 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003084 }
3085
Willy Tarreau71049cc2018-03-28 13:56:39 +02003086 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003087
Olivier Houchard8a786902018-12-15 16:05:40 +01003088 if (h2c->flags & H2_CF_IS_BACK &&
3089 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003090 if (!(h2c->conn->flags &
3091 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3092 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003093 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003094 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3095 h2c->conn->owner = NULL;
3096 if (eb_is_empty(&h2c->streams_by_id)) {
3097 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3098 /* The server doesn't want it, let's kill the connection right away */
3099 h2c->conn->mux->destroy(h2c->conn);
3100 return;
3101 }
3102 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003103 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003104 if (eb_is_empty(&h2c->streams_by_id)) {
3105 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3106 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3107 return;
3108 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003109 /* Never ever allow to reuse a connection from a non-reuse backend */
3110 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3111 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003112 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003113 struct server *srv = objt_server(h2c->conn->target);
3114
3115 if (srv) {
3116 if (h2c->conn->flags & CO_FL_PRIVATE)
3117 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3118 else
3119 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3120 }
3121
3122 }
3123 }
3124 }
3125
Willy Tarreaue323f342018-03-28 13:51:45 +02003126 /* We don't want to close right now unless we're removing the
3127 * last stream, and either the connection is in error, or it
3128 * reached the ID already specified in a GOAWAY frame received
3129 * or sent (as seen by last_sid >= 0).
3130 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003131 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003132 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003133 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003134 }
3135 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003136 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003137 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3138 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003139 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003140 else
3141 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003142 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003143}
3144
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003145/* Performs a synchronous or asynchronous shutr().
3146 * FIXME: guess what the return code tries to indicate!
3147 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003148static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003149{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003150 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003151 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003152
Willy Tarreau721c9742017-11-07 11:05:42 +01003153 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003154 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003155
Willy Tarreau18059042019-01-31 19:12:48 +01003156 /* a connstream may require us to immediately kill the whole connection
3157 * for example because of a "tcp-request content reject" rule that is
3158 * normally used to limit abuse. In this case we schedule a goaway to
3159 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003160 */
Willy Tarreau18059042019-01-31 19:12:48 +01003161 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3162 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3163 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3164 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3165 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003166 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3167 /* Nothing was never sent for this stream, so reset with
3168 * REFUSED_STREAM error to let the client retry the
3169 * request.
3170 */
3171 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3172 }
Willy Tarreau18059042019-01-31 19:12:48 +01003173
Willy Tarreau90c32322017-11-24 08:00:30 +01003174 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003175 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003176 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003177
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003178 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003179 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003180 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003181
Olivier Houchard7a977432019-03-21 15:47:13 +01003182 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003183add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003184 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003185 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003186 if (h2s->flags & H2_SF_BLK_MFCTL) {
3187 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3188 h2s->send_wait = sw;
3189 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3190 h2s->send_wait = sw;
3191 LIST_ADDQ(&h2c->send_list, &h2s->list);
3192 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003193 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003194 /* Let the handler know we want shutr */
3195 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003196 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003197}
3198
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003199/* Performs a synchronous or asynchronous shutw().
3200 * FIXME: guess what the return code tries to indicate!
3201 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003202static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003203{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003204 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003205 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003206
Willy Tarreau721c9742017-11-07 11:05:42 +01003207 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003208 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003209
Willy Tarreau67434202017-11-06 20:20:51 +01003210 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003211 /* we can cleanly close using an empty data frame only after headers */
3212
3213 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3214 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003215 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003216
3217 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003218 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003219 else
3220 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003221 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003222 /* a connstream may require us to immediately kill the whole connection
3223 * for example because of a "tcp-request content reject" rule that is
3224 * normally used to limit abuse. In this case we schedule a goaway to
3225 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003226 */
Willy Tarreau18059042019-01-31 19:12:48 +01003227 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3228 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3229 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3230 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3231 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003232 else {
3233 /* Nothing was never sent for this stream, so reset with
3234 * REFUSED_STREAM error to let the client retry the
3235 * request.
3236 */
3237 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3238 }
Willy Tarreau18059042019-01-31 19:12:48 +01003239
Willy Tarreau90c32322017-11-24 08:00:30 +01003240 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003241 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003242 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003243
Willy Tarreau00dd0782018-03-01 16:31:34 +01003244 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003245 }
3246
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003247 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003248 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003249 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003250
3251 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003252 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003253 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003254 if (h2s->flags & H2_SF_BLK_MFCTL) {
3255 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3256 h2s->send_wait = sw;
3257 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3258 h2s->send_wait = sw;
3259 LIST_ADDQ(&h2c->send_list, &h2s->list);
3260 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003261 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003262 /* let the handler know we want to shutw */
3263 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003264 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003265}
3266
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003267/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3268 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3269 * and prevented the last frame from being emitted.
3270 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003271static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3272{
3273 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003274 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003275 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003276
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003277 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003278 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003279 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003280 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003281 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003282
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003283 if (!ret) {
3284 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003285 LIST_DEL_INIT(&h2s->list);
3286 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003287 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003288 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003289 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003290 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003291
3292 if (h2c_is_dead(h2c))
Christopher Faulet73c12072019-04-08 11:23:22 +02003293 h2_release(h2c);
Olivier Houchard7a977432019-03-21 15:47:13 +01003294 }
3295
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003296 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003297}
3298
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003299/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003300static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3301{
3302 struct h2s *h2s = cs->ctx;
3303
3304 if (!mode)
3305 return;
3306
3307 h2_do_shutr(h2s);
3308}
3309
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003310/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003311static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3312{
3313 struct h2s *h2s = cs->ctx;
3314
3315 h2_do_shutw(h2s);
3316}
3317
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003318/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003319 * HTX request or response depending on the connection's side. Returns a
3320 * positive value on success, a negative value on failure, or 0 if it couldn't
3321 * proceed. May report connection errors in h2c->errcode if the frame is
3322 * non-decodable and the connection unrecoverable. In absence of connection
3323 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003324 *
3325 * The function may fold CONTINUATION frames into the initial HEADERS frame
3326 * by removing padding and next frame header, then moving the CONTINUATION
3327 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3328 * leaving a hole between the main frame and the beginning of the next one.
3329 * The possibly remaining incomplete or next frame at the end may be moved
3330 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3331 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3332 *
3333 * A buffer at the beginning of processing may look like this :
3334 *
3335 * ,---.---------.-----.--------------.--------------.------.---.
3336 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3337 * `---^---------^-----^--------------^--------------^------^---'
3338 * | | <-----> | |
3339 * area | dpl | wrap
3340 * |<--------------> |
3341 * | dfl |
3342 * |<-------------------------------------------------->|
3343 * head data
3344 *
3345 * Padding is automatically overwritten when folding, participating to the
3346 * hole size after dfl :
3347 *
3348 * ,---.------------------------.-----.--------------.------.---.
3349 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3350 * `---^------------------------^-----^--------------^------^---'
3351 * | | <-----> | |
3352 * area | hole | wrap
3353 * |<-----------------------> |
3354 * | dfl |
3355 * |<-------------------------------------------------->|
3356 * head data
3357 *
3358 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3359 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3360 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003361 *
3362 * The <flags> field must point to either the stream's flags or to a copy of it
3363 * so that the function can update the following flags :
3364 * - H2_SF_DATA_CLEN when content-length is seen
3365 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3366 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003367 *
3368 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3369 * decoding, in order to detect if we're dealing with a headers or a trailers
3370 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003371 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003372static 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 +02003373{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003374 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003375 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003376 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003377 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003378 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003379 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003380 int flen; // header frame len
3381 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003382 int ret = 0;
3383 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003384 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003385 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003386
Willy Tarreauea18f862018-12-22 20:19:26 +01003387next_frame:
3388 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3389 goto leave; // incomplete input frame
3390
3391 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3392 * this case, we'll try to paste it immediately after the initial
3393 * HEADERS frame payload and kill any possible padding. The initial
3394 * frame's length will be increased to represent the concatenation
3395 * of the two frames. The next frame is read from position <tlen>
3396 * and written at position <flen> (minus padding if some is present).
3397 */
3398 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3399 struct h2_fh hdr;
3400 int clen; // CONTINUATION frame's payload length
3401
3402 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3403 /* no more data, the buffer may be full, either due to
3404 * too large a frame or because of too large a hole that
3405 * we're going to compact at the end.
3406 */
3407 goto leave;
3408 }
3409
3410 if (hdr.ft != H2_FT_CONTINUATION) {
3411 /* RFC7540#6.10: frame of unexpected type */
3412 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3413 goto fail;
3414 }
3415
3416 if (hdr.sid != h2c->dsi) {
3417 /* RFC7540#6.10: frame of different stream */
3418 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3419 goto fail;
3420 }
3421
3422 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3423 /* RFC7540#4.2: invalid frame length */
3424 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3425 goto fail;
3426 }
3427
3428 /* detect when we must stop aggragating frames */
3429 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3430
3431 /* Take as much as we can of the CONTINUATION frame's payload */
3432 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3433 if (clen > hdr.len)
3434 clen = hdr.len;
3435
3436 /* Move the frame's payload over the padding, hole and frame
3437 * header. At least one of hole or dpl is null (see diagrams
3438 * above). The hole moves after the new aggragated frame.
3439 */
3440 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3441 h2c->dfl += clen - h2c->dpl;
3442 hole += h2c->dpl + 9;
3443 h2c->dpl = 0;
3444 goto next_frame;
3445 }
3446
3447 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003448
Willy Tarreau13278b42017-10-13 19:23:14 +02003449 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003450 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003451 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003452 copy = alloc_trash_chunk();
3453 if (!copy) {
3454 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3455 goto fail;
3456 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003457 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3458 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3459 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003460 }
3461
Willy Tarreau13278b42017-10-13 19:23:14 +02003462 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3463 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003464 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003465 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3466 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003467 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003468 }
3469
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003470 if (flen < 5) {
3471 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3472 goto fail;
3473 }
3474
Willy Tarreau13278b42017-10-13 19:23:14 +02003475 hdrs += 5; // stream dep = 4, weight = 1
3476 flen -= 5;
3477 }
3478
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003479 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003480 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003481 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003482 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003483
Willy Tarreau937f7602018-02-26 15:22:17 +01003484 /* we can't retry a failed decompression operation so we must be very
3485 * careful not to take any risks. In practice the output buffer is
3486 * always empty except maybe for trailers, in which case we simply have
3487 * to wait for the upper layer to finish consuming what is available.
3488 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003489
3490 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003491 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003492 if (!htx_is_empty(htx)) {
3493 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003494 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003495 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003496 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003497 if (b_data(rxbuf)) {
3498 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003499 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003500 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003501
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003502 rxbuf->head = 0;
3503 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003504 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003505
Willy Tarreau25919232019-01-03 14:48:18 +01003506 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003507 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3508 sizeof(list)/sizeof(list[0]), tmp);
3509 if (outlen < 0) {
3510 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3511 goto fail;
3512 }
3513
Willy Tarreau25919232019-01-03 14:48:18 +01003514 /* The PACK decompressor was updated, let's update the input buffer and
3515 * the parser's state to commit these changes and allow us to later
3516 * fail solely on the stream if needed.
3517 */
3518 b_del(&h2c->dbuf, h2c->dfl + hole);
3519 h2c->dfl = hole = 0;
3520 h2c->st0 = H2_CS_FRAME_H;
3521
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003522 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003523 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003524
Willy Tarreau88d138e2019-01-02 19:38:14 +01003525 if (*flags & H2_SF_HEADERS_RCVD)
3526 goto trailers;
3527
3528 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003529 if (htx) {
3530 /* HTX mode */
3531 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003532 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003533 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003534 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003535 } else {
3536 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003537 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003538 if (outlen > 0)
3539 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003540 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003541
3542 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003543 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003544 goto fail;
3545 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003546
Willy Tarreau174b06a2018-04-25 18:13:58 +02003547 if (msgf & H2_MSGF_BODY) {
3548 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003549 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003550 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003551 if (htx)
3552 htx->extra = *body_len;
3553 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003554 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003555 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003556 }
3557
Willy Tarreau88d138e2019-01-02 19:38:14 +01003558 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003559 /* indicate that a HEADERS frame was received for this stream, except
3560 * for 1xx responses. For 1xx responses, another HEADERS frame is
3561 * expected.
3562 */
3563 if (!(msgf & H2_MSGF_RSP_1XX))
3564 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003565
Christopher Faulet0b465482019-02-19 15:14:23 +01003566 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003567 /* Mark the end of message, either using EOM in HTX or with the
3568 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003569 * is not set during headers with END_STREAM. For HTX trailers,
3570 * we must not leave an HTX trailers block not followed by an
3571 * EOM block, the two must be atomic. Thus if we fail to emit
3572 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003573 */
3574 if (htx) {
Willy Tarreau2135f912019-05-07 11:17:32 +02003575 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3576 struct htx_blk *tail = htx_get_tail_blk(htx);
3577
3578 if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR)
3579 htx_remove_blk(htx, tail);
Willy Tarreau88d138e2019-01-02 19:38:14 +01003580 goto fail;
Willy Tarreau2135f912019-05-07 11:17:32 +02003581 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01003582 }
3583 else if (*flags & H2_SF_DATA_CHNK) {
3584 if (!b_putblk(rxbuf, "\r\n", 2))
3585 goto fail;
3586 }
3587 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003588
Willy Tarreau86277d42019-01-02 15:36:11 +01003589 /* success */
3590 ret = 1;
3591
Willy Tarreau68dd9852017-07-03 14:44:26 +02003592 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003593 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003594 * move the remaining data over it.
3595 */
3596 if (hole) {
3597 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3598 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3599 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3600 b_sub(&h2c->dbuf, hole);
3601 }
3602
Willy Tarreau97215ca2019-04-29 10:20:21 +02003603 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003604 /* too large frames */
3605 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003606 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003607 }
3608
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003609 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003610 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003611 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003612 return ret;
3613
Willy Tarreau68dd9852017-07-03 14:44:26 +02003614 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003615 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003616 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003617
3618 trailers:
3619 /* This is the last HEADERS frame hence a trailer */
3620
3621 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3622 /* It's a trailer but it's missing ES flag */
3623 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3624 goto fail;
3625 }
3626
3627 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3628 * block, and when using chunks we must send the 0 CRLF marker. For
3629 * other modes, the trailers are silently dropped.
3630 */
3631 if (htx) {
3632 if (!htx_add_endof(htx, HTX_BLK_EOD))
3633 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003634 if (h2_make_htx_trailers(list, htx) <= 0)
3635 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003636 }
3637 else if (*flags & H2_SF_DATA_CHNK) {
3638 /* Legacy mode with chunked encoding : we must finalize the
3639 * data block message emit the trailing CRLF */
3640 if (!b_putblk(rxbuf, "0\r\n", 3))
3641 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003642
3643 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3644 if (outlen > 0)
3645 b_add(rxbuf, outlen);
3646 else
3647 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003648 }
3649
3650 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003651}
3652
Willy Tarreau454f9052017-10-26 19:40:35 +02003653/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3654 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3655 * in use, a new chunk is emitted for each frame. This is supposed to fit
3656 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3657 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3658 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003659 * parser state is automatically updated. Returns > 0 if it could completely
3660 * send the current frame, 0 if it couldn't complete, in which case
3661 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3662 * DATA frame can return 0 as a valid result). Stream errors are reported in
3663 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3664 * have checked the frame header and ensured that the frame was complete or the
3665 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003666 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003667static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003668{
3669 struct h2c *h2c = h2s->h2c;
3670 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003671 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003672 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003673 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003674 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003675
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003676 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003677
Olivier Houchard638b7992018-08-16 15:41:52 +02003678 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003679 if (!csbuf) {
3680 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003681 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003682 }
3683
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003684try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003685 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003686 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3687 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003688 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003689 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003690
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003691 if (flen > b_data(&h2c->dbuf)) {
3692 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003693 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003694 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003695 }
3696
Willy Tarreaua9b77962019-01-31 07:23:00 +01003697 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003698 block1 = htx_free_data_space(htx);
3699 if (!block1) {
3700 h2c->flags |= H2_CF_DEM_SFULL;
3701 goto fail;
3702 }
3703 if (flen > block1)
3704 flen = block1;
3705
3706 /* here, flen is the max we can copy into the output buffer */
3707 block1 = b_contig_data(&h2c->dbuf, 0);
3708 if (flen > block1)
3709 flen = block1;
3710
3711 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3712 h2c->flags |= H2_CF_DEM_SFULL;
3713 goto fail;
3714 }
3715
3716 b_del(&h2c->dbuf, flen);
3717 h2c->dfl -= flen;
3718 h2c->rcvd_c += flen;
3719 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003720
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003721 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003722 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003723 htx->extra = h2s->body_len;
3724 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003725 goto try_again;
3726 }
3727 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003728 /* it doesn't fit and the buffer is fragmented,
3729 * so let's defragment it and try again.
3730 */
3731 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003732 }
3733
Willy Tarreaueba10f22018-04-25 20:44:22 +02003734 /* chunked-encoding requires more room */
3735 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003736 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003737 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3738 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3739 (chklen < 1048576) ? 4 : 8;
3740 chklen += 4; // CRLF, CRLF
3741 }
3742
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003743 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003744 if (flen + chklen > b_room(csbuf)) {
3745 if (chklen >= b_room(csbuf)) {
3746 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003747 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003748 }
3749 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003750 }
3751
3752 if (h2s->flags & H2_SF_DATA_CHNK) {
3753 /* emit the chunk size */
3754 unsigned int chksz = flen;
3755 char str[10];
3756 char *beg;
3757
3758 beg = str + sizeof(str);
3759 *--beg = '\n';
3760 *--beg = '\r';
3761 do {
3762 *--beg = hextab[chksz & 0xF];
3763 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003764 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003765 }
3766
Willy Tarreau454f9052017-10-26 19:40:35 +02003767 /* Block1 is the length of the first block before the buffer wraps,
3768 * block2 is the optional second block to reach the end of the frame.
3769 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003770 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003771 if (block1 > flen)
3772 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003773 block2 = flen - block1;
3774
3775 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003776 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003777
3778 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003779 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003780
Willy Tarreaueba10f22018-04-25 20:44:22 +02003781 if (h2s->flags & H2_SF_DATA_CHNK) {
3782 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003783 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003784 }
3785
Willy Tarreau454f9052017-10-26 19:40:35 +02003786 /* now mark the input data as consumed (will be deleted from the buffer
3787 * by the caller when seeing FRAME_A after sending the window update).
3788 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003789 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003790 h2c->dfl -= flen;
3791 h2c->rcvd_c += flen;
3792 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3793
Willy Tarreau1915ca22019-01-24 11:49:37 +01003794 if (h2s->flags & H2_SF_DATA_CLEN)
3795 h2s->body_len -= flen;
3796
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003797 if (h2c->dfl > h2c->dpl) {
3798 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003799 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003800 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003801 }
3802
Willy Tarreau4a28da12018-01-04 14:41:00 +01003803 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003804 /* here we're done with the frame, all the payload (except padding) was
3805 * transferred.
3806 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003807
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003808 if (h2c->dff & H2_F_DATA_END_STREAM) {
3809 if (htx) {
3810 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3811 h2c->flags |= H2_CF_DEM_SFULL;
3812 goto fail;
3813 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003814 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003815 else if (h2s->flags & H2_SF_DATA_CHNK) {
3816 /* emit the trailing 0 CRLF CRLF */
3817 if (b_room(csbuf) < 5) {
3818 h2c->flags |= H2_CF_DEM_SFULL;
3819 goto fail;
3820 }
3821 chklen += 5;
3822 b_putblk(csbuf, "0\r\n\r\n", 5);
3823 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003824 }
3825
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003826 h2c->rcvd_c += h2c->dpl;
3827 h2c->rcvd_s += h2c->dpl;
3828 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003829 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003830 if (htx)
3831 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003832 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003833 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003834 if (htx)
3835 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003836 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003837}
3838
Willy Tarreau5dd17352018-06-14 13:33:30 +02003839/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3840 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3841 * number of bytes sent. The caller must check the stream's status to detect
3842 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003843 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003844static 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 +02003845{
3846 struct http_hdr list[MAX_HTTP_HDR];
3847 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003848 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003849 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003850 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003851 int es_now = 0;
3852 int ret = 0;
3853 int hdr;
3854
3855 if (h2c_mux_busy(h2c, h2s)) {
3856 h2s->flags |= H2_SF_BLK_MBUSY;
3857 return 0;
3858 }
3859
Willy Tarreau44e973f2018-03-01 17:49:30 +01003860 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003861 h2c->flags |= H2_CF_MUX_MALLOC;
3862 h2s->flags |= H2_SF_BLK_MROOM;
3863 return 0;
3864 }
3865
3866 /* First, try to parse the H1 response and index it into <list>.
3867 * NOTE! Since it comes from haproxy, we *know* that a response header
3868 * block does not wrap and we can safely read it this way without
3869 * having to realign the buffer.
3870 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003871 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003872 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003873 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003874 /* incomplete or invalid response, this is abnormal coming from
3875 * haproxy and may only result in a bad errorfile or bad Lua code
3876 * so that won't be fixed, raise an error now.
3877 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003878 * FIXME: we should instead add the ability to only return a
3879 * 502 bad gateway. But in theory this is not supposed to
3880 * happen.
3881 */
3882 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3883 ret = 0;
3884 goto end;
3885 }
3886
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003887 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003888
3889 /* certain statuses have no body or an empty one, regardless of
3890 * what the headers say.
3891 */
3892 if (sl.st.status >= 100 && sl.st.status < 200) {
3893 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3894 h1m->curr_len = h1m->body_len = 0;
3895 }
3896 else if (sl.st.status == 204 || sl.st.status == 304) {
3897 /* no contents, claim c-len is present and set to zero */
3898 h1m->flags &= ~H1_MF_CHNK;
3899 h1m->flags |= H1_MF_CLEN;
3900 h1m->curr_len = h1m->body_len = 0;
3901 }
3902
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003903 chunk_reset(&outbuf);
3904
3905 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003906 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003907 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003908 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003909
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003910 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003911 break;
3912 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003913 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003914 }
3915
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003916 if (outbuf.size < 9)
3917 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003918
3919 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003920 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3921 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3922 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003923
3924 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003925 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003926 /* this is an unparsable response */
3927 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3928 ret = 0;
3929 goto end;
3930 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003931
3932 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003933 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003934 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003935 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003936 }
3937
3938 /* encode all headers, stop at empty name */
3939 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003940 /* these ones do not exist in H2 and must be dropped. */
3941 if (isteq(list[hdr].n, ist("connection")) ||
3942 isteq(list[hdr].n, ist("proxy-connection")) ||
3943 isteq(list[hdr].n, ist("keep-alive")) ||
3944 isteq(list[hdr].n, ist("upgrade")) ||
3945 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003946 continue;
3947
3948 if (isteq(list[hdr].n, ist("")))
3949 break; // end
3950
3951 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3952 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003953 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003954 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003955 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003956 }
3957 }
3958
3959 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003960 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003961 es_now = 1;
3962
3963 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003964 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003965
3966 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003967 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003968
3969 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003970 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003971
3972 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003973 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003974 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003975
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003976 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003977 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003978 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003979
Willy Tarreau801250e2018-09-11 11:45:04 +02003980 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003981 h2s->flags |= H2_SF_ES_SENT;
3982 if (h2s->st == H2_SS_OPEN)
3983 h2s->st = H2_SS_HLOC;
3984 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003985 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003986 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003987 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003988 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003989 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003990 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003991 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003992 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003993 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003994
3995 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003996
3997 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003998 //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 +02003999 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004000 full:
4001 h1m_init_res(h1m);
4002 h1m->err_pos = -1; // don't care about errors on the response path
4003 h2c->flags |= H2_CF_MUX_MFULL;
4004 h2s->flags |= H2_SF_BLK_MROOM;
4005 ret = 0;
4006 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004007}
4008
Willy Tarreau5dd17352018-06-14 13:33:30 +02004009/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4010 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4011 * the number of bytes sent. The caller must check the stream's status to
4012 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004013 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004014static 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 +02004015{
4016 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004017 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004018 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004019 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004020 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004021 int es_now = 0;
4022 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004023 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004024 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004025
4026 if (h2c_mux_busy(h2c, h2s)) {
4027 h2s->flags |= H2_SF_BLK_MBUSY;
4028 goto end;
4029 }
4030
Willy Tarreau44e973f2018-03-01 17:49:30 +01004031 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004032 h2c->flags |= H2_CF_MUX_MALLOC;
4033 h2s->flags |= H2_SF_BLK_MROOM;
4034 goto end;
4035 }
4036
4037 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004038 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004039 goto end;
4040
4041 chunk_reset(&outbuf);
4042
4043 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004044 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004045 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004046 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004047
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004048 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004049 break;
4050 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004051 /* If there are pending data in the output buffer, and we have
4052 * less than 1/4 of the mbuf's size and everything fits, we'll
4053 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4054 * is full and wait, to save some slow realign calls.
4055 */
4056 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4057 h2c->flags |= H2_CF_MUX_MFULL;
4058 h2s->flags |= H2_SF_BLK_MROOM;
4059 goto end;
4060 }
4061
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004062 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004063 }
4064
4065 if (outbuf.size < 9) {
4066 h2c->flags |= H2_CF_MUX_MFULL;
4067 h2s->flags |= H2_SF_BLK_MROOM;
4068 goto end;
4069 }
4070
4071 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004072 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4073 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4074 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004075
4076 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4077 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004078 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004079 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004080 break;
4081 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004082 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004083 if ((long long)size > h1m->curr_len)
4084 size = h1m->curr_len;
4085 break;
4086 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004087 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004088 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004089 if (!ret)
4090 goto end;
4091
4092 if (ret < 0) {
4093 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004094 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004095 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4096 goto end;
4097 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004098 max -= ret;
4099 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004100 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004101 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004102 }
4103
Willy Tarreau801250e2018-09-11 11:45:04 +02004104 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004105 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004106 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004107 if (!ret)
4108 goto end;
4109
4110 if (ret < 0) {
4111 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004112 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004113 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4114 goto end;
4115 }
4116
4117 size = chunk;
4118 h1m->curr_len = chunk;
4119 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004120 max -= ret;
4121 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004122 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004123 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004124 if (!size)
4125 goto send_empty;
4126 }
4127
4128 /* in MSG_DATA state, continue below */
4129 size = h1m->curr_len;
4130 break;
4131 }
4132
4133 /* we have in <size> the exact number of bytes we need to copy from
4134 * the H1 buffer. We need to check this against the connection's and
4135 * the stream's send windows, and to ensure that this fits in the max
4136 * frame size and in the buffer's available space minus 9 bytes (for
4137 * the frame header). The connection's flow control is applied last so
4138 * that we can use a separate list of streams which are immediately
4139 * unblocked on window opening. Note: we don't implement padding.
4140 */
4141
Willy Tarreau5dd17352018-06-14 13:33:30 +02004142 if (size > max)
4143 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004144
4145 if (size > h2s->mws)
4146 size = h2s->mws;
4147
4148 if (size <= 0) {
4149 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004150 if (h2s->send_wait) {
4151 LIST_DEL(&h2s->list);
4152 LIST_INIT(&h2s->list);
4153 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004154 goto end;
4155 }
4156
4157 if (h2c->mfs && size > h2c->mfs)
4158 size = h2c->mfs;
4159
4160 if (size + 9 > outbuf.size) {
4161 /* we have an opportunity for enlarging the too small
4162 * available space, let's try.
4163 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004164 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004165 goto realign_again;
4166 size = outbuf.size - 9;
4167 }
4168
4169 if (size <= 0) {
4170 h2c->flags |= H2_CF_MUX_MFULL;
4171 h2s->flags |= H2_SF_BLK_MROOM;
4172 goto end;
4173 }
4174
4175 if (size > h2c->mws)
4176 size = h2c->mws;
4177
4178 if (size <= 0) {
4179 h2s->flags |= H2_SF_BLK_MFCTL;
4180 goto end;
4181 }
4182
4183 /* copy whatever we can */
4184 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004185 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004186 if (ret == 1)
4187 len2 = 0;
4188
4189 if (!ret || len1 + len2 < size) {
4190 /* FIXME: must normally never happen */
4191 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4192 goto end;
4193 }
4194
4195 /* limit len1/len2 to size */
4196 if (len1 + len2 > size) {
4197 int sub = len1 + len2 - size;
4198
4199 if (len2 > sub)
4200 len2 -= sub;
4201 else {
4202 sub -= len2;
4203 len2 = 0;
4204 len1 -= sub;
4205 }
4206 }
4207
4208 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004209 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004210 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004211 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004212
4213 send_empty:
4214 /* we may need to add END_STREAM */
4215 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4216 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004217 *
4218 * FIXME: what we do here is not correct because we send end_stream
4219 * before knowing if we'll have to send a HEADERS frame for the
4220 * trailers. More importantly we're not consuming the trailing CRLF
4221 * after the end of trailers, so it will be left to the caller to
4222 * eat it. The right way to do it would be to measure trailers here
4223 * and to send ES only if there are no trailers.
4224 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004225 */
4226 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004227 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004228 es_now = 1;
4229
4230 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004231 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004232
4233 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004234 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004235
4236 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004237 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004238
4239 /* consume incoming H1 response */
4240 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004241 max -= size;
4242 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004243 total += size;
4244 h1m->curr_len -= size;
4245 h2s->mws -= size;
4246 h2c->mws -= size;
4247
4248 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004249 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004250 goto new_frame;
4251 }
4252 }
4253
4254 if (es_now) {
4255 if (h2s->st == H2_SS_OPEN)
4256 h2s->st = H2_SS_HLOC;
4257 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004258 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004259
Willy Tarreau35a62702018-02-27 15:37:25 +01004260 if (!(h1m->flags & H1_MF_CHNK)) {
4261 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004262 total += max;
4263 ofs += max;
4264 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004265
Willy Tarreau801250e2018-09-11 11:45:04 +02004266 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004267 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004268
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004269 h2s->flags |= H2_SF_ES_SENT;
4270 }
4271
4272 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004273 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 +02004274 return total;
4275}
4276
Willy Tarreau115e83b2018-12-01 19:17:53 +01004277/* Try to send a HEADERS frame matching HTX response present in HTX message
4278 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4279 * must check the stream's status to detect any error which might have happened
4280 * subsequently to a successful send. The htx blocks are automatically removed
4281 * from the message. The htx message is assumed to be valid since produced from
4282 * the internal code, hence it contains a start line, an optional series of
4283 * header blocks and an end of header, otherwise an invalid frame could be
4284 * emitted and the resulting htx message could be left in an inconsistent state.
4285 */
4286static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4287{
4288 struct http_hdr list[MAX_HTTP_HDR];
4289 struct h2c *h2c = h2s->h2c;
4290 struct htx_blk *blk;
4291 struct htx_blk *blk_end;
4292 struct buffer outbuf;
4293 struct htx_sl *sl;
4294 enum htx_blk_type type;
4295 int es_now = 0;
4296 int ret = 0;
4297 int hdr;
4298 int idx;
4299
4300 if (h2c_mux_busy(h2c, h2s)) {
4301 h2s->flags |= H2_SF_BLK_MBUSY;
4302 return 0;
4303 }
4304
4305 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4306 h2c->flags |= H2_CF_MUX_MALLOC;
4307 h2s->flags |= H2_SF_BLK_MROOM;
4308 return 0;
4309 }
4310
4311 /* determine the first block which must not be deleted, blk_end may
4312 * be NULL if all blocks have to be deleted.
4313 */
4314 idx = htx_get_head(htx);
4315 blk_end = NULL;
4316 while (idx != -1) {
4317 type = htx_get_blk_type(htx_get_blk(htx, idx));
4318 idx = htx_get_next(htx, idx);
4319 if (type == HTX_BLK_EOH) {
4320 if (idx != -1)
4321 blk_end = htx_get_blk(htx, idx);
4322 break;
4323 }
4324 }
4325
4326 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004327 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004328 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004329 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004330 if (h2s->status < 100 || h2s->status > 999)
4331 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004332
4333 /* and the rest of the headers, that we dump starting at header 0 */
4334 hdr = 0;
4335
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004336 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004337 while ((idx = htx_get_next(htx, idx)) != -1) {
4338 blk = htx_get_blk(htx, idx);
4339 type = htx_get_blk_type(blk);
4340
4341 if (type == HTX_BLK_UNUSED)
4342 continue;
4343
4344 if (type != HTX_BLK_HDR)
4345 break;
4346
4347 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4348 goto fail;
4349
4350 list[hdr].n = htx_get_blk_name(htx, blk);
4351 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004352 hdr++;
4353 }
4354
4355 /* marker for end of headers */
4356 list[hdr].n = ist("");
4357
4358 if (h2s->status == 204 || h2s->status == 304) {
4359 /* no contents, claim c-len is present and set to zero */
4360 es_now = 1;
4361 }
4362
4363 chunk_reset(&outbuf);
4364
4365 while (1) {
4366 outbuf.area = b_tail(&h2c->mbuf);
4367 outbuf.size = b_contig_space(&h2c->mbuf);
4368 outbuf.data = 0;
4369
4370 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4371 break;
4372 realign_again:
4373 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4374 }
4375
4376 if (outbuf.size < 9)
4377 goto full;
4378
4379 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4380 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4381 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4382 outbuf.data = 9;
4383
4384 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004385 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004386 if (b_space_wraps(&h2c->mbuf))
4387 goto realign_again;
4388 goto full;
4389 }
4390
4391 /* encode all headers, stop at empty name */
4392 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4393 /* these ones do not exist in H2 and must be dropped. */
4394 if (isteq(list[hdr].n, ist("connection")) ||
4395 isteq(list[hdr].n, ist("proxy-connection")) ||
4396 isteq(list[hdr].n, ist("keep-alive")) ||
4397 isteq(list[hdr].n, ist("upgrade")) ||
4398 isteq(list[hdr].n, ist("transfer-encoding")))
4399 continue;
4400
4401 if (isteq(list[hdr].n, ist("")))
4402 break; // end
4403
4404 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4405 /* output full */
4406 if (b_space_wraps(&h2c->mbuf))
4407 goto realign_again;
4408 goto full;
4409 }
4410 }
4411
Christopher Faulet0b465482019-02-19 15:14:23 +01004412 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004413 * FIXME: we should also set it when we know for sure that the
4414 * content-length is zero as well as on 204/304
4415 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004416 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4417 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004418 es_now = 1;
4419
Willy Tarreau927b88b2019-03-04 08:03:25 +01004420 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004421 es_now = 1;
4422
4423 /* update the frame's size */
4424 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4425
4426 if (es_now)
4427 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4428
4429 /* commit the H2 response */
4430 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004431
4432 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4433 * 1xx responses, another HEADERS frame is expected.
4434 */
4435 if (h2s->status >= 200 || h2s->status == 101)
4436 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004437
Willy Tarreau115e83b2018-12-01 19:17:53 +01004438 if (es_now) {
4439 h2s->flags |= H2_SF_ES_SENT;
4440 if (h2s->st == H2_SS_OPEN)
4441 h2s->st = H2_SS_HLOC;
4442 else
4443 h2s_close(h2s);
4444 }
4445
4446 /* OK we could properly deliver the response */
4447
4448 /* remove all header blocks including the EOH and compute the
4449 * corresponding size.
4450 *
4451 * FIXME: We should remove everything when es_now is set.
4452 */
4453 ret = 0;
4454 idx = htx_get_head(htx);
4455 blk = htx_get_blk(htx, idx);
4456 while (blk != blk_end) {
4457 ret += htx_get_blksz(blk);
4458 blk = htx_remove_blk(htx, blk);
4459 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004460
4461 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4462 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004463 end:
4464 return ret;
4465 full:
4466 h2c->flags |= H2_CF_MUX_MFULL;
4467 h2s->flags |= H2_SF_BLK_MROOM;
4468 ret = 0;
4469 goto end;
4470 fail:
4471 /* unparsable HTX messages, too large ones to be produced in the local
4472 * list etc go here (unrecoverable errors).
4473 */
4474 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4475 ret = 0;
4476 goto end;
4477}
4478
Willy Tarreau80739692018-10-05 11:35:57 +02004479/* Try to send a HEADERS frame matching HTX request present in HTX message
4480 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4481 * must check the stream's status to detect any error which might have happened
4482 * subsequently to a successful send. The htx blocks are automatically removed
4483 * from the message. The htx message is assumed to be valid since produced from
4484 * the internal code, hence it contains a start line, an optional series of
4485 * header blocks and an end of header, otherwise an invalid frame could be
4486 * emitted and the resulting htx message could be left in an inconsistent state.
4487 */
4488static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4489{
4490 struct http_hdr list[MAX_HTTP_HDR];
4491 struct h2c *h2c = h2s->h2c;
4492 struct htx_blk *blk;
4493 struct htx_blk *blk_end;
4494 struct buffer outbuf;
4495 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004496 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004497 enum htx_blk_type type;
4498 int es_now = 0;
4499 int ret = 0;
4500 int hdr;
4501 int idx;
4502
4503 if (h2c_mux_busy(h2c, h2s)) {
4504 h2s->flags |= H2_SF_BLK_MBUSY;
4505 return 0;
4506 }
4507
4508 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4509 h2c->flags |= H2_CF_MUX_MALLOC;
4510 h2s->flags |= H2_SF_BLK_MROOM;
4511 return 0;
4512 }
4513
4514 /* determine the first block which must not be deleted, blk_end may
4515 * be NULL if all blocks have to be deleted.
4516 */
4517 idx = htx_get_head(htx);
4518 blk_end = NULL;
4519 while (idx != -1) {
4520 type = htx_get_blk_type(htx_get_blk(htx, idx));
4521 idx = htx_get_next(htx, idx);
4522 if (type == HTX_BLK_EOH) {
4523 if (idx != -1)
4524 blk_end = htx_get_blk(htx, idx);
4525 break;
4526 }
4527 }
4528
4529 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004530 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004531 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004532 meth = htx_sl_req_meth(sl);
4533 path = htx_sl_req_uri(sl);
4534
4535 /* and the rest of the headers, that we dump starting at header 0 */
4536 hdr = 0;
4537
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004538 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004539 while ((idx = htx_get_next(htx, idx)) != -1) {
4540 blk = htx_get_blk(htx, idx);
4541 type = htx_get_blk_type(blk);
4542
4543 if (type == HTX_BLK_UNUSED)
4544 continue;
4545
4546 if (type != HTX_BLK_HDR)
4547 break;
4548
4549 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4550 goto fail;
4551
4552 list[hdr].n = htx_get_blk_name(htx, blk);
4553 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004554 hdr++;
4555 }
4556
4557 /* marker for end of headers */
4558 list[hdr].n = ist("");
4559
4560 chunk_reset(&outbuf);
4561
4562 while (1) {
4563 outbuf.area = b_tail(&h2c->mbuf);
4564 outbuf.size = b_contig_space(&h2c->mbuf);
4565 outbuf.data = 0;
4566
4567 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4568 break;
4569 realign_again:
4570 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4571 }
4572
4573 if (outbuf.size < 9)
4574 goto full;
4575
4576 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4577 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4578 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4579 outbuf.data = 9;
4580
4581 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004582 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004583 if (b_space_wraps(&h2c->mbuf))
4584 goto realign_again;
4585 goto full;
4586 }
4587
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004588 /* RFC7540 #8.3: the CONNECT method must have :
4589 * - :authority set to the URI part (host:port)
4590 * - :method set to CONNECT
4591 * - :scheme and :path omitted
4592 */
4593 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4594 /* encode the scheme which is always "https" (or 0x86 for "http") */
4595 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4596 /* output full */
4597 if (b_space_wraps(&h2c->mbuf))
4598 goto realign_again;
4599 goto full;
4600 }
Willy Tarreau80739692018-10-05 11:35:57 +02004601
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004602 /* encode the path, which necessarily is the second one */
4603 if (!hpack_encode_path(&outbuf, path)) {
4604 /* output full */
4605 if (b_space_wraps(&h2c->mbuf))
4606 goto realign_again;
4607 goto full;
4608 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004609
4610 /* look for the Host header and place it in :authority */
4611 auth = ist2(NULL, 0);
4612 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4613 if (isteq(list[hdr].n, ist("")))
4614 break; // end
4615
4616 if (isteq(list[hdr].n, ist("host"))) {
4617 auth = list[hdr].v;
4618 break;
4619 }
4620 }
4621 }
4622 else {
4623 /* for CONNECT, :authority is taken from the path */
4624 auth = path;
4625 }
4626
4627 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4628 /* output full */
4629 if (b_space_wraps(&h2c->mbuf))
4630 goto realign_again;
4631 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004632 }
4633
4634 /* encode all headers, stop at empty name */
4635 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4636 /* these ones do not exist in H2 and must be dropped. */
4637 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004638 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004639 isteq(list[hdr].n, ist("proxy-connection")) ||
4640 isteq(list[hdr].n, ist("keep-alive")) ||
4641 isteq(list[hdr].n, ist("upgrade")) ||
4642 isteq(list[hdr].n, ist("transfer-encoding")))
4643 continue;
4644
4645 if (isteq(list[hdr].n, ist("")))
4646 break; // end
4647
4648 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4649 /* output full */
4650 if (b_space_wraps(&h2c->mbuf))
4651 goto realign_again;
4652 goto full;
4653 }
4654 }
4655
4656 /* we may need to add END_STREAM if we have no body :
4657 * - request already closed, or :
4658 * - no transfer-encoding, and :
4659 * - no content-length or content-length:0
4660 * Fixme: this doesn't take into account CONNECT requests.
4661 */
4662 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4663 es_now = 1;
4664
4665 if (sl->flags & HTX_SL_F_BODYLESS)
4666 es_now = 1;
4667
Willy Tarreau927b88b2019-03-04 08:03:25 +01004668 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004669 es_now = 1;
4670
4671 /* update the frame's size */
4672 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4673
4674 if (es_now)
4675 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4676
4677 /* commit the H2 response */
4678 b_add(&h2c->mbuf, outbuf.data);
4679 h2s->flags |= H2_SF_HEADERS_SENT;
4680 h2s->st = H2_SS_OPEN;
4681
Willy Tarreau80739692018-10-05 11:35:57 +02004682 if (es_now) {
4683 // trim any possibly pending data (eg: inconsistent content-length)
4684 h2s->flags |= H2_SF_ES_SENT;
4685 h2s->st = H2_SS_HLOC;
4686 }
4687
4688 /* remove all header blocks including the EOH and compute the
4689 * corresponding size.
4690 *
4691 * FIXME: We should remove everything when es_now is set.
4692 */
4693 ret = 0;
4694 idx = htx_get_head(htx);
4695 blk = htx_get_blk(htx, idx);
4696 while (blk != blk_end) {
4697 ret += htx_get_blksz(blk);
4698 blk = htx_remove_blk(htx, blk);
4699 }
4700
4701 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4702 htx_remove_blk(htx, blk_end);
4703
4704 end:
4705 return ret;
4706 full:
4707 h2c->flags |= H2_CF_MUX_MFULL;
4708 h2s->flags |= H2_SF_BLK_MROOM;
4709 ret = 0;
4710 goto end;
4711 fail:
4712 /* unparsable HTX messages, too large ones to be produced in the local
4713 * list etc go here (unrecoverable errors).
4714 */
4715 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4716 ret = 0;
4717 goto end;
4718}
4719
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004720/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004721 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4722 * caller must check the stream's status to detect any error which might have
4723 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004724 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4725 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004726static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004727{
4728 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004729 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004730 struct buffer outbuf;
4731 size_t total = 0;
4732 int es_now = 0;
4733 int bsize; /* htx block size */
4734 int fsize; /* h2 frame size */
4735 struct htx_blk *blk;
4736 enum htx_blk_type type;
4737 int idx;
4738
4739 if (h2c_mux_busy(h2c, h2s)) {
4740 h2s->flags |= H2_SF_BLK_MBUSY;
4741 goto end;
4742 }
4743
4744 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4745 h2c->flags |= H2_CF_MUX_MALLOC;
4746 h2s->flags |= H2_SF_BLK_MROOM;
4747 goto end;
4748 }
4749
Willy Tarreau98de12a2018-12-12 07:03:00 +01004750 htx = htx_from_buf(buf);
4751
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004752 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4753 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4754 * the caller to handle.
4755 */
4756
4757 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004758 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004759 goto end;
4760
4761 idx = htx_get_head(htx);
4762 blk = htx_get_blk(htx, idx);
4763 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4764 bsize = htx_get_blksz(blk);
4765 fsize = bsize;
4766
4767 if (type == HTX_BLK_EOD) {
4768 /* if we have an EOD, we're dealing with chunked data. We may
4769 * have a set of trailers after us that the caller will want to
4770 * deal with. Let's simply remove the EOD and return.
4771 */
4772 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004773 total++; // EOD counts as one byte
4774 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004775 goto end;
4776 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004777 else if (type == HTX_BLK_EOM) {
4778 if (h2s->flags & H2_SF_ES_SENT) {
4779 /* ES already sent */
4780 htx_remove_blk(htx, blk);
4781 total++; // EOM counts as one byte
4782 count--;
4783 goto end;
4784 }
4785 }
4786 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004787 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004788
4789 /* Perform some optimizations to reduce the number of buffer copies.
4790 * First, if the mux's buffer is empty and the htx area contains
4791 * exactly one data block of the same size as the requested count, and
4792 * this count fits within the frame size, the stream's window size, and
4793 * the connection's window size, then it's possible to simply swap the
4794 * caller's buffer with the mux's output buffer and adjust offsets and
4795 * length to match the entire DATA HTX block in the middle. In this
4796 * case we perform a true zero-copy operation from end-to-end. This is
4797 * the situation that happens all the time with large files. Second, if
4798 * this is not possible, but the mux's output buffer is empty, we still
4799 * have an opportunity to avoid the copy to the intermediary buffer, by
4800 * making the intermediary buffer's area point to the output buffer's
4801 * area. In this case we want to skip the HTX header to make sure that
4802 * copies remain aligned and that this operation remains possible all
4803 * the time. This goes for headers, data blocks and any data extracted
4804 * from the HTX blocks.
4805 */
4806 if (unlikely(fsize == count &&
4807 htx->used == 1 && type == HTX_BLK_DATA &&
4808 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4809 void *old_area = h2c->mbuf.area;
4810
4811 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004812 /* Too bad there are data left there. We're willing to memcpy/memmove
4813 * up to 1/4 of the buffer, which means that it's OK to copy a large
4814 * frame into a buffer containing few data if it needs to be realigned,
4815 * and that it's also OK to copy few data without realigning. Otherwise
4816 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004817 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004818 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4819 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4820 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004821 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004822
Willy Tarreau98de12a2018-12-12 07:03:00 +01004823 h2c->flags |= H2_CF_MUX_MFULL;
4824 h2s->flags |= H2_SF_BLK_MROOM;
4825 goto end;
4826 }
4827
4828 /* map an H2 frame to the HTX block so that we can put the
4829 * frame header there.
4830 */
4831 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004832 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004833 h2c->mbuf.data = fsize + 9;
4834 outbuf.area = b_head(&h2c->mbuf);
4835
4836 /* prepend an H2 DATA frame header just before the DATA block */
4837 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4838 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4839 h2_set_frame_size(outbuf.area, fsize);
4840
4841 /* update windows */
4842 h2s->mws -= fsize;
4843 h2c->mws -= fsize;
4844
4845 /* and exchange with our old area */
4846 buf->area = old_area;
4847 buf->data = buf->head = 0;
4848 total += fsize;
4849 goto end;
4850 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004851
Willy Tarreau98de12a2018-12-12 07:03:00 +01004852 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004853 /* for DATA and EOM we'll have to emit a frame, even if empty */
4854
4855 while (1) {
4856 outbuf.area = b_tail(&h2c->mbuf);
4857 outbuf.size = b_contig_space(&h2c->mbuf);
4858 outbuf.data = 0;
4859
4860 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4861 break;
4862 realign_again:
4863 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4864 }
4865
4866 if (outbuf.size < 9) {
4867 h2c->flags |= H2_CF_MUX_MFULL;
4868 h2s->flags |= H2_SF_BLK_MROOM;
4869 goto end;
4870 }
4871
4872 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4873 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4874 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4875 outbuf.data = 9;
4876
4877 /* we have in <fsize> the exact number of bytes we need to copy from
4878 * the HTX buffer. We need to check this against the connection's and
4879 * the stream's send windows, and to ensure that this fits in the max
4880 * frame size and in the buffer's available space minus 9 bytes (for
4881 * the frame header). The connection's flow control is applied last so
4882 * that we can use a separate list of streams which are immediately
4883 * unblocked on window opening. Note: we don't implement padding.
4884 */
4885
4886 /* EOM is presented with bsize==1 but would lead to the emission of an
4887 * empty frame, thus we force it to zero here.
4888 */
4889 if (type == HTX_BLK_EOM)
4890 bsize = fsize = 0;
4891
4892 if (!fsize)
4893 goto send_empty;
4894
4895 if (h2s->mws <= 0) {
4896 h2s->flags |= H2_SF_BLK_SFCTL;
4897 if (h2s->send_wait) {
4898 LIST_DEL(&h2s->list);
4899 LIST_INIT(&h2s->list);
4900 }
4901 goto end;
4902 }
4903
Willy Tarreauee573762018-12-04 15:25:57 +01004904 if (fsize > count)
4905 fsize = count;
4906
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004907 if (fsize > h2s->mws)
4908 fsize = h2s->mws; // >0
4909
4910 if (h2c->mfs && fsize > h2c->mfs)
4911 fsize = h2c->mfs; // >0
4912
4913 if (fsize + 9 > outbuf.size) {
4914 /* we have an opportunity for enlarging the too small
4915 * available space, let's try.
4916 * FIXME: is this really interesting to do? Maybe we'll
4917 * spend lots of time realigning instead of using two
4918 * frames.
4919 */
4920 if (b_space_wraps(&h2c->mbuf))
4921 goto realign_again;
4922 fsize = outbuf.size - 9;
4923
4924 if (fsize <= 0) {
4925 /* no need to send an empty frame here */
4926 h2c->flags |= H2_CF_MUX_MFULL;
4927 h2s->flags |= H2_SF_BLK_MROOM;
4928 goto end;
4929 }
4930 }
4931
4932 if (h2c->mws <= 0) {
4933 h2s->flags |= H2_SF_BLK_MFCTL;
4934 goto end;
4935 }
4936
4937 if (fsize > h2c->mws)
4938 fsize = h2c->mws;
4939
4940 /* now let's copy this this into the output buffer */
4941 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004942 h2s->mws -= fsize;
4943 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004944 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004945
4946 send_empty:
4947 /* update the frame's size */
4948 h2_set_frame_size(outbuf.area, fsize);
4949
4950 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4951 * meeting EOM. We should optimize this later.
4952 */
4953 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004954 total++; // EOM counts as one byte
4955 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004956 es_now = 1;
4957 }
4958
4959 if (es_now)
4960 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4961
4962 /* commit the H2 response */
4963 b_add(&h2c->mbuf, fsize + 9);
4964
4965 /* consume incoming HTX block, including EOM */
4966 total += fsize;
4967 if (fsize == bsize) {
4968 htx_remove_blk(htx, blk);
4969 if (fsize)
4970 goto new_frame;
4971 } else {
4972 /* we've truncated this block */
4973 htx_cut_data_blk(htx, blk, fsize);
4974 }
4975
4976 if (es_now) {
4977 if (h2s->st == H2_SS_OPEN)
4978 h2s->st = H2_SS_HLOC;
4979 else
4980 h2s_close(h2s);
4981
4982 h2s->flags |= H2_SF_ES_SENT;
4983 }
4984
4985 end:
4986 return total;
4987}
4988
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004989/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4990 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4991 * processed. The caller must check the stream's status to detect any error
4992 * which might have happened subsequently to a successful send. The htx blocks
4993 * are automatically removed from the message. The htx message is assumed to be
4994 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02004995 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004996 * as a single frame. The ES flag is always set.
4997 */
4998static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4999{
5000 struct http_hdr list[MAX_HTTP_HDR];
5001 struct h2c *h2c = h2s->h2c;
5002 struct htx_blk *blk;
5003 struct htx_blk *blk_end;
5004 struct buffer outbuf;
5005 struct h1m h1m;
5006 enum htx_blk_type type;
5007 uint32_t size;
5008 int ret = 0;
5009 int hdr;
5010 int idx;
5011 void *start;
5012
5013 if (h2c_mux_busy(h2c, h2s)) {
5014 h2s->flags |= H2_SF_BLK_MBUSY;
5015 goto end;
5016 }
5017
5018 if (!h2_get_buf(h2c, &h2c->mbuf)) {
5019 h2c->flags |= H2_CF_MUX_MALLOC;
5020 h2s->flags |= H2_SF_BLK_MROOM;
5021 goto end;
5022 }
5023
5024 /* The principle is that we parse each and every trailers block using
5025 * the H1 headers parser, and append it to the list. We don't proceed
5026 * until EOM is met. blk_end will point to the EOM block.
5027 */
5028 hdr = 0;
5029 memset(list, 0, sizeof(list));
5030 blk_end = NULL;
5031
5032 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5033 blk = htx_get_blk(htx, idx);
5034 type = htx_get_blk_type(blk);
5035
5036 if (type == HTX_BLK_UNUSED)
5037 continue;
5038
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005039 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005040 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005041
5042 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5043 goto fail;
5044
5045 size = htx_get_blksz(blk);
5046 start = htx_get_blk_ptr(htx, blk);
5047
5048 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5049 h1m.err_pos = 0;
5050 ret = h1_headers_to_hdr_list(start, start + size,
5051 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5052 &h1m, NULL);
5053 if (ret < 0)
5054 goto fail;
5055
5056 /* ret == 0 if an incomplete trailers block was found (missing
5057 * empty line), or > 0 if it was found. We have to continue on
5058 * incomplete messages because the trailers block might be
5059 * incomplete.
5060 */
5061
5062 /* search the new end */
5063 while (hdr <= sizeof(list)/sizeof(list[0])) {
5064 if (!list[hdr].n.len)
5065 break;
5066 hdr++;
5067 }
5068 }
5069
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005070 if (list[hdr].n.len != 0)
5071 goto fail; // empty trailer not found: internal error
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005072
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005073 chunk_reset(&outbuf);
5074
5075 while (1) {
5076 outbuf.area = b_tail(&h2c->mbuf);
5077 outbuf.size = b_contig_space(&h2c->mbuf);
5078 outbuf.data = 0;
5079
5080 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5081 break;
5082 realign_again:
5083 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5084 }
5085
5086 if (outbuf.size < 9)
5087 goto full;
5088
5089 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5090 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5091 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5092 outbuf.data = 9;
5093
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005094 /* encode all headers */
5095 for (idx = 0; idx < hdr; idx++) {
5096 /* these ones do not exist in H2 or must not appear in
5097 * trailers and must be dropped.
5098 */
5099 if (isteq(list[idx].n, ist("host")) ||
5100 isteq(list[idx].n, ist("content-length")) ||
5101 isteq(list[idx].n, ist("connection")) ||
5102 isteq(list[idx].n, ist("proxy-connection")) ||
5103 isteq(list[idx].n, ist("keep-alive")) ||
5104 isteq(list[idx].n, ist("upgrade")) ||
5105 isteq(list[idx].n, ist("te")) ||
5106 isteq(list[idx].n, ist("transfer-encoding")))
5107 continue;
5108
5109 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5110 /* output full */
5111 if (b_space_wraps(&h2c->mbuf))
5112 goto realign_again;
5113 goto full;
5114 }
5115 }
5116
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005117 if (outbuf.data == 9) {
5118 /* here we have a problem, we have nothing to emit (either we
5119 * received an empty trailers block followed or we removed its
5120 * contents above). Because of this we can't send a HEADERS
5121 * frame, so we have to cheat and instead send an empty DATA
5122 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005123 */
5124 outbuf.area[3] = H2_FT_DATA;
5125 outbuf.area[4] = H2_F_DATA_END_STREAM;
5126 }
5127
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005128 /* update the frame's size */
5129 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5130
5131 /* commit the H2 response */
5132 b_add(&h2c->mbuf, outbuf.data);
5133 h2s->flags |= H2_SF_ES_SENT;
5134
5135 if (h2s->st == H2_SS_OPEN)
5136 h2s->st = H2_SS_HLOC;
5137 else
5138 h2s_close(h2s);
5139
5140 /* OK we could properly deliver the response */
5141 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005142 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005143 ret = 0;
5144 idx = htx_get_head(htx);
5145 blk = htx_get_blk(htx, idx);
5146 while (blk != blk_end) {
5147 ret += htx_get_blksz(blk);
5148 blk = htx_remove_blk(htx, blk);
5149 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005150 end:
5151 return ret;
5152 full:
5153 h2c->flags |= H2_CF_MUX_MFULL;
5154 h2s->flags |= H2_SF_BLK_MROOM;
5155 ret = 0;
5156 goto end;
5157 fail:
5158 /* unparsable HTX messages, too large ones to be produced in the local
5159 * list etc go here (unrecoverable errors).
5160 */
5161 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5162 ret = 0;
5163 goto end;
5164}
5165
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005166/* Called from the upper layer, to subscribe to events, such as being able to send.
5167 * The <param> argument here is supposed to be a pointer to a wait_event struct
5168 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5169 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5170 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5171 * returns 0 except for the error above.
5172 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005173static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5174{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005175 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005176 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005177 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005178
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005179 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005180 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005181 if (!(sw->events & SUB_RETRY_RECV)) {
5182 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005183 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005184 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005185 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005186 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005187 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005188 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005189 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005190 if (!(sw->events & SUB_RETRY_SEND)) {
5191 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005192 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005193 h2s->send_wait = sw;
Olivier Houchard9a0f5592019-04-15 19:22:24 +02005194 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5195 LIST_ISEMPTY(&h2s->list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005196 if (h2s->flags & H2_SF_BLK_MFCTL)
5197 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5198 else
5199 LIST_ADDQ(&h2c->send_list, &h2s->list);
5200 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005201 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005202 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005203 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005204 if (event_type != 0)
5205 return -1;
5206 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005207}
5208
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005209/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5210 * The <param> argument here is supposed to be a pointer to the same wait_event
5211 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5212 * It always returns zero.
5213 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005214static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5215{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005216 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005217 struct h2s *h2s = cs->ctx;
5218
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005219 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005220 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005221 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005222 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005223 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005224 }
5225 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005226 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005227 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005228 if (h2s->send_wait == sw) {
5229 LIST_DEL(&h2s->list);
5230 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005231 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard998410a2019-04-15 19:23:37 +02005232 /* We were about to send, make sure it does not happen */
5233 if (!LIST_ISEMPTY(&h2s->sending_list) &&
5234 h2s->send_wait != &h2s->wait_event) {
5235 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5236 LIST_DEL_INIT(&h2s->sending_list);
5237 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005238 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02005239
Olivier Houchardd846c262018-10-19 17:24:29 +02005240 }
5241 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005242 return 0;
5243}
5244
5245
Olivier Houchard511efea2018-08-16 15:30:32 +02005246/* Called from the upper layer, to receive data */
5247static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5248{
Olivier Houchard638b7992018-08-16 15:41:52 +02005249 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005250 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005251 struct htx *h2s_htx = NULL;
5252 struct htx *buf_htx = NULL;
5253 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005254 size_t ret = 0;
5255
5256 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005257 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5258 /* in HTX mode we ignore the count argument */
5259 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005260 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005261 /* Here htx_to_buf() will set buffer data to 0 because
5262 * the HTX is empty.
5263 */
5264 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005265 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005266 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005267
5268 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005269 count = htx_free_data_space(buf_htx);
5270 if (flags & CO_RFL_KEEP_RSV) {
5271 if (count <= global.tune.maxrewrite)
5272 goto end;
5273 count -= global.tune.maxrewrite;
5274 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005275
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005276 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005277
5278 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5279 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5280
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005281 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005282 htx_to_buf(buf_htx, buf);
5283 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005284 ret = htx_ret.ret;
5285 }
5286 else {
5287 ret = b_xfer(buf, &h2s->rxbuf, count);
5288 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005289
Christopher Faulet37070b22019-02-14 15:12:14 +01005290 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005291 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005292 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005293 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005294 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005295 if (cs->flags & CS_FL_REOS)
5296 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005297 if (cs->flags & CS_FL_ERR_PENDING)
5298 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005299 if (b_size(&h2s->rxbuf)) {
5300 b_free(&h2s->rxbuf);
5301 offer_buffers(NULL, tasks_run_queue);
5302 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005303 }
5304
Willy Tarreau082f5592018-11-25 08:03:32 +01005305 if (ret && h2c->dsi == h2s->id) {
5306 /* demux is blocking on this stream's buffer */
5307 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005308 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005309 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005310
Olivier Houchard511efea2018-08-16 15:30:32 +02005311 return ret;
5312}
5313
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005314/* stops all senders of this connection for example when the mux buffer is full.
5315 * They are moved from the sending_list to either fctl_list or send_list.
5316 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005317static void h2_stop_senders(struct h2c *h2c)
5318{
5319 struct h2s *h2s, *h2s_back;
5320
Olivier Houchardd360ac62019-03-22 17:37:16 +01005321 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5322 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005323 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005324 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005325 }
5326}
5327
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005328/* Called from the upper layer, to send data from buffer <buf> for no more than
5329 * <count> bytes. Returns the number of bytes effectively sent. Some status
5330 * flags may be updated on the conn_stream.
5331 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005332static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005333{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005334 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005335 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005336 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005337 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005338 struct htx *htx;
5339 struct htx_blk *blk;
5340 enum htx_blk_type btype;
5341 uint32_t bsize;
5342 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005343
Olivier Houchardd360ac62019-03-22 17:37:16 +01005344 /* If we were not just woken because we wanted to send but couldn't,
5345 * and there's somebody else that is waiting to send, do nothing,
5346 * we will subscribe later and be put at the end of the list
5347 */
Olivier Houchard998410a2019-04-15 19:23:37 +02005348 if (LIST_ISEMPTY(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005349 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5350 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005351 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005352
Olivier Houchard998410a2019-04-15 19:23:37 +02005353 /* We couldn't set it to NULL before, because we needed it in case
5354 * we had to cancel the tasklet
5355 */
5356 h2s->send_wait = NULL;
5357
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005358 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5359 return 0;
5360
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005361 /* htx will be enough to decide if we're using HTX or legacy */
5362 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5363
Willy Tarreau0bad0432018-06-14 16:54:01 +02005364 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005365 h2s->flags |= H2_SF_OUTGOING_DATA;
5366
Willy Tarreau751f2d02018-10-05 09:35:00 +02005367 if (h2s->id == 0) {
5368 int32_t id = h2c_get_next_sid(h2s->h2c);
5369
5370 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005371 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005372 return 0;
5373 }
5374
5375 eb32_delete(&h2s->by_id);
5376 h2s->by_id.key = h2s->id = id;
5377 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005378 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005379 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5380 }
5381
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005382 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005383 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005384 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005385 idx = htx_get_head(htx);
5386 blk = htx_get_blk(htx, idx);
5387 btype = htx_get_blk_type(blk);
5388 bsize = htx_get_blksz(blk);
5389
5390 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005391 case HTX_BLK_REQ_SL:
5392 /* start-line before headers */
5393 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5394 if (ret > 0) {
5395 total += ret;
5396 count -= ret;
5397 if (ret < bsize)
5398 goto done;
5399 }
5400 break;
5401
Willy Tarreau115e83b2018-12-01 19:17:53 +01005402 case HTX_BLK_RES_SL:
5403 /* start-line before headers */
5404 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5405 if (ret > 0) {
5406 total += ret;
5407 count -= ret;
5408 if (ret < bsize)
5409 goto done;
5410 }
5411 break;
5412
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005413 case HTX_BLK_DATA:
5414 case HTX_BLK_EOD:
5415 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005416 /* all these cause the emission of a DATA frame (possibly empty).
5417 * This EOM necessarily is one before trailers, as the EOM following
5418 * trailers would have been consumed by the trailers parser.
5419 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005420 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005421 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005422 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005423 total += ret;
5424 count -= ret;
5425 if (ret < bsize)
5426 goto done;
5427 }
5428 break;
5429
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005430 case HTX_BLK_TLR:
5431 /* This is the first trailers block, all the subsequent ones AND
5432 * the EOM will be swallowed by the parser.
5433 */
5434 ret = h2s_htx_make_trailers(h2s, htx);
5435 if (ret > 0) {
5436 total += ret;
5437 count -= ret;
5438 if (ret < bsize)
5439 goto done;
5440 }
5441 break;
5442
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005443 default:
5444 htx_remove_blk(htx, blk);
5445 total += bsize;
5446 count -= bsize;
5447 break;
5448 }
5449 }
5450 goto done;
5451 }
5452
5453 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005454 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005455 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005456 if (h2s->h2c->flags & H2_CF_IS_BACK)
5457 ret = -1;
5458 else
5459 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005460 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005461 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005462 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005463 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005464 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005465 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005466 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005467
Willy Tarreau5dd17352018-06-14 13:33:30 +02005468 if (unlikely((int)ret <= 0)) {
5469 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005470 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5471 break;
5472 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005473 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005474 total += count;
5475 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005476 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005477 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005478 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005479 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005480 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005481 break;
5482 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005483
5484 total += ret;
5485 count -= ret;
5486
Willy Tarreau2b778482019-05-06 15:00:22 +02005487 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005488 break;
5489
5490 if (h2s->flags & H2_SF_BLK_ANY)
5491 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005492 }
5493
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005494 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005495 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005496 /* trim any possibly pending data after we close (extra CR-LF,
5497 * unprocessed trailers, abnormal extra data, ...)
5498 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005499 total += count;
5500 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005501 }
5502
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005503 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005504 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005505 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005506 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005507 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005508 }
5509
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005510 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005511 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005512 } else {
5513 b_del(buf, total);
5514 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005515
5516 /* The mux is full, cancel the pending tasks */
5517 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5518 (h2s->flags & H2_SF_BLK_MBUSY))
5519 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005520
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005521 /* If we're running HTX, and we read the whole buffer, then pretend
5522 * we read exactly what the caller specified, as with HTX the caller
5523 * will always give the buffer size, instead of the amount of data
5524 * available.
5525 */
5526 if (htx && !b_data(buf))
5527 total = orig_count;
5528
Olivier Houchard7505f942018-08-21 18:10:44 +02005529 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005530 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005531 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005532
Olivier Houchard7505f942018-08-21 18:10:44 +02005533 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005534 /* If we're waiting for flow control, and we got a shutr on the
5535 * connection, we will never be unlocked, so add an error on
5536 * the conn_stream.
5537 */
5538 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5539 !b_data(&h2s->h2c->dbuf) &&
5540 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5541 if (cs->flags & CS_FL_EOS)
5542 cs->flags |= CS_FL_ERROR;
5543 else
5544 cs->flags |= CS_FL_ERR_PENDING;
5545 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005546 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005547 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005548 LIST_DEL_INIT(&h2s->list);
5549 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005550 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005551}
5552
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005553/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005554static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005555{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005556 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005557 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005558 struct eb32_node *node;
5559 int fctl_cnt = 0;
5560 int send_cnt = 0;
5561 int tree_cnt = 0;
5562 int orph_cnt = 0;
5563
5564 if (!h2c)
5565 return;
5566
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005567 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005568 fctl_cnt++;
5569
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005570 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005571 send_cnt++;
5572
Willy Tarreau3af37712018-12-18 14:34:41 +01005573 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005574 node = eb32_first(&h2c->streams_by_id);
5575 while (node) {
5576 h2s = container_of(node, struct h2s, by_id);
5577 tree_cnt++;
5578 if (!h2s->cs)
5579 orph_cnt++;
5580 node = eb32_next(node);
5581 }
5582
Willy Tarreau987c0632018-12-18 10:32:05 +01005583 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5584 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5585 " .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 +02005586 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5587 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005588 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005589 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5590 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5591 h2c->msi,
5592 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5593 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5594
5595 if (h2s) {
5596 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5597 h2s, h2s->id, h2s->flags,
5598 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5599 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5600 h2s->cs);
5601 if (h2s->cs)
5602 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5603 h2s->cs->flags, h2s->cs->data);
5604 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005605}
Willy Tarreau62f52692017-10-08 23:01:42 +02005606
5607/*******************************************************/
5608/* functions below are dedicated to the config parsers */
5609/*******************************************************/
5610
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005611/* config parser for global "tune.h2.header-table-size" */
5612static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5613 struct proxy *defpx, const char *file, int line,
5614 char **err)
5615{
5616 if (too_many_args(1, args, err, NULL))
5617 return -1;
5618
5619 h2_settings_header_table_size = atoi(args[1]);
5620 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5621 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5622 return -1;
5623 }
5624 return 0;
5625}
Willy Tarreau62f52692017-10-08 23:01:42 +02005626
Willy Tarreaue6baec02017-07-27 11:45:11 +02005627/* config parser for global "tune.h2.initial-window-size" */
5628static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5629 struct proxy *defpx, const char *file, int line,
5630 char **err)
5631{
5632 if (too_many_args(1, args, err, NULL))
5633 return -1;
5634
5635 h2_settings_initial_window_size = atoi(args[1]);
5636 if (h2_settings_initial_window_size < 0) {
5637 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5638 return -1;
5639 }
5640 return 0;
5641}
5642
Willy Tarreau5242ef82017-07-27 11:47:28 +02005643/* config parser for global "tune.h2.max-concurrent-streams" */
5644static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5645 struct proxy *defpx, const char *file, int line,
5646 char **err)
5647{
5648 if (too_many_args(1, args, err, NULL))
5649 return -1;
5650
5651 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005652 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005653 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5654 return -1;
5655 }
5656 return 0;
5657}
5658
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005659/* config parser for global "tune.h2.max-frame-size" */
5660static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5661 struct proxy *defpx, const char *file, int line,
5662 char **err)
5663{
5664 if (too_many_args(1, args, err, NULL))
5665 return -1;
5666
5667 h2_settings_max_frame_size = atoi(args[1]);
5668 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5669 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5670 return -1;
5671 }
5672 return 0;
5673}
5674
Willy Tarreau62f52692017-10-08 23:01:42 +02005675
5676/****************************************/
5677/* MUX initialization and instanciation */
5678/***************************************/
5679
5680/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005681static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005682 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005683 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005684 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005685 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005686 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005687 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005688 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005689 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005690 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005691 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005692 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005693 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005694 .shutr = h2_shutr,
5695 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005696 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005697 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005698 .name = "H2",
5699};
5700
Christopher Faulet32f61c02018-04-10 14:33:41 +02005701/* PROTO selection : this mux registers PROTO token "h2" */
5702static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005703 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005704
Willy Tarreau0108d902018-11-25 19:14:37 +01005705INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5706
Christopher Faulet9b579102019-04-11 22:52:25 +02005707
5708/* The mux operations */
5709static const struct mux_ops h2_htx_ops = {
5710 .init = h2_init,
5711 .wake = h2_wake,
5712 .snd_buf = h2_snd_buf,
5713 .rcv_buf = h2_rcv_buf,
5714 .subscribe = h2_subscribe,
5715 .unsubscribe = h2_unsubscribe,
5716 .attach = h2_attach,
5717 .get_first_cs = h2_get_first_cs,
5718 .detach = h2_detach,
5719 .destroy = h2_destroy,
5720 .avail_streams = h2_avail_streams,
5721 .used_streams = h2_used_streams,
5722 .shutr = h2_shutr,
5723 .shutw = h2_shutw,
5724 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005725 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005726 .name = "H2",
5727};
5728
Willy Tarreauf8957272018-10-03 10:25:20 +02005729static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005730 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005731
5732INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5733
Willy Tarreau62f52692017-10-08 23:01:42 +02005734/* config keyword parsers */
5735static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005736 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005737 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005738 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005739 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005740 { 0, NULL, NULL }
5741}};
5742
Willy Tarreau0108d902018-11-25 19:14:37 +01005743INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);