blob: 48876ee20db70119303f7cf6aa5c59ecdd5e94b0 [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 Faulet73c12072019-04-08 11:23:22 +0200622 struct connection *conn = h2c->conn;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200623
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200624 /* The connection was attached to another mux (unexpected but safer to
625 * check) */
626 if (conn && conn->ctx != h2c)
627 conn = NULL;
628
Willy Tarreau32218eb2017-09-22 08:07:25 +0200629 if (h2c) {
630 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200631
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100632 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100633 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100634 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200635
Willy Tarreau44e973f2018-03-01 17:49:30 +0100636 h2_release_buf(h2c, &h2c->dbuf);
637 h2_release_buf(h2c, &h2c->mbuf);
638
Willy Tarreauea392822017-10-31 10:02:25 +0100639 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200640 h2c->task->context = NULL;
641 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100642 h2c->task = NULL;
643 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200644 if (h2c->wait_event.task)
645 tasklet_free(h2c->wait_event.task);
Willy Tarreauea392822017-10-31 10:02:25 +0100646
Willy Tarreaubafbe012017-11-24 17:34:44 +0100647 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200648 }
649
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200650 if (conn) {
651 conn->mux = NULL;
652 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200653
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200654 conn_force_unsubscribe(conn);
655 conn_stop_tracking(conn);
656 conn_full_close(conn);
657 if (conn->destroy_cb)
658 conn->destroy_cb(conn);
659 conn_free(conn);
660 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200661}
662
663
Willy Tarreau71681172017-10-23 14:39:06 +0200664/******************************************************/
665/* functions below are for the H2 protocol processing */
666/******************************************************/
667
668/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100669static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200670{
671 return h2s ? h2s->id : 0;
672}
673
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200674/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100675static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200676{
677 if (h2c->msi < 0)
678 return 0;
679
680 if (h2c->msi == h2s_id(h2s))
681 return 0;
682
683 return 1;
684}
685
Willy Tarreau741d6df2017-10-17 08:00:59 +0200686/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100687static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200688{
689 h2c->errcode = err;
690 h2c->st0 = H2_CS_ERROR;
691}
692
Willy Tarreau175cebb2019-01-24 10:02:24 +0100693/* marks an error on the stream. It may also update an already closed stream
694 * (e.g. to report an error after an RST was received).
695 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100696static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200697{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100698 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200699 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100700 if (h2s->st < H2_SS_ERROR)
701 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100702 if (h2s->cs)
703 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200704 }
705}
706
Willy Tarreau7e094452018-12-19 18:08:52 +0100707/* attempt to notify the data layer of recv availability */
708static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
709{
710 struct wait_event *sw;
711
712 if (h2s->recv_wait) {
713 sw = h2s->recv_wait;
714 sw->events &= ~SUB_RETRY_RECV;
715 tasklet_wakeup(sw->task);
716 h2s->recv_wait = NULL;
717 }
718}
719
720/* attempt to notify the data layer of send availability */
721static void __maybe_unused h2s_notify_send(struct h2s *h2s)
722{
723 struct wait_event *sw;
724
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100725 if (h2s->send_wait && !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100726 sw = h2s->send_wait;
727 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100728 sw->events |= SUB_CALL_UNSUBSCRIBE;
729 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);
878 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200879 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100880 pool_free(pool_head_h2s, h2s);
881}
882
Willy Tarreaua8e49542018-10-03 18:53:55 +0200883/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
884 * stream tree. In case of error, nothing is added and NULL is returned. The
885 * causes of errors can be any failed memory allocation. The caller is
886 * responsible for checking if the connection may support an extra stream
887 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200888 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200889static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200890{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200891 struct h2s *h2s;
892
Willy Tarreaubafbe012017-11-24 17:34:44 +0100893 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200894 if (!h2s)
895 goto out;
896
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200897 h2s->wait_event.task = tasklet_new();
898 if (!h2s->wait_event.task) {
899 pool_free(pool_head_h2s, h2s);
900 goto out;
901 }
902 h2s->send_wait = NULL;
903 h2s->recv_wait = NULL;
904 h2s->wait_event.task->process = h2_deferred_shut;
905 h2s->wait_event.task->context = h2s;
906 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100907 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200908 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100909 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200910 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200911 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200912 h2s->mws = h2c->miw;
913 h2s->flags = H2_SF_NONE;
914 h2s->errcode = H2_ERR_NO_ERROR;
915 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200916 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100917 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200918 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200919
920 if (h2c->flags & H2_CF_IS_BACK) {
921 h1m_init_req(&h2s->h1m);
922 h2s->h1m.err_pos = -1; // don't care about errors on the request path
923 h2s->h1m.flags |= H1_MF_TOLOWER;
924 } else {
925 h1m_init_res(&h2s->h1m);
926 h2s->h1m.err_pos = -1; // don't care about errors on the response path
927 h2s->h1m.flags |= H1_MF_TOLOWER;
928 }
929
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200930 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200931 if (id > 0)
932 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100933 else
934 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200935
936 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100937 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100938 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200939
940 return h2s;
941
942 out_free_h2s:
943 pool_free(pool_head_h2s, h2s);
944 out:
945 return NULL;
946}
947
948/* creates a new stream <id> on the h2c connection and returns it, or NULL in
949 * case of memory allocation error.
950 */
951static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
952{
953 struct session *sess = h2c->conn->owner;
954 struct conn_stream *cs;
955 struct h2s *h2s;
956
957 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
958 goto out;
959
960 h2s = h2s_new(h2c, id);
961 if (!h2s)
962 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200963
964 cs = cs_new(h2c->conn);
965 if (!cs)
966 goto out_close;
967
Olivier Houchard746fb772018-12-15 19:42:00 +0100968 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200969 h2s->cs = cs;
970 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200971 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200972
973 if (stream_create_from_cs(cs) < 0)
974 goto out_free_cs;
975
Willy Tarreau590a0512018-09-05 11:56:48 +0200976 /* We want the accept date presented to the next stream to be the one
977 * we have now, the handshake time to be null (since the next stream
978 * is not delayed by a handshake), and the idle time to count since
979 * right now.
980 */
981 sess->accept_date = date;
982 sess->tv_accept = now;
983 sess->t_handshake = 0;
984
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200985 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100986 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200987 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200988 return h2s;
989
990 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200991 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200992 cs_free(cs);
993 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200994 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200995 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200996 sess_log(sess);
997 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200998}
999
Willy Tarreau751f2d02018-10-05 09:35:00 +02001000/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1001 * and returns it, or NULL in case of memory allocation error or if the highest
1002 * possible stream ID was reached.
1003 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001004static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001005{
1006 struct h2s *h2s = NULL;
1007
Willy Tarreau86949782019-01-31 10:42:05 +01001008 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001009 goto out;
1010
Willy Tarreaua80dca82019-01-24 17:08:28 +01001011 if (h2_streams_left(h2c) < 1)
1012 goto out;
1013
Willy Tarreau751f2d02018-10-05 09:35:00 +02001014 /* Defer choosing the ID until we send the first message to create the stream */
1015 h2s = h2s_new(h2c, 0);
1016 if (!h2s)
1017 goto out;
1018
1019 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001020 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001021 cs->ctx = h2s;
1022 h2c->nb_cs++;
1023
Willy Tarreau751f2d02018-10-05 09:35:00 +02001024 out:
1025 return h2s;
1026}
1027
Willy Tarreaube5b7152017-09-25 16:25:39 +02001028/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1029 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1030 * the various settings codes.
1031 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001032static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001033{
1034 struct buffer *res;
1035 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001036 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001037 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001038 int ret;
1039
1040 if (h2c_mux_busy(h2c, NULL)) {
1041 h2c->flags |= H2_CF_DEM_MBUSY;
1042 return 0;
1043 }
1044
Willy Tarreau44e973f2018-03-01 17:49:30 +01001045 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001046 if (!res) {
1047 h2c->flags |= H2_CF_MUX_MALLOC;
1048 h2c->flags |= H2_CF_DEM_MROOM;
1049 return 0;
1050 }
1051
1052 chunk_init(&buf, buf_data, sizeof(buf_data));
1053 chunk_memcpy(&buf,
1054 "\x00\x00\x00" /* length : 0 for now */
1055 "\x04\x00" /* type : 4 (settings), flags : 0 */
1056 "\x00\x00\x00\x00", /* stream ID : 0 */
1057 9);
1058
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001059 if (h2c->flags & H2_CF_IS_BACK) {
1060 /* send settings_enable_push=0 */
1061 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1062 }
1063
Willy Tarreaube5b7152017-09-25 16:25:39 +02001064 if (h2_settings_header_table_size != 4096) {
1065 char str[6] = "\x00\x01"; /* header_table_size */
1066
1067 write_n32(str + 2, h2_settings_header_table_size);
1068 chunk_memcat(&buf, str, 6);
1069 }
1070
1071 if (h2_settings_initial_window_size != 65535) {
1072 char str[6] = "\x00\x04"; /* initial_window_size */
1073
1074 write_n32(str + 2, h2_settings_initial_window_size);
1075 chunk_memcat(&buf, str, 6);
1076 }
1077
1078 if (h2_settings_max_concurrent_streams != 0) {
1079 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1080
1081 /* Note: 0 means "unlimited" for haproxy's config but not for
1082 * the protocol, so never send this value!
1083 */
1084 write_n32(str + 2, h2_settings_max_concurrent_streams);
1085 chunk_memcat(&buf, str, 6);
1086 }
1087
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001088 mfs = h2_settings_max_frame_size;
1089 if (mfs > global.tune.bufsize)
1090 mfs = global.tune.bufsize;
1091
1092 if (!mfs)
1093 mfs = global.tune.bufsize;
1094
1095 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001096 char str[6] = "\x00\x05"; /* max_frame_size */
1097
1098 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1099 * match bufsize - rewrite size, but at the moment it seems
1100 * that clients don't take care of it.
1101 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001102 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001103 chunk_memcat(&buf, str, 6);
1104 }
1105
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001106 h2_set_frame_size(buf.area, buf.data - 9);
1107 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001108 if (unlikely(ret <= 0)) {
1109 if (!ret) {
1110 h2c->flags |= H2_CF_MUX_MFULL;
1111 h2c->flags |= H2_CF_DEM_MROOM;
1112 return 0;
1113 }
1114 else {
1115 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1116 return 0;
1117 }
1118 }
1119 return ret;
1120}
1121
Willy Tarreau52eed752017-09-22 15:05:09 +02001122/* Try to receive a connection preface, then upon success try to send our
1123 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1124 * missing data. It may return an error in h2c.
1125 */
1126static int h2c_frt_recv_preface(struct h2c *h2c)
1127{
1128 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001129 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001130
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001131 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001132
1133 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001134 if (ret1 < 0)
1135 sess_log(h2c->conn->owner);
1136
Willy Tarreau52eed752017-09-22 15:05:09 +02001137 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1138 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1139 return 0;
1140 }
1141
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001142 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001143 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001144 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001145
Willy Tarreaube5b7152017-09-25 16:25:39 +02001146 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001147}
1148
Willy Tarreau01b44822018-10-03 14:26:37 +02001149/* Try to send a connection preface, then upon success try to send our
1150 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1151 * missing data. It may return an error in h2c.
1152 */
1153static int h2c_bck_send_preface(struct h2c *h2c)
1154{
1155 struct buffer *res;
1156
1157 if (h2c_mux_busy(h2c, NULL)) {
1158 h2c->flags |= H2_CF_DEM_MBUSY;
1159 return 0;
1160 }
1161
1162 res = h2_get_buf(h2c, &h2c->mbuf);
1163 if (!res) {
1164 h2c->flags |= H2_CF_MUX_MALLOC;
1165 h2c->flags |= H2_CF_DEM_MROOM;
1166 return 0;
1167 }
1168
1169 if (!b_data(res)) {
1170 /* preface not yet sent */
1171 b_istput(res, ist(H2_CONN_PREFACE));
1172 }
1173
1174 return h2c_send_settings(h2c);
1175}
1176
Willy Tarreau081d4722017-05-16 21:51:05 +02001177/* try to send a GOAWAY frame on the connection to report an error or a graceful
1178 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1179 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1180 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1181 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1182 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1183 * on unrecoverable failure. It will not attempt to send one again in this last
1184 * case so that it is safe to use h2c_error() to report such errors.
1185 */
1186static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1187{
1188 struct buffer *res;
1189 char str[17];
1190 int ret;
1191
1192 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1193 return 1; // claim that it worked
1194
1195 if (h2c_mux_busy(h2c, h2s)) {
1196 if (h2s)
1197 h2s->flags |= H2_SF_BLK_MBUSY;
1198 else
1199 h2c->flags |= H2_CF_DEM_MBUSY;
1200 return 0;
1201 }
1202
Willy Tarreau44e973f2018-03-01 17:49:30 +01001203 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001204 if (!res) {
1205 h2c->flags |= H2_CF_MUX_MALLOC;
1206 if (h2s)
1207 h2s->flags |= H2_SF_BLK_MROOM;
1208 else
1209 h2c->flags |= H2_CF_DEM_MROOM;
1210 return 0;
1211 }
1212
1213 /* len: 8, type: 7, flags: none, sid: 0 */
1214 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1215
1216 if (h2c->last_sid < 0)
1217 h2c->last_sid = h2c->max_id;
1218
1219 write_n32(str + 9, h2c->last_sid);
1220 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001221 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001222 if (unlikely(ret <= 0)) {
1223 if (!ret) {
1224 h2c->flags |= H2_CF_MUX_MFULL;
1225 if (h2s)
1226 h2s->flags |= H2_SF_BLK_MROOM;
1227 else
1228 h2c->flags |= H2_CF_DEM_MROOM;
1229 return 0;
1230 }
1231 else {
1232 /* we cannot report this error using GOAWAY, so we mark
1233 * it and claim a success.
1234 */
1235 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1236 h2c->flags |= H2_CF_GOAWAY_FAILED;
1237 return 1;
1238 }
1239 }
1240 h2c->flags |= H2_CF_GOAWAY_SENT;
1241 return ret;
1242}
1243
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001244/* Try to send an RST_STREAM frame on the connection for the indicated stream
1245 * during mux operations. This stream must be valid and cannot be closed
1246 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1247 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1248 * not yet.
1249 *
1250 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1251 * to write the message, it subscribes the stream to future notifications.
1252 */
1253static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1254{
1255 struct buffer *res;
1256 char str[13];
1257 int ret;
1258
1259 if (!h2s || h2s->st == H2_SS_CLOSED)
1260 return 1;
1261
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001262 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1263 * RST_STREAM in response to a RST_STREAM frame.
1264 */
1265 if (h2c->dft == H2_FT_RST_STREAM) {
1266 ret = 1;
1267 goto ignore;
1268 }
1269
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001270 if (h2c_mux_busy(h2c, h2s)) {
1271 h2s->flags |= H2_SF_BLK_MBUSY;
1272 return 0;
1273 }
1274
Willy Tarreau44e973f2018-03-01 17:49:30 +01001275 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001276 if (!res) {
1277 h2c->flags |= H2_CF_MUX_MALLOC;
1278 h2s->flags |= H2_SF_BLK_MROOM;
1279 return 0;
1280 }
1281
1282 /* len: 4, type: 3, flags: none */
1283 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1284 write_n32(str + 5, h2s->id);
1285 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001286 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001287
1288 if (unlikely(ret <= 0)) {
1289 if (!ret) {
1290 h2c->flags |= H2_CF_MUX_MFULL;
1291 h2s->flags |= H2_SF_BLK_MROOM;
1292 return 0;
1293 }
1294 else {
1295 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1296 return 0;
1297 }
1298 }
1299
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001300 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001301 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001302 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001303 return ret;
1304}
1305
1306/* Try to send an RST_STREAM frame on the connection for the stream being
1307 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001308 * error code, even if the stream is one of the dummy ones, and will update
1309 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001310 *
1311 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1312 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001313 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001314 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001315 */
1316static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1317{
1318 struct buffer *res;
1319 char str[13];
1320 int ret;
1321
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001322 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1323 * RST_STREAM in response to a RST_STREAM frame.
1324 */
1325 if (h2c->dft == H2_FT_RST_STREAM) {
1326 ret = 1;
1327 goto ignore;
1328 }
1329
Willy Tarreau27a84c92017-10-17 08:10:17 +02001330 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001331 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001332 return 0;
1333 }
1334
Willy Tarreau44e973f2018-03-01 17:49:30 +01001335 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001336 if (!res) {
1337 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001338 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001339 return 0;
1340 }
1341
1342 /* len: 4, type: 3, flags: none */
1343 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001344
Willy Tarreau27a84c92017-10-17 08:10:17 +02001345 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001346 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001347 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001348
Willy Tarreau27a84c92017-10-17 08:10:17 +02001349 if (unlikely(ret <= 0)) {
1350 if (!ret) {
1351 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001352 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001353 return 0;
1354 }
1355 else {
1356 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1357 return 0;
1358 }
1359 }
1360
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001361 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001362 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001363 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001364 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001365 }
1366
Willy Tarreau27a84c92017-10-17 08:10:17 +02001367 return ret;
1368}
1369
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001370/* try to send an empty DATA frame with the ES flag set to notify about the
1371 * end of stream and match a shutdown(write). If an ES was already sent as
1372 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1373 * on success or zero if nothing was done. In case of lack of room to write the
1374 * message, it subscribes the requesting stream to future notifications.
1375 */
1376static int h2_send_empty_data_es(struct h2s *h2s)
1377{
1378 struct h2c *h2c = h2s->h2c;
1379 struct buffer *res;
1380 char str[9];
1381 int ret;
1382
Willy Tarreau721c9742017-11-07 11:05:42 +01001383 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001384 return 1;
1385
1386 if (h2c_mux_busy(h2c, h2s)) {
1387 h2s->flags |= H2_SF_BLK_MBUSY;
1388 return 0;
1389 }
1390
Willy Tarreau44e973f2018-03-01 17:49:30 +01001391 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001392 if (!res) {
1393 h2c->flags |= H2_CF_MUX_MALLOC;
1394 h2s->flags |= H2_SF_BLK_MROOM;
1395 return 0;
1396 }
1397
1398 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1399 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1400 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001401 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001402 if (likely(ret > 0)) {
1403 h2s->flags |= H2_SF_ES_SENT;
1404 }
1405 else if (!ret) {
1406 h2c->flags |= H2_CF_MUX_MFULL;
1407 h2s->flags |= H2_SF_BLK_MROOM;
1408 return 0;
1409 }
1410 else {
1411 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1412 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001413 }
1414 return ret;
1415}
1416
Christopher Fauletf02ca002019-03-07 16:21:34 +01001417/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1418 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1419 * connection. The stream's state is automatically updated accordingly. If the
1420 * stream is orphaned, it is destroyed.
1421 */
1422static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1423{
1424 if (!h2s->cs) {
1425 /* this stream was already orphaned */
1426 h2s_destroy(h2s);
1427 return;
1428 }
1429
1430 h2s->cs->flags |= flags;
1431 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1432 h2s->cs->flags |= CS_FL_ERROR;
1433
1434 h2s_alert(h2s);
1435
1436 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1437 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001438 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001439 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001440 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001441 h2s_close(h2s);
1442}
1443
1444/* wake the streams attached to the connection, whose id is greater than <last>
1445 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001446 */
1447static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1448{
1449 struct eb32_node *node;
1450 struct h2s *h2s;
1451
1452 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001453 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001454
1455 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001456 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001457
Christopher Fauletf02ca002019-03-07 16:21:34 +01001458 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001459 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1460 while (node) {
1461 h2s = container_of(node, struct h2s, by_id);
1462 if (h2s->id <= last)
1463 break;
1464 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001465 h2s_wake_one_stream(h2s, flags);
1466 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001467
Christopher Fauletf02ca002019-03-07 16:21:34 +01001468 /* Wake all streams with unassigned ID (ID == 0) */
1469 node = eb32_lookup(&h2c->streams_by_id, 0);
1470 while (node) {
1471 h2s = container_of(node, struct h2s, by_id);
1472 if (h2s->id > 0)
1473 break;
1474 node = eb32_next(node);
1475 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001476 }
1477}
1478
Willy Tarreau3421aba2017-07-27 15:41:03 +02001479/* Increase all streams' outgoing window size by the difference passed in
1480 * argument. This is needed upon receipt of the settings frame if the initial
1481 * window size is different. The difference may be negative and the resulting
1482 * window size as well, for the time it takes to receive some window updates.
1483 */
1484static void h2c_update_all_ws(struct h2c *h2c, int diff)
1485{
1486 struct h2s *h2s;
1487 struct eb32_node *node;
1488
1489 if (!diff)
1490 return;
1491
1492 node = eb32_first(&h2c->streams_by_id);
1493 while (node) {
1494 h2s = container_of(node, struct h2s, by_id);
1495 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001496
1497 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1498 h2s->flags &= ~H2_SF_BLK_SFCTL;
1499 if (h2s->send_wait)
1500 LIST_ADDQ(&h2c->send_list, &h2s->list);
1501
1502 }
1503
Willy Tarreau3421aba2017-07-27 15:41:03 +02001504 node = eb32_next(node);
1505 }
1506}
1507
1508/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1509 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001510 * return an error in h2c. The caller must have already verified frame length
1511 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001512 */
1513static int h2c_handle_settings(struct h2c *h2c)
1514{
1515 unsigned int offset;
1516 int error;
1517
1518 if (h2c->dff & H2_F_SETTINGS_ACK) {
1519 if (h2c->dfl) {
1520 error = H2_ERR_FRAME_SIZE_ERROR;
1521 goto fail;
1522 }
1523 return 1;
1524 }
1525
Willy Tarreau3421aba2017-07-27 15:41:03 +02001526 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001527 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001528 return 0;
1529
1530 /* parse the frame */
1531 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001532 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1533 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001534
1535 switch (type) {
1536 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1537 /* we need to update all existing streams with the
1538 * difference from the previous iws.
1539 */
1540 if (arg < 0) { // RFC7540#6.5.2
1541 error = H2_ERR_FLOW_CONTROL_ERROR;
1542 goto fail;
1543 }
1544 h2c_update_all_ws(h2c, arg - h2c->miw);
1545 h2c->miw = arg;
1546 break;
1547 case H2_SETTINGS_MAX_FRAME_SIZE:
1548 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1549 error = H2_ERR_PROTOCOL_ERROR;
1550 goto fail;
1551 }
1552 h2c->mfs = arg;
1553 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001554 case H2_SETTINGS_ENABLE_PUSH:
1555 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1556 error = H2_ERR_PROTOCOL_ERROR;
1557 goto fail;
1558 }
1559 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001560 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1561 if (h2c->flags & H2_CF_IS_BACK) {
1562 /* the limit is only for the backend; for the frontend it is our limit */
1563 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1564 arg = h2_settings_max_concurrent_streams;
1565 h2c->streams_limit = arg;
1566 }
1567 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001568 }
1569 }
1570
1571 /* need to ACK this frame now */
1572 h2c->st0 = H2_CS_FRAME_A;
1573 return 1;
1574 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001575 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001576 h2c_error(h2c, error);
1577 return 0;
1578}
1579
1580/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1581 * success or one of the h2_status values.
1582 */
1583static int h2c_ack_settings(struct h2c *h2c)
1584{
1585 struct buffer *res;
1586 char str[9];
1587 int ret = -1;
1588
1589 if (h2c_mux_busy(h2c, NULL)) {
1590 h2c->flags |= H2_CF_DEM_MBUSY;
1591 return 0;
1592 }
1593
Willy Tarreau44e973f2018-03-01 17:49:30 +01001594 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001595 if (!res) {
1596 h2c->flags |= H2_CF_MUX_MALLOC;
1597 h2c->flags |= H2_CF_DEM_MROOM;
1598 return 0;
1599 }
1600
1601 memcpy(str,
1602 "\x00\x00\x00" /* length : 0 (no data) */
1603 "\x04" "\x01" /* type : 4, flags : ACK */
1604 "\x00\x00\x00\x00" /* stream ID */, 9);
1605
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001606 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001607 if (unlikely(ret <= 0)) {
1608 if (!ret) {
1609 h2c->flags |= H2_CF_MUX_MFULL;
1610 h2c->flags |= H2_CF_DEM_MROOM;
1611 return 0;
1612 }
1613 else {
1614 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1615 return 0;
1616 }
1617 }
1618 return ret;
1619}
1620
Willy Tarreaucf68c782017-10-10 17:11:41 +02001621/* processes a PING frame and schedules an ACK if needed. The caller must pass
1622 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001623 * missing data. The caller must have already verified frame length
1624 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001625 */
1626static int h2c_handle_ping(struct h2c *h2c)
1627{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001628 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001629 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001630 h2c->st0 = H2_CS_FRAME_A;
1631 return 1;
1632}
1633
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001634/* Try to send a window update for stream id <sid> and value <increment>.
1635 * Returns > 0 on success or zero on missing room or failure. It may return an
1636 * error in h2c.
1637 */
1638static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1639{
1640 struct buffer *res;
1641 char str[13];
1642 int ret = -1;
1643
1644 if (h2c_mux_busy(h2c, NULL)) {
1645 h2c->flags |= H2_CF_DEM_MBUSY;
1646 return 0;
1647 }
1648
Willy Tarreau44e973f2018-03-01 17:49:30 +01001649 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001650 if (!res) {
1651 h2c->flags |= H2_CF_MUX_MALLOC;
1652 h2c->flags |= H2_CF_DEM_MROOM;
1653 return 0;
1654 }
1655
1656 /* length: 4, type: 8, flags: none */
1657 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1658 write_n32(str + 5, sid);
1659 write_n32(str + 9, increment);
1660
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001661 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001662
1663 if (unlikely(ret <= 0)) {
1664 if (!ret) {
1665 h2c->flags |= H2_CF_MUX_MFULL;
1666 h2c->flags |= H2_CF_DEM_MROOM;
1667 return 0;
1668 }
1669 else {
1670 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1671 return 0;
1672 }
1673 }
1674 return ret;
1675}
1676
1677/* try to send pending window update for the connection. It's safe to call it
1678 * with no pending updates. Returns > 0 on success or zero on missing room or
1679 * failure. It may return an error in h2c.
1680 */
1681static int h2c_send_conn_wu(struct h2c *h2c)
1682{
1683 int ret = 1;
1684
1685 if (h2c->rcvd_c <= 0)
1686 return 1;
1687
Willy Tarreau97aaa672018-12-23 09:49:04 +01001688 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1689 /* increase the advertised connection window to 2G on
1690 * first update.
1691 */
1692 h2c->flags |= H2_CF_WINDOW_OPENED;
1693 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1694 }
1695
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001696 /* send WU for the connection */
1697 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1698 if (ret > 0)
1699 h2c->rcvd_c = 0;
1700
1701 return ret;
1702}
1703
1704/* try to send pending window update for the current dmux stream. It's safe to
1705 * call it with no pending updates. Returns > 0 on success or zero on missing
1706 * room or failure. It may return an error in h2c.
1707 */
1708static int h2c_send_strm_wu(struct h2c *h2c)
1709{
1710 int ret = 1;
1711
1712 if (h2c->rcvd_s <= 0)
1713 return 1;
1714
1715 /* send WU for the stream */
1716 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1717 if (ret > 0)
1718 h2c->rcvd_s = 0;
1719
1720 return ret;
1721}
1722
Willy Tarreaucf68c782017-10-10 17:11:41 +02001723/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1724 * success, 0 on missing data or one of the h2_status values.
1725 */
1726static int h2c_ack_ping(struct h2c *h2c)
1727{
1728 struct buffer *res;
1729 char str[17];
1730 int ret = -1;
1731
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001732 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001733 return 0;
1734
1735 if (h2c_mux_busy(h2c, NULL)) {
1736 h2c->flags |= H2_CF_DEM_MBUSY;
1737 return 0;
1738 }
1739
Willy Tarreau44e973f2018-03-01 17:49:30 +01001740 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001741 if (!res) {
1742 h2c->flags |= H2_CF_MUX_MALLOC;
1743 h2c->flags |= H2_CF_DEM_MROOM;
1744 return 0;
1745 }
1746
1747 memcpy(str,
1748 "\x00\x00\x08" /* length : 8 (same payload) */
1749 "\x06" "\x01" /* type : 6, flags : ACK */
1750 "\x00\x00\x00\x00" /* stream ID */, 9);
1751
1752 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001753 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001754
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001755 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001756 if (unlikely(ret <= 0)) {
1757 if (!ret) {
1758 h2c->flags |= H2_CF_MUX_MFULL;
1759 h2c->flags |= H2_CF_DEM_MROOM;
1760 return 0;
1761 }
1762 else {
1763 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1764 return 0;
1765 }
1766 }
1767 return ret;
1768}
1769
Willy Tarreau26f95952017-07-27 17:18:30 +02001770/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1771 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001772 * h2c or h2s. The caller must have already verified frame length and stream ID
1773 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001774 */
1775static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1776{
1777 int32_t inc;
1778 int error;
1779
Willy Tarreau26f95952017-07-27 17:18:30 +02001780 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001781 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001782 return 0;
1783
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001784 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001785
1786 if (h2c->dsi != 0) {
1787 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001788
1789 /* it's not an error to receive WU on a closed stream */
1790 if (h2s->st == H2_SS_CLOSED)
1791 return 1;
1792
1793 if (!inc) {
1794 error = H2_ERR_PROTOCOL_ERROR;
1795 goto strm_err;
1796 }
1797
1798 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1799 error = H2_ERR_FLOW_CONTROL_ERROR;
1800 goto strm_err;
1801 }
1802
1803 h2s->mws += inc;
1804 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1805 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001806 if (h2s->send_wait)
1807 LIST_ADDQ(&h2c->send_list, &h2s->list);
1808
Willy Tarreau26f95952017-07-27 17:18:30 +02001809 }
1810 }
1811 else {
1812 /* connection window update */
1813 if (!inc) {
1814 error = H2_ERR_PROTOCOL_ERROR;
1815 goto conn_err;
1816 }
1817
1818 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1819 error = H2_ERR_FLOW_CONTROL_ERROR;
1820 goto conn_err;
1821 }
1822
1823 h2c->mws += inc;
1824 }
1825
1826 return 1;
1827
1828 conn_err:
1829 h2c_error(h2c, error);
1830 return 0;
1831
1832 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001833 h2s_error(h2s, error);
1834 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001835 return 0;
1836}
1837
Willy Tarreaue96b0922017-10-30 00:28:29 +01001838/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001839 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1840 * have already verified frame length and stream ID validity. Described in
1841 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001842 */
1843static int h2c_handle_goaway(struct h2c *h2c)
1844{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001845 int last;
1846
Willy Tarreaue96b0922017-10-30 00:28:29 +01001847 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001848 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001849 return 0;
1850
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001851 last = h2_get_n32(&h2c->dbuf, 0);
1852 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001853 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001854 if (h2c->last_sid < 0)
1855 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001856 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001857}
1858
Willy Tarreau92153fc2017-12-03 19:46:19 +01001859/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001860 * invalid. Returns > 0 on success or zero on missing data. It may return an
1861 * error in h2c. The caller must have already verified frame length and stream
1862 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001863 */
1864static int h2c_handle_priority(struct h2c *h2c)
1865{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001866 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001867 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001868 return 0;
1869
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001870 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001871 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001872 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1873 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001874 }
1875 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001876}
1877
Willy Tarreaucd234e92017-08-18 10:59:39 +02001878/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001879 * Returns > 0 on success or zero on missing data. The caller must have already
1880 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001881 */
1882static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1883{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001884 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001885 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001886 return 0;
1887
1888 /* late RST, already handled */
1889 if (h2s->st == H2_SS_CLOSED)
1890 return 1;
1891
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001892 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001893 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001894
1895 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001896 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001897 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001898 }
1899
1900 h2s->flags |= H2_SF_RST_RCVD;
1901 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001902}
1903
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001904/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1905 * It may return an error in h2c or h2s. The caller must consider that the
1906 * return value is the new h2s in case one was allocated (most common case).
1907 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001908 * errors here are reported as connection errors since it's impossible to
1909 * recover from such errors after the compression context has been altered.
1910 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001911static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001912{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001913 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001914 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001915 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001916 int error;
1917
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001918 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001919 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001920
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001921 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001922 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001923
1924 /* now either the frame is complete or the buffer is complete */
1925 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001926 /* The stream exists/existed, this must be a trailers frame */
1927 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001928 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001929 goto out;
1930 goto done;
1931 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001932 /* the connection was already killed by an RST, let's consume
1933 * the data and send another RST.
1934 */
1935 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1936 h2s = (struct h2s*)h2_error_stream;
1937 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001938 }
1939 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1940 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1941 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001942 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001943 goto conn_err;
1944 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001945 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1946 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001947
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001948 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001949
Willy Tarreau25919232019-01-03 14:48:18 +01001950 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001951 if (h2c->st0 >= H2_CS_ERROR)
1952 goto out;
1953
Willy Tarreau25919232019-01-03 14:48:18 +01001954 if (error <= 0) {
1955 if (error == 0)
1956 goto out; // missing data
1957
1958 /* Failed to decode this stream (e.g. too large request)
1959 * but the HPACK decompressor is still synchronized.
1960 */
1961 h2s = (struct h2s*)h2_error_stream;
1962 goto send_rst;
1963 }
1964
Willy Tarreau22de8d32018-09-05 19:55:58 +02001965 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001966 * positively from h2c_frt_stream_new(), the stream will report the error,
1967 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001968 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001969 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001970 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001971 h2s = (struct h2s*)h2_refused_stream;
1972 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001973 }
1974
1975 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001976 h2s->rxbuf = rxbuf;
1977 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001978 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001979
Willy Tarreau88d138e2019-01-02 19:38:14 +01001980 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001981 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001982 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001983
1984 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001985 if (h2s->cs)
1986 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01001987 if (h2s->st == H2_SS_OPEN)
1988 h2s->st = H2_SS_HREM;
1989 else
1990 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02001991 }
1992
Willy Tarreau3a429f02019-01-03 11:41:50 +01001993 /* update the max stream ID if the request is being processed */
1994 if (h2s->id > h2c->max_id)
1995 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001996
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001997 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001998
1999 conn_err:
2000 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002001 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002002
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002003 out:
2004 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002005 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002006
2007 send_rst:
2008 /* make the demux send an RST for the current stream. We may only
2009 * do this if we're certain that the HEADERS frame was properly
2010 * decompressed so that the HPACK decoder is still kept up to date.
2011 */
2012 h2_release_buf(h2c, &rxbuf);
2013 h2c->st0 = H2_CS_FRAME_E;
2014 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002015}
2016
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002017/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2018 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2019 * errors here are reported as connection errors since it's impossible to
2020 * recover from such errors after the compression context has been altered.
2021 */
2022static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2023{
2024 int error;
2025
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002026 if (!b_size(&h2c->dbuf))
2027 return NULL; // empty buffer
2028
2029 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2030 return NULL; // incomplete frame
2031
Willy Tarreau1915ca22019-01-24 11:49:37 +01002032 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002033
Willy Tarreau25919232019-01-03 14:48:18 +01002034 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002035 if (h2c->st0 >= H2_CS_ERROR)
2036 return NULL;
2037
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002038 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2039 /* RFC7540#5.1 */
2040 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2041 h2c->st0 = H2_CS_FRAME_E;
2042 return NULL;
2043 }
2044
Willy Tarreau25919232019-01-03 14:48:18 +01002045 if (error <= 0) {
2046 if (error == 0)
2047 return NULL; // missing data
2048
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002049 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002050 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002051 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002052 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002053 }
2054
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002055 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2056 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002057 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002058 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002059 }
2060
Willy Tarreau927b88b2019-03-04 08:03:25 +01002061 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002062 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002063 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 +02002064 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002065 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002066 h2s_close(h2s);
2067
2068 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002069}
2070
Willy Tarreau454f9052017-10-26 19:40:35 +02002071/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2072 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2073 */
2074static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2075{
2076 int error;
2077
2078 /* note that empty DATA frames are perfectly valid and sometimes used
2079 * to signal an end of stream (with the ES flag).
2080 */
2081
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002082 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002083 return 0; // empty buffer
2084
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002085 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002086 return 0; // incomplete frame
2087
2088 /* now either the frame is complete or the buffer is complete */
2089
Willy Tarreau454f9052017-10-26 19:40:35 +02002090 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2091 /* RFC7540#6.1 */
2092 error = H2_ERR_STREAM_CLOSED;
2093 goto strm_err;
2094 }
2095
Willy Tarreau1915ca22019-01-24 11:49:37 +01002096 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2097 /* RFC7540#8.1.2 */
2098 error = H2_ERR_PROTOCOL_ERROR;
2099 goto strm_err;
2100 }
2101
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002102 if (!h2_frt_transfer_data(h2s))
2103 return 0;
2104
Willy Tarreau454f9052017-10-26 19:40:35 +02002105 /* call the upper layers to process the frame, then let the upper layer
2106 * notify the stream about any change.
2107 */
2108 if (!h2s->cs) {
2109 error = H2_ERR_STREAM_CLOSED;
2110 goto strm_err;
2111 }
2112
Willy Tarreau8f650c32017-11-21 19:36:21 +01002113 if (h2c->st0 >= H2_CS_ERROR)
2114 return 0;
2115
Willy Tarreau721c9742017-11-07 11:05:42 +01002116 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002117 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002118 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002119 }
2120
2121 /* check for completion : the callee will change this to FRAME_A or
2122 * FRAME_H once done.
2123 */
2124 if (h2c->st0 == H2_CS_FRAME_P)
2125 return 0;
2126
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002127 /* last frame */
2128 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002129 if (h2s->cs)
2130 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002131 if (h2s->st == H2_SS_OPEN)
2132 h2s->st = H2_SS_HREM;
2133 else
2134 h2s_close(h2s);
2135
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002136 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002137
2138 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2139 /* RFC7540#8.1.2 */
2140 error = H2_ERR_PROTOCOL_ERROR;
2141 goto strm_err;
2142 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002143 }
2144
Willy Tarreau454f9052017-10-26 19:40:35 +02002145 return 1;
2146
Willy Tarreau454f9052017-10-26 19:40:35 +02002147 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002148 h2s_error(h2s, error);
2149 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002150 return 0;
2151}
2152
Willy Tarreaubc933932017-10-09 16:21:43 +02002153/* process Rx frames to be demultiplexed */
2154static void h2_process_demux(struct h2c *h2c)
2155{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002156 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002157 struct h2_fh hdr;
2158 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002159
Willy Tarreau081d4722017-05-16 21:51:05 +02002160 if (h2c->st0 >= H2_CS_ERROR)
2161 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002162
2163 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2164 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002165 if (h2c->flags & H2_CF_IS_BACK)
2166 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002167 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2168 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002169 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002170 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002171 sess_log(h2c->conn->owner);
2172 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002173 goto fail;
2174 }
2175
2176 h2c->max_id = 0;
2177 h2c->st0 = H2_CS_SETTINGS1;
2178 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002179
2180 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002181 /* ensure that what is pending is a valid SETTINGS frame
2182 * without an ACK.
2183 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002184 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002185 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002186 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002187 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002188 sess_log(h2c->conn->owner);
2189 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002190 goto fail;
2191 }
2192
2193 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2194 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2195 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2196 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002197 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002198 goto fail;
2199 }
2200
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002201 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002202 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2203 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2204 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002205 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002206 goto fail;
2207 }
2208
Willy Tarreau3bf69182018-12-21 15:34:50 +01002209 /* that's OK, switch to FRAME_P to process it. This is
2210 * a SETTINGS frame whose header has already been
2211 * deleted above.
2212 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002213 padlen = 0;
2214 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002215 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002216 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002217
2218 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002219 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002220 int ret = 0;
2221
2222 if (h2c->st0 >= H2_CS_ERROR)
2223 break;
2224
2225 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002226 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002227 break;
2228
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002229 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002230 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002231 if (!h2c->nb_streams) {
2232 /* only log if no other stream can report the error */
2233 sess_log(h2c->conn->owner);
2234 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002235 break;
2236 }
2237
Willy Tarreau3bf69182018-12-21 15:34:50 +01002238 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2239 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2240 * we read the pad length and drop it from the remaining
2241 * payload (one byte + the 9 remaining ones = 10 total
2242 * removed), so we have a frame payload starting after the
2243 * pad len. Flow controlled frames (DATA) also count the
2244 * padlen in the flow control, so it must be adjusted.
2245 */
2246 if (hdr.len < 1) {
2247 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2248 sess_log(h2c->conn->owner);
2249 goto fail;
2250 }
2251 hdr.len--;
2252
2253 if (b_data(&h2c->dbuf) < 10)
2254 break; // missing padlen
2255
2256 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2257
2258 if (padlen > hdr.len) {
2259 /* RFC7540#6.1 : pad length = length of
2260 * frame payload or greater => error.
2261 */
2262 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2263 sess_log(h2c->conn->owner);
2264 goto fail;
2265 }
2266
2267 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2268 h2c->rcvd_c++;
2269 h2c->rcvd_s++;
2270 }
2271 b_del(&h2c->dbuf, 1);
2272 }
2273 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002274
2275 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002276 h2c->dfl = hdr.len;
2277 h2c->dsi = hdr.sid;
2278 h2c->dft = hdr.ft;
2279 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002280 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002281 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002282
2283 /* check for minimum basic frame format validity */
2284 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2285 if (ret != H2_ERR_NO_ERROR) {
2286 h2c_error(h2c, ret);
2287 sess_log(h2c->conn->owner);
2288 goto fail;
2289 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002290 }
2291
2292 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002293 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2294
Willy Tarreau567beb82018-12-18 16:52:44 +01002295 if (tmp_h2s != h2s && h2s && h2s->cs &&
2296 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002297 (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 +01002298 /* we may have to signal the upper layers */
2299 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002300 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002301 }
2302 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002303
Willy Tarreaud7901432017-12-29 11:34:40 +01002304 if (h2c->st0 == H2_CS_FRAME_E)
2305 goto strm_err;
2306
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002307 if (h2s->st == H2_SS_IDLE &&
2308 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2309 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2310 * this state MUST be treated as a connection error
2311 */
2312 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002313 if (!h2c->nb_streams) {
2314 /* only log if no other stream can report the error */
2315 sess_log(h2c->conn->owner);
2316 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002317 break;
2318 }
2319
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002320 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2321 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2322 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002323 * this state MUST be treated as a stream error.
2324 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2325 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002326 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002327 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2328 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2329 else
2330 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002331 goto strm_err;
2332 }
2333
Willy Tarreauab837502017-12-27 15:07:30 +01002334 /* Below the management of frames received in closed state is a
2335 * bit hackish because the spec makes strong differences between
2336 * streams closed by receiving RST, sending RST, and seeing ES
2337 * in both directions. In addition to this, the creation of a
2338 * new stream reusing the identifier of a closed one will be
2339 * detected here. Given that we cannot keep track of all closed
2340 * streams forever, we consider that unknown closed streams were
2341 * closed on RST received, which allows us to respond with an
2342 * RST without breaking the connection (eg: to abort a transfer).
2343 * Some frames have to be silently ignored as well.
2344 */
2345 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002346 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002347 /* #5.1.1: The identifier of a newly
2348 * established stream MUST be numerically
2349 * greater than all streams that the initiating
2350 * endpoint has opened or reserved. This
2351 * governs streams that are opened using a
2352 * HEADERS frame and streams that are reserved
2353 * using PUSH_PROMISE. An endpoint that
2354 * receives an unexpected stream identifier
2355 * MUST respond with a connection error.
2356 */
2357 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2358 goto strm_err;
2359 }
2360
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002361 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002362 /* RFC7540#5.1:closed: an endpoint that
2363 * receives any frame other than PRIORITY after
2364 * receiving a RST_STREAM MUST treat that as a
2365 * stream error of type STREAM_CLOSED.
2366 *
2367 * Note that old streams fall into this category
2368 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002369 *
2370 * However, we cannot generalize this to all frame types. Those
2371 * carrying compression state must still be processed before
2372 * being dropped or we'll desynchronize the decoder. This can
2373 * happen with request trailers received after sending an
2374 * RST_STREAM, or with header/trailers responses received after
2375 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002376 */
2377 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2378 h2c->st0 = H2_CS_FRAME_E;
2379 goto strm_err;
2380 }
2381
2382 /* RFC7540#5.1:closed: if this state is reached as a
2383 * result of sending a RST_STREAM frame, the peer that
2384 * receives the RST_STREAM might have already sent
2385 * frames on the stream that cannot be withdrawn. An
2386 * endpoint MUST ignore frames that it receives on
2387 * closed streams after it has sent a RST_STREAM
2388 * frame. An endpoint MAY choose to limit the period
2389 * over which it ignores frames and treat frames that
2390 * arrive after this time as being in error.
2391 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002392 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002393 /* RFC7540#5.1:closed: any frame other than
2394 * PRIO/WU/RST in this state MUST be treated as
2395 * a connection error
2396 */
2397 if (h2c->dft != H2_FT_RST_STREAM &&
2398 h2c->dft != H2_FT_PRIORITY &&
2399 h2c->dft != H2_FT_WINDOW_UPDATE) {
2400 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2401 goto strm_err;
2402 }
2403 }
2404 }
2405
Willy Tarreauc0da1962017-10-30 18:38:00 +01002406#if 0
2407 // problem below: it is not possible to completely ignore such
2408 // streams as we need to maintain the compression state as well
2409 // and for this we need to completely process these frames (eg:
2410 // HEADERS frames) as well as counting DATA frames to emit
2411 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2412 // This is a typical case of layer violation where the
2413 // transported contents are critical to the connection's
2414 // validity and must be ignored at the same time :-(
2415
2416 /* graceful shutdown, ignore streams whose ID is higher than
2417 * the one advertised in GOAWAY. RFC7540#6.8.
2418 */
2419 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002420 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2421 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002422 h2c->dfl -= ret;
2423 ret = h2c->dfl == 0;
2424 goto strm_err;
2425 }
2426#endif
2427
Willy Tarreau7e98c052017-10-10 15:56:59 +02002428 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002429 case H2_FT_SETTINGS:
2430 if (h2c->st0 == H2_CS_FRAME_P)
2431 ret = h2c_handle_settings(h2c);
2432
2433 if (h2c->st0 == H2_CS_FRAME_A)
2434 ret = h2c_ack_settings(h2c);
2435 break;
2436
Willy Tarreaucf68c782017-10-10 17:11:41 +02002437 case H2_FT_PING:
2438 if (h2c->st0 == H2_CS_FRAME_P)
2439 ret = h2c_handle_ping(h2c);
2440
2441 if (h2c->st0 == H2_CS_FRAME_A)
2442 ret = h2c_ack_ping(h2c);
2443 break;
2444
Willy Tarreau26f95952017-07-27 17:18:30 +02002445 case H2_FT_WINDOW_UPDATE:
2446 if (h2c->st0 == H2_CS_FRAME_P)
2447 ret = h2c_handle_window_update(h2c, h2s);
2448 break;
2449
Willy Tarreau61290ec2017-10-17 08:19:21 +02002450 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002451 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2452 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2453 * frames' parsers consume all following CONTINUATION
2454 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002455 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002456 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2457 sess_log(h2c->conn->owner);
2458 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002459
Willy Tarreau13278b42017-10-13 19:23:14 +02002460 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002461 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002462 if (h2c->flags & H2_CF_IS_BACK)
2463 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2464 else
2465 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002466 if (tmp_h2s) {
2467 h2s = tmp_h2s;
2468 ret = 1;
2469 }
2470 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002471 break;
2472
Willy Tarreau454f9052017-10-26 19:40:35 +02002473 case H2_FT_DATA:
2474 if (h2c->st0 == H2_CS_FRAME_P)
2475 ret = h2c_frt_handle_data(h2c, h2s);
2476
2477 if (h2c->st0 == H2_CS_FRAME_A)
2478 ret = h2c_send_strm_wu(h2c);
2479 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002480
Willy Tarreau92153fc2017-12-03 19:46:19 +01002481 case H2_FT_PRIORITY:
2482 if (h2c->st0 == H2_CS_FRAME_P)
2483 ret = h2c_handle_priority(h2c);
2484 break;
2485
Willy Tarreaucd234e92017-08-18 10:59:39 +02002486 case H2_FT_RST_STREAM:
2487 if (h2c->st0 == H2_CS_FRAME_P)
2488 ret = h2c_handle_rst_stream(h2c, h2s);
2489 break;
2490
Willy Tarreaue96b0922017-10-30 00:28:29 +01002491 case H2_FT_GOAWAY:
2492 if (h2c->st0 == H2_CS_FRAME_P)
2493 ret = h2c_handle_goaway(h2c);
2494 break;
2495
Willy Tarreau1c661982017-10-30 13:52:01 +01002496 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002497 default:
2498 /* drop frames that we ignore. They may be larger than
2499 * the buffer so we drain all of their contents until
2500 * we reach the end.
2501 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002502 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2503 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002504 h2c->dfl -= ret;
2505 ret = h2c->dfl == 0;
2506 }
2507
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002508 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002509 /* We may have to send an RST if not done yet */
2510 if (h2s->st == H2_SS_ERROR)
2511 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002512
Willy Tarreaua20a5192017-12-27 11:02:06 +01002513 if (h2c->st0 == H2_CS_FRAME_E)
2514 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002515
Willy Tarreau7e98c052017-10-10 15:56:59 +02002516 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002517 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002518 break;
2519
2520 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002521 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002522 h2c->st0 = H2_CS_FRAME_H;
2523 }
2524 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002525
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002526 if (h2c->rcvd_c > 0 &&
2527 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2528 h2c_send_conn_wu(h2c);
2529
Willy Tarreau52eed752017-09-22 15:05:09 +02002530 fail:
2531 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002532 if (h2s && h2s->cs &&
2533 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002534 (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 +01002535 /* we may have to signal the upper layers */
2536 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002537 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002538 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002539
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002540 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002541}
2542
2543/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2544 * the end.
2545 */
2546static int h2_process_mux(struct h2c *h2c)
2547{
Olivier Houchardd360ac62019-03-22 17:37:16 +01002548 struct h2s *h2s;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002549
Willy Tarreau01b44822018-10-03 14:26:37 +02002550 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2551 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2552 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2553 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2554 if (h2c->st0 == H2_CS_ERROR) {
2555 h2c->st0 = H2_CS_ERROR2;
2556 sess_log(h2c->conn->owner);
2557 }
2558 goto fail;
2559 }
2560 h2c->st0 = H2_CS_SETTINGS1;
2561 }
2562 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002563 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002564 return 1;
2565 }
2566
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002567 /* start by sending possibly pending window updates */
2568 if (h2c->rcvd_c > 0 &&
2569 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2570 h2c_send_conn_wu(h2c) < 0)
2571 goto fail;
2572
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002573 /* First we always process the flow control list because the streams
2574 * waiting there were already elected for immediate emission but were
2575 * blocked just on this.
2576 */
2577
Olivier Houchardd360ac62019-03-22 17:37:16 +01002578 list_for_each_entry(h2s, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002579 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2580 h2c->st0 >= H2_CS_ERROR)
2581 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002582 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2583 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002584
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002585 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002586 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2587 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002588 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002589 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002590 }
2591
Olivier Houchardd360ac62019-03-22 17:37:16 +01002592 list_for_each_entry(h2s, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002593 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2594 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002595 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2596 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002597
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002598 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002599 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2600 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002601 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002602 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002603 }
2604
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002605 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002606 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002607 if (h2c->st0 == H2_CS_ERROR) {
2608 if (h2c->max_id >= 0) {
2609 h2c_send_goaway_error(h2c, NULL);
2610 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2611 return 0;
2612 }
2613
2614 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2615 }
2616 return 1;
2617 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002618 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002619}
2620
Willy Tarreau62f52692017-10-08 23:01:42 +02002621
Willy Tarreau479998a2018-11-18 06:30:59 +01002622/* Attempt to read data, and subscribe if none available.
2623 * The function returns 1 if data has been received, otherwise zero.
2624 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002625static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002626{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002627 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002628 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002629 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002630 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002631
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002632 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002633 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002634
Willy Tarreau315d8072017-12-10 22:17:57 +01002635 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002636 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002637
Willy Tarreau44e973f2018-03-01 17:49:30 +01002638 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002639 if (!buf) {
2640 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002641 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002642 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002643
Olivier Houchard7505f942018-08-21 18:10:44 +02002644 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002645 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002646 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2647 /* HTX in use : try to pre-align the buffer like the
2648 * rxbufs will be to optimize memory copies. We'll make
2649 * sure that the frame header lands at the end of the
2650 * HTX block to alias it upon recv. We cannot use the
2651 * head because rcv_buf() will realign the buffer if
2652 * it's empty. Thus we cheat and pretend we already
2653 * have a few bytes there.
2654 */
2655 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002656 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002657 }
2658 else
2659 max = b_room(buf);
2660
Olivier Houchard7505f942018-08-21 18:10:44 +02002661 if (max)
2662 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2663 else
2664 ret = 0;
2665 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002666
Olivier Houchard53216e72018-10-10 15:46:36 +02002667 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002668 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002669
Olivier Houcharda1411e62018-08-17 18:42:48 +02002670 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002671 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002672 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002673 }
2674
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002675 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002676 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002677 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002678}
2679
Willy Tarreau479998a2018-11-18 06:30:59 +01002680/* Try to send data if possible.
2681 * The function returns 1 if data have been sent, otherwise zero.
2682 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002683static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002684{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002685 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002686 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002687 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002688
2689 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002690 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002691
Olivier Houchard7505f942018-08-21 18:10:44 +02002692
Willy Tarreaua2af5122017-10-09 11:56:46 +02002693 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2694 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002695 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002696 }
2697
Willy Tarreaubc933932017-10-09 16:21:43 +02002698 /* This loop is quite simple : it tries to fill as much as it can from
2699 * pending streams into the existing buffer until it's reportedly full
2700 * or the end of send requests is reached. Then it tries to send this
2701 * buffer's contents out, marks it not full if at least one byte could
2702 * be sent, and tries again.
2703 *
2704 * The snd_buf() function normally takes a "flags" argument which may
2705 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2706 * data immediately comes and CO_SFL_STREAMER to indicate that the
2707 * connection is streaming lots of data (used to increase TLS record
2708 * size at the expense of latency). The former can be sent any time
2709 * there's a buffer full flag, as it indicates at least one stream
2710 * attempted to send and failed so there are pending data. An
2711 * alternative would be to set it as long as there's an active stream
2712 * but that would be problematic for ACKs until we have an absolute
2713 * guarantee that all waiters have at least one byte to send. The
2714 * latter should possibly not be set for now.
2715 */
2716
2717 done = 0;
2718 while (!done) {
2719 unsigned int flags = 0;
2720
2721 /* fill as much as we can into the current buffer */
2722 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2723 done = h2_process_mux(h2c);
2724
Olivier Houchard2b094432019-01-29 18:28:36 +01002725 if (h2c->flags & H2_CF_MUX_MALLOC)
2726 break;
2727
Willy Tarreaubc933932017-10-09 16:21:43 +02002728 if (conn->flags & CO_FL_ERROR)
2729 break;
2730
2731 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2732 flags |= CO_SFL_MSG_MORE;
2733
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002734 if (b_data(&h2c->mbuf)) {
2735 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002736 if (!ret)
2737 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002738 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002739 b_del(&h2c->mbuf, ret);
2740 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002741 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002742
2743 /* wrote at least one byte, the buffer is not full anymore */
2744 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2745 }
2746
Willy Tarreaua2af5122017-10-09 11:56:46 +02002747 if (conn->flags & CO_FL_SOCK_WR_SH) {
2748 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002749 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002750 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002751 /* We're not full anymore, so we can wake any task that are waiting
2752 * for us.
2753 */
2754 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002755 struct h2s *h2s;
2756
2757 list_for_each_entry(h2s, &h2c->send_list, list) {
2758 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2759 break;
2760 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2761 continue;
2762
2763 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002764 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2765 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002766 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002767 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002768 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002769 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002770 /* We're done, no more to send */
2771 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002772 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002773schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002774 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2775 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002776 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002777}
2778
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002779/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002780static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2781{
2782 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002783 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002784
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002785 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002786 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002787 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002788 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002789 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002790 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002791 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002792}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002793
Willy Tarreau62f52692017-10-08 23:01:42 +02002794/* callback called on any event by the connection handler.
2795 * It applies changes and returns zero, or < 0 if it wants immediate
2796 * destruction of the connection (which normally doesn not happen in h2).
2797 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002798static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002799{
Olivier Houchard7505f942018-08-21 18:10:44 +02002800 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002801
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002802 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002803 h2_process_demux(h2c);
2804
2805 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002806 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002807
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002808 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002809 h2c->flags &= ~H2_CF_DEM_DFULL;
2810 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002811 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002812
Willy Tarreau0b37d652018-10-03 10:33:02 +02002813 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002814 /* frontend is stopping, reload likely in progress, let's try
2815 * to announce a graceful shutdown if not yet done. We don't
2816 * care if it fails, it will be tried again later.
2817 */
2818 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2819 if (h2c->last_sid < 0)
2820 h2c->last_sid = (1U << 31) - 1;
2821 h2c_send_goaway_error(h2c, NULL);
2822 }
2823 }
2824
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002825 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002826 * If we received early data, and the handshake is done, wake
2827 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002828 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002829 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2830 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2831 struct eb32_node *node;
2832 struct h2s *h2s;
2833
2834 h2c->flags |= H2_CF_WAIT_FOR_HS;
2835 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2836
2837 while (node) {
2838 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002839 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002840 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002841 node = eb32_next(node);
2842 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002843 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002844
Willy Tarreau26bd7612017-10-09 16:47:04 +02002845 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002846 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2847 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2848 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002849 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002850
2851 if (eb_is_empty(&h2c->streams_by_id)) {
2852 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002853 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002854 return -1;
2855 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002856 }
2857
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002858 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002859 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002860
Olivier Houchard53216e72018-10-10 15:46:36 +02002861 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2862 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2863 (h2c->st0 != H2_CS_ERROR &&
2864 !b_data(&h2c->mbuf) &&
2865 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2866 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002867 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002868
Willy Tarreau3f133572017-10-31 19:21:06 +01002869 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002870 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002871 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002872 task_queue(h2c->task);
2873 }
2874 else
2875 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002876 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002877
Olivier Houchard7505f942018-08-21 18:10:44 +02002878 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002879 return 0;
2880}
2881
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002882/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002883static int h2_wake(struct connection *conn)
2884{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002885 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002886
2887 return (h2_process(h2c));
2888}
2889
Willy Tarreauea392822017-10-31 10:02:25 +01002890/* Connection timeout management. The principle is that if there's no receipt
2891 * nor sending for a certain amount of time, the connection is closed. If the
2892 * MUX buffer still has lying data or is not allocatable, the connection is
2893 * immediately killed. If it's allocatable and empty, we attempt to send a
2894 * GOAWAY frame.
2895 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002896static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002897{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002898 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002899 int expired = tick_is_expired(t->expire, now_ms);
2900
Willy Tarreau0975f112018-03-29 15:22:59 +02002901 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002902 return t;
2903
Willy Tarreau0975f112018-03-29 15:22:59 +02002904 task_delete(t);
2905 task_free(t);
2906
2907 if (!h2c) {
2908 /* resources were already deleted */
2909 return NULL;
2910 }
2911
2912 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002913 h2c_error(h2c, H2_ERR_NO_ERROR);
2914 h2_wake_some_streams(h2c, 0, 0);
2915
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002916 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002917 /* don't even try to send a GOAWAY, the buffer is stuck */
2918 h2c->flags |= H2_CF_GOAWAY_FAILED;
2919 }
2920
2921 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002922 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002923 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2924 h2c->flags |= H2_CF_GOAWAY_FAILED;
2925
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002926 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2927 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002928 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002929 b_del(&h2c->mbuf, ret);
2930 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002931 }
2932 }
Willy Tarreauea392822017-10-31 10:02:25 +01002933
Willy Tarreau0975f112018-03-29 15:22:59 +02002934 /* either we can release everything now or it will be done later once
2935 * the last stream closes.
2936 */
2937 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002938 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002939
Willy Tarreauea392822017-10-31 10:02:25 +01002940 return NULL;
2941}
2942
2943
Willy Tarreau62f52692017-10-08 23:01:42 +02002944/*******************************************/
2945/* functions below are used by the streams */
2946/*******************************************/
2947
2948/*
2949 * Attach a new stream to a connection
2950 * (Used for outgoing connections)
2951 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002952static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002953{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002954 struct conn_stream *cs;
2955 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002956 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002957
2958 cs = cs_new(conn);
2959 if (!cs)
2960 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002961 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002962 if (!h2s) {
2963 cs_free(cs);
2964 return NULL;
2965 }
2966 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002967}
2968
Willy Tarreaufafd3982018-11-18 21:29:20 +01002969/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2970 * We have to scan because we may have some orphan streams. It might be
2971 * beneficial to scan backwards from the end to reduce the likeliness to find
2972 * orphans.
2973 */
2974static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2975{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002976 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002977 struct h2s *h2s;
2978 struct eb32_node *node;
2979
2980 node = eb32_first(&h2c->streams_by_id);
2981 while (node) {
2982 h2s = container_of(node, struct h2s, by_id);
2983 if (h2s->cs)
2984 return h2s->cs;
2985 node = eb32_next(node);
2986 }
2987 return NULL;
2988}
2989
Willy Tarreau62f52692017-10-08 23:01:42 +02002990/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002991 * Destroy the mux and the associated connection, if it is no longer used
2992 */
Christopher Faulet73c12072019-04-08 11:23:22 +02002993static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01002994{
Christopher Faulet73c12072019-04-08 11:23:22 +02002995 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002996
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002997 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002998 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01002999}
3000
3001/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003002 * Detach the stream from the connection and possibly release the connection.
3003 */
3004static void h2_detach(struct conn_stream *cs)
3005{
Willy Tarreau60935142017-10-16 18:11:19 +02003006 struct h2s *h2s = cs->ctx;
3007 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003008 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003009
3010 cs->ctx = NULL;
3011 if (!h2s)
3012 return;
3013
Olivier Houchardf502aca2018-12-14 19:42:40 +01003014 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003015 h2c = h2s->h2c;
3016 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003017 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003018 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3019 !h2_frt_has_too_many_cs(h2c)) {
3020 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003021 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003022 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003023 }
Willy Tarreau60935142017-10-16 18:11:19 +02003024
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003025 /* this stream may be blocked waiting for some data to leave (possibly
3026 * an ES or RST frame), so orphan it in this case.
3027 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003028 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003029 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003030 (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 +01003031 return;
3032
Willy Tarreau45f752e2017-10-30 15:44:59 +01003033 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3034 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3035 /* unblock the connection if it was blocked on this
3036 * stream.
3037 */
3038 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3039 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003040 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003041 }
3042
Willy Tarreau71049cc2018-03-28 13:56:39 +02003043 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003044
Olivier Houchard8a786902018-12-15 16:05:40 +01003045 if (h2c->flags & H2_CF_IS_BACK &&
3046 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003047 if (!(h2c->conn->flags &
3048 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3049 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003050 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003051 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3052 h2c->conn->owner = NULL;
3053 if (eb_is_empty(&h2c->streams_by_id)) {
3054 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3055 /* The server doesn't want it, let's kill the connection right away */
3056 h2c->conn->mux->destroy(h2c->conn);
3057 return;
3058 }
3059 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003060 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003061 if (eb_is_empty(&h2c->streams_by_id)) {
3062 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3063 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3064 return;
3065 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003066 /* Never ever allow to reuse a connection from a non-reuse backend */
3067 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3068 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003069 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003070 struct server *srv = objt_server(h2c->conn->target);
3071
3072 if (srv) {
3073 if (h2c->conn->flags & CO_FL_PRIVATE)
3074 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3075 else
3076 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3077 }
3078
3079 }
3080 }
3081 }
3082
Willy Tarreaue323f342018-03-28 13:51:45 +02003083 /* We don't want to close right now unless we're removing the
3084 * last stream, and either the connection is in error, or it
3085 * reached the ID already specified in a GOAWAY frame received
3086 * or sent (as seen by last_sid >= 0).
3087 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003088 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003089 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003090 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003091 }
3092 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003093 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003094 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3095 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003096 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003097 else
3098 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003099 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003100}
3101
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003102/* Performs a synchronous or asynchronous shutr().
3103 * FIXME: guess what the return code tries to indicate!
3104 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003105static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003106{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003107 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003108 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003109
Willy Tarreau721c9742017-11-07 11:05:42 +01003110 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003111 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003112
Willy Tarreau18059042019-01-31 19:12:48 +01003113 /* a connstream may require us to immediately kill the whole connection
3114 * for example because of a "tcp-request content reject" rule that is
3115 * normally used to limit abuse. In this case we schedule a goaway to
3116 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003117 */
Willy Tarreau18059042019-01-31 19:12:48 +01003118 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3119 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3120 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3121 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3122 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003123 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3124 /* Nothing was never sent for this stream, so reset with
3125 * REFUSED_STREAM error to let the client retry the
3126 * request.
3127 */
3128 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3129 }
Willy Tarreau18059042019-01-31 19:12:48 +01003130
Willy Tarreau90c32322017-11-24 08:00:30 +01003131 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003132 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003133 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003134
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003135 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003136 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003137 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003138
Olivier Houchard7a977432019-03-21 15:47:13 +01003139 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003140add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003141 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003142 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003143 if (h2s->flags & H2_SF_BLK_MFCTL) {
3144 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3145 h2s->send_wait = sw;
3146 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3147 h2s->send_wait = sw;
3148 LIST_ADDQ(&h2c->send_list, &h2s->list);
3149 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003150 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003151 /* Let the handler know we want shutr */
3152 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003153 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003154}
3155
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003156/* Performs a synchronous or asynchronous shutw().
3157 * FIXME: guess what the return code tries to indicate!
3158 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003159static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003160{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003161 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003162 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003163
Willy Tarreau721c9742017-11-07 11:05:42 +01003164 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003165 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003166
Willy Tarreau67434202017-11-06 20:20:51 +01003167 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003168 /* we can cleanly close using an empty data frame only after headers */
3169
3170 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3171 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003172 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003173
3174 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003175 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003176 else
3177 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003178 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003179 /* a connstream may require us to immediately kill the whole connection
3180 * for example because of a "tcp-request content reject" rule that is
3181 * normally used to limit abuse. In this case we schedule a goaway to
3182 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003183 */
Willy Tarreau18059042019-01-31 19:12:48 +01003184 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3185 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3186 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3187 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3188 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003189 else {
3190 /* Nothing was never sent for this stream, so reset with
3191 * REFUSED_STREAM error to let the client retry the
3192 * request.
3193 */
3194 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3195 }
Willy Tarreau18059042019-01-31 19:12:48 +01003196
Willy Tarreau90c32322017-11-24 08:00:30 +01003197 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003198 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003199 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003200
Willy Tarreau00dd0782018-03-01 16:31:34 +01003201 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003202 }
3203
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003204 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003205 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003206 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003207
3208 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003209 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003210 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003211 if (h2s->flags & H2_SF_BLK_MFCTL) {
3212 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3213 h2s->send_wait = sw;
3214 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3215 h2s->send_wait = sw;
3216 LIST_ADDQ(&h2c->send_list, &h2s->list);
3217 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003218 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003219 /* let the handler know we want to shutw */
3220 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003221 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003222}
3223
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003224/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3225 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3226 * and prevented the last frame from being emitted.
3227 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003228static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3229{
3230 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003231 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003232 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003233
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003234 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003235 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003236 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003237 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003238 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003239 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003240 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003241 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003242 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003243
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003244 if (!ret) {
3245 /* We're done trying to send, remove ourself from the send_list */
3246 h2s->send_wait->events &= ~SUB_RETRY_SEND;
3247 h2s->send_wait = NULL;
3248 LIST_DEL_INIT(&h2s->list);
3249 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003250 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003251 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003252 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003253 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003254
3255 if (h2c_is_dead(h2c))
Christopher Faulet73c12072019-04-08 11:23:22 +02003256 h2_release(h2c);
Olivier Houchard7a977432019-03-21 15:47:13 +01003257 }
3258
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003259 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003260}
3261
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003262/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003263static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3264{
3265 struct h2s *h2s = cs->ctx;
3266
3267 if (!mode)
3268 return;
3269
3270 h2_do_shutr(h2s);
3271}
3272
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003273/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003274static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3275{
3276 struct h2s *h2s = cs->ctx;
3277
3278 h2_do_shutw(h2s);
3279}
3280
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003281/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003282 * HTX request or response depending on the connection's side. Returns a
3283 * positive value on success, a negative value on failure, or 0 if it couldn't
3284 * proceed. May report connection errors in h2c->errcode if the frame is
3285 * non-decodable and the connection unrecoverable. In absence of connection
3286 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003287 *
3288 * The function may fold CONTINUATION frames into the initial HEADERS frame
3289 * by removing padding and next frame header, then moving the CONTINUATION
3290 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3291 * leaving a hole between the main frame and the beginning of the next one.
3292 * The possibly remaining incomplete or next frame at the end may be moved
3293 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3294 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3295 *
3296 * A buffer at the beginning of processing may look like this :
3297 *
3298 * ,---.---------.-----.--------------.--------------.------.---.
3299 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3300 * `---^---------^-----^--------------^--------------^------^---'
3301 * | | <-----> | |
3302 * area | dpl | wrap
3303 * |<--------------> |
3304 * | dfl |
3305 * |<-------------------------------------------------->|
3306 * head data
3307 *
3308 * Padding is automatically overwritten when folding, participating to the
3309 * hole size after dfl :
3310 *
3311 * ,---.------------------------.-----.--------------.------.---.
3312 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3313 * `---^------------------------^-----^--------------^------^---'
3314 * | | <-----> | |
3315 * area | hole | wrap
3316 * |<-----------------------> |
3317 * | dfl |
3318 * |<-------------------------------------------------->|
3319 * head data
3320 *
3321 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3322 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3323 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003324 *
3325 * The <flags> field must point to either the stream's flags or to a copy of it
3326 * so that the function can update the following flags :
3327 * - H2_SF_DATA_CLEN when content-length is seen
3328 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3329 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003330 *
3331 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3332 * decoding, in order to detect if we're dealing with a headers or a trailers
3333 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003334 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003335static 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 +02003336{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003337 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003338 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003339 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003340 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003341 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003342 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003343 int flen; // header frame len
3344 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003345 int ret = 0;
3346 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003347 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003348 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003349
Willy Tarreauea18f862018-12-22 20:19:26 +01003350next_frame:
3351 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3352 goto leave; // incomplete input frame
3353
3354 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3355 * this case, we'll try to paste it immediately after the initial
3356 * HEADERS frame payload and kill any possible padding. The initial
3357 * frame's length will be increased to represent the concatenation
3358 * of the two frames. The next frame is read from position <tlen>
3359 * and written at position <flen> (minus padding if some is present).
3360 */
3361 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3362 struct h2_fh hdr;
3363 int clen; // CONTINUATION frame's payload length
3364
3365 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3366 /* no more data, the buffer may be full, either due to
3367 * too large a frame or because of too large a hole that
3368 * we're going to compact at the end.
3369 */
3370 goto leave;
3371 }
3372
3373 if (hdr.ft != H2_FT_CONTINUATION) {
3374 /* RFC7540#6.10: frame of unexpected type */
3375 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3376 goto fail;
3377 }
3378
3379 if (hdr.sid != h2c->dsi) {
3380 /* RFC7540#6.10: frame of different stream */
3381 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3382 goto fail;
3383 }
3384
3385 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3386 /* RFC7540#4.2: invalid frame length */
3387 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3388 goto fail;
3389 }
3390
3391 /* detect when we must stop aggragating frames */
3392 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3393
3394 /* Take as much as we can of the CONTINUATION frame's payload */
3395 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3396 if (clen > hdr.len)
3397 clen = hdr.len;
3398
3399 /* Move the frame's payload over the padding, hole and frame
3400 * header. At least one of hole or dpl is null (see diagrams
3401 * above). The hole moves after the new aggragated frame.
3402 */
3403 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3404 h2c->dfl += clen - h2c->dpl;
3405 hole += h2c->dpl + 9;
3406 h2c->dpl = 0;
3407 goto next_frame;
3408 }
3409
3410 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003411
Willy Tarreau13278b42017-10-13 19:23:14 +02003412 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003413 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003414 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003415 copy = alloc_trash_chunk();
3416 if (!copy) {
3417 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3418 goto fail;
3419 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003420 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3421 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3422 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003423 }
3424
Willy Tarreau13278b42017-10-13 19:23:14 +02003425 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3426 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003427 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003428 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3429 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003430 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003431 }
3432
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003433 if (flen < 5) {
3434 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3435 goto fail;
3436 }
3437
Willy Tarreau13278b42017-10-13 19:23:14 +02003438 hdrs += 5; // stream dep = 4, weight = 1
3439 flen -= 5;
3440 }
3441
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003442 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003443 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003444 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003445 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003446
Willy Tarreau937f7602018-02-26 15:22:17 +01003447 /* we can't retry a failed decompression operation so we must be very
3448 * careful not to take any risks. In practice the output buffer is
3449 * always empty except maybe for trailers, in which case we simply have
3450 * to wait for the upper layer to finish consuming what is available.
3451 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003452
3453 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003454 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003455 if (!htx_is_empty(htx)) {
3456 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003457 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003458 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003459 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003460 if (b_data(rxbuf)) {
3461 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003462 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003463 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003464
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003465 rxbuf->head = 0;
3466 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003467 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003468
Willy Tarreau25919232019-01-03 14:48:18 +01003469 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003470 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3471 sizeof(list)/sizeof(list[0]), tmp);
3472 if (outlen < 0) {
3473 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3474 goto fail;
3475 }
3476
Willy Tarreau25919232019-01-03 14:48:18 +01003477 /* The PACK decompressor was updated, let's update the input buffer and
3478 * the parser's state to commit these changes and allow us to later
3479 * fail solely on the stream if needed.
3480 */
3481 b_del(&h2c->dbuf, h2c->dfl + hole);
3482 h2c->dfl = hole = 0;
3483 h2c->st0 = H2_CS_FRAME_H;
3484
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003485 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003486 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003487
Willy Tarreau88d138e2019-01-02 19:38:14 +01003488 if (*flags & H2_SF_HEADERS_RCVD)
3489 goto trailers;
3490
3491 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003492 if (htx) {
3493 /* HTX mode */
3494 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003495 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003496 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003497 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003498 } else {
3499 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003500 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003501 if (outlen > 0)
3502 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003503 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003504
3505 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003506 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003507 goto fail;
3508 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003509
Willy Tarreau174b06a2018-04-25 18:13:58 +02003510 if (msgf & H2_MSGF_BODY) {
3511 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003512 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003513 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003514 if (htx)
3515 htx->extra = *body_len;
3516 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003517 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003518 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003519 }
3520
Willy Tarreau88d138e2019-01-02 19:38:14 +01003521 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003522 /* indicate that a HEADERS frame was received for this stream, except
3523 * for 1xx responses. For 1xx responses, another HEADERS frame is
3524 * expected.
3525 */
3526 if (!(msgf & H2_MSGF_RSP_1XX))
3527 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003528
Christopher Faulet0b465482019-02-19 15:14:23 +01003529 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003530 /* Mark the end of message, either using EOM in HTX or with the
3531 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3532 * is not set during headers with END_STREAM.
3533 */
3534 if (htx) {
3535 if (!htx_add_endof(htx, HTX_BLK_EOM))
3536 goto fail;
3537 }
3538 else if (*flags & H2_SF_DATA_CHNK) {
3539 if (!b_putblk(rxbuf, "\r\n", 2))
3540 goto fail;
3541 }
3542 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003543
Willy Tarreau86277d42019-01-02 15:36:11 +01003544 /* success */
3545 ret = 1;
3546
Willy Tarreau68dd9852017-07-03 14:44:26 +02003547 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003548 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003549 * move the remaining data over it.
3550 */
3551 if (hole) {
3552 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3553 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3554 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3555 b_sub(&h2c->dbuf, hole);
3556 }
3557
3558 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3559 /* too large frames */
3560 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003561 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003562 }
3563
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003564 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003565 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003566 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003567 return ret;
3568
Willy Tarreau68dd9852017-07-03 14:44:26 +02003569 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003570 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003571 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003572
3573 trailers:
3574 /* This is the last HEADERS frame hence a trailer */
3575
3576 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3577 /* It's a trailer but it's missing ES flag */
3578 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3579 goto fail;
3580 }
3581
3582 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3583 * block, and when using chunks we must send the 0 CRLF marker. For
3584 * other modes, the trailers are silently dropped.
3585 */
3586 if (htx) {
3587 if (!htx_add_endof(htx, HTX_BLK_EOD))
3588 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003589 if (h2_make_htx_trailers(list, htx) <= 0)
3590 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003591 }
3592 else if (*flags & H2_SF_DATA_CHNK) {
3593 /* Legacy mode with chunked encoding : we must finalize the
3594 * data block message emit the trailing CRLF */
3595 if (!b_putblk(rxbuf, "0\r\n", 3))
3596 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003597
3598 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3599 if (outlen > 0)
3600 b_add(rxbuf, outlen);
3601 else
3602 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003603 }
3604
3605 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003606}
3607
Willy Tarreau454f9052017-10-26 19:40:35 +02003608/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3609 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3610 * in use, a new chunk is emitted for each frame. This is supposed to fit
3611 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3612 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3613 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003614 * parser state is automatically updated. Returns > 0 if it could completely
3615 * send the current frame, 0 if it couldn't complete, in which case
3616 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3617 * DATA frame can return 0 as a valid result). Stream errors are reported in
3618 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3619 * have checked the frame header and ensured that the frame was complete or the
3620 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003621 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003622static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003623{
3624 struct h2c *h2c = h2s->h2c;
3625 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003626 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003627 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003628 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003629 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003630
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003631 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003632
Olivier Houchard638b7992018-08-16 15:41:52 +02003633 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003634 if (!csbuf) {
3635 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003636 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003637 }
3638
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003639try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003640 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003641 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3642 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003643 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003644 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003645
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003646 if (flen > b_data(&h2c->dbuf)) {
3647 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003648 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003649 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003650 }
3651
Willy Tarreaua9b77962019-01-31 07:23:00 +01003652 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003653 block1 = htx_free_data_space(htx);
3654 if (!block1) {
3655 h2c->flags |= H2_CF_DEM_SFULL;
3656 goto fail;
3657 }
3658 if (flen > block1)
3659 flen = block1;
3660
3661 /* here, flen is the max we can copy into the output buffer */
3662 block1 = b_contig_data(&h2c->dbuf, 0);
3663 if (flen > block1)
3664 flen = block1;
3665
3666 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3667 h2c->flags |= H2_CF_DEM_SFULL;
3668 goto fail;
3669 }
3670
3671 b_del(&h2c->dbuf, flen);
3672 h2c->dfl -= flen;
3673 h2c->rcvd_c += flen;
3674 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003675
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003676 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003677 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003678 htx->extra = h2s->body_len;
3679 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003680 goto try_again;
3681 }
3682 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003683 /* it doesn't fit and the buffer is fragmented,
3684 * so let's defragment it and try again.
3685 */
3686 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003687 }
3688
Willy Tarreaueba10f22018-04-25 20:44:22 +02003689 /* chunked-encoding requires more room */
3690 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003691 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003692 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3693 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3694 (chklen < 1048576) ? 4 : 8;
3695 chklen += 4; // CRLF, CRLF
3696 }
3697
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003698 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003699 if (flen + chklen > b_room(csbuf)) {
3700 if (chklen >= b_room(csbuf)) {
3701 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003702 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003703 }
3704 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003705 }
3706
3707 if (h2s->flags & H2_SF_DATA_CHNK) {
3708 /* emit the chunk size */
3709 unsigned int chksz = flen;
3710 char str[10];
3711 char *beg;
3712
3713 beg = str + sizeof(str);
3714 *--beg = '\n';
3715 *--beg = '\r';
3716 do {
3717 *--beg = hextab[chksz & 0xF];
3718 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003719 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003720 }
3721
Willy Tarreau454f9052017-10-26 19:40:35 +02003722 /* Block1 is the length of the first block before the buffer wraps,
3723 * block2 is the optional second block to reach the end of the frame.
3724 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003725 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003726 if (block1 > flen)
3727 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003728 block2 = flen - block1;
3729
3730 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003731 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003732
3733 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003734 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003735
Willy Tarreaueba10f22018-04-25 20:44:22 +02003736 if (h2s->flags & H2_SF_DATA_CHNK) {
3737 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003738 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003739 }
3740
Willy Tarreau454f9052017-10-26 19:40:35 +02003741 /* now mark the input data as consumed (will be deleted from the buffer
3742 * by the caller when seeing FRAME_A after sending the window update).
3743 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003744 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003745 h2c->dfl -= flen;
3746 h2c->rcvd_c += flen;
3747 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3748
Willy Tarreau1915ca22019-01-24 11:49:37 +01003749 if (h2s->flags & H2_SF_DATA_CLEN)
3750 h2s->body_len -= flen;
3751
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003752 if (h2c->dfl > h2c->dpl) {
3753 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003754 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003755 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003756 }
3757
Willy Tarreau4a28da12018-01-04 14:41:00 +01003758 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003759 /* here we're done with the frame, all the payload (except padding) was
3760 * transferred.
3761 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003762
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003763 if (h2c->dff & H2_F_DATA_END_STREAM) {
3764 if (htx) {
3765 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3766 h2c->flags |= H2_CF_DEM_SFULL;
3767 goto fail;
3768 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003769 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003770 else if (h2s->flags & H2_SF_DATA_CHNK) {
3771 /* emit the trailing 0 CRLF CRLF */
3772 if (b_room(csbuf) < 5) {
3773 h2c->flags |= H2_CF_DEM_SFULL;
3774 goto fail;
3775 }
3776 chklen += 5;
3777 b_putblk(csbuf, "0\r\n\r\n", 5);
3778 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003779 }
3780
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003781 h2c->rcvd_c += h2c->dpl;
3782 h2c->rcvd_s += h2c->dpl;
3783 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003784 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003785 if (htx)
3786 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003787 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003788 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003789 if (htx)
3790 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003791 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003792}
3793
Willy Tarreau5dd17352018-06-14 13:33:30 +02003794/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3795 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3796 * number of bytes sent. The caller must check the stream's status to detect
3797 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003798 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003799static 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 +02003800{
3801 struct http_hdr list[MAX_HTTP_HDR];
3802 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003803 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003804 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003805 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003806 int es_now = 0;
3807 int ret = 0;
3808 int hdr;
3809
3810 if (h2c_mux_busy(h2c, h2s)) {
3811 h2s->flags |= H2_SF_BLK_MBUSY;
3812 return 0;
3813 }
3814
Willy Tarreau44e973f2018-03-01 17:49:30 +01003815 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003816 h2c->flags |= H2_CF_MUX_MALLOC;
3817 h2s->flags |= H2_SF_BLK_MROOM;
3818 return 0;
3819 }
3820
3821 /* First, try to parse the H1 response and index it into <list>.
3822 * NOTE! Since it comes from haproxy, we *know* that a response header
3823 * block does not wrap and we can safely read it this way without
3824 * having to realign the buffer.
3825 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003826 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003827 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003828 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003829 /* incomplete or invalid response, this is abnormal coming from
3830 * haproxy and may only result in a bad errorfile or bad Lua code
3831 * so that won't be fixed, raise an error now.
3832 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003833 * FIXME: we should instead add the ability to only return a
3834 * 502 bad gateway. But in theory this is not supposed to
3835 * happen.
3836 */
3837 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3838 ret = 0;
3839 goto end;
3840 }
3841
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003842 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003843
3844 /* certain statuses have no body or an empty one, regardless of
3845 * what the headers say.
3846 */
3847 if (sl.st.status >= 100 && sl.st.status < 200) {
3848 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3849 h1m->curr_len = h1m->body_len = 0;
3850 }
3851 else if (sl.st.status == 204 || sl.st.status == 304) {
3852 /* no contents, claim c-len is present and set to zero */
3853 h1m->flags &= ~H1_MF_CHNK;
3854 h1m->flags |= H1_MF_CLEN;
3855 h1m->curr_len = h1m->body_len = 0;
3856 }
3857
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003858 chunk_reset(&outbuf);
3859
3860 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003861 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003862 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003863 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003864
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003865 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003866 break;
3867 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003868 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003869 }
3870
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003871 if (outbuf.size < 9)
3872 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003873
3874 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003875 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3876 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3877 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003878
3879 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003880 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003881 /* this is an unparsable response */
3882 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3883 ret = 0;
3884 goto end;
3885 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003886
3887 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003888 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003889 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003890 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003891 }
3892
3893 /* encode all headers, stop at empty name */
3894 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003895 /* these ones do not exist in H2 and must be dropped. */
3896 if (isteq(list[hdr].n, ist("connection")) ||
3897 isteq(list[hdr].n, ist("proxy-connection")) ||
3898 isteq(list[hdr].n, ist("keep-alive")) ||
3899 isteq(list[hdr].n, ist("upgrade")) ||
3900 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003901 continue;
3902
3903 if (isteq(list[hdr].n, ist("")))
3904 break; // end
3905
3906 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3907 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003908 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003909 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003910 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003911 }
3912 }
3913
3914 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003915 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003916 es_now = 1;
3917
3918 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003919 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003920
3921 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003922 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003923
3924 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003925 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003926
3927 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003928 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003929 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003930
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003931 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003932 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003933 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003934
Willy Tarreau801250e2018-09-11 11:45:04 +02003935 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003936 h2s->flags |= H2_SF_ES_SENT;
3937 if (h2s->st == H2_SS_OPEN)
3938 h2s->st = H2_SS_HLOC;
3939 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003940 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003941 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003942 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003943 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003944 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003945 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003946 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003947 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003948 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003949
3950 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003951
3952 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003953 //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 +02003954 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003955 full:
3956 h1m_init_res(h1m);
3957 h1m->err_pos = -1; // don't care about errors on the response path
3958 h2c->flags |= H2_CF_MUX_MFULL;
3959 h2s->flags |= H2_SF_BLK_MROOM;
3960 ret = 0;
3961 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003962}
3963
Willy Tarreau5dd17352018-06-14 13:33:30 +02003964/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3965 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3966 * the number of bytes sent. The caller must check the stream's status to
3967 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003968 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003969static 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 +02003970{
3971 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003972 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003973 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003974 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003975 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003976 int es_now = 0;
3977 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003978 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003979 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003980
3981 if (h2c_mux_busy(h2c, h2s)) {
3982 h2s->flags |= H2_SF_BLK_MBUSY;
3983 goto end;
3984 }
3985
Willy Tarreau44e973f2018-03-01 17:49:30 +01003986 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003987 h2c->flags |= H2_CF_MUX_MALLOC;
3988 h2s->flags |= H2_SF_BLK_MROOM;
3989 goto end;
3990 }
3991
3992 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003993 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003994 goto end;
3995
3996 chunk_reset(&outbuf);
3997
3998 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003999 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004000 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004001 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004002
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004003 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004004 break;
4005 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004006 /* If there are pending data in the output buffer, and we have
4007 * less than 1/4 of the mbuf's size and everything fits, we'll
4008 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4009 * is full and wait, to save some slow realign calls.
4010 */
4011 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4012 h2c->flags |= H2_CF_MUX_MFULL;
4013 h2s->flags |= H2_SF_BLK_MROOM;
4014 goto end;
4015 }
4016
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004017 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004018 }
4019
4020 if (outbuf.size < 9) {
4021 h2c->flags |= H2_CF_MUX_MFULL;
4022 h2s->flags |= H2_SF_BLK_MROOM;
4023 goto end;
4024 }
4025
4026 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004027 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4028 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4029 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004030
4031 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4032 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004033 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004034 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004035 break;
4036 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004037 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004038 if ((long long)size > h1m->curr_len)
4039 size = h1m->curr_len;
4040 break;
4041 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004042 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004043 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004044 if (!ret)
4045 goto end;
4046
4047 if (ret < 0) {
4048 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004049 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004050 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4051 goto end;
4052 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004053 max -= ret;
4054 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004055 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004056 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004057 }
4058
Willy Tarreau801250e2018-09-11 11:45:04 +02004059 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004060 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004061 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004062 if (!ret)
4063 goto end;
4064
4065 if (ret < 0) {
4066 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004067 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004068 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4069 goto end;
4070 }
4071
4072 size = chunk;
4073 h1m->curr_len = chunk;
4074 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004075 max -= ret;
4076 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004077 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004078 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004079 if (!size)
4080 goto send_empty;
4081 }
4082
4083 /* in MSG_DATA state, continue below */
4084 size = h1m->curr_len;
4085 break;
4086 }
4087
4088 /* we have in <size> the exact number of bytes we need to copy from
4089 * the H1 buffer. We need to check this against the connection's and
4090 * the stream's send windows, and to ensure that this fits in the max
4091 * frame size and in the buffer's available space minus 9 bytes (for
4092 * the frame header). The connection's flow control is applied last so
4093 * that we can use a separate list of streams which are immediately
4094 * unblocked on window opening. Note: we don't implement padding.
4095 */
4096
Willy Tarreau5dd17352018-06-14 13:33:30 +02004097 if (size > max)
4098 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004099
4100 if (size > h2s->mws)
4101 size = h2s->mws;
4102
4103 if (size <= 0) {
4104 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004105 if (h2s->send_wait) {
4106 LIST_DEL(&h2s->list);
4107 LIST_INIT(&h2s->list);
4108 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004109 goto end;
4110 }
4111
4112 if (h2c->mfs && size > h2c->mfs)
4113 size = h2c->mfs;
4114
4115 if (size + 9 > outbuf.size) {
4116 /* we have an opportunity for enlarging the too small
4117 * available space, let's try.
4118 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004119 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004120 goto realign_again;
4121 size = outbuf.size - 9;
4122 }
4123
4124 if (size <= 0) {
4125 h2c->flags |= H2_CF_MUX_MFULL;
4126 h2s->flags |= H2_SF_BLK_MROOM;
4127 goto end;
4128 }
4129
4130 if (size > h2c->mws)
4131 size = h2c->mws;
4132
4133 if (size <= 0) {
4134 h2s->flags |= H2_SF_BLK_MFCTL;
4135 goto end;
4136 }
4137
4138 /* copy whatever we can */
4139 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004140 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004141 if (ret == 1)
4142 len2 = 0;
4143
4144 if (!ret || len1 + len2 < size) {
4145 /* FIXME: must normally never happen */
4146 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4147 goto end;
4148 }
4149
4150 /* limit len1/len2 to size */
4151 if (len1 + len2 > size) {
4152 int sub = len1 + len2 - size;
4153
4154 if (len2 > sub)
4155 len2 -= sub;
4156 else {
4157 sub -= len2;
4158 len2 = 0;
4159 len1 -= sub;
4160 }
4161 }
4162
4163 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004164 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004165 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004166 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004167
4168 send_empty:
4169 /* we may need to add END_STREAM */
4170 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4171 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004172 *
4173 * FIXME: what we do here is not correct because we send end_stream
4174 * before knowing if we'll have to send a HEADERS frame for the
4175 * trailers. More importantly we're not consuming the trailing CRLF
4176 * after the end of trailers, so it will be left to the caller to
4177 * eat it. The right way to do it would be to measure trailers here
4178 * and to send ES only if there are no trailers.
4179 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004180 */
4181 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004182 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004183 es_now = 1;
4184
4185 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004186 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004187
4188 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004189 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004190
4191 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004192 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004193
4194 /* consume incoming H1 response */
4195 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004196 max -= size;
4197 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004198 total += size;
4199 h1m->curr_len -= size;
4200 h2s->mws -= size;
4201 h2c->mws -= size;
4202
4203 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004204 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004205 goto new_frame;
4206 }
4207 }
4208
4209 if (es_now) {
4210 if (h2s->st == H2_SS_OPEN)
4211 h2s->st = H2_SS_HLOC;
4212 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004213 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004214
Willy Tarreau35a62702018-02-27 15:37:25 +01004215 if (!(h1m->flags & H1_MF_CHNK)) {
4216 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004217 total += max;
4218 ofs += max;
4219 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004220
Willy Tarreau801250e2018-09-11 11:45:04 +02004221 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004222 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004223
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004224 h2s->flags |= H2_SF_ES_SENT;
4225 }
4226
4227 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004228 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 +02004229 return total;
4230}
4231
Willy Tarreau115e83b2018-12-01 19:17:53 +01004232/* Try to send a HEADERS frame matching HTX response present in HTX message
4233 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4234 * must check the stream's status to detect any error which might have happened
4235 * subsequently to a successful send. The htx blocks are automatically removed
4236 * from the message. The htx message is assumed to be valid since produced from
4237 * the internal code, hence it contains a start line, an optional series of
4238 * header blocks and an end of header, otherwise an invalid frame could be
4239 * emitted and the resulting htx message could be left in an inconsistent state.
4240 */
4241static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4242{
4243 struct http_hdr list[MAX_HTTP_HDR];
4244 struct h2c *h2c = h2s->h2c;
4245 struct htx_blk *blk;
4246 struct htx_blk *blk_end;
4247 struct buffer outbuf;
4248 struct htx_sl *sl;
4249 enum htx_blk_type type;
4250 int es_now = 0;
4251 int ret = 0;
4252 int hdr;
4253 int idx;
4254
4255 if (h2c_mux_busy(h2c, h2s)) {
4256 h2s->flags |= H2_SF_BLK_MBUSY;
4257 return 0;
4258 }
4259
4260 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4261 h2c->flags |= H2_CF_MUX_MALLOC;
4262 h2s->flags |= H2_SF_BLK_MROOM;
4263 return 0;
4264 }
4265
4266 /* determine the first block which must not be deleted, blk_end may
4267 * be NULL if all blocks have to be deleted.
4268 */
4269 idx = htx_get_head(htx);
4270 blk_end = NULL;
4271 while (idx != -1) {
4272 type = htx_get_blk_type(htx_get_blk(htx, idx));
4273 idx = htx_get_next(htx, idx);
4274 if (type == HTX_BLK_EOH) {
4275 if (idx != -1)
4276 blk_end = htx_get_blk(htx, idx);
4277 break;
4278 }
4279 }
4280
4281 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004282 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004283 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004284 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004285 if (h2s->status < 100 || h2s->status > 999)
4286 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004287
4288 /* and the rest of the headers, that we dump starting at header 0 */
4289 hdr = 0;
4290
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004291 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004292 while ((idx = htx_get_next(htx, idx)) != -1) {
4293 blk = htx_get_blk(htx, idx);
4294 type = htx_get_blk_type(blk);
4295
4296 if (type == HTX_BLK_UNUSED)
4297 continue;
4298
4299 if (type != HTX_BLK_HDR)
4300 break;
4301
4302 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4303 goto fail;
4304
4305 list[hdr].n = htx_get_blk_name(htx, blk);
4306 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004307 hdr++;
4308 }
4309
4310 /* marker for end of headers */
4311 list[hdr].n = ist("");
4312
4313 if (h2s->status == 204 || h2s->status == 304) {
4314 /* no contents, claim c-len is present and set to zero */
4315 es_now = 1;
4316 }
4317
4318 chunk_reset(&outbuf);
4319
4320 while (1) {
4321 outbuf.area = b_tail(&h2c->mbuf);
4322 outbuf.size = b_contig_space(&h2c->mbuf);
4323 outbuf.data = 0;
4324
4325 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4326 break;
4327 realign_again:
4328 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4329 }
4330
4331 if (outbuf.size < 9)
4332 goto full;
4333
4334 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4335 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4336 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4337 outbuf.data = 9;
4338
4339 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004340 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004341 if (b_space_wraps(&h2c->mbuf))
4342 goto realign_again;
4343 goto full;
4344 }
4345
4346 /* encode all headers, stop at empty name */
4347 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4348 /* these ones do not exist in H2 and must be dropped. */
4349 if (isteq(list[hdr].n, ist("connection")) ||
4350 isteq(list[hdr].n, ist("proxy-connection")) ||
4351 isteq(list[hdr].n, ist("keep-alive")) ||
4352 isteq(list[hdr].n, ist("upgrade")) ||
4353 isteq(list[hdr].n, ist("transfer-encoding")))
4354 continue;
4355
4356 if (isteq(list[hdr].n, ist("")))
4357 break; // end
4358
4359 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4360 /* output full */
4361 if (b_space_wraps(&h2c->mbuf))
4362 goto realign_again;
4363 goto full;
4364 }
4365 }
4366
Christopher Faulet0b465482019-02-19 15:14:23 +01004367 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004368 * FIXME: we should also set it when we know for sure that the
4369 * content-length is zero as well as on 204/304
4370 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004371 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4372 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004373 es_now = 1;
4374
Willy Tarreau927b88b2019-03-04 08:03:25 +01004375 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004376 es_now = 1;
4377
4378 /* update the frame's size */
4379 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4380
4381 if (es_now)
4382 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4383
4384 /* commit the H2 response */
4385 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004386
4387 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4388 * 1xx responses, another HEADERS frame is expected.
4389 */
4390 if (h2s->status >= 200 || h2s->status == 101)
4391 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004392
Willy Tarreau115e83b2018-12-01 19:17:53 +01004393 if (es_now) {
4394 h2s->flags |= H2_SF_ES_SENT;
4395 if (h2s->st == H2_SS_OPEN)
4396 h2s->st = H2_SS_HLOC;
4397 else
4398 h2s_close(h2s);
4399 }
4400
4401 /* OK we could properly deliver the response */
4402
4403 /* remove all header blocks including the EOH and compute the
4404 * corresponding size.
4405 *
4406 * FIXME: We should remove everything when es_now is set.
4407 */
4408 ret = 0;
4409 idx = htx_get_head(htx);
4410 blk = htx_get_blk(htx, idx);
4411 while (blk != blk_end) {
4412 ret += htx_get_blksz(blk);
4413 blk = htx_remove_blk(htx, blk);
4414 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004415
4416 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4417 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004418 end:
4419 return ret;
4420 full:
4421 h2c->flags |= H2_CF_MUX_MFULL;
4422 h2s->flags |= H2_SF_BLK_MROOM;
4423 ret = 0;
4424 goto end;
4425 fail:
4426 /* unparsable HTX messages, too large ones to be produced in the local
4427 * list etc go here (unrecoverable errors).
4428 */
4429 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4430 ret = 0;
4431 goto end;
4432}
4433
Willy Tarreau80739692018-10-05 11:35:57 +02004434/* Try to send a HEADERS frame matching HTX request present in HTX message
4435 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4436 * must check the stream's status to detect any error which might have happened
4437 * subsequently to a successful send. The htx blocks are automatically removed
4438 * from the message. The htx message is assumed to be valid since produced from
4439 * the internal code, hence it contains a start line, an optional series of
4440 * header blocks and an end of header, otherwise an invalid frame could be
4441 * emitted and the resulting htx message could be left in an inconsistent state.
4442 */
4443static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4444{
4445 struct http_hdr list[MAX_HTTP_HDR];
4446 struct h2c *h2c = h2s->h2c;
4447 struct htx_blk *blk;
4448 struct htx_blk *blk_end;
4449 struct buffer outbuf;
4450 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004451 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004452 enum htx_blk_type type;
4453 int es_now = 0;
4454 int ret = 0;
4455 int hdr;
4456 int idx;
4457
4458 if (h2c_mux_busy(h2c, h2s)) {
4459 h2s->flags |= H2_SF_BLK_MBUSY;
4460 return 0;
4461 }
4462
4463 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4464 h2c->flags |= H2_CF_MUX_MALLOC;
4465 h2s->flags |= H2_SF_BLK_MROOM;
4466 return 0;
4467 }
4468
4469 /* determine the first block which must not be deleted, blk_end may
4470 * be NULL if all blocks have to be deleted.
4471 */
4472 idx = htx_get_head(htx);
4473 blk_end = NULL;
4474 while (idx != -1) {
4475 type = htx_get_blk_type(htx_get_blk(htx, idx));
4476 idx = htx_get_next(htx, idx);
4477 if (type == HTX_BLK_EOH) {
4478 if (idx != -1)
4479 blk_end = htx_get_blk(htx, idx);
4480 break;
4481 }
4482 }
4483
4484 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004485 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004486 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004487 meth = htx_sl_req_meth(sl);
4488 path = htx_sl_req_uri(sl);
4489
4490 /* and the rest of the headers, that we dump starting at header 0 */
4491 hdr = 0;
4492
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004493 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004494 while ((idx = htx_get_next(htx, idx)) != -1) {
4495 blk = htx_get_blk(htx, idx);
4496 type = htx_get_blk_type(blk);
4497
4498 if (type == HTX_BLK_UNUSED)
4499 continue;
4500
4501 if (type != HTX_BLK_HDR)
4502 break;
4503
4504 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4505 goto fail;
4506
4507 list[hdr].n = htx_get_blk_name(htx, blk);
4508 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004509 hdr++;
4510 }
4511
4512 /* marker for end of headers */
4513 list[hdr].n = ist("");
4514
4515 chunk_reset(&outbuf);
4516
4517 while (1) {
4518 outbuf.area = b_tail(&h2c->mbuf);
4519 outbuf.size = b_contig_space(&h2c->mbuf);
4520 outbuf.data = 0;
4521
4522 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4523 break;
4524 realign_again:
4525 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4526 }
4527
4528 if (outbuf.size < 9)
4529 goto full;
4530
4531 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4532 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4533 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4534 outbuf.data = 9;
4535
4536 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004537 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004538 if (b_space_wraps(&h2c->mbuf))
4539 goto realign_again;
4540 goto full;
4541 }
4542
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004543 /* RFC7540 #8.3: the CONNECT method must have :
4544 * - :authority set to the URI part (host:port)
4545 * - :method set to CONNECT
4546 * - :scheme and :path omitted
4547 */
4548 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4549 /* encode the scheme which is always "https" (or 0x86 for "http") */
4550 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4551 /* output full */
4552 if (b_space_wraps(&h2c->mbuf))
4553 goto realign_again;
4554 goto full;
4555 }
Willy Tarreau80739692018-10-05 11:35:57 +02004556
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004557 /* encode the path, which necessarily is the second one */
4558 if (!hpack_encode_path(&outbuf, path)) {
4559 /* output full */
4560 if (b_space_wraps(&h2c->mbuf))
4561 goto realign_again;
4562 goto full;
4563 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004564
4565 /* look for the Host header and place it in :authority */
4566 auth = ist2(NULL, 0);
4567 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4568 if (isteq(list[hdr].n, ist("")))
4569 break; // end
4570
4571 if (isteq(list[hdr].n, ist("host"))) {
4572 auth = list[hdr].v;
4573 break;
4574 }
4575 }
4576 }
4577 else {
4578 /* for CONNECT, :authority is taken from the path */
4579 auth = path;
4580 }
4581
4582 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4583 /* output full */
4584 if (b_space_wraps(&h2c->mbuf))
4585 goto realign_again;
4586 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004587 }
4588
4589 /* encode all headers, stop at empty name */
4590 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4591 /* these ones do not exist in H2 and must be dropped. */
4592 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004593 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004594 isteq(list[hdr].n, ist("proxy-connection")) ||
4595 isteq(list[hdr].n, ist("keep-alive")) ||
4596 isteq(list[hdr].n, ist("upgrade")) ||
4597 isteq(list[hdr].n, ist("transfer-encoding")))
4598 continue;
4599
4600 if (isteq(list[hdr].n, ist("")))
4601 break; // end
4602
4603 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4604 /* output full */
4605 if (b_space_wraps(&h2c->mbuf))
4606 goto realign_again;
4607 goto full;
4608 }
4609 }
4610
4611 /* we may need to add END_STREAM if we have no body :
4612 * - request already closed, or :
4613 * - no transfer-encoding, and :
4614 * - no content-length or content-length:0
4615 * Fixme: this doesn't take into account CONNECT requests.
4616 */
4617 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4618 es_now = 1;
4619
4620 if (sl->flags & HTX_SL_F_BODYLESS)
4621 es_now = 1;
4622
Willy Tarreau927b88b2019-03-04 08:03:25 +01004623 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004624 es_now = 1;
4625
4626 /* update the frame's size */
4627 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4628
4629 if (es_now)
4630 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4631
4632 /* commit the H2 response */
4633 b_add(&h2c->mbuf, outbuf.data);
4634 h2s->flags |= H2_SF_HEADERS_SENT;
4635 h2s->st = H2_SS_OPEN;
4636
Willy Tarreau80739692018-10-05 11:35:57 +02004637 if (es_now) {
4638 // trim any possibly pending data (eg: inconsistent content-length)
4639 h2s->flags |= H2_SF_ES_SENT;
4640 h2s->st = H2_SS_HLOC;
4641 }
4642
4643 /* remove all header blocks including the EOH and compute the
4644 * corresponding size.
4645 *
4646 * FIXME: We should remove everything when es_now is set.
4647 */
4648 ret = 0;
4649 idx = htx_get_head(htx);
4650 blk = htx_get_blk(htx, idx);
4651 while (blk != blk_end) {
4652 ret += htx_get_blksz(blk);
4653 blk = htx_remove_blk(htx, blk);
4654 }
4655
4656 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4657 htx_remove_blk(htx, blk_end);
4658
4659 end:
4660 return ret;
4661 full:
4662 h2c->flags |= H2_CF_MUX_MFULL;
4663 h2s->flags |= H2_SF_BLK_MROOM;
4664 ret = 0;
4665 goto end;
4666 fail:
4667 /* unparsable HTX messages, too large ones to be produced in the local
4668 * list etc go here (unrecoverable errors).
4669 */
4670 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4671 ret = 0;
4672 goto end;
4673}
4674
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004675/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004676 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4677 * caller must check the stream's status to detect any error which might have
4678 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004679 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4680 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004681static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004682{
4683 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004684 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004685 struct buffer outbuf;
4686 size_t total = 0;
4687 int es_now = 0;
4688 int bsize; /* htx block size */
4689 int fsize; /* h2 frame size */
4690 struct htx_blk *blk;
4691 enum htx_blk_type type;
4692 int idx;
4693
4694 if (h2c_mux_busy(h2c, h2s)) {
4695 h2s->flags |= H2_SF_BLK_MBUSY;
4696 goto end;
4697 }
4698
4699 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4700 h2c->flags |= H2_CF_MUX_MALLOC;
4701 h2s->flags |= H2_SF_BLK_MROOM;
4702 goto end;
4703 }
4704
Willy Tarreau98de12a2018-12-12 07:03:00 +01004705 htx = htx_from_buf(buf);
4706
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004707 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4708 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4709 * the caller to handle.
4710 */
4711
4712 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004713 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004714 goto end;
4715
4716 idx = htx_get_head(htx);
4717 blk = htx_get_blk(htx, idx);
4718 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4719 bsize = htx_get_blksz(blk);
4720 fsize = bsize;
4721
4722 if (type == HTX_BLK_EOD) {
4723 /* if we have an EOD, we're dealing with chunked data. We may
4724 * have a set of trailers after us that the caller will want to
4725 * deal with. Let's simply remove the EOD and return.
4726 */
4727 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004728 total++; // EOD counts as one byte
4729 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004730 goto end;
4731 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004732 else if (type == HTX_BLK_EOM) {
4733 if (h2s->flags & H2_SF_ES_SENT) {
4734 /* ES already sent */
4735 htx_remove_blk(htx, blk);
4736 total++; // EOM counts as one byte
4737 count--;
4738 goto end;
4739 }
4740 }
4741 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004742 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004743
4744 /* Perform some optimizations to reduce the number of buffer copies.
4745 * First, if the mux's buffer is empty and the htx area contains
4746 * exactly one data block of the same size as the requested count, and
4747 * this count fits within the frame size, the stream's window size, and
4748 * the connection's window size, then it's possible to simply swap the
4749 * caller's buffer with the mux's output buffer and adjust offsets and
4750 * length to match the entire DATA HTX block in the middle. In this
4751 * case we perform a true zero-copy operation from end-to-end. This is
4752 * the situation that happens all the time with large files. Second, if
4753 * this is not possible, but the mux's output buffer is empty, we still
4754 * have an opportunity to avoid the copy to the intermediary buffer, by
4755 * making the intermediary buffer's area point to the output buffer's
4756 * area. In this case we want to skip the HTX header to make sure that
4757 * copies remain aligned and that this operation remains possible all
4758 * the time. This goes for headers, data blocks and any data extracted
4759 * from the HTX blocks.
4760 */
4761 if (unlikely(fsize == count &&
4762 htx->used == 1 && type == HTX_BLK_DATA &&
4763 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4764 void *old_area = h2c->mbuf.area;
4765
4766 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004767 /* Too bad there are data left there. We're willing to memcpy/memmove
4768 * up to 1/4 of the buffer, which means that it's OK to copy a large
4769 * frame into a buffer containing few data if it needs to be realigned,
4770 * and that it's also OK to copy few data without realigning. Otherwise
4771 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004772 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004773 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4774 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4775 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004776 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004777
Willy Tarreau98de12a2018-12-12 07:03:00 +01004778 h2c->flags |= H2_CF_MUX_MFULL;
4779 h2s->flags |= H2_SF_BLK_MROOM;
4780 goto end;
4781 }
4782
4783 /* map an H2 frame to the HTX block so that we can put the
4784 * frame header there.
4785 */
4786 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004787 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004788 h2c->mbuf.data = fsize + 9;
4789 outbuf.area = b_head(&h2c->mbuf);
4790
4791 /* prepend an H2 DATA frame header just before the DATA block */
4792 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4793 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4794 h2_set_frame_size(outbuf.area, fsize);
4795
4796 /* update windows */
4797 h2s->mws -= fsize;
4798 h2c->mws -= fsize;
4799
4800 /* and exchange with our old area */
4801 buf->area = old_area;
4802 buf->data = buf->head = 0;
4803 total += fsize;
4804 goto end;
4805 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004806
Willy Tarreau98de12a2018-12-12 07:03:00 +01004807 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004808 /* for DATA and EOM we'll have to emit a frame, even if empty */
4809
4810 while (1) {
4811 outbuf.area = b_tail(&h2c->mbuf);
4812 outbuf.size = b_contig_space(&h2c->mbuf);
4813 outbuf.data = 0;
4814
4815 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4816 break;
4817 realign_again:
4818 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4819 }
4820
4821 if (outbuf.size < 9) {
4822 h2c->flags |= H2_CF_MUX_MFULL;
4823 h2s->flags |= H2_SF_BLK_MROOM;
4824 goto end;
4825 }
4826
4827 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4828 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4829 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4830 outbuf.data = 9;
4831
4832 /* we have in <fsize> the exact number of bytes we need to copy from
4833 * the HTX buffer. We need to check this against the connection's and
4834 * the stream's send windows, and to ensure that this fits in the max
4835 * frame size and in the buffer's available space minus 9 bytes (for
4836 * the frame header). The connection's flow control is applied last so
4837 * that we can use a separate list of streams which are immediately
4838 * unblocked on window opening. Note: we don't implement padding.
4839 */
4840
4841 /* EOM is presented with bsize==1 but would lead to the emission of an
4842 * empty frame, thus we force it to zero here.
4843 */
4844 if (type == HTX_BLK_EOM)
4845 bsize = fsize = 0;
4846
4847 if (!fsize)
4848 goto send_empty;
4849
4850 if (h2s->mws <= 0) {
4851 h2s->flags |= H2_SF_BLK_SFCTL;
4852 if (h2s->send_wait) {
4853 LIST_DEL(&h2s->list);
4854 LIST_INIT(&h2s->list);
4855 }
4856 goto end;
4857 }
4858
Willy Tarreauee573762018-12-04 15:25:57 +01004859 if (fsize > count)
4860 fsize = count;
4861
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004862 if (fsize > h2s->mws)
4863 fsize = h2s->mws; // >0
4864
4865 if (h2c->mfs && fsize > h2c->mfs)
4866 fsize = h2c->mfs; // >0
4867
4868 if (fsize + 9 > outbuf.size) {
4869 /* we have an opportunity for enlarging the too small
4870 * available space, let's try.
4871 * FIXME: is this really interesting to do? Maybe we'll
4872 * spend lots of time realigning instead of using two
4873 * frames.
4874 */
4875 if (b_space_wraps(&h2c->mbuf))
4876 goto realign_again;
4877 fsize = outbuf.size - 9;
4878
4879 if (fsize <= 0) {
4880 /* no need to send an empty frame here */
4881 h2c->flags |= H2_CF_MUX_MFULL;
4882 h2s->flags |= H2_SF_BLK_MROOM;
4883 goto end;
4884 }
4885 }
4886
4887 if (h2c->mws <= 0) {
4888 h2s->flags |= H2_SF_BLK_MFCTL;
4889 goto end;
4890 }
4891
4892 if (fsize > h2c->mws)
4893 fsize = h2c->mws;
4894
4895 /* now let's copy this this into the output buffer */
4896 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004897 h2s->mws -= fsize;
4898 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004899 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004900
4901 send_empty:
4902 /* update the frame's size */
4903 h2_set_frame_size(outbuf.area, fsize);
4904
4905 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4906 * meeting EOM. We should optimize this later.
4907 */
4908 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004909 total++; // EOM counts as one byte
4910 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004911 es_now = 1;
4912 }
4913
4914 if (es_now)
4915 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4916
4917 /* commit the H2 response */
4918 b_add(&h2c->mbuf, fsize + 9);
4919
4920 /* consume incoming HTX block, including EOM */
4921 total += fsize;
4922 if (fsize == bsize) {
4923 htx_remove_blk(htx, blk);
4924 if (fsize)
4925 goto new_frame;
4926 } else {
4927 /* we've truncated this block */
4928 htx_cut_data_blk(htx, blk, fsize);
4929 }
4930
4931 if (es_now) {
4932 if (h2s->st == H2_SS_OPEN)
4933 h2s->st = H2_SS_HLOC;
4934 else
4935 h2s_close(h2s);
4936
4937 h2s->flags |= H2_SF_ES_SENT;
4938 }
4939
4940 end:
4941 return total;
4942}
4943
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004944/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4945 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4946 * processed. The caller must check the stream's status to detect any error
4947 * which might have happened subsequently to a successful send. The htx blocks
4948 * are automatically removed from the message. The htx message is assumed to be
4949 * valid since produced from the internal code. Processing stops when meeting
4950 * the EOM, which is also removed. All trailers are processed at once and sent
4951 * as a single frame. The ES flag is always set.
4952 */
4953static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4954{
4955 struct http_hdr list[MAX_HTTP_HDR];
4956 struct h2c *h2c = h2s->h2c;
4957 struct htx_blk *blk;
4958 struct htx_blk *blk_end;
4959 struct buffer outbuf;
4960 struct h1m h1m;
4961 enum htx_blk_type type;
4962 uint32_t size;
4963 int ret = 0;
4964 int hdr;
4965 int idx;
4966 void *start;
4967
4968 if (h2c_mux_busy(h2c, h2s)) {
4969 h2s->flags |= H2_SF_BLK_MBUSY;
4970 goto end;
4971 }
4972
4973 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4974 h2c->flags |= H2_CF_MUX_MALLOC;
4975 h2s->flags |= H2_SF_BLK_MROOM;
4976 goto end;
4977 }
4978
4979 /* The principle is that we parse each and every trailers block using
4980 * the H1 headers parser, and append it to the list. We don't proceed
4981 * until EOM is met. blk_end will point to the EOM block.
4982 */
4983 hdr = 0;
4984 memset(list, 0, sizeof(list));
4985 blk_end = NULL;
4986
4987 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4988 blk = htx_get_blk(htx, idx);
4989 type = htx_get_blk_type(blk);
4990
4991 if (type == HTX_BLK_UNUSED)
4992 continue;
4993
4994 if (type != HTX_BLK_TLR) {
4995 if (type == HTX_BLK_EOM)
4996 blk_end = blk;
4997 break;
4998 }
4999
5000 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5001 goto fail;
5002
5003 size = htx_get_blksz(blk);
5004 start = htx_get_blk_ptr(htx, blk);
5005
5006 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5007 h1m.err_pos = 0;
5008 ret = h1_headers_to_hdr_list(start, start + size,
5009 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5010 &h1m, NULL);
5011 if (ret < 0)
5012 goto fail;
5013
5014 /* ret == 0 if an incomplete trailers block was found (missing
5015 * empty line), or > 0 if it was found. We have to continue on
5016 * incomplete messages because the trailers block might be
5017 * incomplete.
5018 */
5019
5020 /* search the new end */
5021 while (hdr <= sizeof(list)/sizeof(list[0])) {
5022 if (!list[hdr].n.len)
5023 break;
5024 hdr++;
5025 }
5026 }
5027
5028 if (!blk_end)
5029 goto end; // end not found yet
5030
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005031 chunk_reset(&outbuf);
5032
5033 while (1) {
5034 outbuf.area = b_tail(&h2c->mbuf);
5035 outbuf.size = b_contig_space(&h2c->mbuf);
5036 outbuf.data = 0;
5037
5038 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5039 break;
5040 realign_again:
5041 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5042 }
5043
5044 if (outbuf.size < 9)
5045 goto full;
5046
5047 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5048 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5049 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5050 outbuf.data = 9;
5051
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005052 /* encode all headers */
5053 for (idx = 0; idx < hdr; idx++) {
5054 /* these ones do not exist in H2 or must not appear in
5055 * trailers and must be dropped.
5056 */
5057 if (isteq(list[idx].n, ist("host")) ||
5058 isteq(list[idx].n, ist("content-length")) ||
5059 isteq(list[idx].n, ist("connection")) ||
5060 isteq(list[idx].n, ist("proxy-connection")) ||
5061 isteq(list[idx].n, ist("keep-alive")) ||
5062 isteq(list[idx].n, ist("upgrade")) ||
5063 isteq(list[idx].n, ist("te")) ||
5064 isteq(list[idx].n, ist("transfer-encoding")))
5065 continue;
5066
5067 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5068 /* output full */
5069 if (b_space_wraps(&h2c->mbuf))
5070 goto realign_again;
5071 goto full;
5072 }
5073 }
5074
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005075 if (!hdr) {
5076 /* here we have a problem, we've received an empty trailers
5077 * block followed by an EOM. Because of this we can't send a
5078 * HEADERS frame, so we have to cheat and instead send an empty
5079 * DATA frame conveying the ES flag.
5080 */
5081 outbuf.area[3] = H2_FT_DATA;
5082 outbuf.area[4] = H2_F_DATA_END_STREAM;
5083 }
5084
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005085 /* update the frame's size */
5086 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5087
5088 /* commit the H2 response */
5089 b_add(&h2c->mbuf, outbuf.data);
5090 h2s->flags |= H2_SF_ES_SENT;
5091
5092 if (h2s->st == H2_SS_OPEN)
5093 h2s->st = H2_SS_HLOC;
5094 else
5095 h2s_close(h2s);
5096
5097 /* OK we could properly deliver the response */
5098 done:
5099 /* remove all header blocks including EOM and compute the corresponding size. */
5100 ret = 0;
5101 idx = htx_get_head(htx);
5102 blk = htx_get_blk(htx, idx);
5103 while (blk != blk_end) {
5104 ret += htx_get_blksz(blk);
5105 blk = htx_remove_blk(htx, blk);
5106 }
5107 blk = htx_remove_blk(htx, blk);
5108 end:
5109 return ret;
5110 full:
5111 h2c->flags |= H2_CF_MUX_MFULL;
5112 h2s->flags |= H2_SF_BLK_MROOM;
5113 ret = 0;
5114 goto end;
5115 fail:
5116 /* unparsable HTX messages, too large ones to be produced in the local
5117 * list etc go here (unrecoverable errors).
5118 */
5119 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5120 ret = 0;
5121 goto end;
5122}
5123
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005124/* Called from the upper layer, to subscribe to events, such as being able to send.
5125 * The <param> argument here is supposed to be a pointer to a wait_event struct
5126 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5127 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5128 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5129 * returns 0 except for the error above.
5130 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005131static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5132{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005133 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005134 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005135 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005136
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005137 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005138 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005139 if (!(sw->events & SUB_RETRY_RECV)) {
5140 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005141 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005142 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005143 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005144 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005145 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005146 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005147 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005148 if (!(sw->events & SUB_RETRY_SEND)) {
5149 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005150 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005151 h2s->send_wait = sw;
5152 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5153 if (h2s->flags & H2_SF_BLK_MFCTL)
5154 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5155 else
5156 LIST_ADDQ(&h2c->send_list, &h2s->list);
5157 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005158 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005159 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005160 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005161 if (event_type != 0)
5162 return -1;
5163 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005164}
5165
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005166/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5167 * The <param> argument here is supposed to be a pointer to the same wait_event
5168 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5169 * It always returns zero.
5170 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005171static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5172{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005173 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005174 struct h2s *h2s = cs->ctx;
5175
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005176 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005177 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005178 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005179 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005180 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005181 }
5182 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005183 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005184 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005185 if (h2s->send_wait == sw) {
5186 LIST_DEL(&h2s->list);
5187 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005188 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005189 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005190 }
5191 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005192 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5193 sw = param;
5194 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005195 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Willy Tarreaue73256f2019-03-25 18:02:54 +01005196 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005197 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005198 LIST_DEL(&h2s->list);
5199 LIST_INIT(&h2s->list);
Olivier Houchard3ea35132019-03-25 14:10:42 +01005200 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005201 }
5202 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005203 return 0;
5204}
5205
5206
Olivier Houchard511efea2018-08-16 15:30:32 +02005207/* Called from the upper layer, to receive data */
5208static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5209{
Olivier Houchard638b7992018-08-16 15:41:52 +02005210 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005211 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005212 struct htx *h2s_htx = NULL;
5213 struct htx *buf_htx = NULL;
5214 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005215 size_t ret = 0;
5216
5217 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005218 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5219 /* in HTX mode we ignore the count argument */
5220 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005221 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005222 /* Here htx_to_buf() will set buffer data to 0 because
5223 * the HTX is empty.
5224 */
5225 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005226 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005227 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005228
5229 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005230 count = htx_free_data_space(buf_htx);
5231 if (flags & CO_RFL_KEEP_RSV) {
5232 if (count <= global.tune.maxrewrite)
5233 goto end;
5234 count -= global.tune.maxrewrite;
5235 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005236
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005237 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005238
5239 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5240 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5241
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005242 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005243 htx_to_buf(buf_htx, buf);
5244 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005245 ret = htx_ret.ret;
5246 }
5247 else {
5248 ret = b_xfer(buf, &h2s->rxbuf, count);
5249 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005250
Christopher Faulet37070b22019-02-14 15:12:14 +01005251 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005252 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005253 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005254 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005255 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005256 if (cs->flags & CS_FL_REOS)
5257 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005258 if (cs->flags & CS_FL_ERR_PENDING)
5259 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005260 if (b_size(&h2s->rxbuf)) {
5261 b_free(&h2s->rxbuf);
5262 offer_buffers(NULL, tasks_run_queue);
5263 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005264 }
5265
Willy Tarreau082f5592018-11-25 08:03:32 +01005266 if (ret && h2c->dsi == h2s->id) {
5267 /* demux is blocking on this stream's buffer */
5268 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005269 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005270 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005271
Olivier Houchard511efea2018-08-16 15:30:32 +02005272 return ret;
5273}
5274
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005275/* stops all senders of this connection for example when the mux buffer is full.
5276 * They are moved from the sending_list to either fctl_list or send_list.
5277 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005278static void h2_stop_senders(struct h2c *h2c)
5279{
5280 struct h2s *h2s, *h2s_back;
5281
Olivier Houchardd360ac62019-03-22 17:37:16 +01005282 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5283 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005284 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005285 h2s->send_wait->events |= SUB_RETRY_SEND;
5286 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005287 }
5288}
5289
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005290/* Called from the upper layer, to send data from buffer <buf> for no more than
5291 * <count> bytes. Returns the number of bytes effectively sent. Some status
5292 * flags may be updated on the conn_stream.
5293 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005294static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005295{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005296 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005297 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005298 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005299 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005300 struct htx *htx;
5301 struct htx_blk *blk;
5302 enum htx_blk_type btype;
5303 uint32_t bsize;
5304 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005305
Olivier Houchardd360ac62019-03-22 17:37:16 +01005306 /* If we were not just woken because we wanted to send but couldn't,
5307 * and there's somebody else that is waiting to send, do nothing,
5308 * we will subscribe later and be put at the end of the list
5309 */
5310 LIST_DEL_INIT(&h2s->sending_list);
5311 if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) &&
5312 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5313 return 0;
5314
Olivier Houchardd846c262018-10-19 17:24:29 +02005315 if (h2s->send_wait) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005316 /* We want to stay in the send_list, so prepare ourself to be
5317 * eventually recalled if needed, and only remove ourself from
5318 * the list if we managed to send anything.
5319 */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005320 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01005321 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005322 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005323 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5324 return 0;
5325
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005326 /* htx will be enough to decide if we're using HTX or legacy */
5327 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5328
Willy Tarreau0bad0432018-06-14 16:54:01 +02005329 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005330 h2s->flags |= H2_SF_OUTGOING_DATA;
5331
Willy Tarreau751f2d02018-10-05 09:35:00 +02005332 if (h2s->id == 0) {
5333 int32_t id = h2c_get_next_sid(h2s->h2c);
5334
5335 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005336 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005337 return 0;
5338 }
5339
5340 eb32_delete(&h2s->by_id);
5341 h2s->by_id.key = h2s->id = id;
5342 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005343 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005344 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5345 }
5346
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005347 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005348 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5349 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005350 idx = htx_get_head(htx);
5351 blk = htx_get_blk(htx, idx);
5352 btype = htx_get_blk_type(blk);
5353 bsize = htx_get_blksz(blk);
5354
5355 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005356 case HTX_BLK_REQ_SL:
5357 /* start-line before headers */
5358 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5359 if (ret > 0) {
5360 total += ret;
5361 count -= ret;
5362 if (ret < bsize)
5363 goto done;
5364 }
5365 break;
5366
Willy Tarreau115e83b2018-12-01 19:17:53 +01005367 case HTX_BLK_RES_SL:
5368 /* start-line before headers */
5369 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5370 if (ret > 0) {
5371 total += ret;
5372 count -= ret;
5373 if (ret < bsize)
5374 goto done;
5375 }
5376 break;
5377
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005378 case HTX_BLK_DATA:
5379 case HTX_BLK_EOD:
5380 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005381 /* all these cause the emission of a DATA frame (possibly empty).
5382 * This EOM necessarily is one before trailers, as the EOM following
5383 * trailers would have been consumed by the trailers parser.
5384 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005385 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005386 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005387 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005388 total += ret;
5389 count -= ret;
5390 if (ret < bsize)
5391 goto done;
5392 }
5393 break;
5394
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005395 case HTX_BLK_TLR:
5396 /* This is the first trailers block, all the subsequent ones AND
5397 * the EOM will be swallowed by the parser.
5398 */
5399 ret = h2s_htx_make_trailers(h2s, htx);
5400 if (ret > 0) {
5401 total += ret;
5402 count -= ret;
5403 if (ret < bsize)
5404 goto done;
5405 }
5406 break;
5407
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005408 default:
5409 htx_remove_blk(htx, blk);
5410 total += bsize;
5411 count -= bsize;
5412 break;
5413 }
5414 }
5415 goto done;
5416 }
5417
5418 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005419 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005420 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005421 if (h2s->h2c->flags & H2_CF_IS_BACK)
5422 ret = -1;
5423 else
5424 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005425 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005426 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005427 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005428 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005429 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005430 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005431 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005432
Willy Tarreau5dd17352018-06-14 13:33:30 +02005433 if (unlikely((int)ret <= 0)) {
5434 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005435 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5436 break;
5437 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005438 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005439 total += count;
5440 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005441 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005442 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005443 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005444 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005445 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005446 break;
5447 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005448
5449 total += ret;
5450 count -= ret;
5451
5452 if (h2s->st >= H2_SS_ERROR)
5453 break;
5454
5455 if (h2s->flags & H2_SF_BLK_ANY)
5456 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005457 }
5458
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005459 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005460 if (h2s->st >= H2_SS_ERROR) {
5461 /* trim any possibly pending data after we close (extra CR-LF,
5462 * unprocessed trailers, abnormal extra data, ...)
5463 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005464 total += count;
5465 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005466 }
5467
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005468 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005469 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005470 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005471 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005472 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005473 }
5474
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005475 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005476 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005477 } else {
5478 b_del(buf, total);
5479 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005480
5481 /* The mux is full, cancel the pending tasks */
5482 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5483 (h2s->flags & H2_SF_BLK_MBUSY))
5484 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005485
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005486 /* If we're running HTX, and we read the whole buffer, then pretend
5487 * we read exactly what the caller specified, as with HTX the caller
5488 * will always give the buffer size, instead of the amount of data
5489 * available.
5490 */
5491 if (htx && !b_data(buf))
5492 total = orig_count;
5493
Olivier Houchard7505f942018-08-21 18:10:44 +02005494 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005495 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005496 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005497
Olivier Houchard7505f942018-08-21 18:10:44 +02005498 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005499 /* If we're waiting for flow control, and we got a shutr on the
5500 * connection, we will never be unlocked, so add an error on
5501 * the conn_stream.
5502 */
5503 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5504 !b_data(&h2s->h2c->dbuf) &&
5505 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5506 if (cs->flags & CS_FL_EOS)
5507 cs->flags |= CS_FL_ERROR;
5508 else
5509 cs->flags |= CS_FL_ERR_PENDING;
5510 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01005511 if (total > 0 && h2s->send_wait) {
5512 /* Ok we managed to send something, leave the send_list */
5513 h2s->send_wait->events &= ~SUB_RETRY_SEND;
5514 h2s->send_wait = NULL;
5515 LIST_DEL_INIT(&h2s->list);
5516 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005517 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005518}
5519
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005520/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005521static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005522{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005523 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005524 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005525 struct eb32_node *node;
5526 int fctl_cnt = 0;
5527 int send_cnt = 0;
5528 int tree_cnt = 0;
5529 int orph_cnt = 0;
5530
5531 if (!h2c)
5532 return;
5533
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005534 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005535 fctl_cnt++;
5536
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005537 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005538 send_cnt++;
5539
Willy Tarreau3af37712018-12-18 14:34:41 +01005540 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005541 node = eb32_first(&h2c->streams_by_id);
5542 while (node) {
5543 h2s = container_of(node, struct h2s, by_id);
5544 tree_cnt++;
5545 if (!h2s->cs)
5546 orph_cnt++;
5547 node = eb32_next(node);
5548 }
5549
Willy Tarreau987c0632018-12-18 10:32:05 +01005550 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5551 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5552 " .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 +02005553 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5554 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005555 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005556 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5557 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5558 h2c->msi,
5559 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5560 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5561
5562 if (h2s) {
5563 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5564 h2s, h2s->id, h2s->flags,
5565 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5566 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5567 h2s->cs);
5568 if (h2s->cs)
5569 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5570 h2s->cs->flags, h2s->cs->data);
5571 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005572}
Willy Tarreau62f52692017-10-08 23:01:42 +02005573
5574/*******************************************************/
5575/* functions below are dedicated to the config parsers */
5576/*******************************************************/
5577
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005578/* config parser for global "tune.h2.header-table-size" */
5579static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5580 struct proxy *defpx, const char *file, int line,
5581 char **err)
5582{
5583 if (too_many_args(1, args, err, NULL))
5584 return -1;
5585
5586 h2_settings_header_table_size = atoi(args[1]);
5587 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5588 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5589 return -1;
5590 }
5591 return 0;
5592}
Willy Tarreau62f52692017-10-08 23:01:42 +02005593
Willy Tarreaue6baec02017-07-27 11:45:11 +02005594/* config parser for global "tune.h2.initial-window-size" */
5595static int h2_parse_initial_window_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_initial_window_size = atoi(args[1]);
5603 if (h2_settings_initial_window_size < 0) {
5604 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5605 return -1;
5606 }
5607 return 0;
5608}
5609
Willy Tarreau5242ef82017-07-27 11:47:28 +02005610/* config parser for global "tune.h2.max-concurrent-streams" */
5611static int h2_parse_max_concurrent_streams(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_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005619 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005620 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5621 return -1;
5622 }
5623 return 0;
5624}
5625
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005626/* config parser for global "tune.h2.max-frame-size" */
5627static int h2_parse_max_frame_size(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_frame_size = atoi(args[1]);
5635 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5636 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5637 return -1;
5638 }
5639 return 0;
5640}
5641
Willy Tarreau62f52692017-10-08 23:01:42 +02005642
5643/****************************************/
5644/* MUX initialization and instanciation */
5645/***************************************/
5646
5647/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005648static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005649 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005650 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005651 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005652 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005653 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005654 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005655 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005656 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005657 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005658 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005659 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005660 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005661 .shutr = h2_shutr,
5662 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005663 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005664 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005665 .name = "H2",
5666};
5667
Christopher Faulet32f61c02018-04-10 14:33:41 +02005668/* PROTO selection : this mux registers PROTO token "h2" */
5669static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005670 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005671
Willy Tarreau0108d902018-11-25 19:14:37 +01005672INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5673
Christopher Faulet9b579102019-04-11 22:52:25 +02005674
5675/* The mux operations */
5676static const struct mux_ops h2_htx_ops = {
5677 .init = h2_init,
5678 .wake = h2_wake,
5679 .snd_buf = h2_snd_buf,
5680 .rcv_buf = h2_rcv_buf,
5681 .subscribe = h2_subscribe,
5682 .unsubscribe = h2_unsubscribe,
5683 .attach = h2_attach,
5684 .get_first_cs = h2_get_first_cs,
5685 .detach = h2_detach,
5686 .destroy = h2_destroy,
5687 .avail_streams = h2_avail_streams,
5688 .used_streams = h2_used_streams,
5689 .shutr = h2_shutr,
5690 .shutw = h2_shutw,
5691 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005692 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005693 .name = "H2",
5694};
5695
Willy Tarreauf8957272018-10-03 10:25:20 +02005696static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005697 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005698
5699INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5700
Willy Tarreau62f52692017-10-08 23:01:42 +02005701/* config keyword parsers */
5702static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005703 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005704 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005705 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005706 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005707 { 0, NULL, NULL }
5708}};
5709
Willy Tarreau0108d902018-11-25 19:14:37 +01005710INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);