blob: 0a500f61045d51efad37518355cc47639be7fb98 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
147/* HTTP/2 stream flags (32 bit), in h2s->flags */
148#define H2_SF_NONE 0x00000000
149#define H2_SF_ES_RCVD 0x00000001
150#define H2_SF_ES_SENT 0x00000002
151
152#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
153#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
154
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200155/* stream flags indicating the reason the stream is blocked */
156#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
157#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
158#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
159#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
160#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
161
Willy Tarreau454f9052017-10-26 19:40:35 +0200162/* stream flags indicating how data is supposed to be sent */
163#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
164#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
165
166/* step we're currently in when sending chunks. This is needed because we may
167 * have to transfer chunks as large as a full buffer so there's no room left
168 * for size nor crlf around.
169 */
170#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
171#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
172#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
173
174#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
175
Willy Tarreau67434202017-11-06 20:20:51 +0100176#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100177#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100178
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100179#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
180
Willy Tarreau18312642017-10-11 07:57:07 +0200181/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
182 * it is being processed in the internal HTTP representation (H1 for now).
183 */
184struct h2s {
185 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100186 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200187 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200188 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200190 int32_t id; /* stream ID */
191 uint32_t flags; /* H2_SF_* */
192 int mws; /* mux window size for this stream */
193 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
194 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200195 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100196 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200197 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200198 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100199 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
200 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100202 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200203};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200204
Willy Tarreauc6405142017-09-21 20:23:50 +0200205/* descriptor for an h2 frame header */
206struct h2_fh {
207 uint32_t len; /* length, host order, 24 bits */
208 uint32_t sid; /* stream id, host order, 31 bits */
209 uint8_t ft; /* frame type */
210 uint8_t ff; /* frame flags */
211};
212
Willy Tarreau8ceae722018-11-26 11:58:30 +0100213/* the h2c connection pool */
214DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
215
216/* the h2s stream pool */
217DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
218
Willy Tarreaudc572362018-12-12 08:08:05 +0100219/* The default connection window size is 65535, it may only be enlarged using
220 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
221 * we'll pretend we already received the difference between the two to send
222 * an equivalent window update to enlarge it to 2G-1.
223 */
224#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
225
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226/* a few settings from the global section */
227static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200228static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100229static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100230static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau2a856182017-05-16 15:20:39 +0200232/* a dmumy closed stream */
233static const struct h2s *h2_closed_stream = &(const struct h2s){
234 .cs = NULL,
235 .h2c = NULL,
236 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100237 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100238 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200239 .id = 0,
240};
241
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100242/* a dmumy closed stream returning a PROTOCOL_ERROR error */
243static const struct h2s *h2_error_stream = &(const struct h2s){
244 .cs = NULL,
245 .h2c = NULL,
246 .st = H2_SS_CLOSED,
247 .errcode = H2_ERR_PROTOCOL_ERROR,
248 .flags = 0,
249 .id = 0,
250};
251
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100252/* a dmumy closed stream returning a REFUSED_STREAM error */
253static const struct h2s *h2_refused_stream = &(const struct h2s){
254 .cs = NULL,
255 .h2c = NULL,
256 .st = H2_SS_CLOSED,
257 .errcode = H2_ERR_REFUSED_STREAM,
258 .flags = 0,
259 .id = 0,
260};
261
Willy Tarreau2a856182017-05-16 15:20:39 +0200262/* and a dummy idle stream for use with any unannounced stream */
263static const struct h2s *h2_idle_stream = &(const struct h2s){
264 .cs = NULL,
265 .h2c = NULL,
266 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100267 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200268 .id = 0,
269};
270
Olivier Houchard9f6af332018-05-25 14:04:04 +0200271static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200272static int h2_send(struct h2c *h2c);
273static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200274static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200275static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100276static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100277static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100278static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200279static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100280static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100281static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200282
Olivier Houchard7a977432019-03-21 15:47:13 +0100283static __inline int
284h2c_is_dead(struct h2c *h2c)
285{
286 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
287 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
288 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
289 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
290 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
291 (conn_xprt_read0_pending(h2c->conn) ||
292 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
293 return 1;
294
295 return 0;
296
297}
298
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200299/*****************************************************/
300/* functions below are for dynamic buffer management */
301/*****************************************************/
302
Willy Tarreau315d8072017-12-10 22:17:57 +0100303/* indicates whether or not the we may call the h2_recv() function to attempt
304 * to receive data into the buffer and/or demux pending data. The condition is
305 * a bit complex due to some API limits for now. The rules are the following :
306 * - if an error or a shutdown was detected on the connection and the buffer
307 * is empty, we must not attempt to receive
308 * - if the demux buf failed to be allocated, we must not try to receive and
309 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100310 * - if no flag indicates a blocking condition, we may attempt to receive,
311 * regardless of whether the demux buffer is full or not, so that only
312 * de demux part decides whether or not to block. This is needed because
313 * the connection API indeed prevents us from re-enabling receipt that is
314 * already enabled in a polled state, so we must always immediately stop
315 * as soon as the demux can't proceed so as never to hit an end of read
316 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100317 * - otherwise must may not attempt
318 */
319static inline int h2_recv_allowed(const struct h2c *h2c)
320{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100322 (h2c->st0 >= H2_CS_ERROR ||
323 h2c->conn->flags & CO_FL_ERROR ||
324 conn_xprt_read0_pending(h2c->conn)))
325 return 0;
326
327 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100328 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100329 return 1;
330
331 return 0;
332}
333
Willy Tarreau47b515a2018-12-21 16:09:41 +0100334/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200335static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100336{
337 if (!h2_recv_allowed(h2c))
338 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200339 if ((!consider_buffer || !b_data(&h2c->dbuf))
340 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100341 return;
342 tasklet_wakeup(h2c->wait_event.task);
343}
344
345
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100346/* returns true if the front connection has too many conn_streams attached */
347static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200348{
Willy Tarreaua8754662018-12-23 20:43:58 +0100349 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200350}
351
Willy Tarreau44e973f2018-03-01 17:49:30 +0100352/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
353 * flags are used to figure what buffer was requested. It returns 1 if the
354 * allocation succeeds, in which case the connection is woken up, or 0 if it's
355 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200356 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100357static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200358{
359 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100360 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200361
Willy Tarreau44e973f2018-03-01 17:49:30 +0100362 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200363 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200364 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200365 return 1;
366 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200367
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
369 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200370
371 if (h2c->flags & H2_CF_DEM_MROOM) {
372 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200373 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200374 }
Willy Tarreau14398122017-09-22 14:26:04 +0200375 return 1;
376 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100377
378 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
379 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200380 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100381 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200382 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100383 return 1;
384 }
385
Willy Tarreau14398122017-09-22 14:26:04 +0200386 return 0;
387}
388
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200389static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200390{
391 struct buffer *buf = NULL;
392
Willy Tarreau44e973f2018-03-01 17:49:30 +0100393 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
394 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
395 h2c->buf_wait.target = h2c;
396 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100397 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100398 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100399 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200400 __conn_xprt_stop_recv(h2c->conn);
401 }
402 return buf;
403}
404
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200405static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200406{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200407 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100408 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200409 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200410 }
411}
412
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100413/* returns the number of allocatable outgoing streams for the connection taking
414 * the last_sid and the reserved ones into account.
415 */
416static inline int h2_streams_left(const struct h2c *h2c)
417{
418 int ret;
419
420 /* consider the number of outgoing streams we're allowed to create before
421 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
422 * nb_reserved is the number of streams which don't yet have an ID.
423 */
424 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
425 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
426 if (ret < 0)
427 ret = 0;
428 return ret;
429}
430
Willy Tarreau00f18a32019-01-26 12:19:01 +0100431/* returns the number of streams in use on a connection to figure if it's
432 * idle or not. We check nb_cs and not nb_streams as the caller will want
433 * to know if it was the last one after a detach().
434 */
435static int h2_used_streams(struct connection *conn)
436{
437 struct h2c *h2c = conn->ctx;
438
439 return h2c->nb_cs;
440}
441
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100442/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100443static int h2_avail_streams(struct connection *conn)
444{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100445 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100446 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100447 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100448
Willy Tarreau6afec462019-01-28 06:40:19 +0100449 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
450 * streams on the connection.
451 */
452 if (h2c->last_sid >= 0)
453 return 0;
454
Willy Tarreau86949782019-01-31 10:42:05 +0100455 /* note: may be negative if a SETTINGS frame changes the limit */
456 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100457
458 /* we must also consider the limit imposed by stream IDs */
459 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100460 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100461 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100462 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
463 ret1 = MIN(ret1, ret2);
464 }
465 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100466}
467
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200468
Willy Tarreau62f52692017-10-08 23:01:42 +0200469/*****************************************************************/
470/* functions below are dedicated to the mux setup and management */
471/*****************************************************************/
472
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200473/* Initialize the mux once it's attached. For outgoing connections, the context
474 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200475 * connections from the fact that the context is still NULL (even during mux
476 * upgrades). <input> is always used as Input buffer and may contain data. It is
477 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200478 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200479static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
480 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481{
482 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100483 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200484
Willy Tarreaubafbe012017-11-24 17:34:44 +0100485 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200486 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200487 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200488
Christopher Faulete9b70722019-04-08 10:46:02 +0200489 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200490 h2c->flags = H2_CF_IS_BACK;
491 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
492 if (tick_isset(prx->timeout.serverfin))
493 h2c->shut_timeout = prx->timeout.serverfin;
494 } else {
495 h2c->flags = H2_CF_NONE;
496 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
497 if (tick_isset(prx->timeout.clientfin))
498 h2c->shut_timeout = prx->timeout.clientfin;
499 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100500
Willy Tarreau0b37d652018-10-03 10:33:02 +0200501 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100502 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100503 if (tick_isset(h2c->timeout)) {
504 t = task_new(tid_bit);
505 if (!t)
506 goto fail;
507
508 h2c->task = t;
509 t->process = h2_timeout_task;
510 t->context = h2c;
511 t->expire = tick_add(now_ms, h2c->timeout);
512 }
Willy Tarreauea392822017-10-31 10:02:25 +0100513
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200514 h2c->wait_event.task = tasklet_new();
515 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200516 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200517 h2c->wait_event.task->process = h2_io_cb;
518 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100519 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200520
Willy Tarreau32218eb2017-09-22 08:07:25 +0200521 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
522 if (!h2c->ddht)
523 goto fail;
524
525 /* Initialise the context. */
526 h2c->st0 = H2_CS_PREFACE;
527 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100528 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 h2c->max_id = -1;
530 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100531 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200532 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100533 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200534 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100535 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100536 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200537
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200538 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539 h2c->dsi = -1;
540 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100541
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 h2c->last_sid = -1;
543
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200544 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200545 h2c->miw = 65535; /* mux initial window size */
546 h2c->mws = 65535; /* mux window size */
547 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200548 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200549 LIST_INIT(&h2c->send_list);
550 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200551 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100552 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553
Willy Tarreau3f133572017-10-31 19:21:06 +0100554 if (t)
555 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100556
Willy Tarreau01b44822018-10-03 14:26:37 +0200557 if (h2c->flags & H2_CF_IS_BACK) {
558 /* FIXME: this is temporary, for outgoing connections we need
559 * to immediately allocate a stream until the code is modified
560 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100561 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200562 */
563 struct h2s *h2s;
564
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100565 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200566 if (!h2s)
567 goto fail_stream;
568 }
569
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100570 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200571
Willy Tarreau0f383582018-10-03 14:22:21 +0200572 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200573 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200574 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200575 fail_stream:
576 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200577 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100578 if (t)
579 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200580 if (h2c->wait_event.task)
581 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100582 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200583 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200584 return -1;
585}
586
Willy Tarreau751f2d02018-10-05 09:35:00 +0200587/* returns the next allocatable outgoing stream ID for the H2 connection, or
588 * -1 if no more is allocatable.
589 */
590static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
591{
592 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100593
594 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200595 id = -1;
596 return id;
597}
598
Willy Tarreau2373acc2017-10-12 17:35:14 +0200599/* returns the stream associated with id <id> or NULL if not found */
600static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
601{
602 struct eb32_node *node;
603
Willy Tarreau751f2d02018-10-05 09:35:00 +0200604 if (id == 0)
605 return (struct h2s *)h2_closed_stream;
606
Willy Tarreau2a856182017-05-16 15:20:39 +0200607 if (id > h2c->max_id)
608 return (struct h2s *)h2_idle_stream;
609
Willy Tarreau2373acc2017-10-12 17:35:14 +0200610 node = eb32_lookup(&h2c->streams_by_id, id);
611 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200612 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200613
614 return container_of(node, struct h2s, by_id);
615}
616
Christopher Faulet73c12072019-04-08 11:23:22 +0200617/* release function. This one should be called to free all resources allocated
618 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200619 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200620static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200621{
Christopher Faulet61840e72019-04-15 09:33:32 +0200622 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200623
Willy Tarreau32218eb2017-09-22 08:07:25 +0200624 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200625 /* The connection must be aattached to this mux to be released */
626 if (h2c->conn && h2c->conn->ctx == h2c)
627 conn = h2c->conn;
628
Willy Tarreau32218eb2017-09-22 08:07:25 +0200629 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200630
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100631 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100632 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100633 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200634
Willy Tarreau44e973f2018-03-01 17:49:30 +0100635 h2_release_buf(h2c, &h2c->dbuf);
636 h2_release_buf(h2c, &h2c->mbuf);
637
Willy Tarreauea392822017-10-31 10:02:25 +0100638 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200639 h2c->task->context = NULL;
640 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100641 h2c->task = NULL;
642 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200643 if (h2c->wait_event.task)
644 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200645 if (h2c->wait_event.events != 0)
646 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
647 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100648
Willy Tarreaubafbe012017-11-24 17:34:44 +0100649 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200650 }
651
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200652 if (conn) {
653 conn->mux = NULL;
654 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200655
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200656 conn_stop_tracking(conn);
657 conn_full_close(conn);
658 if (conn->destroy_cb)
659 conn->destroy_cb(conn);
660 conn_free(conn);
661 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200662}
663
664
Willy Tarreau71681172017-10-23 14:39:06 +0200665/******************************************************/
666/* functions below are for the H2 protocol processing */
667/******************************************************/
668
669/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100670static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200671{
672 return h2s ? h2s->id : 0;
673}
674
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200675/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100676static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200677{
678 if (h2c->msi < 0)
679 return 0;
680
681 if (h2c->msi == h2s_id(h2s))
682 return 0;
683
684 return 1;
685}
686
Willy Tarreau741d6df2017-10-17 08:00:59 +0200687/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100688static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200689{
690 h2c->errcode = err;
691 h2c->st0 = H2_CS_ERROR;
692}
693
Willy Tarreau175cebb2019-01-24 10:02:24 +0100694/* marks an error on the stream. It may also update an already closed stream
695 * (e.g. to report an error after an RST was received).
696 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100697static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200698{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100699 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200700 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100701 if (h2s->st < H2_SS_ERROR)
702 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100703 if (h2s->cs)
704 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200705 }
706}
707
Willy Tarreau7e094452018-12-19 18:08:52 +0100708/* attempt to notify the data layer of recv availability */
709static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
710{
711 struct wait_event *sw;
712
713 if (h2s->recv_wait) {
714 sw = h2s->recv_wait;
715 sw->events &= ~SUB_RETRY_RECV;
716 tasklet_wakeup(sw->task);
717 h2s->recv_wait = NULL;
718 }
719}
720
721/* attempt to notify the data layer of send availability */
722static void __maybe_unused h2s_notify_send(struct h2s *h2s)
723{
724 struct wait_event *sw;
725
Olivier Houchard998410a2019-04-15 19:23:37 +0200726 if (h2s->send_wait && LIST_ISEMPTY(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100727 sw = h2s->send_wait;
728 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100729 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100730 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100731 }
732}
733
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100734/* alerts the data layer, trying to wake it up by all means, following
735 * this sequence :
736 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
737 * - if its subscribed to send, then it's woken up for send
738 * - if it was subscribed to neither, its ->wake() callback is called
739 * It is safe to call this function with a closed stream which doesn't have a
740 * conn_stream anymore.
741 */
742static void __maybe_unused h2s_alert(struct h2s *h2s)
743{
744 if (h2s->recv_wait || h2s->send_wait) {
745 h2s_notify_recv(h2s);
746 h2s_notify_send(h2s);
747 }
748 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
749 h2s->cs->data_cb->wake(h2s->cs);
750}
751
Willy Tarreaue4820742017-07-27 13:37:23 +0200752/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100753static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200754{
755 uint8_t *out = frame;
756
757 *out = len >> 16;
758 write_n16(out + 1, len);
759}
760
Willy Tarreau54c15062017-10-10 17:10:03 +0200761/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
762 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
763 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200764 * available in the buffer's input prior to calling this function. The buffer
765 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200766 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100767static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200768 const struct buffer *b, int o)
769{
Willy Tarreau591d4452018-06-15 17:21:00 +0200770 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200771}
772
Willy Tarreau1f094672017-11-20 21:27:45 +0100773static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200774{
Willy Tarreau591d4452018-06-15 17:21:00 +0200775 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200776}
777
Willy Tarreau1f094672017-11-20 21:27:45 +0100778static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200779{
Willy Tarreau591d4452018-06-15 17:21:00 +0200780 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200781}
782
Willy Tarreau1f094672017-11-20 21:27:45 +0100783static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200784{
Willy Tarreau591d4452018-06-15 17:21:00 +0200785 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200786}
787
788
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100789/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
790 * The algorithm is not obvious. It turns out that H2 headers are neither
791 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
792 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200793 *
794 * b0 b1 b2 b3 b4 b5..b8
795 * +----------+---------+--------+----+----+----------------------+
796 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
797 * +----------+---------+--------+----+----+----------------------+
798 *
799 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
800 * we get the sid properly aligned and ordered, and 16 bits of len properly
801 * ordered as well. The type and flags can be extracted using bit shifts from
802 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200803 * Returns zero if some bytes are missing, otherwise non-zero on success. The
804 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200805 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100806static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200807{
808 uint64_t w;
809
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100810 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200811 return 0;
812
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100813 w = h2_get_n64(b, o + 1);
814 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200815 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
816 h->ff = w >> 32;
817 h->ft = w >> 40;
818 h->len += w >> 48;
819 return 1;
820}
821
822/* skip the next 9 bytes corresponding to the frame header possibly parsed by
823 * h2_peek_frame_hdr() above.
824 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100825static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200826{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200827 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200828}
829
830/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100831static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200832{
833 int ret;
834
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100835 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200836 if (ret > 0)
837 h2_skip_frame_hdr(b);
838 return ret;
839}
840
Willy Tarreau00dd0782018-03-01 16:31:34 +0100841/* marks stream <h2s> as CLOSED and decrement the number of active streams for
842 * its connection if the stream was not yet closed. Please use this exclusively
843 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100844 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100845static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100846{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100847 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100848 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100849 if (!h2s->id)
850 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100851 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100852 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100853 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
854 h2s_notify_recv(h2s);
855 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100856 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100857 h2s->st = H2_SS_CLOSED;
858}
859
Willy Tarreau71049cc2018-03-28 13:56:39 +0200860/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
861static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100862{
863 h2s_close(h2s);
864 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200865 if (b_size(&h2s->rxbuf)) {
866 b_free(&h2s->rxbuf);
867 offer_buffers(NULL, tasks_run_queue);
868 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200869 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100870 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200871 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100872 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800873 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200874 * reference left would be in the h2c send_list/fctl_list, and if
875 * we're in it, we're getting out anyway
876 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100877 LIST_DEL_INIT(&h2s->list);
Olivier Houchard998410a2019-04-15 19:23:37 +0200878 if (!LIST_ISEMPTY(&h2s->sending_list)) {
879 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
880 LIST_DEL_INIT(&h2s->sending_list);
881 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200882 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100883 pool_free(pool_head_h2s, h2s);
884}
885
Willy Tarreaua8e49542018-10-03 18:53:55 +0200886/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
887 * stream tree. In case of error, nothing is added and NULL is returned. The
888 * causes of errors can be any failed memory allocation. The caller is
889 * responsible for checking if the connection may support an extra stream
890 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200891 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200892static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200893{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200894 struct h2s *h2s;
895
Willy Tarreaubafbe012017-11-24 17:34:44 +0100896 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200897 if (!h2s)
898 goto out;
899
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200900 h2s->wait_event.task = tasklet_new();
901 if (!h2s->wait_event.task) {
902 pool_free(pool_head_h2s, h2s);
903 goto out;
904 }
905 h2s->send_wait = NULL;
906 h2s->recv_wait = NULL;
907 h2s->wait_event.task->process = h2_deferred_shut;
908 h2s->wait_event.task->context = h2s;
909 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100910 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200911 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100912 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200913 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200914 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200915 h2s->mws = h2c->miw;
916 h2s->flags = H2_SF_NONE;
917 h2s->errcode = H2_ERR_NO_ERROR;
918 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200919 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100920 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200921 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200922
923 if (h2c->flags & H2_CF_IS_BACK) {
924 h1m_init_req(&h2s->h1m);
925 h2s->h1m.err_pos = -1; // don't care about errors on the request path
926 h2s->h1m.flags |= H1_MF_TOLOWER;
927 } else {
928 h1m_init_res(&h2s->h1m);
929 h2s->h1m.err_pos = -1; // don't care about errors on the response path
930 h2s->h1m.flags |= H1_MF_TOLOWER;
931 }
932
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200933 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200934 if (id > 0)
935 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100936 else
937 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200938
939 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100940 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100941 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200942
943 return h2s;
944
945 out_free_h2s:
946 pool_free(pool_head_h2s, h2s);
947 out:
948 return NULL;
949}
950
951/* creates a new stream <id> on the h2c connection and returns it, or NULL in
952 * case of memory allocation error.
953 */
954static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
955{
956 struct session *sess = h2c->conn->owner;
957 struct conn_stream *cs;
958 struct h2s *h2s;
959
960 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
961 goto out;
962
963 h2s = h2s_new(h2c, id);
964 if (!h2s)
965 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200966
967 cs = cs_new(h2c->conn);
968 if (!cs)
969 goto out_close;
970
Olivier Houchard746fb772018-12-15 19:42:00 +0100971 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200972 h2s->cs = cs;
973 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200974 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200975
976 if (stream_create_from_cs(cs) < 0)
977 goto out_free_cs;
978
Willy Tarreau590a0512018-09-05 11:56:48 +0200979 /* We want the accept date presented to the next stream to be the one
980 * we have now, the handshake time to be null (since the next stream
981 * is not delayed by a handshake), and the idle time to count since
982 * right now.
983 */
984 sess->accept_date = date;
985 sess->tv_accept = now;
986 sess->t_handshake = 0;
987
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200988 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100989 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200990 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200991 return h2s;
992
993 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200994 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200995 cs_free(cs);
996 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200997 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200998 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200999 sess_log(sess);
1000 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001001}
1002
Willy Tarreau751f2d02018-10-05 09:35:00 +02001003/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1004 * and returns it, or NULL in case of memory allocation error or if the highest
1005 * possible stream ID was reached.
1006 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001007static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001008{
1009 struct h2s *h2s = NULL;
1010
Willy Tarreau86949782019-01-31 10:42:05 +01001011 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001012 goto out;
1013
Willy Tarreaua80dca82019-01-24 17:08:28 +01001014 if (h2_streams_left(h2c) < 1)
1015 goto out;
1016
Willy Tarreau751f2d02018-10-05 09:35:00 +02001017 /* Defer choosing the ID until we send the first message to create the stream */
1018 h2s = h2s_new(h2c, 0);
1019 if (!h2s)
1020 goto out;
1021
1022 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001023 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001024 cs->ctx = h2s;
1025 h2c->nb_cs++;
1026
Willy Tarreau751f2d02018-10-05 09:35:00 +02001027 out:
1028 return h2s;
1029}
1030
Willy Tarreaube5b7152017-09-25 16:25:39 +02001031/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1032 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1033 * the various settings codes.
1034 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001035static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001036{
1037 struct buffer *res;
1038 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001039 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001040 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001041 int ret;
1042
1043 if (h2c_mux_busy(h2c, NULL)) {
1044 h2c->flags |= H2_CF_DEM_MBUSY;
1045 return 0;
1046 }
1047
Willy Tarreau44e973f2018-03-01 17:49:30 +01001048 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001049 if (!res) {
1050 h2c->flags |= H2_CF_MUX_MALLOC;
1051 h2c->flags |= H2_CF_DEM_MROOM;
1052 return 0;
1053 }
1054
1055 chunk_init(&buf, buf_data, sizeof(buf_data));
1056 chunk_memcpy(&buf,
1057 "\x00\x00\x00" /* length : 0 for now */
1058 "\x04\x00" /* type : 4 (settings), flags : 0 */
1059 "\x00\x00\x00\x00", /* stream ID : 0 */
1060 9);
1061
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001062 if (h2c->flags & H2_CF_IS_BACK) {
1063 /* send settings_enable_push=0 */
1064 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1065 }
1066
Willy Tarreaube5b7152017-09-25 16:25:39 +02001067 if (h2_settings_header_table_size != 4096) {
1068 char str[6] = "\x00\x01"; /* header_table_size */
1069
1070 write_n32(str + 2, h2_settings_header_table_size);
1071 chunk_memcat(&buf, str, 6);
1072 }
1073
1074 if (h2_settings_initial_window_size != 65535) {
1075 char str[6] = "\x00\x04"; /* initial_window_size */
1076
1077 write_n32(str + 2, h2_settings_initial_window_size);
1078 chunk_memcat(&buf, str, 6);
1079 }
1080
1081 if (h2_settings_max_concurrent_streams != 0) {
1082 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1083
1084 /* Note: 0 means "unlimited" for haproxy's config but not for
1085 * the protocol, so never send this value!
1086 */
1087 write_n32(str + 2, h2_settings_max_concurrent_streams);
1088 chunk_memcat(&buf, str, 6);
1089 }
1090
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001091 mfs = h2_settings_max_frame_size;
1092 if (mfs > global.tune.bufsize)
1093 mfs = global.tune.bufsize;
1094
1095 if (!mfs)
1096 mfs = global.tune.bufsize;
1097
1098 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001099 char str[6] = "\x00\x05"; /* max_frame_size */
1100
1101 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1102 * match bufsize - rewrite size, but at the moment it seems
1103 * that clients don't take care of it.
1104 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001105 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001106 chunk_memcat(&buf, str, 6);
1107 }
1108
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001109 h2_set_frame_size(buf.area, buf.data - 9);
1110 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001111 if (unlikely(ret <= 0)) {
1112 if (!ret) {
1113 h2c->flags |= H2_CF_MUX_MFULL;
1114 h2c->flags |= H2_CF_DEM_MROOM;
1115 return 0;
1116 }
1117 else {
1118 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1119 return 0;
1120 }
1121 }
1122 return ret;
1123}
1124
Willy Tarreau52eed752017-09-22 15:05:09 +02001125/* Try to receive a connection preface, then upon success try to send our
1126 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1127 * missing data. It may return an error in h2c.
1128 */
1129static int h2c_frt_recv_preface(struct h2c *h2c)
1130{
1131 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001132 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001133
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001134 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001135
1136 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001137 if (ret1 < 0)
1138 sess_log(h2c->conn->owner);
1139
Willy Tarreau52eed752017-09-22 15:05:09 +02001140 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1141 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1142 return 0;
1143 }
1144
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001145 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001146 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001147 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001148
Willy Tarreaube5b7152017-09-25 16:25:39 +02001149 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001150}
1151
Willy Tarreau01b44822018-10-03 14:26:37 +02001152/* Try to send a connection preface, then upon success try to send our
1153 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1154 * missing data. It may return an error in h2c.
1155 */
1156static int h2c_bck_send_preface(struct h2c *h2c)
1157{
1158 struct buffer *res;
1159
1160 if (h2c_mux_busy(h2c, NULL)) {
1161 h2c->flags |= H2_CF_DEM_MBUSY;
1162 return 0;
1163 }
1164
1165 res = h2_get_buf(h2c, &h2c->mbuf);
1166 if (!res) {
1167 h2c->flags |= H2_CF_MUX_MALLOC;
1168 h2c->flags |= H2_CF_DEM_MROOM;
1169 return 0;
1170 }
1171
1172 if (!b_data(res)) {
1173 /* preface not yet sent */
1174 b_istput(res, ist(H2_CONN_PREFACE));
1175 }
1176
1177 return h2c_send_settings(h2c);
1178}
1179
Willy Tarreau081d4722017-05-16 21:51:05 +02001180/* try to send a GOAWAY frame on the connection to report an error or a graceful
1181 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1182 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1183 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1184 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1185 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1186 * on unrecoverable failure. It will not attempt to send one again in this last
1187 * case so that it is safe to use h2c_error() to report such errors.
1188 */
1189static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1190{
1191 struct buffer *res;
1192 char str[17];
1193 int ret;
1194
1195 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1196 return 1; // claim that it worked
1197
1198 if (h2c_mux_busy(h2c, h2s)) {
1199 if (h2s)
1200 h2s->flags |= H2_SF_BLK_MBUSY;
1201 else
1202 h2c->flags |= H2_CF_DEM_MBUSY;
1203 return 0;
1204 }
1205
Willy Tarreau44e973f2018-03-01 17:49:30 +01001206 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001207 if (!res) {
1208 h2c->flags |= H2_CF_MUX_MALLOC;
1209 if (h2s)
1210 h2s->flags |= H2_SF_BLK_MROOM;
1211 else
1212 h2c->flags |= H2_CF_DEM_MROOM;
1213 return 0;
1214 }
1215
1216 /* len: 8, type: 7, flags: none, sid: 0 */
1217 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1218
1219 if (h2c->last_sid < 0)
1220 h2c->last_sid = h2c->max_id;
1221
1222 write_n32(str + 9, h2c->last_sid);
1223 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001224 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001225 if (unlikely(ret <= 0)) {
1226 if (!ret) {
1227 h2c->flags |= H2_CF_MUX_MFULL;
1228 if (h2s)
1229 h2s->flags |= H2_SF_BLK_MROOM;
1230 else
1231 h2c->flags |= H2_CF_DEM_MROOM;
1232 return 0;
1233 }
1234 else {
1235 /* we cannot report this error using GOAWAY, so we mark
1236 * it and claim a success.
1237 */
1238 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1239 h2c->flags |= H2_CF_GOAWAY_FAILED;
1240 return 1;
1241 }
1242 }
1243 h2c->flags |= H2_CF_GOAWAY_SENT;
1244 return ret;
1245}
1246
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001247/* Try to send an RST_STREAM frame on the connection for the indicated stream
1248 * during mux operations. This stream must be valid and cannot be closed
1249 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1250 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1251 * not yet.
1252 *
1253 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1254 * to write the message, it subscribes the stream to future notifications.
1255 */
1256static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1257{
1258 struct buffer *res;
1259 char str[13];
1260 int ret;
1261
1262 if (!h2s || h2s->st == H2_SS_CLOSED)
1263 return 1;
1264
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001265 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1266 * RST_STREAM in response to a RST_STREAM frame.
1267 */
1268 if (h2c->dft == H2_FT_RST_STREAM) {
1269 ret = 1;
1270 goto ignore;
1271 }
1272
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001273 if (h2c_mux_busy(h2c, h2s)) {
1274 h2s->flags |= H2_SF_BLK_MBUSY;
1275 return 0;
1276 }
1277
Willy Tarreau44e973f2018-03-01 17:49:30 +01001278 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001279 if (!res) {
1280 h2c->flags |= H2_CF_MUX_MALLOC;
1281 h2s->flags |= H2_SF_BLK_MROOM;
1282 return 0;
1283 }
1284
1285 /* len: 4, type: 3, flags: none */
1286 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1287 write_n32(str + 5, h2s->id);
1288 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001289 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001290
1291 if (unlikely(ret <= 0)) {
1292 if (!ret) {
1293 h2c->flags |= H2_CF_MUX_MFULL;
1294 h2s->flags |= H2_SF_BLK_MROOM;
1295 return 0;
1296 }
1297 else {
1298 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1299 return 0;
1300 }
1301 }
1302
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001303 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001304 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001305 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001306 return ret;
1307}
1308
1309/* Try to send an RST_STREAM frame on the connection for the stream being
1310 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001311 * error code, even if the stream is one of the dummy ones, and will update
1312 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001313 *
1314 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1315 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001316 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001317 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001318 */
1319static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1320{
1321 struct buffer *res;
1322 char str[13];
1323 int ret;
1324
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001325 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1326 * RST_STREAM in response to a RST_STREAM frame.
1327 */
1328 if (h2c->dft == H2_FT_RST_STREAM) {
1329 ret = 1;
1330 goto ignore;
1331 }
1332
Willy Tarreau27a84c92017-10-17 08:10:17 +02001333 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001334 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001335 return 0;
1336 }
1337
Willy Tarreau44e973f2018-03-01 17:49:30 +01001338 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001339 if (!res) {
1340 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001341 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001342 return 0;
1343 }
1344
1345 /* len: 4, type: 3, flags: none */
1346 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001347
Willy Tarreau27a84c92017-10-17 08:10:17 +02001348 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001349 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001350 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001351
Willy Tarreau27a84c92017-10-17 08:10:17 +02001352 if (unlikely(ret <= 0)) {
1353 if (!ret) {
1354 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001355 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001356 return 0;
1357 }
1358 else {
1359 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1360 return 0;
1361 }
1362 }
1363
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001364 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001365 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001366 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001367 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001368 }
1369
Willy Tarreau27a84c92017-10-17 08:10:17 +02001370 return ret;
1371}
1372
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001373/* try to send an empty DATA frame with the ES flag set to notify about the
1374 * end of stream and match a shutdown(write). If an ES was already sent as
1375 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1376 * on success or zero if nothing was done. In case of lack of room to write the
1377 * message, it subscribes the requesting stream to future notifications.
1378 */
1379static int h2_send_empty_data_es(struct h2s *h2s)
1380{
1381 struct h2c *h2c = h2s->h2c;
1382 struct buffer *res;
1383 char str[9];
1384 int ret;
1385
Willy Tarreau721c9742017-11-07 11:05:42 +01001386 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001387 return 1;
1388
1389 if (h2c_mux_busy(h2c, h2s)) {
1390 h2s->flags |= H2_SF_BLK_MBUSY;
1391 return 0;
1392 }
1393
Willy Tarreau44e973f2018-03-01 17:49:30 +01001394 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001395 if (!res) {
1396 h2c->flags |= H2_CF_MUX_MALLOC;
1397 h2s->flags |= H2_SF_BLK_MROOM;
1398 return 0;
1399 }
1400
1401 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1402 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1403 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001404 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001405 if (likely(ret > 0)) {
1406 h2s->flags |= H2_SF_ES_SENT;
1407 }
1408 else if (!ret) {
1409 h2c->flags |= H2_CF_MUX_MFULL;
1410 h2s->flags |= H2_SF_BLK_MROOM;
1411 return 0;
1412 }
1413 else {
1414 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1415 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001416 }
1417 return ret;
1418}
1419
Christopher Fauletf02ca002019-03-07 16:21:34 +01001420/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1421 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1422 * connection. The stream's state is automatically updated accordingly. If the
1423 * stream is orphaned, it is destroyed.
1424 */
1425static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1426{
1427 if (!h2s->cs) {
1428 /* this stream was already orphaned */
1429 h2s_destroy(h2s);
1430 return;
1431 }
1432
1433 h2s->cs->flags |= flags;
1434 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1435 h2s->cs->flags |= CS_FL_ERROR;
1436
1437 h2s_alert(h2s);
1438
1439 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1440 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001441 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001442 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001443 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001444 h2s_close(h2s);
1445}
1446
1447/* wake the streams attached to the connection, whose id is greater than <last>
1448 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001449 */
1450static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1451{
1452 struct eb32_node *node;
1453 struct h2s *h2s;
1454
1455 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001456 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001457
1458 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001459 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001460
Christopher Fauletf02ca002019-03-07 16:21:34 +01001461 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001462 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1463 while (node) {
1464 h2s = container_of(node, struct h2s, by_id);
1465 if (h2s->id <= last)
1466 break;
1467 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001468 h2s_wake_one_stream(h2s, flags);
1469 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001470
Christopher Fauletf02ca002019-03-07 16:21:34 +01001471 /* Wake all streams with unassigned ID (ID == 0) */
1472 node = eb32_lookup(&h2c->streams_by_id, 0);
1473 while (node) {
1474 h2s = container_of(node, struct h2s, by_id);
1475 if (h2s->id > 0)
1476 break;
1477 node = eb32_next(node);
1478 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001479 }
1480}
1481
Willy Tarreau3421aba2017-07-27 15:41:03 +02001482/* Increase all streams' outgoing window size by the difference passed in
1483 * argument. This is needed upon receipt of the settings frame if the initial
1484 * window size is different. The difference may be negative and the resulting
1485 * window size as well, for the time it takes to receive some window updates.
1486 */
1487static void h2c_update_all_ws(struct h2c *h2c, int diff)
1488{
1489 struct h2s *h2s;
1490 struct eb32_node *node;
1491
1492 if (!diff)
1493 return;
1494
1495 node = eb32_first(&h2c->streams_by_id);
1496 while (node) {
1497 h2s = container_of(node, struct h2s, by_id);
1498 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001499
1500 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1501 h2s->flags &= ~H2_SF_BLK_SFCTL;
1502 if (h2s->send_wait)
1503 LIST_ADDQ(&h2c->send_list, &h2s->list);
1504
1505 }
1506
Willy Tarreau3421aba2017-07-27 15:41:03 +02001507 node = eb32_next(node);
1508 }
1509}
1510
1511/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1512 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001513 * return an error in h2c. The caller must have already verified frame length
1514 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001515 */
1516static int h2c_handle_settings(struct h2c *h2c)
1517{
1518 unsigned int offset;
1519 int error;
1520
1521 if (h2c->dff & H2_F_SETTINGS_ACK) {
1522 if (h2c->dfl) {
1523 error = H2_ERR_FRAME_SIZE_ERROR;
1524 goto fail;
1525 }
1526 return 1;
1527 }
1528
Willy Tarreau3421aba2017-07-27 15:41:03 +02001529 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001530 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001531 return 0;
1532
1533 /* parse the frame */
1534 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001535 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1536 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001537
1538 switch (type) {
1539 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1540 /* we need to update all existing streams with the
1541 * difference from the previous iws.
1542 */
1543 if (arg < 0) { // RFC7540#6.5.2
1544 error = H2_ERR_FLOW_CONTROL_ERROR;
1545 goto fail;
1546 }
1547 h2c_update_all_ws(h2c, arg - h2c->miw);
1548 h2c->miw = arg;
1549 break;
1550 case H2_SETTINGS_MAX_FRAME_SIZE:
1551 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1552 error = H2_ERR_PROTOCOL_ERROR;
1553 goto fail;
1554 }
1555 h2c->mfs = arg;
1556 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001557 case H2_SETTINGS_ENABLE_PUSH:
1558 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1559 error = H2_ERR_PROTOCOL_ERROR;
1560 goto fail;
1561 }
1562 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001563 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1564 if (h2c->flags & H2_CF_IS_BACK) {
1565 /* the limit is only for the backend; for the frontend it is our limit */
1566 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1567 arg = h2_settings_max_concurrent_streams;
1568 h2c->streams_limit = arg;
1569 }
1570 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001571 }
1572 }
1573
1574 /* need to ACK this frame now */
1575 h2c->st0 = H2_CS_FRAME_A;
1576 return 1;
1577 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001578 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001579 h2c_error(h2c, error);
1580 return 0;
1581}
1582
1583/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1584 * success or one of the h2_status values.
1585 */
1586static int h2c_ack_settings(struct h2c *h2c)
1587{
1588 struct buffer *res;
1589 char str[9];
1590 int ret = -1;
1591
1592 if (h2c_mux_busy(h2c, NULL)) {
1593 h2c->flags |= H2_CF_DEM_MBUSY;
1594 return 0;
1595 }
1596
Willy Tarreau44e973f2018-03-01 17:49:30 +01001597 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001598 if (!res) {
1599 h2c->flags |= H2_CF_MUX_MALLOC;
1600 h2c->flags |= H2_CF_DEM_MROOM;
1601 return 0;
1602 }
1603
1604 memcpy(str,
1605 "\x00\x00\x00" /* length : 0 (no data) */
1606 "\x04" "\x01" /* type : 4, flags : ACK */
1607 "\x00\x00\x00\x00" /* stream ID */, 9);
1608
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001609 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001610 if (unlikely(ret <= 0)) {
1611 if (!ret) {
1612 h2c->flags |= H2_CF_MUX_MFULL;
1613 h2c->flags |= H2_CF_DEM_MROOM;
1614 return 0;
1615 }
1616 else {
1617 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1618 return 0;
1619 }
1620 }
1621 return ret;
1622}
1623
Willy Tarreaucf68c782017-10-10 17:11:41 +02001624/* processes a PING frame and schedules an ACK if needed. The caller must pass
1625 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001626 * missing data. The caller must have already verified frame length
1627 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001628 */
1629static int h2c_handle_ping(struct h2c *h2c)
1630{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001631 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001632 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001633 h2c->st0 = H2_CS_FRAME_A;
1634 return 1;
1635}
1636
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001637/* Try to send a window update for stream id <sid> and value <increment>.
1638 * Returns > 0 on success or zero on missing room or failure. It may return an
1639 * error in h2c.
1640 */
1641static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1642{
1643 struct buffer *res;
1644 char str[13];
1645 int ret = -1;
1646
1647 if (h2c_mux_busy(h2c, NULL)) {
1648 h2c->flags |= H2_CF_DEM_MBUSY;
1649 return 0;
1650 }
1651
Willy Tarreau44e973f2018-03-01 17:49:30 +01001652 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001653 if (!res) {
1654 h2c->flags |= H2_CF_MUX_MALLOC;
1655 h2c->flags |= H2_CF_DEM_MROOM;
1656 return 0;
1657 }
1658
1659 /* length: 4, type: 8, flags: none */
1660 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1661 write_n32(str + 5, sid);
1662 write_n32(str + 9, increment);
1663
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001664 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001665
1666 if (unlikely(ret <= 0)) {
1667 if (!ret) {
1668 h2c->flags |= H2_CF_MUX_MFULL;
1669 h2c->flags |= H2_CF_DEM_MROOM;
1670 return 0;
1671 }
1672 else {
1673 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1674 return 0;
1675 }
1676 }
1677 return ret;
1678}
1679
1680/* try to send pending window update for the connection. It's safe to call it
1681 * with no pending updates. Returns > 0 on success or zero on missing room or
1682 * failure. It may return an error in h2c.
1683 */
1684static int h2c_send_conn_wu(struct h2c *h2c)
1685{
1686 int ret = 1;
1687
1688 if (h2c->rcvd_c <= 0)
1689 return 1;
1690
Willy Tarreau97aaa672018-12-23 09:49:04 +01001691 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1692 /* increase the advertised connection window to 2G on
1693 * first update.
1694 */
1695 h2c->flags |= H2_CF_WINDOW_OPENED;
1696 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1697 }
1698
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001699 /* send WU for the connection */
1700 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1701 if (ret > 0)
1702 h2c->rcvd_c = 0;
1703
1704 return ret;
1705}
1706
1707/* try to send pending window update for the current dmux stream. It's safe to
1708 * call it with no pending updates. Returns > 0 on success or zero on missing
1709 * room or failure. It may return an error in h2c.
1710 */
1711static int h2c_send_strm_wu(struct h2c *h2c)
1712{
1713 int ret = 1;
1714
1715 if (h2c->rcvd_s <= 0)
1716 return 1;
1717
1718 /* send WU for the stream */
1719 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1720 if (ret > 0)
1721 h2c->rcvd_s = 0;
1722
1723 return ret;
1724}
1725
Willy Tarreaucf68c782017-10-10 17:11:41 +02001726/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1727 * success, 0 on missing data or one of the h2_status values.
1728 */
1729static int h2c_ack_ping(struct h2c *h2c)
1730{
1731 struct buffer *res;
1732 char str[17];
1733 int ret = -1;
1734
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001735 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001736 return 0;
1737
1738 if (h2c_mux_busy(h2c, NULL)) {
1739 h2c->flags |= H2_CF_DEM_MBUSY;
1740 return 0;
1741 }
1742
Willy Tarreau44e973f2018-03-01 17:49:30 +01001743 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001744 if (!res) {
1745 h2c->flags |= H2_CF_MUX_MALLOC;
1746 h2c->flags |= H2_CF_DEM_MROOM;
1747 return 0;
1748 }
1749
1750 memcpy(str,
1751 "\x00\x00\x08" /* length : 8 (same payload) */
1752 "\x06" "\x01" /* type : 6, flags : ACK */
1753 "\x00\x00\x00\x00" /* stream ID */, 9);
1754
1755 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001756 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001757
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001758 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001759 if (unlikely(ret <= 0)) {
1760 if (!ret) {
1761 h2c->flags |= H2_CF_MUX_MFULL;
1762 h2c->flags |= H2_CF_DEM_MROOM;
1763 return 0;
1764 }
1765 else {
1766 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1767 return 0;
1768 }
1769 }
1770 return ret;
1771}
1772
Willy Tarreau26f95952017-07-27 17:18:30 +02001773/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1774 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001775 * h2c or h2s. The caller must have already verified frame length and stream ID
1776 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001777 */
1778static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1779{
1780 int32_t inc;
1781 int error;
1782
Willy Tarreau26f95952017-07-27 17:18:30 +02001783 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001784 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001785 return 0;
1786
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001787 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001788
1789 if (h2c->dsi != 0) {
1790 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001791
1792 /* it's not an error to receive WU on a closed stream */
1793 if (h2s->st == H2_SS_CLOSED)
1794 return 1;
1795
1796 if (!inc) {
1797 error = H2_ERR_PROTOCOL_ERROR;
1798 goto strm_err;
1799 }
1800
1801 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1802 error = H2_ERR_FLOW_CONTROL_ERROR;
1803 goto strm_err;
1804 }
1805
1806 h2s->mws += inc;
1807 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1808 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001809 if (h2s->send_wait)
1810 LIST_ADDQ(&h2c->send_list, &h2s->list);
1811
Willy Tarreau26f95952017-07-27 17:18:30 +02001812 }
1813 }
1814 else {
1815 /* connection window update */
1816 if (!inc) {
1817 error = H2_ERR_PROTOCOL_ERROR;
1818 goto conn_err;
1819 }
1820
1821 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1822 error = H2_ERR_FLOW_CONTROL_ERROR;
1823 goto conn_err;
1824 }
1825
1826 h2c->mws += inc;
1827 }
1828
1829 return 1;
1830
1831 conn_err:
1832 h2c_error(h2c, error);
1833 return 0;
1834
1835 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001836 h2s_error(h2s, error);
1837 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001838 return 0;
1839}
1840
Willy Tarreaue96b0922017-10-30 00:28:29 +01001841/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001842 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1843 * have already verified frame length and stream ID validity. Described in
1844 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001845 */
1846static int h2c_handle_goaway(struct h2c *h2c)
1847{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001848 int last;
1849
Willy Tarreaue96b0922017-10-30 00:28:29 +01001850 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001851 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001852 return 0;
1853
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001854 last = h2_get_n32(&h2c->dbuf, 0);
1855 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001856 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001857 if (h2c->last_sid < 0)
1858 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001859 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001860}
1861
Willy Tarreau92153fc2017-12-03 19:46:19 +01001862/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001863 * invalid. Returns > 0 on success or zero on missing data. It may return an
1864 * error in h2c. The caller must have already verified frame length and stream
1865 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001866 */
1867static int h2c_handle_priority(struct h2c *h2c)
1868{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001869 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001870 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001871 return 0;
1872
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001873 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001874 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001875 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1876 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001877 }
1878 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001879}
1880
Willy Tarreaucd234e92017-08-18 10:59:39 +02001881/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001882 * Returns > 0 on success or zero on missing data. The caller must have already
1883 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001884 */
1885static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1886{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001887 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001888 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001889 return 0;
1890
1891 /* late RST, already handled */
1892 if (h2s->st == H2_SS_CLOSED)
1893 return 1;
1894
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001895 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001896 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001897
1898 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001899 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001900 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001901 }
1902
1903 h2s->flags |= H2_SF_RST_RCVD;
1904 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001905}
1906
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001907/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1908 * It may return an error in h2c or h2s. The caller must consider that the
1909 * return value is the new h2s in case one was allocated (most common case).
1910 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001911 * errors here are reported as connection errors since it's impossible to
1912 * recover from such errors after the compression context has been altered.
1913 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001914static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001915{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001916 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001917 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001918 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001919 int error;
1920
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001921 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001922 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001923
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001924 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001925 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001926
1927 /* now either the frame is complete or the buffer is complete */
1928 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001929 /* The stream exists/existed, this must be a trailers frame */
1930 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001931 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001932 goto out;
1933 goto done;
1934 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001935 /* the connection was already killed by an RST, let's consume
1936 * the data and send another RST.
1937 */
1938 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1939 h2s = (struct h2s*)h2_error_stream;
1940 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001941 }
1942 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1943 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1944 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001945 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001946 goto conn_err;
1947 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001948 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1949 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001950
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001951 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001952
Willy Tarreau25919232019-01-03 14:48:18 +01001953 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001954 if (h2c->st0 >= H2_CS_ERROR)
1955 goto out;
1956
Willy Tarreau25919232019-01-03 14:48:18 +01001957 if (error <= 0) {
1958 if (error == 0)
1959 goto out; // missing data
1960
1961 /* Failed to decode this stream (e.g. too large request)
1962 * but the HPACK decompressor is still synchronized.
1963 */
1964 h2s = (struct h2s*)h2_error_stream;
1965 goto send_rst;
1966 }
1967
Willy Tarreau22de8d32018-09-05 19:55:58 +02001968 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001969 * positively from h2c_frt_stream_new(), the stream will report the error,
1970 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001971 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001972 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001973 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001974 h2s = (struct h2s*)h2_refused_stream;
1975 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001976 }
1977
1978 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001979 h2s->rxbuf = rxbuf;
1980 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001981 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001982
Willy Tarreau88d138e2019-01-02 19:38:14 +01001983 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001984 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001985 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001986
1987 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001988 if (h2s->cs)
1989 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01001990 if (h2s->st == H2_SS_OPEN)
1991 h2s->st = H2_SS_HREM;
1992 else
1993 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02001994 }
1995
Willy Tarreau3a429f02019-01-03 11:41:50 +01001996 /* update the max stream ID if the request is being processed */
1997 if (h2s->id > h2c->max_id)
1998 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001999
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002000 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002001
2002 conn_err:
2003 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002004 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002005
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002006 out:
2007 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002008 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002009
2010 send_rst:
2011 /* make the demux send an RST for the current stream. We may only
2012 * do this if we're certain that the HEADERS frame was properly
2013 * decompressed so that the HPACK decoder is still kept up to date.
2014 */
2015 h2_release_buf(h2c, &rxbuf);
2016 h2c->st0 = H2_CS_FRAME_E;
2017 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002018}
2019
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002020/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2021 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2022 * errors here are reported as connection errors since it's impossible to
2023 * recover from such errors after the compression context has been altered.
2024 */
2025static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2026{
2027 int error;
2028
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002029 if (!b_size(&h2c->dbuf))
2030 return NULL; // empty buffer
2031
2032 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2033 return NULL; // incomplete frame
2034
Willy Tarreau1915ca22019-01-24 11:49:37 +01002035 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002036
Willy Tarreau25919232019-01-03 14:48:18 +01002037 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002038 if (h2c->st0 >= H2_CS_ERROR)
2039 return NULL;
2040
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002041 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2042 /* RFC7540#5.1 */
2043 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2044 h2c->st0 = H2_CS_FRAME_E;
2045 return NULL;
2046 }
2047
Willy Tarreau25919232019-01-03 14:48:18 +01002048 if (error <= 0) {
2049 if (error == 0)
2050 return NULL; // missing data
2051
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002052 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002053 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002054 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002055 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002056 }
2057
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002058 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2059 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002060 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002061 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002062 }
2063
Willy Tarreau927b88b2019-03-04 08:03:25 +01002064 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002065 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002066 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 +02002067 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002068 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002069 h2s_close(h2s);
2070
2071 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002072}
2073
Willy Tarreau454f9052017-10-26 19:40:35 +02002074/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2075 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2076 */
2077static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2078{
2079 int error;
2080
2081 /* note that empty DATA frames are perfectly valid and sometimes used
2082 * to signal an end of stream (with the ES flag).
2083 */
2084
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002085 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002086 return 0; // empty buffer
2087
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002088 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002089 return 0; // incomplete frame
2090
2091 /* now either the frame is complete or the buffer is complete */
2092
Willy Tarreau454f9052017-10-26 19:40:35 +02002093 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2094 /* RFC7540#6.1 */
2095 error = H2_ERR_STREAM_CLOSED;
2096 goto strm_err;
2097 }
2098
Willy Tarreau1915ca22019-01-24 11:49:37 +01002099 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2100 /* RFC7540#8.1.2 */
2101 error = H2_ERR_PROTOCOL_ERROR;
2102 goto strm_err;
2103 }
2104
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002105 if (!h2_frt_transfer_data(h2s))
2106 return 0;
2107
Willy Tarreau454f9052017-10-26 19:40:35 +02002108 /* call the upper layers to process the frame, then let the upper layer
2109 * notify the stream about any change.
2110 */
2111 if (!h2s->cs) {
2112 error = H2_ERR_STREAM_CLOSED;
2113 goto strm_err;
2114 }
2115
Willy Tarreau8f650c32017-11-21 19:36:21 +01002116 if (h2c->st0 >= H2_CS_ERROR)
2117 return 0;
2118
Willy Tarreau721c9742017-11-07 11:05:42 +01002119 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002120 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002121 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002122 }
2123
2124 /* check for completion : the callee will change this to FRAME_A or
2125 * FRAME_H once done.
2126 */
2127 if (h2c->st0 == H2_CS_FRAME_P)
2128 return 0;
2129
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002130 /* last frame */
2131 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002132 if (h2s->cs)
2133 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002134 if (h2s->st == H2_SS_OPEN)
2135 h2s->st = H2_SS_HREM;
2136 else
2137 h2s_close(h2s);
2138
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002139 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002140
2141 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2142 /* RFC7540#8.1.2 */
2143 error = H2_ERR_PROTOCOL_ERROR;
2144 goto strm_err;
2145 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002146 }
2147
Willy Tarreau454f9052017-10-26 19:40:35 +02002148 return 1;
2149
Willy Tarreau454f9052017-10-26 19:40:35 +02002150 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002151 h2s_error(h2s, error);
2152 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002153 return 0;
2154}
2155
Willy Tarreaubc933932017-10-09 16:21:43 +02002156/* process Rx frames to be demultiplexed */
2157static void h2_process_demux(struct h2c *h2c)
2158{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002159 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002160 struct h2_fh hdr;
2161 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002162
Willy Tarreau081d4722017-05-16 21:51:05 +02002163 if (h2c->st0 >= H2_CS_ERROR)
2164 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002165
2166 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2167 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002168 if (h2c->flags & H2_CF_IS_BACK)
2169 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002170 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2171 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002172 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002173 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002174 sess_log(h2c->conn->owner);
2175 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002176 goto fail;
2177 }
2178
2179 h2c->max_id = 0;
2180 h2c->st0 = H2_CS_SETTINGS1;
2181 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002182
2183 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002184 /* ensure that what is pending is a valid SETTINGS frame
2185 * without an ACK.
2186 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002187 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002188 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002189 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002190 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002191 sess_log(h2c->conn->owner);
2192 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002193 goto fail;
2194 }
2195
2196 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2197 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2198 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2199 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002200 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002201 goto fail;
2202 }
2203
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002204 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002205 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2206 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2207 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002208 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002209 goto fail;
2210 }
2211
Willy Tarreau3bf69182018-12-21 15:34:50 +01002212 /* that's OK, switch to FRAME_P to process it. This is
2213 * a SETTINGS frame whose header has already been
2214 * deleted above.
2215 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002216 padlen = 0;
2217 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002218 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002219 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002220
2221 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002222 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002223 int ret = 0;
2224
2225 if (h2c->st0 >= H2_CS_ERROR)
2226 break;
2227
2228 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002229 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002230 break;
2231
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002232 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002233 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002234 if (!h2c->nb_streams) {
2235 /* only log if no other stream can report the error */
2236 sess_log(h2c->conn->owner);
2237 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002238 break;
2239 }
2240
Willy Tarreau3bf69182018-12-21 15:34:50 +01002241 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2242 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2243 * we read the pad length and drop it from the remaining
2244 * payload (one byte + the 9 remaining ones = 10 total
2245 * removed), so we have a frame payload starting after the
2246 * pad len. Flow controlled frames (DATA) also count the
2247 * padlen in the flow control, so it must be adjusted.
2248 */
2249 if (hdr.len < 1) {
2250 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2251 sess_log(h2c->conn->owner);
2252 goto fail;
2253 }
2254 hdr.len--;
2255
2256 if (b_data(&h2c->dbuf) < 10)
2257 break; // missing padlen
2258
2259 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2260
2261 if (padlen > hdr.len) {
2262 /* RFC7540#6.1 : pad length = length of
2263 * frame payload or greater => error.
2264 */
2265 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2266 sess_log(h2c->conn->owner);
2267 goto fail;
2268 }
2269
2270 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2271 h2c->rcvd_c++;
2272 h2c->rcvd_s++;
2273 }
2274 b_del(&h2c->dbuf, 1);
2275 }
2276 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002277
2278 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002279 h2c->dfl = hdr.len;
2280 h2c->dsi = hdr.sid;
2281 h2c->dft = hdr.ft;
2282 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002283 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002284 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002285
2286 /* check for minimum basic frame format validity */
2287 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2288 if (ret != H2_ERR_NO_ERROR) {
2289 h2c_error(h2c, ret);
2290 sess_log(h2c->conn->owner);
2291 goto fail;
2292 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002293 }
2294
2295 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002296 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2297
Willy Tarreau567beb82018-12-18 16:52:44 +01002298 if (tmp_h2s != h2s && h2s && h2s->cs &&
2299 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002300 (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 +01002301 /* we may have to signal the upper layers */
2302 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002303 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002304 }
2305 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002306
Willy Tarreaud7901432017-12-29 11:34:40 +01002307 if (h2c->st0 == H2_CS_FRAME_E)
2308 goto strm_err;
2309
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002310 if (h2s->st == H2_SS_IDLE &&
2311 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2312 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2313 * this state MUST be treated as a connection error
2314 */
2315 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002316 if (!h2c->nb_streams) {
2317 /* only log if no other stream can report the error */
2318 sess_log(h2c->conn->owner);
2319 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002320 break;
2321 }
2322
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002323 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2324 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2325 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002326 * this state MUST be treated as a stream error.
2327 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2328 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002329 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002330 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2331 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2332 else
2333 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002334 goto strm_err;
2335 }
2336
Willy Tarreauab837502017-12-27 15:07:30 +01002337 /* Below the management of frames received in closed state is a
2338 * bit hackish because the spec makes strong differences between
2339 * streams closed by receiving RST, sending RST, and seeing ES
2340 * in both directions. In addition to this, the creation of a
2341 * new stream reusing the identifier of a closed one will be
2342 * detected here. Given that we cannot keep track of all closed
2343 * streams forever, we consider that unknown closed streams were
2344 * closed on RST received, which allows us to respond with an
2345 * RST without breaking the connection (eg: to abort a transfer).
2346 * Some frames have to be silently ignored as well.
2347 */
2348 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002349 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002350 /* #5.1.1: The identifier of a newly
2351 * established stream MUST be numerically
2352 * greater than all streams that the initiating
2353 * endpoint has opened or reserved. This
2354 * governs streams that are opened using a
2355 * HEADERS frame and streams that are reserved
2356 * using PUSH_PROMISE. An endpoint that
2357 * receives an unexpected stream identifier
2358 * MUST respond with a connection error.
2359 */
2360 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2361 goto strm_err;
2362 }
2363
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002364 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002365 /* RFC7540#5.1:closed: an endpoint that
2366 * receives any frame other than PRIORITY after
2367 * receiving a RST_STREAM MUST treat that as a
2368 * stream error of type STREAM_CLOSED.
2369 *
2370 * Note that old streams fall into this category
2371 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002372 *
2373 * However, we cannot generalize this to all frame types. Those
2374 * carrying compression state must still be processed before
2375 * being dropped or we'll desynchronize the decoder. This can
2376 * happen with request trailers received after sending an
2377 * RST_STREAM, or with header/trailers responses received after
2378 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002379 */
2380 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2381 h2c->st0 = H2_CS_FRAME_E;
2382 goto strm_err;
2383 }
2384
2385 /* RFC7540#5.1:closed: if this state is reached as a
2386 * result of sending a RST_STREAM frame, the peer that
2387 * receives the RST_STREAM might have already sent
2388 * frames on the stream that cannot be withdrawn. An
2389 * endpoint MUST ignore frames that it receives on
2390 * closed streams after it has sent a RST_STREAM
2391 * frame. An endpoint MAY choose to limit the period
2392 * over which it ignores frames and treat frames that
2393 * arrive after this time as being in error.
2394 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002395 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002396 /* RFC7540#5.1:closed: any frame other than
2397 * PRIO/WU/RST in this state MUST be treated as
2398 * a connection error
2399 */
2400 if (h2c->dft != H2_FT_RST_STREAM &&
2401 h2c->dft != H2_FT_PRIORITY &&
2402 h2c->dft != H2_FT_WINDOW_UPDATE) {
2403 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2404 goto strm_err;
2405 }
2406 }
2407 }
2408
Willy Tarreauc0da1962017-10-30 18:38:00 +01002409#if 0
2410 // problem below: it is not possible to completely ignore such
2411 // streams as we need to maintain the compression state as well
2412 // and for this we need to completely process these frames (eg:
2413 // HEADERS frames) as well as counting DATA frames to emit
2414 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2415 // This is a typical case of layer violation where the
2416 // transported contents are critical to the connection's
2417 // validity and must be ignored at the same time :-(
2418
2419 /* graceful shutdown, ignore streams whose ID is higher than
2420 * the one advertised in GOAWAY. RFC7540#6.8.
2421 */
2422 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002423 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2424 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002425 h2c->dfl -= ret;
2426 ret = h2c->dfl == 0;
2427 goto strm_err;
2428 }
2429#endif
2430
Willy Tarreau7e98c052017-10-10 15:56:59 +02002431 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002432 case H2_FT_SETTINGS:
2433 if (h2c->st0 == H2_CS_FRAME_P)
2434 ret = h2c_handle_settings(h2c);
2435
2436 if (h2c->st0 == H2_CS_FRAME_A)
2437 ret = h2c_ack_settings(h2c);
2438 break;
2439
Willy Tarreaucf68c782017-10-10 17:11:41 +02002440 case H2_FT_PING:
2441 if (h2c->st0 == H2_CS_FRAME_P)
2442 ret = h2c_handle_ping(h2c);
2443
2444 if (h2c->st0 == H2_CS_FRAME_A)
2445 ret = h2c_ack_ping(h2c);
2446 break;
2447
Willy Tarreau26f95952017-07-27 17:18:30 +02002448 case H2_FT_WINDOW_UPDATE:
2449 if (h2c->st0 == H2_CS_FRAME_P)
2450 ret = h2c_handle_window_update(h2c, h2s);
2451 break;
2452
Willy Tarreau61290ec2017-10-17 08:19:21 +02002453 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002454 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2455 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2456 * frames' parsers consume all following CONTINUATION
2457 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002458 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002459 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2460 sess_log(h2c->conn->owner);
2461 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002462
Willy Tarreau13278b42017-10-13 19:23:14 +02002463 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002464 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002465 if (h2c->flags & H2_CF_IS_BACK)
2466 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2467 else
2468 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002469 if (tmp_h2s) {
2470 h2s = tmp_h2s;
2471 ret = 1;
2472 }
2473 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002474 break;
2475
Willy Tarreau454f9052017-10-26 19:40:35 +02002476 case H2_FT_DATA:
2477 if (h2c->st0 == H2_CS_FRAME_P)
2478 ret = h2c_frt_handle_data(h2c, h2s);
2479
2480 if (h2c->st0 == H2_CS_FRAME_A)
2481 ret = h2c_send_strm_wu(h2c);
2482 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002483
Willy Tarreau92153fc2017-12-03 19:46:19 +01002484 case H2_FT_PRIORITY:
2485 if (h2c->st0 == H2_CS_FRAME_P)
2486 ret = h2c_handle_priority(h2c);
2487 break;
2488
Willy Tarreaucd234e92017-08-18 10:59:39 +02002489 case H2_FT_RST_STREAM:
2490 if (h2c->st0 == H2_CS_FRAME_P)
2491 ret = h2c_handle_rst_stream(h2c, h2s);
2492 break;
2493
Willy Tarreaue96b0922017-10-30 00:28:29 +01002494 case H2_FT_GOAWAY:
2495 if (h2c->st0 == H2_CS_FRAME_P)
2496 ret = h2c_handle_goaway(h2c);
2497 break;
2498
Willy Tarreau1c661982017-10-30 13:52:01 +01002499 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002500 default:
2501 /* drop frames that we ignore. They may be larger than
2502 * the buffer so we drain all of their contents until
2503 * we reach the end.
2504 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002505 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2506 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002507 h2c->dfl -= ret;
2508 ret = h2c->dfl == 0;
2509 }
2510
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002511 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002512 /* We may have to send an RST if not done yet */
2513 if (h2s->st == H2_SS_ERROR)
2514 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002515
Willy Tarreaua20a5192017-12-27 11:02:06 +01002516 if (h2c->st0 == H2_CS_FRAME_E)
2517 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002518
Willy Tarreau7e98c052017-10-10 15:56:59 +02002519 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002520 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002521 break;
2522
2523 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002524 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002525 h2c->st0 = H2_CS_FRAME_H;
2526 }
2527 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002528
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002529 if (h2c->rcvd_c > 0 &&
2530 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2531 h2c_send_conn_wu(h2c);
2532
Willy Tarreau52eed752017-09-22 15:05:09 +02002533 fail:
2534 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002535 if (h2s && h2s->cs &&
2536 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002537 (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 +01002538 /* we may have to signal the upper layers */
2539 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002540 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002541 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002542
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002543 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002544}
2545
2546/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2547 * the end.
2548 */
2549static int h2_process_mux(struct h2c *h2c)
2550{
Olivier Houchard998410a2019-04-15 19:23:37 +02002551 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002552
Willy Tarreau01b44822018-10-03 14:26:37 +02002553 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2554 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2555 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2556 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2557 if (h2c->st0 == H2_CS_ERROR) {
2558 h2c->st0 = H2_CS_ERROR2;
2559 sess_log(h2c->conn->owner);
2560 }
2561 goto fail;
2562 }
2563 h2c->st0 = H2_CS_SETTINGS1;
2564 }
2565 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002566 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002567 return 1;
2568 }
2569
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002570 /* start by sending possibly pending window updates */
2571 if (h2c->rcvd_c > 0 &&
2572 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2573 h2c_send_conn_wu(h2c) < 0)
2574 goto fail;
2575
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002576 /* First we always process the flow control list because the streams
2577 * waiting there were already elected for immediate emission but were
2578 * blocked just on this.
2579 */
2580
Olivier Houchard998410a2019-04-15 19:23:37 +02002581 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002582 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2583 h2c->st0 >= H2_CS_ERROR)
2584 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002585
2586 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002587 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002588
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002589 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002590 /* For some reason, the upper layer failed to subsribe again,
2591 * so remove it from the send_list
2592 */
2593 if (!h2s->send_wait) {
2594 LIST_DEL_INIT(&h2s->list);
2595 continue;
2596 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002597 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002598 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002599 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002600 }
2601
Olivier Houchard998410a2019-04-15 19:23:37 +02002602 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002603 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2604 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002605
2606 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002607 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002608
Olivier Houchard998410a2019-04-15 19:23:37 +02002609 /* For some reason, the upper layer failed to subsribe again,
2610 * so remove it from the send_list
2611 */
2612 if (!h2s->send_wait) {
2613 LIST_DEL_INIT(&h2s->list);
2614 continue;
2615 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002616 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002617 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002618 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002619 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002620 }
2621
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002622 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002623 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002624 if (h2c->st0 == H2_CS_ERROR) {
2625 if (h2c->max_id >= 0) {
2626 h2c_send_goaway_error(h2c, NULL);
2627 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2628 return 0;
2629 }
2630
2631 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2632 }
2633 return 1;
2634 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002635 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002636}
2637
Willy Tarreau62f52692017-10-08 23:01:42 +02002638
Willy Tarreau479998a2018-11-18 06:30:59 +01002639/* Attempt to read data, and subscribe if none available.
2640 * The function returns 1 if data has been received, otherwise zero.
2641 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002642static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002643{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002644 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002645 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002646 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002647 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002648
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002649 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002650 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002651
Willy Tarreau315d8072017-12-10 22:17:57 +01002652 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002653 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002654
Willy Tarreau44e973f2018-03-01 17:49:30 +01002655 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002656 if (!buf) {
2657 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002658 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002659 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002660
Olivier Houchard7505f942018-08-21 18:10:44 +02002661 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002662 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002663 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2664 /* HTX in use : try to pre-align the buffer like the
2665 * rxbufs will be to optimize memory copies. We'll make
2666 * sure that the frame header lands at the end of the
2667 * HTX block to alias it upon recv. We cannot use the
2668 * head because rcv_buf() will realign the buffer if
2669 * it's empty. Thus we cheat and pretend we already
2670 * have a few bytes there.
2671 */
2672 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002673 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002674 }
2675 else
2676 max = b_room(buf);
2677
Olivier Houchard7505f942018-08-21 18:10:44 +02002678 if (max)
2679 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2680 else
2681 ret = 0;
2682 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002683
Olivier Houchard53216e72018-10-10 15:46:36 +02002684 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002685 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002686
Olivier Houcharda1411e62018-08-17 18:42:48 +02002687 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002688 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002689 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002690 }
2691
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002692 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002693 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002694 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002695}
2696
Willy Tarreau479998a2018-11-18 06:30:59 +01002697/* Try to send data if possible.
2698 * The function returns 1 if data have been sent, otherwise zero.
2699 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002700static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002701{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002702 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002703 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002704 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002705
2706 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002707 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002708
Olivier Houchard7505f942018-08-21 18:10:44 +02002709
Willy Tarreaua2af5122017-10-09 11:56:46 +02002710 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2711 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002712 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002713 }
2714
Willy Tarreaubc933932017-10-09 16:21:43 +02002715 /* This loop is quite simple : it tries to fill as much as it can from
2716 * pending streams into the existing buffer until it's reportedly full
2717 * or the end of send requests is reached. Then it tries to send this
2718 * buffer's contents out, marks it not full if at least one byte could
2719 * be sent, and tries again.
2720 *
2721 * The snd_buf() function normally takes a "flags" argument which may
2722 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2723 * data immediately comes and CO_SFL_STREAMER to indicate that the
2724 * connection is streaming lots of data (used to increase TLS record
2725 * size at the expense of latency). The former can be sent any time
2726 * there's a buffer full flag, as it indicates at least one stream
2727 * attempted to send and failed so there are pending data. An
2728 * alternative would be to set it as long as there's an active stream
2729 * but that would be problematic for ACKs until we have an absolute
2730 * guarantee that all waiters have at least one byte to send. The
2731 * latter should possibly not be set for now.
2732 */
2733
2734 done = 0;
2735 while (!done) {
2736 unsigned int flags = 0;
2737
2738 /* fill as much as we can into the current buffer */
2739 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2740 done = h2_process_mux(h2c);
2741
Olivier Houchard2b094432019-01-29 18:28:36 +01002742 if (h2c->flags & H2_CF_MUX_MALLOC)
2743 break;
2744
Willy Tarreaubc933932017-10-09 16:21:43 +02002745 if (conn->flags & CO_FL_ERROR)
2746 break;
2747
2748 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2749 flags |= CO_SFL_MSG_MORE;
2750
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002751 if (b_data(&h2c->mbuf)) {
2752 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002753 if (!ret)
2754 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002755 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002756 b_del(&h2c->mbuf, ret);
2757 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002758 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002759
2760 /* wrote at least one byte, the buffer is not full anymore */
2761 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2762 }
2763
Willy Tarreaua2af5122017-10-09 11:56:46 +02002764 if (conn->flags & CO_FL_SOCK_WR_SH) {
2765 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002766 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002767 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002768 /* We're not full anymore, so we can wake any task that are waiting
2769 * for us.
2770 */
2771 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002772 struct h2s *h2s;
2773
2774 list_for_each_entry(h2s, &h2c->send_list, list) {
2775 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2776 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002777
2778 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002779 continue;
2780
Olivier Houchard998410a2019-04-15 19:23:37 +02002781 /* For some reason, the upper layer failed to subsribe again,
2782 * so remove it from the send_list
2783 */
2784 if (!h2s->send_wait) {
2785 LIST_DEL_INIT(&h2s->list);
2786 continue;
2787 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002788 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002789 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002790 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002791 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002792 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002793 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002794 /* We're done, no more to send */
2795 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002796 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002797schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002798 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2799 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002800 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002801}
2802
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002803/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002804static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2805{
2806 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002807 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002808
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002809 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002810 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002811 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002812 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002813 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002814 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002815 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002816}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002817
Willy Tarreau62f52692017-10-08 23:01:42 +02002818/* callback called on any event by the connection handler.
2819 * It applies changes and returns zero, or < 0 if it wants immediate
2820 * destruction of the connection (which normally doesn not happen in h2).
2821 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002822static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002823{
Olivier Houchard7505f942018-08-21 18:10:44 +02002824 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002825
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002826 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002827 h2_process_demux(h2c);
2828
2829 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002830 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002831
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002832 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002833 h2c->flags &= ~H2_CF_DEM_DFULL;
2834 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002835 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002836
Willy Tarreau0b37d652018-10-03 10:33:02 +02002837 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002838 /* frontend is stopping, reload likely in progress, let's try
2839 * to announce a graceful shutdown if not yet done. We don't
2840 * care if it fails, it will be tried again later.
2841 */
2842 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2843 if (h2c->last_sid < 0)
2844 h2c->last_sid = (1U << 31) - 1;
2845 h2c_send_goaway_error(h2c, NULL);
2846 }
2847 }
2848
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002849 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002850 * If we received early data, and the handshake is done, wake
2851 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002852 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002853 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2854 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2855 struct eb32_node *node;
2856 struct h2s *h2s;
2857
2858 h2c->flags |= H2_CF_WAIT_FOR_HS;
2859 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2860
2861 while (node) {
2862 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002863 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002864 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002865 node = eb32_next(node);
2866 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002867 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002868
Willy Tarreau26bd7612017-10-09 16:47:04 +02002869 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002870 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2871 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2872 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002873 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002874
2875 if (eb_is_empty(&h2c->streams_by_id)) {
2876 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002877 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002878 return -1;
2879 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002880 }
2881
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002882 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002883 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002884
Olivier Houchard53216e72018-10-10 15:46:36 +02002885 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2886 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2887 (h2c->st0 != H2_CS_ERROR &&
2888 !b_data(&h2c->mbuf) &&
2889 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2890 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002891 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002892
Willy Tarreau3f133572017-10-31 19:21:06 +01002893 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002894 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002895 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002896 task_queue(h2c->task);
2897 }
2898 else
2899 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002900 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002901
Olivier Houchard7505f942018-08-21 18:10:44 +02002902 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002903 return 0;
2904}
2905
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002906/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002907static int h2_wake(struct connection *conn)
2908{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002909 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002910
2911 return (h2_process(h2c));
2912}
2913
Willy Tarreauea392822017-10-31 10:02:25 +01002914/* Connection timeout management. The principle is that if there's no receipt
2915 * nor sending for a certain amount of time, the connection is closed. If the
2916 * MUX buffer still has lying data or is not allocatable, the connection is
2917 * immediately killed. If it's allocatable and empty, we attempt to send a
2918 * GOAWAY frame.
2919 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002920static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002921{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002922 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002923 int expired = tick_is_expired(t->expire, now_ms);
2924
Willy Tarreau0975f112018-03-29 15:22:59 +02002925 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002926 return t;
2927
Willy Tarreau0975f112018-03-29 15:22:59 +02002928 task_delete(t);
2929 task_free(t);
2930
2931 if (!h2c) {
2932 /* resources were already deleted */
2933 return NULL;
2934 }
2935
2936 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002937 h2c_error(h2c, H2_ERR_NO_ERROR);
2938 h2_wake_some_streams(h2c, 0, 0);
2939
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002940 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002941 /* don't even try to send a GOAWAY, the buffer is stuck */
2942 h2c->flags |= H2_CF_GOAWAY_FAILED;
2943 }
2944
2945 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002946 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002947 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2948 h2c->flags |= H2_CF_GOAWAY_FAILED;
2949
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002950 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2951 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002952 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002953 b_del(&h2c->mbuf, ret);
2954 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002955 }
2956 }
Willy Tarreauea392822017-10-31 10:02:25 +01002957
Willy Tarreau0975f112018-03-29 15:22:59 +02002958 /* either we can release everything now or it will be done later once
2959 * the last stream closes.
2960 */
2961 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002962 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002963
Willy Tarreauea392822017-10-31 10:02:25 +01002964 return NULL;
2965}
2966
2967
Willy Tarreau62f52692017-10-08 23:01:42 +02002968/*******************************************/
2969/* functions below are used by the streams */
2970/*******************************************/
2971
2972/*
2973 * Attach a new stream to a connection
2974 * (Used for outgoing connections)
2975 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002976static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002977{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002978 struct conn_stream *cs;
2979 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002980 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002981
2982 cs = cs_new(conn);
2983 if (!cs)
2984 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002985 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002986 if (!h2s) {
2987 cs_free(cs);
2988 return NULL;
2989 }
2990 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002991}
2992
Willy Tarreaufafd3982018-11-18 21:29:20 +01002993/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2994 * We have to scan because we may have some orphan streams. It might be
2995 * beneficial to scan backwards from the end to reduce the likeliness to find
2996 * orphans.
2997 */
2998static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2999{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003000 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003001 struct h2s *h2s;
3002 struct eb32_node *node;
3003
3004 node = eb32_first(&h2c->streams_by_id);
3005 while (node) {
3006 h2s = container_of(node, struct h2s, by_id);
3007 if (h2s->cs)
3008 return h2s->cs;
3009 node = eb32_next(node);
3010 }
3011 return NULL;
3012}
3013
Willy Tarreau62f52692017-10-08 23:01:42 +02003014/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003015 * Destroy the mux and the associated connection, if it is no longer used
3016 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003017static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003018{
Christopher Faulet73c12072019-04-08 11:23:22 +02003019 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003020
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003021 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003022 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003023}
3024
3025/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003026 * Detach the stream from the connection and possibly release the connection.
3027 */
3028static void h2_detach(struct conn_stream *cs)
3029{
Willy Tarreau60935142017-10-16 18:11:19 +02003030 struct h2s *h2s = cs->ctx;
3031 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003032 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003033
3034 cs->ctx = NULL;
3035 if (!h2s)
3036 return;
3037
Olivier Houchard998410a2019-04-15 19:23:37 +02003038 /* The stream is about to die, so no need to attempt to run its task */
3039 if (!LIST_ISEMPTY(&h2s->sending_list) &&
3040 h2s->send_wait != &h2s->wait_event) {
3041 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3042 LIST_DEL_INIT(&h2s->sending_list);
3043 }
3044
Olivier Houchardf502aca2018-12-14 19:42:40 +01003045 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003046 h2c = h2s->h2c;
3047 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003048 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003049 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3050 !h2_frt_has_too_many_cs(h2c)) {
3051 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003052 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003053 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003054 }
Willy Tarreau60935142017-10-16 18:11:19 +02003055
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003056 /* this stream may be blocked waiting for some data to leave (possibly
3057 * an ES or RST frame), so orphan it in this case.
3058 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003059 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003060 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003061 (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 +01003062 return;
3063
Willy Tarreau45f752e2017-10-30 15:44:59 +01003064 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3065 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3066 /* unblock the connection if it was blocked on this
3067 * stream.
3068 */
3069 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3070 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003071 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003072 }
3073
Willy Tarreau71049cc2018-03-28 13:56:39 +02003074 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003075
Olivier Houchard8a786902018-12-15 16:05:40 +01003076 if (h2c->flags & H2_CF_IS_BACK &&
3077 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003078 if (!(h2c->conn->flags &
3079 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3080 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003081 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003082 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3083 h2c->conn->owner = NULL;
3084 if (eb_is_empty(&h2c->streams_by_id)) {
3085 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3086 /* The server doesn't want it, let's kill the connection right away */
3087 h2c->conn->mux->destroy(h2c->conn);
3088 return;
3089 }
3090 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003091 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003092 if (eb_is_empty(&h2c->streams_by_id)) {
3093 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3094 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3095 return;
3096 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003097 /* Never ever allow to reuse a connection from a non-reuse backend */
3098 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3099 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003100 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003101 struct server *srv = objt_server(h2c->conn->target);
3102
3103 if (srv) {
3104 if (h2c->conn->flags & CO_FL_PRIVATE)
3105 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3106 else
3107 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3108 }
3109
3110 }
3111 }
3112 }
3113
Willy Tarreaue323f342018-03-28 13:51:45 +02003114 /* We don't want to close right now unless we're removing the
3115 * last stream, and either the connection is in error, or it
3116 * reached the ID already specified in a GOAWAY frame received
3117 * or sent (as seen by last_sid >= 0).
3118 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003119 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003120 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003121 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003122 }
3123 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003124 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003125 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3126 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003127 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003128 else
3129 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003130 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003131}
3132
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003133/* Performs a synchronous or asynchronous shutr().
3134 * FIXME: guess what the return code tries to indicate!
3135 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003136static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003137{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003138 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003139 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003140
Willy Tarreau721c9742017-11-07 11:05:42 +01003141 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003142 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003143
Willy Tarreau18059042019-01-31 19:12:48 +01003144 /* a connstream may require us to immediately kill the whole connection
3145 * for example because of a "tcp-request content reject" rule that is
3146 * normally used to limit abuse. In this case we schedule a goaway to
3147 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003148 */
Willy Tarreau18059042019-01-31 19:12:48 +01003149 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3150 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3151 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3152 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3153 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003154 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3155 /* Nothing was never sent for this stream, so reset with
3156 * REFUSED_STREAM error to let the client retry the
3157 * request.
3158 */
3159 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3160 }
Willy Tarreau18059042019-01-31 19:12:48 +01003161
Willy Tarreau90c32322017-11-24 08:00:30 +01003162 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003163 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003164 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003165
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003166 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003167 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003168 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003169
Olivier Houchard7a977432019-03-21 15:47:13 +01003170 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003171add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003172 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003173 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003174 if (h2s->flags & H2_SF_BLK_MFCTL) {
3175 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3176 h2s->send_wait = sw;
3177 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3178 h2s->send_wait = sw;
3179 LIST_ADDQ(&h2c->send_list, &h2s->list);
3180 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003181 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003182 /* Let the handler know we want shutr */
3183 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003184 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003185}
3186
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003187/* Performs a synchronous or asynchronous shutw().
3188 * FIXME: guess what the return code tries to indicate!
3189 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003190static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003191{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003192 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003193 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003194
Willy Tarreau721c9742017-11-07 11:05:42 +01003195 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003196 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003197
Willy Tarreau67434202017-11-06 20:20:51 +01003198 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003199 /* we can cleanly close using an empty data frame only after headers */
3200
3201 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3202 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003203 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003204
3205 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003206 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003207 else
3208 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003209 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003210 /* a connstream may require us to immediately kill the whole connection
3211 * for example because of a "tcp-request content reject" rule that is
3212 * normally used to limit abuse. In this case we schedule a goaway to
3213 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003214 */
Willy Tarreau18059042019-01-31 19:12:48 +01003215 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3216 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3217 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3218 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3219 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003220 else {
3221 /* Nothing was never sent for this stream, so reset with
3222 * REFUSED_STREAM error to let the client retry the
3223 * request.
3224 */
3225 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3226 }
Willy Tarreau18059042019-01-31 19:12:48 +01003227
Willy Tarreau90c32322017-11-24 08:00:30 +01003228 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003229 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003230 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003231
Willy Tarreau00dd0782018-03-01 16:31:34 +01003232 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003233 }
3234
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003235 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003236 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003237 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003238
3239 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003240 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003241 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003242 if (h2s->flags & H2_SF_BLK_MFCTL) {
3243 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3244 h2s->send_wait = sw;
3245 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3246 h2s->send_wait = sw;
3247 LIST_ADDQ(&h2c->send_list, &h2s->list);
3248 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003249 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003250 /* let the handler know we want to shutw */
3251 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003252 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003253}
3254
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003255/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3256 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3257 * and prevented the last frame from being emitted.
3258 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003259static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3260{
3261 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003262 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003263 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003264
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003265 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003266 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003267 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003268 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003269 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003270
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003271 if (!ret) {
3272 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003273 LIST_DEL_INIT(&h2s->list);
3274 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003275 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003276 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003277 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003278 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003279
3280 if (h2c_is_dead(h2c))
Christopher Faulet73c12072019-04-08 11:23:22 +02003281 h2_release(h2c);
Olivier Houchard7a977432019-03-21 15:47:13 +01003282 }
3283
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003284 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003285}
3286
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003287/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003288static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3289{
3290 struct h2s *h2s = cs->ctx;
3291
3292 if (!mode)
3293 return;
3294
3295 h2_do_shutr(h2s);
3296}
3297
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003298/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003299static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3300{
3301 struct h2s *h2s = cs->ctx;
3302
3303 h2_do_shutw(h2s);
3304}
3305
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003306/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003307 * HTX request or response depending on the connection's side. Returns a
3308 * positive value on success, a negative value on failure, or 0 if it couldn't
3309 * proceed. May report connection errors in h2c->errcode if the frame is
3310 * non-decodable and the connection unrecoverable. In absence of connection
3311 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003312 *
3313 * The function may fold CONTINUATION frames into the initial HEADERS frame
3314 * by removing padding and next frame header, then moving the CONTINUATION
3315 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3316 * leaving a hole between the main frame and the beginning of the next one.
3317 * The possibly remaining incomplete or next frame at the end may be moved
3318 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3319 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3320 *
3321 * A buffer at the beginning of processing may look like this :
3322 *
3323 * ,---.---------.-----.--------------.--------------.------.---.
3324 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3325 * `---^---------^-----^--------------^--------------^------^---'
3326 * | | <-----> | |
3327 * area | dpl | wrap
3328 * |<--------------> |
3329 * | dfl |
3330 * |<-------------------------------------------------->|
3331 * head data
3332 *
3333 * Padding is automatically overwritten when folding, participating to the
3334 * hole size after dfl :
3335 *
3336 * ,---.------------------------.-----.--------------.------.---.
3337 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3338 * `---^------------------------^-----^--------------^------^---'
3339 * | | <-----> | |
3340 * area | hole | wrap
3341 * |<-----------------------> |
3342 * | dfl |
3343 * |<-------------------------------------------------->|
3344 * head data
3345 *
3346 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3347 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3348 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003349 *
3350 * The <flags> field must point to either the stream's flags or to a copy of it
3351 * so that the function can update the following flags :
3352 * - H2_SF_DATA_CLEN when content-length is seen
3353 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3354 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003355 *
3356 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3357 * decoding, in order to detect if we're dealing with a headers or a trailers
3358 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003359 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003360static 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 +02003361{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003362 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003363 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003364 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003365 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003366 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003367 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003368 int flen; // header frame len
3369 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003370 int ret = 0;
3371 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003372 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003373 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003374
Willy Tarreauea18f862018-12-22 20:19:26 +01003375next_frame:
3376 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3377 goto leave; // incomplete input frame
3378
3379 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3380 * this case, we'll try to paste it immediately after the initial
3381 * HEADERS frame payload and kill any possible padding. The initial
3382 * frame's length will be increased to represent the concatenation
3383 * of the two frames. The next frame is read from position <tlen>
3384 * and written at position <flen> (minus padding if some is present).
3385 */
3386 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3387 struct h2_fh hdr;
3388 int clen; // CONTINUATION frame's payload length
3389
3390 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3391 /* no more data, the buffer may be full, either due to
3392 * too large a frame or because of too large a hole that
3393 * we're going to compact at the end.
3394 */
3395 goto leave;
3396 }
3397
3398 if (hdr.ft != H2_FT_CONTINUATION) {
3399 /* RFC7540#6.10: frame of unexpected type */
3400 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3401 goto fail;
3402 }
3403
3404 if (hdr.sid != h2c->dsi) {
3405 /* RFC7540#6.10: frame of different stream */
3406 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3407 goto fail;
3408 }
3409
3410 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3411 /* RFC7540#4.2: invalid frame length */
3412 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3413 goto fail;
3414 }
3415
3416 /* detect when we must stop aggragating frames */
3417 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3418
3419 /* Take as much as we can of the CONTINUATION frame's payload */
3420 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3421 if (clen > hdr.len)
3422 clen = hdr.len;
3423
3424 /* Move the frame's payload over the padding, hole and frame
3425 * header. At least one of hole or dpl is null (see diagrams
3426 * above). The hole moves after the new aggragated frame.
3427 */
3428 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3429 h2c->dfl += clen - h2c->dpl;
3430 hole += h2c->dpl + 9;
3431 h2c->dpl = 0;
3432 goto next_frame;
3433 }
3434
3435 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003436
Willy Tarreau13278b42017-10-13 19:23:14 +02003437 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003438 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003439 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003440 copy = alloc_trash_chunk();
3441 if (!copy) {
3442 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3443 goto fail;
3444 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003445 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3446 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3447 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003448 }
3449
Willy Tarreau13278b42017-10-13 19:23:14 +02003450 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3451 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003452 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003453 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3454 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003455 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003456 }
3457
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003458 if (flen < 5) {
3459 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3460 goto fail;
3461 }
3462
Willy Tarreau13278b42017-10-13 19:23:14 +02003463 hdrs += 5; // stream dep = 4, weight = 1
3464 flen -= 5;
3465 }
3466
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003467 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003468 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003469 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003470 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003471
Willy Tarreau937f7602018-02-26 15:22:17 +01003472 /* we can't retry a failed decompression operation so we must be very
3473 * careful not to take any risks. In practice the output buffer is
3474 * always empty except maybe for trailers, in which case we simply have
3475 * to wait for the upper layer to finish consuming what is available.
3476 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003477
3478 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003479 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003480 if (!htx_is_empty(htx)) {
3481 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003482 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003483 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003484 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003485 if (b_data(rxbuf)) {
3486 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003487 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003488 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003489
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003490 rxbuf->head = 0;
3491 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003492 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003493
Willy Tarreau25919232019-01-03 14:48:18 +01003494 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003495 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3496 sizeof(list)/sizeof(list[0]), tmp);
3497 if (outlen < 0) {
3498 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3499 goto fail;
3500 }
3501
Willy Tarreau25919232019-01-03 14:48:18 +01003502 /* The PACK decompressor was updated, let's update the input buffer and
3503 * the parser's state to commit these changes and allow us to later
3504 * fail solely on the stream if needed.
3505 */
3506 b_del(&h2c->dbuf, h2c->dfl + hole);
3507 h2c->dfl = hole = 0;
3508 h2c->st0 = H2_CS_FRAME_H;
3509
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003510 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003511 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003512
Willy Tarreau88d138e2019-01-02 19:38:14 +01003513 if (*flags & H2_SF_HEADERS_RCVD)
3514 goto trailers;
3515
3516 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003517 if (htx) {
3518 /* HTX mode */
3519 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003520 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003521 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003522 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003523 } else {
3524 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003525 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003526 if (outlen > 0)
3527 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003528 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003529
3530 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003531 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003532 goto fail;
3533 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003534
Willy Tarreau174b06a2018-04-25 18:13:58 +02003535 if (msgf & H2_MSGF_BODY) {
3536 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003537 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003538 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003539 if (htx)
3540 htx->extra = *body_len;
3541 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003542 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003543 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003544 }
3545
Willy Tarreau88d138e2019-01-02 19:38:14 +01003546 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003547 /* indicate that a HEADERS frame was received for this stream, except
3548 * for 1xx responses. For 1xx responses, another HEADERS frame is
3549 * expected.
3550 */
3551 if (!(msgf & H2_MSGF_RSP_1XX))
3552 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003553
Christopher Faulet0b465482019-02-19 15:14:23 +01003554 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003555 /* Mark the end of message, either using EOM in HTX or with the
3556 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3557 * is not set during headers with END_STREAM.
3558 */
3559 if (htx) {
3560 if (!htx_add_endof(htx, HTX_BLK_EOM))
3561 goto fail;
3562 }
3563 else if (*flags & H2_SF_DATA_CHNK) {
3564 if (!b_putblk(rxbuf, "\r\n", 2))
3565 goto fail;
3566 }
3567 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003568
Willy Tarreau86277d42019-01-02 15:36:11 +01003569 /* success */
3570 ret = 1;
3571
Willy Tarreau68dd9852017-07-03 14:44:26 +02003572 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003573 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003574 * move the remaining data over it.
3575 */
3576 if (hole) {
3577 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3578 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3579 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3580 b_sub(&h2c->dbuf, hole);
3581 }
3582
3583 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3584 /* too large frames */
3585 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003586 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003587 }
3588
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003589 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003590 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003591 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003592 return ret;
3593
Willy Tarreau68dd9852017-07-03 14:44:26 +02003594 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003595 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003596 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003597
3598 trailers:
3599 /* This is the last HEADERS frame hence a trailer */
3600
3601 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3602 /* It's a trailer but it's missing ES flag */
3603 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3604 goto fail;
3605 }
3606
3607 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3608 * block, and when using chunks we must send the 0 CRLF marker. For
3609 * other modes, the trailers are silently dropped.
3610 */
3611 if (htx) {
3612 if (!htx_add_endof(htx, HTX_BLK_EOD))
3613 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003614 if (h2_make_htx_trailers(list, htx) <= 0)
3615 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003616 }
3617 else if (*flags & H2_SF_DATA_CHNK) {
3618 /* Legacy mode with chunked encoding : we must finalize the
3619 * data block message emit the trailing CRLF */
3620 if (!b_putblk(rxbuf, "0\r\n", 3))
3621 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003622
3623 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3624 if (outlen > 0)
3625 b_add(rxbuf, outlen);
3626 else
3627 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003628 }
3629
3630 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003631}
3632
Willy Tarreau454f9052017-10-26 19:40:35 +02003633/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3634 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3635 * in use, a new chunk is emitted for each frame. This is supposed to fit
3636 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3637 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3638 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003639 * parser state is automatically updated. Returns > 0 if it could completely
3640 * send the current frame, 0 if it couldn't complete, in which case
3641 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3642 * DATA frame can return 0 as a valid result). Stream errors are reported in
3643 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3644 * have checked the frame header and ensured that the frame was complete or the
3645 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003646 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003647static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003648{
3649 struct h2c *h2c = h2s->h2c;
3650 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003651 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003652 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003653 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003654 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003655
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003656 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003657
Olivier Houchard638b7992018-08-16 15:41:52 +02003658 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003659 if (!csbuf) {
3660 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003661 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003662 }
3663
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003664try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003665 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003666 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3667 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003668 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003669 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003670
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003671 if (flen > b_data(&h2c->dbuf)) {
3672 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003673 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003674 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003675 }
3676
Willy Tarreaua9b77962019-01-31 07:23:00 +01003677 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003678 block1 = htx_free_data_space(htx);
3679 if (!block1) {
3680 h2c->flags |= H2_CF_DEM_SFULL;
3681 goto fail;
3682 }
3683 if (flen > block1)
3684 flen = block1;
3685
3686 /* here, flen is the max we can copy into the output buffer */
3687 block1 = b_contig_data(&h2c->dbuf, 0);
3688 if (flen > block1)
3689 flen = block1;
3690
3691 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3692 h2c->flags |= H2_CF_DEM_SFULL;
3693 goto fail;
3694 }
3695
3696 b_del(&h2c->dbuf, flen);
3697 h2c->dfl -= flen;
3698 h2c->rcvd_c += flen;
3699 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003700
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003701 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003702 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003703 htx->extra = h2s->body_len;
3704 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003705 goto try_again;
3706 }
3707 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003708 /* it doesn't fit and the buffer is fragmented,
3709 * so let's defragment it and try again.
3710 */
3711 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003712 }
3713
Willy Tarreaueba10f22018-04-25 20:44:22 +02003714 /* chunked-encoding requires more room */
3715 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003716 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003717 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3718 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3719 (chklen < 1048576) ? 4 : 8;
3720 chklen += 4; // CRLF, CRLF
3721 }
3722
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003723 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003724 if (flen + chklen > b_room(csbuf)) {
3725 if (chklen >= b_room(csbuf)) {
3726 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003727 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003728 }
3729 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003730 }
3731
3732 if (h2s->flags & H2_SF_DATA_CHNK) {
3733 /* emit the chunk size */
3734 unsigned int chksz = flen;
3735 char str[10];
3736 char *beg;
3737
3738 beg = str + sizeof(str);
3739 *--beg = '\n';
3740 *--beg = '\r';
3741 do {
3742 *--beg = hextab[chksz & 0xF];
3743 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003744 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003745 }
3746
Willy Tarreau454f9052017-10-26 19:40:35 +02003747 /* Block1 is the length of the first block before the buffer wraps,
3748 * block2 is the optional second block to reach the end of the frame.
3749 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003750 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003751 if (block1 > flen)
3752 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003753 block2 = flen - block1;
3754
3755 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003756 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003757
3758 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003759 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003760
Willy Tarreaueba10f22018-04-25 20:44:22 +02003761 if (h2s->flags & H2_SF_DATA_CHNK) {
3762 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003763 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003764 }
3765
Willy Tarreau454f9052017-10-26 19:40:35 +02003766 /* now mark the input data as consumed (will be deleted from the buffer
3767 * by the caller when seeing FRAME_A after sending the window update).
3768 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003769 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003770 h2c->dfl -= flen;
3771 h2c->rcvd_c += flen;
3772 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3773
Willy Tarreau1915ca22019-01-24 11:49:37 +01003774 if (h2s->flags & H2_SF_DATA_CLEN)
3775 h2s->body_len -= flen;
3776
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003777 if (h2c->dfl > h2c->dpl) {
3778 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003779 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003780 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003781 }
3782
Willy Tarreau4a28da12018-01-04 14:41:00 +01003783 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003784 /* here we're done with the frame, all the payload (except padding) was
3785 * transferred.
3786 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003787
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003788 if (h2c->dff & H2_F_DATA_END_STREAM) {
3789 if (htx) {
3790 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3791 h2c->flags |= H2_CF_DEM_SFULL;
3792 goto fail;
3793 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003794 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003795 else if (h2s->flags & H2_SF_DATA_CHNK) {
3796 /* emit the trailing 0 CRLF CRLF */
3797 if (b_room(csbuf) < 5) {
3798 h2c->flags |= H2_CF_DEM_SFULL;
3799 goto fail;
3800 }
3801 chklen += 5;
3802 b_putblk(csbuf, "0\r\n\r\n", 5);
3803 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003804 }
3805
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003806 h2c->rcvd_c += h2c->dpl;
3807 h2c->rcvd_s += h2c->dpl;
3808 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003809 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003810 if (htx)
3811 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003812 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003813 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003814 if (htx)
3815 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003816 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003817}
3818
Willy Tarreau5dd17352018-06-14 13:33:30 +02003819/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3820 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3821 * number of bytes sent. The caller must check the stream's status to detect
3822 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003823 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003824static 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 +02003825{
3826 struct http_hdr list[MAX_HTTP_HDR];
3827 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003828 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003829 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003830 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003831 int es_now = 0;
3832 int ret = 0;
3833 int hdr;
3834
3835 if (h2c_mux_busy(h2c, h2s)) {
3836 h2s->flags |= H2_SF_BLK_MBUSY;
3837 return 0;
3838 }
3839
Willy Tarreau44e973f2018-03-01 17:49:30 +01003840 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003841 h2c->flags |= H2_CF_MUX_MALLOC;
3842 h2s->flags |= H2_SF_BLK_MROOM;
3843 return 0;
3844 }
3845
3846 /* First, try to parse the H1 response and index it into <list>.
3847 * NOTE! Since it comes from haproxy, we *know* that a response header
3848 * block does not wrap and we can safely read it this way without
3849 * having to realign the buffer.
3850 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003851 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003852 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003853 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003854 /* incomplete or invalid response, this is abnormal coming from
3855 * haproxy and may only result in a bad errorfile or bad Lua code
3856 * so that won't be fixed, raise an error now.
3857 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003858 * FIXME: we should instead add the ability to only return a
3859 * 502 bad gateway. But in theory this is not supposed to
3860 * happen.
3861 */
3862 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3863 ret = 0;
3864 goto end;
3865 }
3866
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003867 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003868
3869 /* certain statuses have no body or an empty one, regardless of
3870 * what the headers say.
3871 */
3872 if (sl.st.status >= 100 && sl.st.status < 200) {
3873 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3874 h1m->curr_len = h1m->body_len = 0;
3875 }
3876 else if (sl.st.status == 204 || sl.st.status == 304) {
3877 /* no contents, claim c-len is present and set to zero */
3878 h1m->flags &= ~H1_MF_CHNK;
3879 h1m->flags |= H1_MF_CLEN;
3880 h1m->curr_len = h1m->body_len = 0;
3881 }
3882
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003883 chunk_reset(&outbuf);
3884
3885 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003886 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003887 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003888 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003889
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003890 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003891 break;
3892 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003893 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003894 }
3895
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003896 if (outbuf.size < 9)
3897 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003898
3899 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003900 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3901 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3902 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003903
3904 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003905 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003906 /* this is an unparsable response */
3907 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3908 ret = 0;
3909 goto end;
3910 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003911
3912 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003913 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003914 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003915 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003916 }
3917
3918 /* encode all headers, stop at empty name */
3919 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003920 /* these ones do not exist in H2 and must be dropped. */
3921 if (isteq(list[hdr].n, ist("connection")) ||
3922 isteq(list[hdr].n, ist("proxy-connection")) ||
3923 isteq(list[hdr].n, ist("keep-alive")) ||
3924 isteq(list[hdr].n, ist("upgrade")) ||
3925 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003926 continue;
3927
3928 if (isteq(list[hdr].n, ist("")))
3929 break; // end
3930
3931 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3932 /* output full */
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
3939 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003940 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003941 es_now = 1;
3942
3943 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003944 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003945
3946 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003947 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003948
3949 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003950 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003951
3952 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003953 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003954 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003955
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003956 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003957 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003958 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003959
Willy Tarreau801250e2018-09-11 11:45:04 +02003960 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003961 h2s->flags |= H2_SF_ES_SENT;
3962 if (h2s->st == H2_SS_OPEN)
3963 h2s->st = H2_SS_HLOC;
3964 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003965 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003966 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003967 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003968 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003969 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003970 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003971 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003972 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003973 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003974
3975 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003976
3977 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003978 //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 +02003979 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003980 full:
3981 h1m_init_res(h1m);
3982 h1m->err_pos = -1; // don't care about errors on the response path
3983 h2c->flags |= H2_CF_MUX_MFULL;
3984 h2s->flags |= H2_SF_BLK_MROOM;
3985 ret = 0;
3986 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003987}
3988
Willy Tarreau5dd17352018-06-14 13:33:30 +02003989/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3990 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3991 * the number of bytes sent. The caller must check the stream's status to
3992 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003993 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003994static 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 +02003995{
3996 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003997 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003998 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003999 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004000 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004001 int es_now = 0;
4002 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004003 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004004 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004005
4006 if (h2c_mux_busy(h2c, h2s)) {
4007 h2s->flags |= H2_SF_BLK_MBUSY;
4008 goto end;
4009 }
4010
Willy Tarreau44e973f2018-03-01 17:49:30 +01004011 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004012 h2c->flags |= H2_CF_MUX_MALLOC;
4013 h2s->flags |= H2_SF_BLK_MROOM;
4014 goto end;
4015 }
4016
4017 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004018 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004019 goto end;
4020
4021 chunk_reset(&outbuf);
4022
4023 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004024 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004025 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004026 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004027
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004028 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004029 break;
4030 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004031 /* If there are pending data in the output buffer, and we have
4032 * less than 1/4 of the mbuf's size and everything fits, we'll
4033 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4034 * is full and wait, to save some slow realign calls.
4035 */
4036 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4037 h2c->flags |= H2_CF_MUX_MFULL;
4038 h2s->flags |= H2_SF_BLK_MROOM;
4039 goto end;
4040 }
4041
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004042 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004043 }
4044
4045 if (outbuf.size < 9) {
4046 h2c->flags |= H2_CF_MUX_MFULL;
4047 h2s->flags |= H2_SF_BLK_MROOM;
4048 goto end;
4049 }
4050
4051 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004052 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4053 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4054 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004055
4056 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4057 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004058 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004059 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004060 break;
4061 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004062 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004063 if ((long long)size > h1m->curr_len)
4064 size = h1m->curr_len;
4065 break;
4066 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004067 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004068 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004069 if (!ret)
4070 goto end;
4071
4072 if (ret < 0) {
4073 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004074 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004075 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4076 goto end;
4077 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004078 max -= ret;
4079 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004080 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004081 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004082 }
4083
Willy Tarreau801250e2018-09-11 11:45:04 +02004084 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004085 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004086 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004087 if (!ret)
4088 goto end;
4089
4090 if (ret < 0) {
4091 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004092 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004093 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4094 goto end;
4095 }
4096
4097 size = chunk;
4098 h1m->curr_len = chunk;
4099 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004100 max -= ret;
4101 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004102 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004103 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004104 if (!size)
4105 goto send_empty;
4106 }
4107
4108 /* in MSG_DATA state, continue below */
4109 size = h1m->curr_len;
4110 break;
4111 }
4112
4113 /* we have in <size> the exact number of bytes we need to copy from
4114 * the H1 buffer. We need to check this against the connection's and
4115 * the stream's send windows, and to ensure that this fits in the max
4116 * frame size and in the buffer's available space minus 9 bytes (for
4117 * the frame header). The connection's flow control is applied last so
4118 * that we can use a separate list of streams which are immediately
4119 * unblocked on window opening. Note: we don't implement padding.
4120 */
4121
Willy Tarreau5dd17352018-06-14 13:33:30 +02004122 if (size > max)
4123 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004124
4125 if (size > h2s->mws)
4126 size = h2s->mws;
4127
4128 if (size <= 0) {
4129 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004130 if (h2s->send_wait) {
4131 LIST_DEL(&h2s->list);
4132 LIST_INIT(&h2s->list);
4133 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004134 goto end;
4135 }
4136
4137 if (h2c->mfs && size > h2c->mfs)
4138 size = h2c->mfs;
4139
4140 if (size + 9 > outbuf.size) {
4141 /* we have an opportunity for enlarging the too small
4142 * available space, let's try.
4143 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004144 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004145 goto realign_again;
4146 size = outbuf.size - 9;
4147 }
4148
4149 if (size <= 0) {
4150 h2c->flags |= H2_CF_MUX_MFULL;
4151 h2s->flags |= H2_SF_BLK_MROOM;
4152 goto end;
4153 }
4154
4155 if (size > h2c->mws)
4156 size = h2c->mws;
4157
4158 if (size <= 0) {
4159 h2s->flags |= H2_SF_BLK_MFCTL;
4160 goto end;
4161 }
4162
4163 /* copy whatever we can */
4164 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004165 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004166 if (ret == 1)
4167 len2 = 0;
4168
4169 if (!ret || len1 + len2 < size) {
4170 /* FIXME: must normally never happen */
4171 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4172 goto end;
4173 }
4174
4175 /* limit len1/len2 to size */
4176 if (len1 + len2 > size) {
4177 int sub = len1 + len2 - size;
4178
4179 if (len2 > sub)
4180 len2 -= sub;
4181 else {
4182 sub -= len2;
4183 len2 = 0;
4184 len1 -= sub;
4185 }
4186 }
4187
4188 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004189 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004190 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004191 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004192
4193 send_empty:
4194 /* we may need to add END_STREAM */
4195 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4196 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004197 *
4198 * FIXME: what we do here is not correct because we send end_stream
4199 * before knowing if we'll have to send a HEADERS frame for the
4200 * trailers. More importantly we're not consuming the trailing CRLF
4201 * after the end of trailers, so it will be left to the caller to
4202 * eat it. The right way to do it would be to measure trailers here
4203 * and to send ES only if there are no trailers.
4204 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004205 */
4206 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004207 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004208 es_now = 1;
4209
4210 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004211 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004212
4213 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004214 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004215
4216 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004217 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004218
4219 /* consume incoming H1 response */
4220 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004221 max -= size;
4222 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004223 total += size;
4224 h1m->curr_len -= size;
4225 h2s->mws -= size;
4226 h2c->mws -= size;
4227
4228 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004229 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004230 goto new_frame;
4231 }
4232 }
4233
4234 if (es_now) {
4235 if (h2s->st == H2_SS_OPEN)
4236 h2s->st = H2_SS_HLOC;
4237 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004238 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004239
Willy Tarreau35a62702018-02-27 15:37:25 +01004240 if (!(h1m->flags & H1_MF_CHNK)) {
4241 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004242 total += max;
4243 ofs += max;
4244 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004245
Willy Tarreau801250e2018-09-11 11:45:04 +02004246 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004247 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004248
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004249 h2s->flags |= H2_SF_ES_SENT;
4250 }
4251
4252 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004253 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 +02004254 return total;
4255}
4256
Willy Tarreau115e83b2018-12-01 19:17:53 +01004257/* Try to send a HEADERS frame matching HTX response present in HTX message
4258 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4259 * must check the stream's status to detect any error which might have happened
4260 * subsequently to a successful send. The htx blocks are automatically removed
4261 * from the message. The htx message is assumed to be valid since produced from
4262 * the internal code, hence it contains a start line, an optional series of
4263 * header blocks and an end of header, otherwise an invalid frame could be
4264 * emitted and the resulting htx message could be left in an inconsistent state.
4265 */
4266static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4267{
4268 struct http_hdr list[MAX_HTTP_HDR];
4269 struct h2c *h2c = h2s->h2c;
4270 struct htx_blk *blk;
4271 struct htx_blk *blk_end;
4272 struct buffer outbuf;
4273 struct htx_sl *sl;
4274 enum htx_blk_type type;
4275 int es_now = 0;
4276 int ret = 0;
4277 int hdr;
4278 int idx;
4279
4280 if (h2c_mux_busy(h2c, h2s)) {
4281 h2s->flags |= H2_SF_BLK_MBUSY;
4282 return 0;
4283 }
4284
4285 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4286 h2c->flags |= H2_CF_MUX_MALLOC;
4287 h2s->flags |= H2_SF_BLK_MROOM;
4288 return 0;
4289 }
4290
4291 /* determine the first block which must not be deleted, blk_end may
4292 * be NULL if all blocks have to be deleted.
4293 */
4294 idx = htx_get_head(htx);
4295 blk_end = NULL;
4296 while (idx != -1) {
4297 type = htx_get_blk_type(htx_get_blk(htx, idx));
4298 idx = htx_get_next(htx, idx);
4299 if (type == HTX_BLK_EOH) {
4300 if (idx != -1)
4301 blk_end = htx_get_blk(htx, idx);
4302 break;
4303 }
4304 }
4305
4306 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004307 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004308 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004309 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004310 if (h2s->status < 100 || h2s->status > 999)
4311 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004312
4313 /* and the rest of the headers, that we dump starting at header 0 */
4314 hdr = 0;
4315
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004316 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004317 while ((idx = htx_get_next(htx, idx)) != -1) {
4318 blk = htx_get_blk(htx, idx);
4319 type = htx_get_blk_type(blk);
4320
4321 if (type == HTX_BLK_UNUSED)
4322 continue;
4323
4324 if (type != HTX_BLK_HDR)
4325 break;
4326
4327 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4328 goto fail;
4329
4330 list[hdr].n = htx_get_blk_name(htx, blk);
4331 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004332 hdr++;
4333 }
4334
4335 /* marker for end of headers */
4336 list[hdr].n = ist("");
4337
4338 if (h2s->status == 204 || h2s->status == 304) {
4339 /* no contents, claim c-len is present and set to zero */
4340 es_now = 1;
4341 }
4342
4343 chunk_reset(&outbuf);
4344
4345 while (1) {
4346 outbuf.area = b_tail(&h2c->mbuf);
4347 outbuf.size = b_contig_space(&h2c->mbuf);
4348 outbuf.data = 0;
4349
4350 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4351 break;
4352 realign_again:
4353 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4354 }
4355
4356 if (outbuf.size < 9)
4357 goto full;
4358
4359 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4360 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4361 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4362 outbuf.data = 9;
4363
4364 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004365 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004366 if (b_space_wraps(&h2c->mbuf))
4367 goto realign_again;
4368 goto full;
4369 }
4370
4371 /* encode all headers, stop at empty name */
4372 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4373 /* these ones do not exist in H2 and must be dropped. */
4374 if (isteq(list[hdr].n, ist("connection")) ||
4375 isteq(list[hdr].n, ist("proxy-connection")) ||
4376 isteq(list[hdr].n, ist("keep-alive")) ||
4377 isteq(list[hdr].n, ist("upgrade")) ||
4378 isteq(list[hdr].n, ist("transfer-encoding")))
4379 continue;
4380
4381 if (isteq(list[hdr].n, ist("")))
4382 break; // end
4383
4384 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4385 /* output full */
4386 if (b_space_wraps(&h2c->mbuf))
4387 goto realign_again;
4388 goto full;
4389 }
4390 }
4391
Christopher Faulet0b465482019-02-19 15:14:23 +01004392 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004393 * FIXME: we should also set it when we know for sure that the
4394 * content-length is zero as well as on 204/304
4395 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004396 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4397 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004398 es_now = 1;
4399
Willy Tarreau927b88b2019-03-04 08:03:25 +01004400 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004401 es_now = 1;
4402
4403 /* update the frame's size */
4404 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4405
4406 if (es_now)
4407 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4408
4409 /* commit the H2 response */
4410 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004411
4412 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4413 * 1xx responses, another HEADERS frame is expected.
4414 */
4415 if (h2s->status >= 200 || h2s->status == 101)
4416 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004417
Willy Tarreau115e83b2018-12-01 19:17:53 +01004418 if (es_now) {
4419 h2s->flags |= H2_SF_ES_SENT;
4420 if (h2s->st == H2_SS_OPEN)
4421 h2s->st = H2_SS_HLOC;
4422 else
4423 h2s_close(h2s);
4424 }
4425
4426 /* OK we could properly deliver the response */
4427
4428 /* remove all header blocks including the EOH and compute the
4429 * corresponding size.
4430 *
4431 * FIXME: We should remove everything when es_now is set.
4432 */
4433 ret = 0;
4434 idx = htx_get_head(htx);
4435 blk = htx_get_blk(htx, idx);
4436 while (blk != blk_end) {
4437 ret += htx_get_blksz(blk);
4438 blk = htx_remove_blk(htx, blk);
4439 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004440
4441 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4442 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004443 end:
4444 return ret;
4445 full:
4446 h2c->flags |= H2_CF_MUX_MFULL;
4447 h2s->flags |= H2_SF_BLK_MROOM;
4448 ret = 0;
4449 goto end;
4450 fail:
4451 /* unparsable HTX messages, too large ones to be produced in the local
4452 * list etc go here (unrecoverable errors).
4453 */
4454 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4455 ret = 0;
4456 goto end;
4457}
4458
Willy Tarreau80739692018-10-05 11:35:57 +02004459/* Try to send a HEADERS frame matching HTX request present in HTX message
4460 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4461 * must check the stream's status to detect any error which might have happened
4462 * subsequently to a successful send. The htx blocks are automatically removed
4463 * from the message. The htx message is assumed to be valid since produced from
4464 * the internal code, hence it contains a start line, an optional series of
4465 * header blocks and an end of header, otherwise an invalid frame could be
4466 * emitted and the resulting htx message could be left in an inconsistent state.
4467 */
4468static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4469{
4470 struct http_hdr list[MAX_HTTP_HDR];
4471 struct h2c *h2c = h2s->h2c;
4472 struct htx_blk *blk;
4473 struct htx_blk *blk_end;
4474 struct buffer outbuf;
4475 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004476 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004477 enum htx_blk_type type;
4478 int es_now = 0;
4479 int ret = 0;
4480 int hdr;
4481 int idx;
4482
4483 if (h2c_mux_busy(h2c, h2s)) {
4484 h2s->flags |= H2_SF_BLK_MBUSY;
4485 return 0;
4486 }
4487
4488 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4489 h2c->flags |= H2_CF_MUX_MALLOC;
4490 h2s->flags |= H2_SF_BLK_MROOM;
4491 return 0;
4492 }
4493
4494 /* determine the first block which must not be deleted, blk_end may
4495 * be NULL if all blocks have to be deleted.
4496 */
4497 idx = htx_get_head(htx);
4498 blk_end = NULL;
4499 while (idx != -1) {
4500 type = htx_get_blk_type(htx_get_blk(htx, idx));
4501 idx = htx_get_next(htx, idx);
4502 if (type == HTX_BLK_EOH) {
4503 if (idx != -1)
4504 blk_end = htx_get_blk(htx, idx);
4505 break;
4506 }
4507 }
4508
4509 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004510 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004511 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004512 meth = htx_sl_req_meth(sl);
4513 path = htx_sl_req_uri(sl);
4514
4515 /* and the rest of the headers, that we dump starting at header 0 */
4516 hdr = 0;
4517
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004518 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004519 while ((idx = htx_get_next(htx, idx)) != -1) {
4520 blk = htx_get_blk(htx, idx);
4521 type = htx_get_blk_type(blk);
4522
4523 if (type == HTX_BLK_UNUSED)
4524 continue;
4525
4526 if (type != HTX_BLK_HDR)
4527 break;
4528
4529 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4530 goto fail;
4531
4532 list[hdr].n = htx_get_blk_name(htx, blk);
4533 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004534 hdr++;
4535 }
4536
4537 /* marker for end of headers */
4538 list[hdr].n = ist("");
4539
4540 chunk_reset(&outbuf);
4541
4542 while (1) {
4543 outbuf.area = b_tail(&h2c->mbuf);
4544 outbuf.size = b_contig_space(&h2c->mbuf);
4545 outbuf.data = 0;
4546
4547 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4548 break;
4549 realign_again:
4550 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4551 }
4552
4553 if (outbuf.size < 9)
4554 goto full;
4555
4556 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4557 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4558 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4559 outbuf.data = 9;
4560
4561 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004562 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004563 if (b_space_wraps(&h2c->mbuf))
4564 goto realign_again;
4565 goto full;
4566 }
4567
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004568 /* RFC7540 #8.3: the CONNECT method must have :
4569 * - :authority set to the URI part (host:port)
4570 * - :method set to CONNECT
4571 * - :scheme and :path omitted
4572 */
4573 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4574 /* encode the scheme which is always "https" (or 0x86 for "http") */
4575 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4576 /* output full */
4577 if (b_space_wraps(&h2c->mbuf))
4578 goto realign_again;
4579 goto full;
4580 }
Willy Tarreau80739692018-10-05 11:35:57 +02004581
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004582 /* encode the path, which necessarily is the second one */
4583 if (!hpack_encode_path(&outbuf, path)) {
4584 /* output full */
4585 if (b_space_wraps(&h2c->mbuf))
4586 goto realign_again;
4587 goto full;
4588 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004589
4590 /* look for the Host header and place it in :authority */
4591 auth = ist2(NULL, 0);
4592 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4593 if (isteq(list[hdr].n, ist("")))
4594 break; // end
4595
4596 if (isteq(list[hdr].n, ist("host"))) {
4597 auth = list[hdr].v;
4598 break;
4599 }
4600 }
4601 }
4602 else {
4603 /* for CONNECT, :authority is taken from the path */
4604 auth = path;
4605 }
4606
4607 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4608 /* output full */
4609 if (b_space_wraps(&h2c->mbuf))
4610 goto realign_again;
4611 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004612 }
4613
4614 /* encode all headers, stop at empty name */
4615 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4616 /* these ones do not exist in H2 and must be dropped. */
4617 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004618 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004619 isteq(list[hdr].n, ist("proxy-connection")) ||
4620 isteq(list[hdr].n, ist("keep-alive")) ||
4621 isteq(list[hdr].n, ist("upgrade")) ||
4622 isteq(list[hdr].n, ist("transfer-encoding")))
4623 continue;
4624
4625 if (isteq(list[hdr].n, ist("")))
4626 break; // end
4627
4628 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4629 /* output full */
4630 if (b_space_wraps(&h2c->mbuf))
4631 goto realign_again;
4632 goto full;
4633 }
4634 }
4635
4636 /* we may need to add END_STREAM if we have no body :
4637 * - request already closed, or :
4638 * - no transfer-encoding, and :
4639 * - no content-length or content-length:0
4640 * Fixme: this doesn't take into account CONNECT requests.
4641 */
4642 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4643 es_now = 1;
4644
4645 if (sl->flags & HTX_SL_F_BODYLESS)
4646 es_now = 1;
4647
Willy Tarreau927b88b2019-03-04 08:03:25 +01004648 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004649 es_now = 1;
4650
4651 /* update the frame's size */
4652 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4653
4654 if (es_now)
4655 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4656
4657 /* commit the H2 response */
4658 b_add(&h2c->mbuf, outbuf.data);
4659 h2s->flags |= H2_SF_HEADERS_SENT;
4660 h2s->st = H2_SS_OPEN;
4661
Willy Tarreau80739692018-10-05 11:35:57 +02004662 if (es_now) {
4663 // trim any possibly pending data (eg: inconsistent content-length)
4664 h2s->flags |= H2_SF_ES_SENT;
4665 h2s->st = H2_SS_HLOC;
4666 }
4667
4668 /* remove all header blocks including the EOH and compute the
4669 * corresponding size.
4670 *
4671 * FIXME: We should remove everything when es_now is set.
4672 */
4673 ret = 0;
4674 idx = htx_get_head(htx);
4675 blk = htx_get_blk(htx, idx);
4676 while (blk != blk_end) {
4677 ret += htx_get_blksz(blk);
4678 blk = htx_remove_blk(htx, blk);
4679 }
4680
4681 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4682 htx_remove_blk(htx, blk_end);
4683
4684 end:
4685 return ret;
4686 full:
4687 h2c->flags |= H2_CF_MUX_MFULL;
4688 h2s->flags |= H2_SF_BLK_MROOM;
4689 ret = 0;
4690 goto end;
4691 fail:
4692 /* unparsable HTX messages, too large ones to be produced in the local
4693 * list etc go here (unrecoverable errors).
4694 */
4695 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4696 ret = 0;
4697 goto end;
4698}
4699
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004700/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004701 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4702 * caller must check the stream's status to detect any error which might have
4703 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004704 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4705 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004706static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004707{
4708 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004709 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004710 struct buffer outbuf;
4711 size_t total = 0;
4712 int es_now = 0;
4713 int bsize; /* htx block size */
4714 int fsize; /* h2 frame size */
4715 struct htx_blk *blk;
4716 enum htx_blk_type type;
4717 int idx;
4718
4719 if (h2c_mux_busy(h2c, h2s)) {
4720 h2s->flags |= H2_SF_BLK_MBUSY;
4721 goto end;
4722 }
4723
4724 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4725 h2c->flags |= H2_CF_MUX_MALLOC;
4726 h2s->flags |= H2_SF_BLK_MROOM;
4727 goto end;
4728 }
4729
Willy Tarreau98de12a2018-12-12 07:03:00 +01004730 htx = htx_from_buf(buf);
4731
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004732 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4733 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4734 * the caller to handle.
4735 */
4736
4737 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004738 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004739 goto end;
4740
4741 idx = htx_get_head(htx);
4742 blk = htx_get_blk(htx, idx);
4743 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4744 bsize = htx_get_blksz(blk);
4745 fsize = bsize;
4746
4747 if (type == HTX_BLK_EOD) {
4748 /* if we have an EOD, we're dealing with chunked data. We may
4749 * have a set of trailers after us that the caller will want to
4750 * deal with. Let's simply remove the EOD and return.
4751 */
4752 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004753 total++; // EOD counts as one byte
4754 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004755 goto end;
4756 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004757 else if (type == HTX_BLK_EOM) {
4758 if (h2s->flags & H2_SF_ES_SENT) {
4759 /* ES already sent */
4760 htx_remove_blk(htx, blk);
4761 total++; // EOM counts as one byte
4762 count--;
4763 goto end;
4764 }
4765 }
4766 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004767 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004768
4769 /* Perform some optimizations to reduce the number of buffer copies.
4770 * First, if the mux's buffer is empty and the htx area contains
4771 * exactly one data block of the same size as the requested count, and
4772 * this count fits within the frame size, the stream's window size, and
4773 * the connection's window size, then it's possible to simply swap the
4774 * caller's buffer with the mux's output buffer and adjust offsets and
4775 * length to match the entire DATA HTX block in the middle. In this
4776 * case we perform a true zero-copy operation from end-to-end. This is
4777 * the situation that happens all the time with large files. Second, if
4778 * this is not possible, but the mux's output buffer is empty, we still
4779 * have an opportunity to avoid the copy to the intermediary buffer, by
4780 * making the intermediary buffer's area point to the output buffer's
4781 * area. In this case we want to skip the HTX header to make sure that
4782 * copies remain aligned and that this operation remains possible all
4783 * the time. This goes for headers, data blocks and any data extracted
4784 * from the HTX blocks.
4785 */
4786 if (unlikely(fsize == count &&
4787 htx->used == 1 && type == HTX_BLK_DATA &&
4788 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4789 void *old_area = h2c->mbuf.area;
4790
4791 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004792 /* Too bad there are data left there. We're willing to memcpy/memmove
4793 * up to 1/4 of the buffer, which means that it's OK to copy a large
4794 * frame into a buffer containing few data if it needs to be realigned,
4795 * and that it's also OK to copy few data without realigning. Otherwise
4796 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004797 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004798 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4799 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4800 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004801 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004802
Willy Tarreau98de12a2018-12-12 07:03:00 +01004803 h2c->flags |= H2_CF_MUX_MFULL;
4804 h2s->flags |= H2_SF_BLK_MROOM;
4805 goto end;
4806 }
4807
4808 /* map an H2 frame to the HTX block so that we can put the
4809 * frame header there.
4810 */
4811 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004812 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004813 h2c->mbuf.data = fsize + 9;
4814 outbuf.area = b_head(&h2c->mbuf);
4815
4816 /* prepend an H2 DATA frame header just before the DATA block */
4817 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4818 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4819 h2_set_frame_size(outbuf.area, fsize);
4820
4821 /* update windows */
4822 h2s->mws -= fsize;
4823 h2c->mws -= fsize;
4824
4825 /* and exchange with our old area */
4826 buf->area = old_area;
4827 buf->data = buf->head = 0;
4828 total += fsize;
4829 goto end;
4830 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004831
Willy Tarreau98de12a2018-12-12 07:03:00 +01004832 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004833 /* for DATA and EOM we'll have to emit a frame, even if empty */
4834
4835 while (1) {
4836 outbuf.area = b_tail(&h2c->mbuf);
4837 outbuf.size = b_contig_space(&h2c->mbuf);
4838 outbuf.data = 0;
4839
4840 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4841 break;
4842 realign_again:
4843 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4844 }
4845
4846 if (outbuf.size < 9) {
4847 h2c->flags |= H2_CF_MUX_MFULL;
4848 h2s->flags |= H2_SF_BLK_MROOM;
4849 goto end;
4850 }
4851
4852 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4853 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4854 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4855 outbuf.data = 9;
4856
4857 /* we have in <fsize> the exact number of bytes we need to copy from
4858 * the HTX buffer. We need to check this against the connection's and
4859 * the stream's send windows, and to ensure that this fits in the max
4860 * frame size and in the buffer's available space minus 9 bytes (for
4861 * the frame header). The connection's flow control is applied last so
4862 * that we can use a separate list of streams which are immediately
4863 * unblocked on window opening. Note: we don't implement padding.
4864 */
4865
4866 /* EOM is presented with bsize==1 but would lead to the emission of an
4867 * empty frame, thus we force it to zero here.
4868 */
4869 if (type == HTX_BLK_EOM)
4870 bsize = fsize = 0;
4871
4872 if (!fsize)
4873 goto send_empty;
4874
4875 if (h2s->mws <= 0) {
4876 h2s->flags |= H2_SF_BLK_SFCTL;
4877 if (h2s->send_wait) {
4878 LIST_DEL(&h2s->list);
4879 LIST_INIT(&h2s->list);
4880 }
4881 goto end;
4882 }
4883
Willy Tarreauee573762018-12-04 15:25:57 +01004884 if (fsize > count)
4885 fsize = count;
4886
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004887 if (fsize > h2s->mws)
4888 fsize = h2s->mws; // >0
4889
4890 if (h2c->mfs && fsize > h2c->mfs)
4891 fsize = h2c->mfs; // >0
4892
4893 if (fsize + 9 > outbuf.size) {
4894 /* we have an opportunity for enlarging the too small
4895 * available space, let's try.
4896 * FIXME: is this really interesting to do? Maybe we'll
4897 * spend lots of time realigning instead of using two
4898 * frames.
4899 */
4900 if (b_space_wraps(&h2c->mbuf))
4901 goto realign_again;
4902 fsize = outbuf.size - 9;
4903
4904 if (fsize <= 0) {
4905 /* no need to send an empty frame here */
4906 h2c->flags |= H2_CF_MUX_MFULL;
4907 h2s->flags |= H2_SF_BLK_MROOM;
4908 goto end;
4909 }
4910 }
4911
4912 if (h2c->mws <= 0) {
4913 h2s->flags |= H2_SF_BLK_MFCTL;
4914 goto end;
4915 }
4916
4917 if (fsize > h2c->mws)
4918 fsize = h2c->mws;
4919
4920 /* now let's copy this this into the output buffer */
4921 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004922 h2s->mws -= fsize;
4923 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004924 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004925
4926 send_empty:
4927 /* update the frame's size */
4928 h2_set_frame_size(outbuf.area, fsize);
4929
4930 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4931 * meeting EOM. We should optimize this later.
4932 */
4933 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004934 total++; // EOM counts as one byte
4935 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004936 es_now = 1;
4937 }
4938
4939 if (es_now)
4940 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4941
4942 /* commit the H2 response */
4943 b_add(&h2c->mbuf, fsize + 9);
4944
4945 /* consume incoming HTX block, including EOM */
4946 total += fsize;
4947 if (fsize == bsize) {
4948 htx_remove_blk(htx, blk);
4949 if (fsize)
4950 goto new_frame;
4951 } else {
4952 /* we've truncated this block */
4953 htx_cut_data_blk(htx, blk, fsize);
4954 }
4955
4956 if (es_now) {
4957 if (h2s->st == H2_SS_OPEN)
4958 h2s->st = H2_SS_HLOC;
4959 else
4960 h2s_close(h2s);
4961
4962 h2s->flags |= H2_SF_ES_SENT;
4963 }
4964
4965 end:
4966 return total;
4967}
4968
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004969/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4970 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4971 * processed. The caller must check the stream's status to detect any error
4972 * which might have happened subsequently to a successful send. The htx blocks
4973 * are automatically removed from the message. The htx message is assumed to be
4974 * valid since produced from the internal code. Processing stops when meeting
4975 * the EOM, which is also removed. All trailers are processed at once and sent
4976 * as a single frame. The ES flag is always set.
4977 */
4978static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4979{
4980 struct http_hdr list[MAX_HTTP_HDR];
4981 struct h2c *h2c = h2s->h2c;
4982 struct htx_blk *blk;
4983 struct htx_blk *blk_end;
4984 struct buffer outbuf;
4985 struct h1m h1m;
4986 enum htx_blk_type type;
4987 uint32_t size;
4988 int ret = 0;
4989 int hdr;
4990 int idx;
4991 void *start;
4992
4993 if (h2c_mux_busy(h2c, h2s)) {
4994 h2s->flags |= H2_SF_BLK_MBUSY;
4995 goto end;
4996 }
4997
4998 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4999 h2c->flags |= H2_CF_MUX_MALLOC;
5000 h2s->flags |= H2_SF_BLK_MROOM;
5001 goto end;
5002 }
5003
5004 /* The principle is that we parse each and every trailers block using
5005 * the H1 headers parser, and append it to the list. We don't proceed
5006 * until EOM is met. blk_end will point to the EOM block.
5007 */
5008 hdr = 0;
5009 memset(list, 0, sizeof(list));
5010 blk_end = NULL;
5011
5012 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5013 blk = htx_get_blk(htx, idx);
5014 type = htx_get_blk_type(blk);
5015
5016 if (type == HTX_BLK_UNUSED)
5017 continue;
5018
5019 if (type != HTX_BLK_TLR) {
5020 if (type == HTX_BLK_EOM)
5021 blk_end = blk;
5022 break;
5023 }
5024
5025 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5026 goto fail;
5027
5028 size = htx_get_blksz(blk);
5029 start = htx_get_blk_ptr(htx, blk);
5030
5031 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5032 h1m.err_pos = 0;
5033 ret = h1_headers_to_hdr_list(start, start + size,
5034 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5035 &h1m, NULL);
5036 if (ret < 0)
5037 goto fail;
5038
5039 /* ret == 0 if an incomplete trailers block was found (missing
5040 * empty line), or > 0 if it was found. We have to continue on
5041 * incomplete messages because the trailers block might be
5042 * incomplete.
5043 */
5044
5045 /* search the new end */
5046 while (hdr <= sizeof(list)/sizeof(list[0])) {
5047 if (!list[hdr].n.len)
5048 break;
5049 hdr++;
5050 }
5051 }
5052
5053 if (!blk_end)
5054 goto end; // end not found yet
5055
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005056 chunk_reset(&outbuf);
5057
5058 while (1) {
5059 outbuf.area = b_tail(&h2c->mbuf);
5060 outbuf.size = b_contig_space(&h2c->mbuf);
5061 outbuf.data = 0;
5062
5063 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5064 break;
5065 realign_again:
5066 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5067 }
5068
5069 if (outbuf.size < 9)
5070 goto full;
5071
5072 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5073 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5074 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5075 outbuf.data = 9;
5076
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005077 /* encode all headers */
5078 for (idx = 0; idx < hdr; idx++) {
5079 /* these ones do not exist in H2 or must not appear in
5080 * trailers and must be dropped.
5081 */
5082 if (isteq(list[idx].n, ist("host")) ||
5083 isteq(list[idx].n, ist("content-length")) ||
5084 isteq(list[idx].n, ist("connection")) ||
5085 isteq(list[idx].n, ist("proxy-connection")) ||
5086 isteq(list[idx].n, ist("keep-alive")) ||
5087 isteq(list[idx].n, ist("upgrade")) ||
5088 isteq(list[idx].n, ist("te")) ||
5089 isteq(list[idx].n, ist("transfer-encoding")))
5090 continue;
5091
5092 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5093 /* output full */
5094 if (b_space_wraps(&h2c->mbuf))
5095 goto realign_again;
5096 goto full;
5097 }
5098 }
5099
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005100 if (!hdr) {
5101 /* here we have a problem, we've received an empty trailers
5102 * block followed by an EOM. Because of this we can't send a
5103 * HEADERS frame, so we have to cheat and instead send an empty
5104 * DATA frame conveying the ES flag.
5105 */
5106 outbuf.area[3] = H2_FT_DATA;
5107 outbuf.area[4] = H2_F_DATA_END_STREAM;
5108 }
5109
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005110 /* update the frame's size */
5111 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5112
5113 /* commit the H2 response */
5114 b_add(&h2c->mbuf, outbuf.data);
5115 h2s->flags |= H2_SF_ES_SENT;
5116
5117 if (h2s->st == H2_SS_OPEN)
5118 h2s->st = H2_SS_HLOC;
5119 else
5120 h2s_close(h2s);
5121
5122 /* OK we could properly deliver the response */
5123 done:
5124 /* remove all header blocks including EOM and compute the corresponding size. */
5125 ret = 0;
5126 idx = htx_get_head(htx);
5127 blk = htx_get_blk(htx, idx);
5128 while (blk != blk_end) {
5129 ret += htx_get_blksz(blk);
5130 blk = htx_remove_blk(htx, blk);
5131 }
5132 blk = htx_remove_blk(htx, blk);
5133 end:
5134 return ret;
5135 full:
5136 h2c->flags |= H2_CF_MUX_MFULL;
5137 h2s->flags |= H2_SF_BLK_MROOM;
5138 ret = 0;
5139 goto end;
5140 fail:
5141 /* unparsable HTX messages, too large ones to be produced in the local
5142 * list etc go here (unrecoverable errors).
5143 */
5144 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5145 ret = 0;
5146 goto end;
5147}
5148
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005149/* Called from the upper layer, to subscribe to events, such as being able to send.
5150 * The <param> argument here is supposed to be a pointer to a wait_event struct
5151 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5152 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5153 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5154 * returns 0 except for the error above.
5155 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005156static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5157{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005158 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005159 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005160 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005161
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005162 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005163 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005164 if (!(sw->events & SUB_RETRY_RECV)) {
5165 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005166 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005167 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005168 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005169 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005170 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005171 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005172 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005173 if (!(sw->events & SUB_RETRY_SEND)) {
5174 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005175 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005176 h2s->send_wait = sw;
Olivier Houchard9a0f5592019-04-15 19:22:24 +02005177 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5178 LIST_ISEMPTY(&h2s->list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005179 if (h2s->flags & H2_SF_BLK_MFCTL)
5180 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5181 else
5182 LIST_ADDQ(&h2c->send_list, &h2s->list);
5183 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005184 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005185 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005186 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005187 if (event_type != 0)
5188 return -1;
5189 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005190}
5191
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005192/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5193 * The <param> argument here is supposed to be a pointer to the same wait_event
5194 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5195 * It always returns zero.
5196 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005197static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5198{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005199 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005200 struct h2s *h2s = cs->ctx;
5201
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005202 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005203 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005204 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005205 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005206 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005207 }
5208 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005209 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005210 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005211 if (h2s->send_wait == sw) {
5212 LIST_DEL(&h2s->list);
5213 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005214 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard998410a2019-04-15 19:23:37 +02005215 /* We were about to send, make sure it does not happen */
5216 if (!LIST_ISEMPTY(&h2s->sending_list) &&
5217 h2s->send_wait != &h2s->wait_event) {
5218 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5219 LIST_DEL_INIT(&h2s->sending_list);
5220 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005221 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02005222
Olivier Houchardd846c262018-10-19 17:24:29 +02005223 }
5224 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005225 return 0;
5226}
5227
5228
Olivier Houchard511efea2018-08-16 15:30:32 +02005229/* Called from the upper layer, to receive data */
5230static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5231{
Olivier Houchard638b7992018-08-16 15:41:52 +02005232 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005233 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005234 struct htx *h2s_htx = NULL;
5235 struct htx *buf_htx = NULL;
5236 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005237 size_t ret = 0;
5238
5239 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005240 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5241 /* in HTX mode we ignore the count argument */
5242 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005243 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005244 /* Here htx_to_buf() will set buffer data to 0 because
5245 * the HTX is empty.
5246 */
5247 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005248 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005249 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005250
5251 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005252 count = htx_free_data_space(buf_htx);
5253 if (flags & CO_RFL_KEEP_RSV) {
5254 if (count <= global.tune.maxrewrite)
5255 goto end;
5256 count -= global.tune.maxrewrite;
5257 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005258
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005259 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005260
5261 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5262 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5263
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005264 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005265 htx_to_buf(buf_htx, buf);
5266 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005267 ret = htx_ret.ret;
5268 }
5269 else {
5270 ret = b_xfer(buf, &h2s->rxbuf, count);
5271 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005272
Christopher Faulet37070b22019-02-14 15:12:14 +01005273 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005274 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005275 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005276 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005277 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005278 if (cs->flags & CS_FL_REOS)
5279 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005280 if (cs->flags & CS_FL_ERR_PENDING)
5281 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005282 if (b_size(&h2s->rxbuf)) {
5283 b_free(&h2s->rxbuf);
5284 offer_buffers(NULL, tasks_run_queue);
5285 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005286 }
5287
Willy Tarreau082f5592018-11-25 08:03:32 +01005288 if (ret && h2c->dsi == h2s->id) {
5289 /* demux is blocking on this stream's buffer */
5290 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005291 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005292 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005293
Olivier Houchard511efea2018-08-16 15:30:32 +02005294 return ret;
5295}
5296
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005297/* stops all senders of this connection for example when the mux buffer is full.
5298 * They are moved from the sending_list to either fctl_list or send_list.
5299 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005300static void h2_stop_senders(struct h2c *h2c)
5301{
5302 struct h2s *h2s, *h2s_back;
5303
Olivier Houchardd360ac62019-03-22 17:37:16 +01005304 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5305 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005306 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005307 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005308 }
5309}
5310
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005311/* Called from the upper layer, to send data from buffer <buf> for no more than
5312 * <count> bytes. Returns the number of bytes effectively sent. Some status
5313 * flags may be updated on the conn_stream.
5314 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005315static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005316{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005317 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005318 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005319 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005320 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005321 struct htx *htx;
5322 struct htx_blk *blk;
5323 enum htx_blk_type btype;
5324 uint32_t bsize;
5325 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005326
Olivier Houchardd360ac62019-03-22 17:37:16 +01005327 /* If we were not just woken because we wanted to send but couldn't,
5328 * and there's somebody else that is waiting to send, do nothing,
5329 * we will subscribe later and be put at the end of the list
5330 */
Olivier Houchard998410a2019-04-15 19:23:37 +02005331 if (LIST_ISEMPTY(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005332 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5333 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005334 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005335
Olivier Houchard998410a2019-04-15 19:23:37 +02005336 /* We couldn't set it to NULL before, because we needed it in case
5337 * we had to cancel the tasklet
5338 */
5339 h2s->send_wait = NULL;
5340
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005341 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5342 return 0;
5343
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005344 /* htx will be enough to decide if we're using HTX or legacy */
5345 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5346
Willy Tarreau0bad0432018-06-14 16:54:01 +02005347 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005348 h2s->flags |= H2_SF_OUTGOING_DATA;
5349
Willy Tarreau751f2d02018-10-05 09:35:00 +02005350 if (h2s->id == 0) {
5351 int32_t id = h2c_get_next_sid(h2s->h2c);
5352
5353 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005354 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005355 return 0;
5356 }
5357
5358 eb32_delete(&h2s->by_id);
5359 h2s->by_id.key = h2s->id = id;
5360 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005361 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005362 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5363 }
5364
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005365 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005366 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5367 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005368 idx = htx_get_head(htx);
5369 blk = htx_get_blk(htx, idx);
5370 btype = htx_get_blk_type(blk);
5371 bsize = htx_get_blksz(blk);
5372
5373 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005374 case HTX_BLK_REQ_SL:
5375 /* start-line before headers */
5376 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5377 if (ret > 0) {
5378 total += ret;
5379 count -= ret;
5380 if (ret < bsize)
5381 goto done;
5382 }
5383 break;
5384
Willy Tarreau115e83b2018-12-01 19:17:53 +01005385 case HTX_BLK_RES_SL:
5386 /* start-line before headers */
5387 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5388 if (ret > 0) {
5389 total += ret;
5390 count -= ret;
5391 if (ret < bsize)
5392 goto done;
5393 }
5394 break;
5395
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005396 case HTX_BLK_DATA:
5397 case HTX_BLK_EOD:
5398 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005399 /* all these cause the emission of a DATA frame (possibly empty).
5400 * This EOM necessarily is one before trailers, as the EOM following
5401 * trailers would have been consumed by the trailers parser.
5402 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005403 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005404 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005405 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005406 total += ret;
5407 count -= ret;
5408 if (ret < bsize)
5409 goto done;
5410 }
5411 break;
5412
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005413 case HTX_BLK_TLR:
5414 /* This is the first trailers block, all the subsequent ones AND
5415 * the EOM will be swallowed by the parser.
5416 */
5417 ret = h2s_htx_make_trailers(h2s, htx);
5418 if (ret > 0) {
5419 total += ret;
5420 count -= ret;
5421 if (ret < bsize)
5422 goto done;
5423 }
5424 break;
5425
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005426 default:
5427 htx_remove_blk(htx, blk);
5428 total += bsize;
5429 count -= bsize;
5430 break;
5431 }
5432 }
5433 goto done;
5434 }
5435
5436 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005437 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005438 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005439 if (h2s->h2c->flags & H2_CF_IS_BACK)
5440 ret = -1;
5441 else
5442 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005443 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005444 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005445 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005446 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005447 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005448 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005449 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005450
Willy Tarreau5dd17352018-06-14 13:33:30 +02005451 if (unlikely((int)ret <= 0)) {
5452 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005453 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5454 break;
5455 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005456 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005457 total += count;
5458 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005459 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005460 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005461 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005462 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005463 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005464 break;
5465 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005466
5467 total += ret;
5468 count -= ret;
5469
5470 if (h2s->st >= H2_SS_ERROR)
5471 break;
5472
5473 if (h2s->flags & H2_SF_BLK_ANY)
5474 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005475 }
5476
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005477 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005478 if (h2s->st >= H2_SS_ERROR) {
5479 /* trim any possibly pending data after we close (extra CR-LF,
5480 * unprocessed trailers, abnormal extra data, ...)
5481 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005482 total += count;
5483 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005484 }
5485
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005486 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005487 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005488 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005489 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005490 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005491 }
5492
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005493 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005494 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005495 } else {
5496 b_del(buf, total);
5497 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005498
5499 /* The mux is full, cancel the pending tasks */
5500 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5501 (h2s->flags & H2_SF_BLK_MBUSY))
5502 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005503
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005504 /* If we're running HTX, and we read the whole buffer, then pretend
5505 * we read exactly what the caller specified, as with HTX the caller
5506 * will always give the buffer size, instead of the amount of data
5507 * available.
5508 */
5509 if (htx && !b_data(buf))
5510 total = orig_count;
5511
Olivier Houchard7505f942018-08-21 18:10:44 +02005512 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005513 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005514 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005515
Olivier Houchard7505f942018-08-21 18:10:44 +02005516 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005517 /* If we're waiting for flow control, and we got a shutr on the
5518 * connection, we will never be unlocked, so add an error on
5519 * the conn_stream.
5520 */
5521 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5522 !b_data(&h2s->h2c->dbuf) &&
5523 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5524 if (cs->flags & CS_FL_EOS)
5525 cs->flags |= CS_FL_ERROR;
5526 else
5527 cs->flags |= CS_FL_ERR_PENDING;
5528 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005529 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005530 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005531 LIST_DEL_INIT(&h2s->list);
5532 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005533 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005534}
5535
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005536/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005537static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005538{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005539 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005540 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005541 struct eb32_node *node;
5542 int fctl_cnt = 0;
5543 int send_cnt = 0;
5544 int tree_cnt = 0;
5545 int orph_cnt = 0;
5546
5547 if (!h2c)
5548 return;
5549
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005550 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005551 fctl_cnt++;
5552
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005553 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005554 send_cnt++;
5555
Willy Tarreau3af37712018-12-18 14:34:41 +01005556 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005557 node = eb32_first(&h2c->streams_by_id);
5558 while (node) {
5559 h2s = container_of(node, struct h2s, by_id);
5560 tree_cnt++;
5561 if (!h2s->cs)
5562 orph_cnt++;
5563 node = eb32_next(node);
5564 }
5565
Willy Tarreau987c0632018-12-18 10:32:05 +01005566 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5567 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5568 " .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 +02005569 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5570 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005571 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005572 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5573 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5574 h2c->msi,
5575 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5576 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5577
5578 if (h2s) {
5579 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5580 h2s, h2s->id, h2s->flags,
5581 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5582 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5583 h2s->cs);
5584 if (h2s->cs)
5585 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5586 h2s->cs->flags, h2s->cs->data);
5587 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005588}
Willy Tarreau62f52692017-10-08 23:01:42 +02005589
5590/*******************************************************/
5591/* functions below are dedicated to the config parsers */
5592/*******************************************************/
5593
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005594/* config parser for global "tune.h2.header-table-size" */
5595static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5596 struct proxy *defpx, const char *file, int line,
5597 char **err)
5598{
5599 if (too_many_args(1, args, err, NULL))
5600 return -1;
5601
5602 h2_settings_header_table_size = atoi(args[1]);
5603 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5604 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5605 return -1;
5606 }
5607 return 0;
5608}
Willy Tarreau62f52692017-10-08 23:01:42 +02005609
Willy Tarreaue6baec02017-07-27 11:45:11 +02005610/* config parser for global "tune.h2.initial-window-size" */
5611static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5612 struct proxy *defpx, const char *file, int line,
5613 char **err)
5614{
5615 if (too_many_args(1, args, err, NULL))
5616 return -1;
5617
5618 h2_settings_initial_window_size = atoi(args[1]);
5619 if (h2_settings_initial_window_size < 0) {
5620 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5621 return -1;
5622 }
5623 return 0;
5624}
5625
Willy Tarreau5242ef82017-07-27 11:47:28 +02005626/* config parser for global "tune.h2.max-concurrent-streams" */
5627static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5628 struct proxy *defpx, const char *file, int line,
5629 char **err)
5630{
5631 if (too_many_args(1, args, err, NULL))
5632 return -1;
5633
5634 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005635 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005636 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5637 return -1;
5638 }
5639 return 0;
5640}
5641
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005642/* config parser for global "tune.h2.max-frame-size" */
5643static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5644 struct proxy *defpx, const char *file, int line,
5645 char **err)
5646{
5647 if (too_many_args(1, args, err, NULL))
5648 return -1;
5649
5650 h2_settings_max_frame_size = atoi(args[1]);
5651 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5652 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5653 return -1;
5654 }
5655 return 0;
5656}
5657
Willy Tarreau62f52692017-10-08 23:01:42 +02005658
5659/****************************************/
5660/* MUX initialization and instanciation */
5661/***************************************/
5662
5663/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005664static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005665 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005666 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005667 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005668 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005669 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005670 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005671 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005672 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005673 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005674 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005675 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005676 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005677 .shutr = h2_shutr,
5678 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005679 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005680 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005681 .name = "H2",
5682};
5683
Christopher Faulet32f61c02018-04-10 14:33:41 +02005684/* PROTO selection : this mux registers PROTO token "h2" */
5685static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005686 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005687
Willy Tarreau0108d902018-11-25 19:14:37 +01005688INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5689
Christopher Faulet9b579102019-04-11 22:52:25 +02005690
5691/* The mux operations */
5692static const struct mux_ops h2_htx_ops = {
5693 .init = h2_init,
5694 .wake = h2_wake,
5695 .snd_buf = h2_snd_buf,
5696 .rcv_buf = h2_rcv_buf,
5697 .subscribe = h2_subscribe,
5698 .unsubscribe = h2_unsubscribe,
5699 .attach = h2_attach,
5700 .get_first_cs = h2_get_first_cs,
5701 .detach = h2_detach,
5702 .destroy = h2_destroy,
5703 .avail_streams = h2_avail_streams,
5704 .used_streams = h2_used_streams,
5705 .shutr = h2_shutr,
5706 .shutw = h2_shutw,
5707 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005708 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005709 .name = "H2",
5710};
5711
Willy Tarreauf8957272018-10-03 10:25:20 +02005712static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005713 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005714
5715INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5716
Willy Tarreau62f52692017-10-08 23:01:42 +02005717/* config keyword parsers */
5718static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005719 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005720 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005721 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005722 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005723 { 0, NULL, NULL }
5724}};
5725
Willy Tarreau0108d902018-11-25 19:14:37 +01005726INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);