blob: 5448ea820a8ba3bfcef732f9763ea71eb07f92e5 [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
475 * connections from the fact that the context is still NULL. Returns < 0 on
476 * error.
477 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100478static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200479{
480 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100481 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200482
Willy Tarreaubafbe012017-11-24 17:34:44 +0100483 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200484 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200485 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200486
Christopher Faulete9b70722019-04-08 10:46:02 +0200487 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200488 h2c->flags = H2_CF_IS_BACK;
489 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
490 if (tick_isset(prx->timeout.serverfin))
491 h2c->shut_timeout = prx->timeout.serverfin;
492 } else {
493 h2c->flags = H2_CF_NONE;
494 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
495 if (tick_isset(prx->timeout.clientfin))
496 h2c->shut_timeout = prx->timeout.clientfin;
497 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100498
Willy Tarreau0b37d652018-10-03 10:33:02 +0200499 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100500 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100501 if (tick_isset(h2c->timeout)) {
502 t = task_new(tid_bit);
503 if (!t)
504 goto fail;
505
506 h2c->task = t;
507 t->process = h2_timeout_task;
508 t->context = h2c;
509 t->expire = tick_add(now_ms, h2c->timeout);
510 }
Willy Tarreauea392822017-10-31 10:02:25 +0100511
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200512 h2c->wait_event.task = tasklet_new();
513 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200514 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200515 h2c->wait_event.task->process = h2_io_cb;
516 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100517 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200518
Willy Tarreau32218eb2017-09-22 08:07:25 +0200519 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
520 if (!h2c->ddht)
521 goto fail;
522
523 /* Initialise the context. */
524 h2c->st0 = H2_CS_PREFACE;
525 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100526 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200527 h2c->max_id = -1;
528 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100529 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200530 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100531 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200532 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100533 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100534 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200535
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200536 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200537 h2c->dsi = -1;
538 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100539
Willy Tarreau32218eb2017-09-22 08:07:25 +0200540 h2c->last_sid = -1;
541
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200542 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200543 h2c->miw = 65535; /* mux initial window size */
544 h2c->mws = 65535; /* mux window size */
545 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200546 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200547 LIST_INIT(&h2c->send_list);
548 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200549 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100550 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200551
Willy Tarreau3f133572017-10-31 19:21:06 +0100552 if (t)
553 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100554
Willy Tarreau01b44822018-10-03 14:26:37 +0200555 if (h2c->flags & H2_CF_IS_BACK) {
556 /* FIXME: this is temporary, for outgoing connections we need
557 * to immediately allocate a stream until the code is modified
558 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100559 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200560 */
561 struct h2s *h2s;
562
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100563 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200564 if (!h2s)
565 goto fail_stream;
566 }
567
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100568 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200569
Willy Tarreau0f383582018-10-03 14:22:21 +0200570 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200571 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200572 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200573 fail_stream:
574 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200575 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100576 if (t)
577 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200578 if (h2c->wait_event.task)
579 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100580 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200581 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200582 return -1;
583}
584
Willy Tarreau751f2d02018-10-05 09:35:00 +0200585/* returns the next allocatable outgoing stream ID for the H2 connection, or
586 * -1 if no more is allocatable.
587 */
588static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
589{
590 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100591
592 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200593 id = -1;
594 return id;
595}
596
Willy Tarreau2373acc2017-10-12 17:35:14 +0200597/* returns the stream associated with id <id> or NULL if not found */
598static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
599{
600 struct eb32_node *node;
601
Willy Tarreau751f2d02018-10-05 09:35:00 +0200602 if (id == 0)
603 return (struct h2s *)h2_closed_stream;
604
Willy Tarreau2a856182017-05-16 15:20:39 +0200605 if (id > h2c->max_id)
606 return (struct h2s *)h2_idle_stream;
607
Willy Tarreau2373acc2017-10-12 17:35:14 +0200608 node = eb32_lookup(&h2c->streams_by_id, id);
609 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200610 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200611
612 return container_of(node, struct h2s, by_id);
613}
614
Willy Tarreau62f52692017-10-08 23:01:42 +0200615/* release function for a connection. This one should be called to free all
616 * resources allocated to the mux.
617 */
618static void h2_release(struct connection *conn)
619{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100620 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200621
Willy Tarreau32218eb2017-09-22 08:07:25 +0200622 if (h2c) {
623 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200624
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100625 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100626 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100627 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200628
Willy Tarreau44e973f2018-03-01 17:49:30 +0100629 h2_release_buf(h2c, &h2c->dbuf);
630 h2_release_buf(h2c, &h2c->mbuf);
631
Willy Tarreauea392822017-10-31 10:02:25 +0100632 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200633 h2c->task->context = NULL;
634 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100635 h2c->task = NULL;
636 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200637 if (h2c->wait_event.task)
638 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100639 if (h2c->wait_event.events != 0)
640 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200641 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100642
Willy Tarreaubafbe012017-11-24 17:34:44 +0100643 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200644 }
645
646 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100647 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200648
649 conn_stop_tracking(conn);
650 conn_full_close(conn);
651 if (conn->destroy_cb)
652 conn->destroy_cb(conn);
653 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200654}
655
656
Willy Tarreau71681172017-10-23 14:39:06 +0200657/******************************************************/
658/* functions below are for the H2 protocol processing */
659/******************************************************/
660
661/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100662static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200663{
664 return h2s ? h2s->id : 0;
665}
666
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200667/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100668static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200669{
670 if (h2c->msi < 0)
671 return 0;
672
673 if (h2c->msi == h2s_id(h2s))
674 return 0;
675
676 return 1;
677}
678
Willy Tarreau741d6df2017-10-17 08:00:59 +0200679/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100680static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200681{
682 h2c->errcode = err;
683 h2c->st0 = H2_CS_ERROR;
684}
685
Willy Tarreau175cebb2019-01-24 10:02:24 +0100686/* marks an error on the stream. It may also update an already closed stream
687 * (e.g. to report an error after an RST was received).
688 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100689static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200690{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100691 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200692 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100693 if (h2s->st < H2_SS_ERROR)
694 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100695 if (h2s->cs)
696 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200697 }
698}
699
Willy Tarreau7e094452018-12-19 18:08:52 +0100700/* attempt to notify the data layer of recv availability */
701static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
702{
703 struct wait_event *sw;
704
705 if (h2s->recv_wait) {
706 sw = h2s->recv_wait;
707 sw->events &= ~SUB_RETRY_RECV;
708 tasklet_wakeup(sw->task);
709 h2s->recv_wait = NULL;
710 }
711}
712
713/* attempt to notify the data layer of send availability */
714static void __maybe_unused h2s_notify_send(struct h2s *h2s)
715{
716 struct wait_event *sw;
717
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100718 if (h2s->send_wait && !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100719 sw = h2s->send_wait;
720 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100721 sw->events |= SUB_CALL_UNSUBSCRIBE;
722 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100723 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100724 }
725}
726
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100727/* alerts the data layer, trying to wake it up by all means, following
728 * this sequence :
729 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
730 * - if its subscribed to send, then it's woken up for send
731 * - if it was subscribed to neither, its ->wake() callback is called
732 * It is safe to call this function with a closed stream which doesn't have a
733 * conn_stream anymore.
734 */
735static void __maybe_unused h2s_alert(struct h2s *h2s)
736{
737 if (h2s->recv_wait || h2s->send_wait) {
738 h2s_notify_recv(h2s);
739 h2s_notify_send(h2s);
740 }
741 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
742 h2s->cs->data_cb->wake(h2s->cs);
743}
744
Willy Tarreaue4820742017-07-27 13:37:23 +0200745/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100746static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200747{
748 uint8_t *out = frame;
749
750 *out = len >> 16;
751 write_n16(out + 1, len);
752}
753
Willy Tarreau54c15062017-10-10 17:10:03 +0200754/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
755 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
756 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200757 * available in the buffer's input prior to calling this function. The buffer
758 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200759 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100760static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200761 const struct buffer *b, int o)
762{
Willy Tarreau591d4452018-06-15 17:21:00 +0200763 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 +0200764}
765
Willy Tarreau1f094672017-11-20 21:27:45 +0100766static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200767{
Willy Tarreau591d4452018-06-15 17:21:00 +0200768 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200769}
770
Willy Tarreau1f094672017-11-20 21:27:45 +0100771static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200772{
Willy Tarreau591d4452018-06-15 17:21:00 +0200773 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200774}
775
Willy Tarreau1f094672017-11-20 21:27:45 +0100776static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200777{
Willy Tarreau591d4452018-06-15 17:21:00 +0200778 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200779}
780
781
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100782/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
783 * The algorithm is not obvious. It turns out that H2 headers are neither
784 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
785 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200786 *
787 * b0 b1 b2 b3 b4 b5..b8
788 * +----------+---------+--------+----+----+----------------------+
789 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
790 * +----------+---------+--------+----+----+----------------------+
791 *
792 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
793 * we get the sid properly aligned and ordered, and 16 bits of len properly
794 * ordered as well. The type and flags can be extracted using bit shifts from
795 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200796 * Returns zero if some bytes are missing, otherwise non-zero on success. The
797 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200798 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100799static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200800{
801 uint64_t w;
802
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100803 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200804 return 0;
805
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100806 w = h2_get_n64(b, o + 1);
807 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200808 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
809 h->ff = w >> 32;
810 h->ft = w >> 40;
811 h->len += w >> 48;
812 return 1;
813}
814
815/* skip the next 9 bytes corresponding to the frame header possibly parsed by
816 * h2_peek_frame_hdr() above.
817 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100818static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200819{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200820 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200821}
822
823/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100824static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200825{
826 int ret;
827
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100828 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200829 if (ret > 0)
830 h2_skip_frame_hdr(b);
831 return ret;
832}
833
Willy Tarreau00dd0782018-03-01 16:31:34 +0100834/* marks stream <h2s> as CLOSED and decrement the number of active streams for
835 * its connection if the stream was not yet closed. Please use this exclusively
836 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100837 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100838static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100839{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100840 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100841 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100842 if (!h2s->id)
843 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100844 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100845 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100846 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
847 h2s_notify_recv(h2s);
848 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100849 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100850 h2s->st = H2_SS_CLOSED;
851}
852
Willy Tarreau71049cc2018-03-28 13:56:39 +0200853/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
854static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100855{
856 h2s_close(h2s);
857 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200858 if (b_size(&h2s->rxbuf)) {
859 b_free(&h2s->rxbuf);
860 offer_buffers(NULL, tasks_run_queue);
861 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200862 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100863 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200864 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100865 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800866 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200867 * reference left would be in the h2c send_list/fctl_list, and if
868 * we're in it, we're getting out anyway
869 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100870 LIST_DEL_INIT(&h2s->list);
871 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200872 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100873 pool_free(pool_head_h2s, h2s);
874}
875
Willy Tarreaua8e49542018-10-03 18:53:55 +0200876/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
877 * stream tree. In case of error, nothing is added and NULL is returned. The
878 * causes of errors can be any failed memory allocation. The caller is
879 * responsible for checking if the connection may support an extra stream
880 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200881 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200882static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200883{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200884 struct h2s *h2s;
885
Willy Tarreaubafbe012017-11-24 17:34:44 +0100886 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200887 if (!h2s)
888 goto out;
889
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200890 h2s->wait_event.task = tasklet_new();
891 if (!h2s->wait_event.task) {
892 pool_free(pool_head_h2s, h2s);
893 goto out;
894 }
895 h2s->send_wait = NULL;
896 h2s->recv_wait = NULL;
897 h2s->wait_event.task->process = h2_deferred_shut;
898 h2s->wait_event.task->context = h2s;
899 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100900 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200901 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100902 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200903 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200904 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200905 h2s->mws = h2c->miw;
906 h2s->flags = H2_SF_NONE;
907 h2s->errcode = H2_ERR_NO_ERROR;
908 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200909 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100910 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200911 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200912
913 if (h2c->flags & H2_CF_IS_BACK) {
914 h1m_init_req(&h2s->h1m);
915 h2s->h1m.err_pos = -1; // don't care about errors on the request path
916 h2s->h1m.flags |= H1_MF_TOLOWER;
917 } else {
918 h1m_init_res(&h2s->h1m);
919 h2s->h1m.err_pos = -1; // don't care about errors on the response path
920 h2s->h1m.flags |= H1_MF_TOLOWER;
921 }
922
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200923 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200924 if (id > 0)
925 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100926 else
927 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200928
929 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100930 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100931 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200932
933 return h2s;
934
935 out_free_h2s:
936 pool_free(pool_head_h2s, h2s);
937 out:
938 return NULL;
939}
940
941/* creates a new stream <id> on the h2c connection and returns it, or NULL in
942 * case of memory allocation error.
943 */
944static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
945{
946 struct session *sess = h2c->conn->owner;
947 struct conn_stream *cs;
948 struct h2s *h2s;
949
950 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
951 goto out;
952
953 h2s = h2s_new(h2c, id);
954 if (!h2s)
955 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200956
957 cs = cs_new(h2c->conn);
958 if (!cs)
959 goto out_close;
960
Olivier Houchard746fb772018-12-15 19:42:00 +0100961 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200962 h2s->cs = cs;
963 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200964 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200965
966 if (stream_create_from_cs(cs) < 0)
967 goto out_free_cs;
968
Willy Tarreau590a0512018-09-05 11:56:48 +0200969 /* We want the accept date presented to the next stream to be the one
970 * we have now, the handshake time to be null (since the next stream
971 * is not delayed by a handshake), and the idle time to count since
972 * right now.
973 */
974 sess->accept_date = date;
975 sess->tv_accept = now;
976 sess->t_handshake = 0;
977
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200978 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100979 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200980 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200981 return h2s;
982
983 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200984 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200985 cs_free(cs);
986 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200987 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200988 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200989 sess_log(sess);
990 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200991}
992
Willy Tarreau751f2d02018-10-05 09:35:00 +0200993/* allocates a new stream associated to conn_stream <cs> on the h2c connection
994 * and returns it, or NULL in case of memory allocation error or if the highest
995 * possible stream ID was reached.
996 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100997static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200998{
999 struct h2s *h2s = NULL;
1000
Willy Tarreau86949782019-01-31 10:42:05 +01001001 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001002 goto out;
1003
Willy Tarreaua80dca82019-01-24 17:08:28 +01001004 if (h2_streams_left(h2c) < 1)
1005 goto out;
1006
Willy Tarreau751f2d02018-10-05 09:35:00 +02001007 /* Defer choosing the ID until we send the first message to create the stream */
1008 h2s = h2s_new(h2c, 0);
1009 if (!h2s)
1010 goto out;
1011
1012 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001013 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001014 cs->ctx = h2s;
1015 h2c->nb_cs++;
1016
Willy Tarreau751f2d02018-10-05 09:35:00 +02001017 out:
1018 return h2s;
1019}
1020
Willy Tarreaube5b7152017-09-25 16:25:39 +02001021/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1022 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1023 * the various settings codes.
1024 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001025static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001026{
1027 struct buffer *res;
1028 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001029 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001030 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001031 int ret;
1032
1033 if (h2c_mux_busy(h2c, NULL)) {
1034 h2c->flags |= H2_CF_DEM_MBUSY;
1035 return 0;
1036 }
1037
Willy Tarreau44e973f2018-03-01 17:49:30 +01001038 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001039 if (!res) {
1040 h2c->flags |= H2_CF_MUX_MALLOC;
1041 h2c->flags |= H2_CF_DEM_MROOM;
1042 return 0;
1043 }
1044
1045 chunk_init(&buf, buf_data, sizeof(buf_data));
1046 chunk_memcpy(&buf,
1047 "\x00\x00\x00" /* length : 0 for now */
1048 "\x04\x00" /* type : 4 (settings), flags : 0 */
1049 "\x00\x00\x00\x00", /* stream ID : 0 */
1050 9);
1051
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001052 if (h2c->flags & H2_CF_IS_BACK) {
1053 /* send settings_enable_push=0 */
1054 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1055 }
1056
Willy Tarreaube5b7152017-09-25 16:25:39 +02001057 if (h2_settings_header_table_size != 4096) {
1058 char str[6] = "\x00\x01"; /* header_table_size */
1059
1060 write_n32(str + 2, h2_settings_header_table_size);
1061 chunk_memcat(&buf, str, 6);
1062 }
1063
1064 if (h2_settings_initial_window_size != 65535) {
1065 char str[6] = "\x00\x04"; /* initial_window_size */
1066
1067 write_n32(str + 2, h2_settings_initial_window_size);
1068 chunk_memcat(&buf, str, 6);
1069 }
1070
1071 if (h2_settings_max_concurrent_streams != 0) {
1072 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1073
1074 /* Note: 0 means "unlimited" for haproxy's config but not for
1075 * the protocol, so never send this value!
1076 */
1077 write_n32(str + 2, h2_settings_max_concurrent_streams);
1078 chunk_memcat(&buf, str, 6);
1079 }
1080
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001081 mfs = h2_settings_max_frame_size;
1082 if (mfs > global.tune.bufsize)
1083 mfs = global.tune.bufsize;
1084
1085 if (!mfs)
1086 mfs = global.tune.bufsize;
1087
1088 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001089 char str[6] = "\x00\x05"; /* max_frame_size */
1090
1091 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1092 * match bufsize - rewrite size, but at the moment it seems
1093 * that clients don't take care of it.
1094 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001095 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001096 chunk_memcat(&buf, str, 6);
1097 }
1098
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001099 h2_set_frame_size(buf.area, buf.data - 9);
1100 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001101 if (unlikely(ret <= 0)) {
1102 if (!ret) {
1103 h2c->flags |= H2_CF_MUX_MFULL;
1104 h2c->flags |= H2_CF_DEM_MROOM;
1105 return 0;
1106 }
1107 else {
1108 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1109 return 0;
1110 }
1111 }
1112 return ret;
1113}
1114
Willy Tarreau52eed752017-09-22 15:05:09 +02001115/* Try to receive a connection preface, then upon success try to send our
1116 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1117 * missing data. It may return an error in h2c.
1118 */
1119static int h2c_frt_recv_preface(struct h2c *h2c)
1120{
1121 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001122 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001123
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001124 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001125
1126 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001127 if (ret1 < 0)
1128 sess_log(h2c->conn->owner);
1129
Willy Tarreau52eed752017-09-22 15:05:09 +02001130 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1131 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1132 return 0;
1133 }
1134
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001135 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001136 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001137 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001138
Willy Tarreaube5b7152017-09-25 16:25:39 +02001139 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001140}
1141
Willy Tarreau01b44822018-10-03 14:26:37 +02001142/* Try to send a connection preface, then upon success try to send our
1143 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1144 * missing data. It may return an error in h2c.
1145 */
1146static int h2c_bck_send_preface(struct h2c *h2c)
1147{
1148 struct buffer *res;
1149
1150 if (h2c_mux_busy(h2c, NULL)) {
1151 h2c->flags |= H2_CF_DEM_MBUSY;
1152 return 0;
1153 }
1154
1155 res = h2_get_buf(h2c, &h2c->mbuf);
1156 if (!res) {
1157 h2c->flags |= H2_CF_MUX_MALLOC;
1158 h2c->flags |= H2_CF_DEM_MROOM;
1159 return 0;
1160 }
1161
1162 if (!b_data(res)) {
1163 /* preface not yet sent */
1164 b_istput(res, ist(H2_CONN_PREFACE));
1165 }
1166
1167 return h2c_send_settings(h2c);
1168}
1169
Willy Tarreau081d4722017-05-16 21:51:05 +02001170/* try to send a GOAWAY frame on the connection to report an error or a graceful
1171 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1172 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1173 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1174 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1175 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1176 * on unrecoverable failure. It will not attempt to send one again in this last
1177 * case so that it is safe to use h2c_error() to report such errors.
1178 */
1179static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1180{
1181 struct buffer *res;
1182 char str[17];
1183 int ret;
1184
1185 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1186 return 1; // claim that it worked
1187
1188 if (h2c_mux_busy(h2c, h2s)) {
1189 if (h2s)
1190 h2s->flags |= H2_SF_BLK_MBUSY;
1191 else
1192 h2c->flags |= H2_CF_DEM_MBUSY;
1193 return 0;
1194 }
1195
Willy Tarreau44e973f2018-03-01 17:49:30 +01001196 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001197 if (!res) {
1198 h2c->flags |= H2_CF_MUX_MALLOC;
1199 if (h2s)
1200 h2s->flags |= H2_SF_BLK_MROOM;
1201 else
1202 h2c->flags |= H2_CF_DEM_MROOM;
1203 return 0;
1204 }
1205
1206 /* len: 8, type: 7, flags: none, sid: 0 */
1207 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1208
1209 if (h2c->last_sid < 0)
1210 h2c->last_sid = h2c->max_id;
1211
1212 write_n32(str + 9, h2c->last_sid);
1213 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001214 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001215 if (unlikely(ret <= 0)) {
1216 if (!ret) {
1217 h2c->flags |= H2_CF_MUX_MFULL;
1218 if (h2s)
1219 h2s->flags |= H2_SF_BLK_MROOM;
1220 else
1221 h2c->flags |= H2_CF_DEM_MROOM;
1222 return 0;
1223 }
1224 else {
1225 /* we cannot report this error using GOAWAY, so we mark
1226 * it and claim a success.
1227 */
1228 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1229 h2c->flags |= H2_CF_GOAWAY_FAILED;
1230 return 1;
1231 }
1232 }
1233 h2c->flags |= H2_CF_GOAWAY_SENT;
1234 return ret;
1235}
1236
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001237/* Try to send an RST_STREAM frame on the connection for the indicated stream
1238 * during mux operations. This stream must be valid and cannot be closed
1239 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1240 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1241 * not yet.
1242 *
1243 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1244 * to write the message, it subscribes the stream to future notifications.
1245 */
1246static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1247{
1248 struct buffer *res;
1249 char str[13];
1250 int ret;
1251
1252 if (!h2s || h2s->st == H2_SS_CLOSED)
1253 return 1;
1254
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001255 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1256 * RST_STREAM in response to a RST_STREAM frame.
1257 */
1258 if (h2c->dft == H2_FT_RST_STREAM) {
1259 ret = 1;
1260 goto ignore;
1261 }
1262
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001263 if (h2c_mux_busy(h2c, h2s)) {
1264 h2s->flags |= H2_SF_BLK_MBUSY;
1265 return 0;
1266 }
1267
Willy Tarreau44e973f2018-03-01 17:49:30 +01001268 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001269 if (!res) {
1270 h2c->flags |= H2_CF_MUX_MALLOC;
1271 h2s->flags |= H2_SF_BLK_MROOM;
1272 return 0;
1273 }
1274
1275 /* len: 4, type: 3, flags: none */
1276 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1277 write_n32(str + 5, h2s->id);
1278 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001279 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001280
1281 if (unlikely(ret <= 0)) {
1282 if (!ret) {
1283 h2c->flags |= H2_CF_MUX_MFULL;
1284 h2s->flags |= H2_SF_BLK_MROOM;
1285 return 0;
1286 }
1287 else {
1288 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1289 return 0;
1290 }
1291 }
1292
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001293 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001294 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001295 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001296 return ret;
1297}
1298
1299/* Try to send an RST_STREAM frame on the connection for the stream being
1300 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001301 * error code, even if the stream is one of the dummy ones, and will update
1302 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001303 *
1304 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1305 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001306 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001307 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001308 */
1309static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1310{
1311 struct buffer *res;
1312 char str[13];
1313 int ret;
1314
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001315 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1316 * RST_STREAM in response to a RST_STREAM frame.
1317 */
1318 if (h2c->dft == H2_FT_RST_STREAM) {
1319 ret = 1;
1320 goto ignore;
1321 }
1322
Willy Tarreau27a84c92017-10-17 08:10:17 +02001323 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001324 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001325 return 0;
1326 }
1327
Willy Tarreau44e973f2018-03-01 17:49:30 +01001328 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001329 if (!res) {
1330 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001331 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001332 return 0;
1333 }
1334
1335 /* len: 4, type: 3, flags: none */
1336 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001337
Willy Tarreau27a84c92017-10-17 08:10:17 +02001338 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001339 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001340 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001341
Willy Tarreau27a84c92017-10-17 08:10:17 +02001342 if (unlikely(ret <= 0)) {
1343 if (!ret) {
1344 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001345 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001346 return 0;
1347 }
1348 else {
1349 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1350 return 0;
1351 }
1352 }
1353
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001354 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001355 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001356 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001357 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001358 }
1359
Willy Tarreau27a84c92017-10-17 08:10:17 +02001360 return ret;
1361}
1362
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001363/* try to send an empty DATA frame with the ES flag set to notify about the
1364 * end of stream and match a shutdown(write). If an ES was already sent as
1365 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1366 * on success or zero if nothing was done. In case of lack of room to write the
1367 * message, it subscribes the requesting stream to future notifications.
1368 */
1369static int h2_send_empty_data_es(struct h2s *h2s)
1370{
1371 struct h2c *h2c = h2s->h2c;
1372 struct buffer *res;
1373 char str[9];
1374 int ret;
1375
Willy Tarreau721c9742017-11-07 11:05:42 +01001376 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001377 return 1;
1378
1379 if (h2c_mux_busy(h2c, h2s)) {
1380 h2s->flags |= H2_SF_BLK_MBUSY;
1381 return 0;
1382 }
1383
Willy Tarreau44e973f2018-03-01 17:49:30 +01001384 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001385 if (!res) {
1386 h2c->flags |= H2_CF_MUX_MALLOC;
1387 h2s->flags |= H2_SF_BLK_MROOM;
1388 return 0;
1389 }
1390
1391 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1392 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1393 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001394 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001395 if (likely(ret > 0)) {
1396 h2s->flags |= H2_SF_ES_SENT;
1397 }
1398 else if (!ret) {
1399 h2c->flags |= H2_CF_MUX_MFULL;
1400 h2s->flags |= H2_SF_BLK_MROOM;
1401 return 0;
1402 }
1403 else {
1404 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1405 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001406 }
1407 return ret;
1408}
1409
Christopher Fauletf02ca002019-03-07 16:21:34 +01001410/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1411 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1412 * connection. The stream's state is automatically updated accordingly. If the
1413 * stream is orphaned, it is destroyed.
1414 */
1415static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1416{
1417 if (!h2s->cs) {
1418 /* this stream was already orphaned */
1419 h2s_destroy(h2s);
1420 return;
1421 }
1422
1423 h2s->cs->flags |= flags;
1424 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1425 h2s->cs->flags |= CS_FL_ERROR;
1426
1427 h2s_alert(h2s);
1428
1429 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1430 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001431 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001432 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001433 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001434 h2s_close(h2s);
1435}
1436
1437/* wake the streams attached to the connection, whose id is greater than <last>
1438 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001439 */
1440static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1441{
1442 struct eb32_node *node;
1443 struct h2s *h2s;
1444
1445 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001446 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001447
1448 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001449 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001450
Christopher Fauletf02ca002019-03-07 16:21:34 +01001451 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001452 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1453 while (node) {
1454 h2s = container_of(node, struct h2s, by_id);
1455 if (h2s->id <= last)
1456 break;
1457 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001458 h2s_wake_one_stream(h2s, flags);
1459 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001460
Christopher Fauletf02ca002019-03-07 16:21:34 +01001461 /* Wake all streams with unassigned ID (ID == 0) */
1462 node = eb32_lookup(&h2c->streams_by_id, 0);
1463 while (node) {
1464 h2s = container_of(node, struct h2s, by_id);
1465 if (h2s->id > 0)
1466 break;
1467 node = eb32_next(node);
1468 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001469 }
1470}
1471
Willy Tarreau3421aba2017-07-27 15:41:03 +02001472/* Increase all streams' outgoing window size by the difference passed in
1473 * argument. This is needed upon receipt of the settings frame if the initial
1474 * window size is different. The difference may be negative and the resulting
1475 * window size as well, for the time it takes to receive some window updates.
1476 */
1477static void h2c_update_all_ws(struct h2c *h2c, int diff)
1478{
1479 struct h2s *h2s;
1480 struct eb32_node *node;
1481
1482 if (!diff)
1483 return;
1484
1485 node = eb32_first(&h2c->streams_by_id);
1486 while (node) {
1487 h2s = container_of(node, struct h2s, by_id);
1488 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001489
1490 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1491 h2s->flags &= ~H2_SF_BLK_SFCTL;
1492 if (h2s->send_wait)
1493 LIST_ADDQ(&h2c->send_list, &h2s->list);
1494
1495 }
1496
Willy Tarreau3421aba2017-07-27 15:41:03 +02001497 node = eb32_next(node);
1498 }
1499}
1500
1501/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1502 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001503 * return an error in h2c. The caller must have already verified frame length
1504 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001505 */
1506static int h2c_handle_settings(struct h2c *h2c)
1507{
1508 unsigned int offset;
1509 int error;
1510
1511 if (h2c->dff & H2_F_SETTINGS_ACK) {
1512 if (h2c->dfl) {
1513 error = H2_ERR_FRAME_SIZE_ERROR;
1514 goto fail;
1515 }
1516 return 1;
1517 }
1518
Willy Tarreau3421aba2017-07-27 15:41:03 +02001519 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001520 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001521 return 0;
1522
1523 /* parse the frame */
1524 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001525 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1526 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001527
1528 switch (type) {
1529 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1530 /* we need to update all existing streams with the
1531 * difference from the previous iws.
1532 */
1533 if (arg < 0) { // RFC7540#6.5.2
1534 error = H2_ERR_FLOW_CONTROL_ERROR;
1535 goto fail;
1536 }
1537 h2c_update_all_ws(h2c, arg - h2c->miw);
1538 h2c->miw = arg;
1539 break;
1540 case H2_SETTINGS_MAX_FRAME_SIZE:
1541 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1542 error = H2_ERR_PROTOCOL_ERROR;
1543 goto fail;
1544 }
1545 h2c->mfs = arg;
1546 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001547 case H2_SETTINGS_ENABLE_PUSH:
1548 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1549 error = H2_ERR_PROTOCOL_ERROR;
1550 goto fail;
1551 }
1552 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001553 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1554 if (h2c->flags & H2_CF_IS_BACK) {
1555 /* the limit is only for the backend; for the frontend it is our limit */
1556 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1557 arg = h2_settings_max_concurrent_streams;
1558 h2c->streams_limit = arg;
1559 }
1560 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001561 }
1562 }
1563
1564 /* need to ACK this frame now */
1565 h2c->st0 = H2_CS_FRAME_A;
1566 return 1;
1567 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001568 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001569 h2c_error(h2c, error);
1570 return 0;
1571}
1572
1573/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1574 * success or one of the h2_status values.
1575 */
1576static int h2c_ack_settings(struct h2c *h2c)
1577{
1578 struct buffer *res;
1579 char str[9];
1580 int ret = -1;
1581
1582 if (h2c_mux_busy(h2c, NULL)) {
1583 h2c->flags |= H2_CF_DEM_MBUSY;
1584 return 0;
1585 }
1586
Willy Tarreau44e973f2018-03-01 17:49:30 +01001587 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001588 if (!res) {
1589 h2c->flags |= H2_CF_MUX_MALLOC;
1590 h2c->flags |= H2_CF_DEM_MROOM;
1591 return 0;
1592 }
1593
1594 memcpy(str,
1595 "\x00\x00\x00" /* length : 0 (no data) */
1596 "\x04" "\x01" /* type : 4, flags : ACK */
1597 "\x00\x00\x00\x00" /* stream ID */, 9);
1598
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001599 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001600 if (unlikely(ret <= 0)) {
1601 if (!ret) {
1602 h2c->flags |= H2_CF_MUX_MFULL;
1603 h2c->flags |= H2_CF_DEM_MROOM;
1604 return 0;
1605 }
1606 else {
1607 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1608 return 0;
1609 }
1610 }
1611 return ret;
1612}
1613
Willy Tarreaucf68c782017-10-10 17:11:41 +02001614/* processes a PING frame and schedules an ACK if needed. The caller must pass
1615 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001616 * missing data. The caller must have already verified frame length
1617 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001618 */
1619static int h2c_handle_ping(struct h2c *h2c)
1620{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001621 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001622 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001623 h2c->st0 = H2_CS_FRAME_A;
1624 return 1;
1625}
1626
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001627/* Try to send a window update for stream id <sid> and value <increment>.
1628 * Returns > 0 on success or zero on missing room or failure. It may return an
1629 * error in h2c.
1630 */
1631static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1632{
1633 struct buffer *res;
1634 char str[13];
1635 int ret = -1;
1636
1637 if (h2c_mux_busy(h2c, NULL)) {
1638 h2c->flags |= H2_CF_DEM_MBUSY;
1639 return 0;
1640 }
1641
Willy Tarreau44e973f2018-03-01 17:49:30 +01001642 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001643 if (!res) {
1644 h2c->flags |= H2_CF_MUX_MALLOC;
1645 h2c->flags |= H2_CF_DEM_MROOM;
1646 return 0;
1647 }
1648
1649 /* length: 4, type: 8, flags: none */
1650 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1651 write_n32(str + 5, sid);
1652 write_n32(str + 9, increment);
1653
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001654 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001655
1656 if (unlikely(ret <= 0)) {
1657 if (!ret) {
1658 h2c->flags |= H2_CF_MUX_MFULL;
1659 h2c->flags |= H2_CF_DEM_MROOM;
1660 return 0;
1661 }
1662 else {
1663 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1664 return 0;
1665 }
1666 }
1667 return ret;
1668}
1669
1670/* try to send pending window update for the connection. It's safe to call it
1671 * with no pending updates. Returns > 0 on success or zero on missing room or
1672 * failure. It may return an error in h2c.
1673 */
1674static int h2c_send_conn_wu(struct h2c *h2c)
1675{
1676 int ret = 1;
1677
1678 if (h2c->rcvd_c <= 0)
1679 return 1;
1680
Willy Tarreau97aaa672018-12-23 09:49:04 +01001681 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1682 /* increase the advertised connection window to 2G on
1683 * first update.
1684 */
1685 h2c->flags |= H2_CF_WINDOW_OPENED;
1686 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1687 }
1688
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001689 /* send WU for the connection */
1690 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1691 if (ret > 0)
1692 h2c->rcvd_c = 0;
1693
1694 return ret;
1695}
1696
1697/* try to send pending window update for the current dmux stream. It's safe to
1698 * call it with no pending updates. Returns > 0 on success or zero on missing
1699 * room or failure. It may return an error in h2c.
1700 */
1701static int h2c_send_strm_wu(struct h2c *h2c)
1702{
1703 int ret = 1;
1704
1705 if (h2c->rcvd_s <= 0)
1706 return 1;
1707
1708 /* send WU for the stream */
1709 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1710 if (ret > 0)
1711 h2c->rcvd_s = 0;
1712
1713 return ret;
1714}
1715
Willy Tarreaucf68c782017-10-10 17:11:41 +02001716/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1717 * success, 0 on missing data or one of the h2_status values.
1718 */
1719static int h2c_ack_ping(struct h2c *h2c)
1720{
1721 struct buffer *res;
1722 char str[17];
1723 int ret = -1;
1724
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001725 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001726 return 0;
1727
1728 if (h2c_mux_busy(h2c, NULL)) {
1729 h2c->flags |= H2_CF_DEM_MBUSY;
1730 return 0;
1731 }
1732
Willy Tarreau44e973f2018-03-01 17:49:30 +01001733 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001734 if (!res) {
1735 h2c->flags |= H2_CF_MUX_MALLOC;
1736 h2c->flags |= H2_CF_DEM_MROOM;
1737 return 0;
1738 }
1739
1740 memcpy(str,
1741 "\x00\x00\x08" /* length : 8 (same payload) */
1742 "\x06" "\x01" /* type : 6, flags : ACK */
1743 "\x00\x00\x00\x00" /* stream ID */, 9);
1744
1745 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001746 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001747
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001748 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001749 if (unlikely(ret <= 0)) {
1750 if (!ret) {
1751 h2c->flags |= H2_CF_MUX_MFULL;
1752 h2c->flags |= H2_CF_DEM_MROOM;
1753 return 0;
1754 }
1755 else {
1756 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1757 return 0;
1758 }
1759 }
1760 return ret;
1761}
1762
Willy Tarreau26f95952017-07-27 17:18:30 +02001763/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1764 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001765 * h2c or h2s. The caller must have already verified frame length and stream ID
1766 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001767 */
1768static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1769{
1770 int32_t inc;
1771 int error;
1772
Willy Tarreau26f95952017-07-27 17:18:30 +02001773 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001774 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001775 return 0;
1776
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001777 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001778
1779 if (h2c->dsi != 0) {
1780 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001781
1782 /* it's not an error to receive WU on a closed stream */
1783 if (h2s->st == H2_SS_CLOSED)
1784 return 1;
1785
1786 if (!inc) {
1787 error = H2_ERR_PROTOCOL_ERROR;
1788 goto strm_err;
1789 }
1790
1791 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1792 error = H2_ERR_FLOW_CONTROL_ERROR;
1793 goto strm_err;
1794 }
1795
1796 h2s->mws += inc;
1797 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1798 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001799 if (h2s->send_wait)
1800 LIST_ADDQ(&h2c->send_list, &h2s->list);
1801
Willy Tarreau26f95952017-07-27 17:18:30 +02001802 }
1803 }
1804 else {
1805 /* connection window update */
1806 if (!inc) {
1807 error = H2_ERR_PROTOCOL_ERROR;
1808 goto conn_err;
1809 }
1810
1811 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1812 error = H2_ERR_FLOW_CONTROL_ERROR;
1813 goto conn_err;
1814 }
1815
1816 h2c->mws += inc;
1817 }
1818
1819 return 1;
1820
1821 conn_err:
1822 h2c_error(h2c, error);
1823 return 0;
1824
1825 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001826 h2s_error(h2s, error);
1827 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001828 return 0;
1829}
1830
Willy Tarreaue96b0922017-10-30 00:28:29 +01001831/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001832 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1833 * have already verified frame length and stream ID validity. Described in
1834 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001835 */
1836static int h2c_handle_goaway(struct h2c *h2c)
1837{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001838 int last;
1839
Willy Tarreaue96b0922017-10-30 00:28:29 +01001840 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001841 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001842 return 0;
1843
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001844 last = h2_get_n32(&h2c->dbuf, 0);
1845 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001846 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001847 if (h2c->last_sid < 0)
1848 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001849 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001850}
1851
Willy Tarreau92153fc2017-12-03 19:46:19 +01001852/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001853 * invalid. Returns > 0 on success or zero on missing data. It may return an
1854 * error in h2c. The caller must have already verified frame length and stream
1855 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001856 */
1857static int h2c_handle_priority(struct h2c *h2c)
1858{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001859 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001860 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001861 return 0;
1862
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001863 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001864 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001865 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1866 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001867 }
1868 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001869}
1870
Willy Tarreaucd234e92017-08-18 10:59:39 +02001871/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001872 * Returns > 0 on success or zero on missing data. The caller must have already
1873 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001874 */
1875static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1876{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001877 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001878 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001879 return 0;
1880
1881 /* late RST, already handled */
1882 if (h2s->st == H2_SS_CLOSED)
1883 return 1;
1884
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001885 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001886 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001887
1888 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001889 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001890 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001891 }
1892
1893 h2s->flags |= H2_SF_RST_RCVD;
1894 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001895}
1896
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001897/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1898 * It may return an error in h2c or h2s. The caller must consider that the
1899 * return value is the new h2s in case one was allocated (most common case).
1900 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001901 * errors here are reported as connection errors since it's impossible to
1902 * recover from such errors after the compression context has been altered.
1903 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001904static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001905{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001906 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001907 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001908 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001909 int error;
1910
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001911 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001912 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001913
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001914 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001915 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001916
1917 /* now either the frame is complete or the buffer is complete */
1918 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001919 /* The stream exists/existed, this must be a trailers frame */
1920 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001921 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001922 goto out;
1923 goto done;
1924 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001925 /* the connection was already killed by an RST, let's consume
1926 * the data and send another RST.
1927 */
1928 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1929 h2s = (struct h2s*)h2_error_stream;
1930 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001931 }
1932 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1933 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1934 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001935 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001936 goto conn_err;
1937 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001938 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1939 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001940
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001941 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001942
Willy Tarreau25919232019-01-03 14:48:18 +01001943 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001944 if (h2c->st0 >= H2_CS_ERROR)
1945 goto out;
1946
Willy Tarreau25919232019-01-03 14:48:18 +01001947 if (error <= 0) {
1948 if (error == 0)
1949 goto out; // missing data
1950
1951 /* Failed to decode this stream (e.g. too large request)
1952 * but the HPACK decompressor is still synchronized.
1953 */
1954 h2s = (struct h2s*)h2_error_stream;
1955 goto send_rst;
1956 }
1957
Willy Tarreau22de8d32018-09-05 19:55:58 +02001958 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001959 * positively from h2c_frt_stream_new(), the stream will report the error,
1960 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001961 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001962 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001963 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001964 h2s = (struct h2s*)h2_refused_stream;
1965 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001966 }
1967
1968 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001969 h2s->rxbuf = rxbuf;
1970 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001971 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001972
Willy Tarreau88d138e2019-01-02 19:38:14 +01001973 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001974 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001975 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001976
1977 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001978 if (h2s->cs)
1979 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01001980 if (h2s->st == H2_SS_OPEN)
1981 h2s->st = H2_SS_HREM;
1982 else
1983 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02001984 }
1985
Willy Tarreau3a429f02019-01-03 11:41:50 +01001986 /* update the max stream ID if the request is being processed */
1987 if (h2s->id > h2c->max_id)
1988 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001989
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001990 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001991
1992 conn_err:
1993 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001994 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001995
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001996 out:
1997 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001998 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001999
2000 send_rst:
2001 /* make the demux send an RST for the current stream. We may only
2002 * do this if we're certain that the HEADERS frame was properly
2003 * decompressed so that the HPACK decoder is still kept up to date.
2004 */
2005 h2_release_buf(h2c, &rxbuf);
2006 h2c->st0 = H2_CS_FRAME_E;
2007 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002008}
2009
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002010/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2011 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2012 * errors here are reported as connection errors since it's impossible to
2013 * recover from such errors after the compression context has been altered.
2014 */
2015static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2016{
2017 int error;
2018
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002019 if (!b_size(&h2c->dbuf))
2020 return NULL; // empty buffer
2021
2022 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2023 return NULL; // incomplete frame
2024
Willy Tarreau1915ca22019-01-24 11:49:37 +01002025 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002026
Willy Tarreau25919232019-01-03 14:48:18 +01002027 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002028 if (h2c->st0 >= H2_CS_ERROR)
2029 return NULL;
2030
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002031 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2032 /* RFC7540#5.1 */
2033 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2034 h2c->st0 = H2_CS_FRAME_E;
2035 return NULL;
2036 }
2037
Willy Tarreau25919232019-01-03 14:48:18 +01002038 if (error <= 0) {
2039 if (error == 0)
2040 return NULL; // missing data
2041
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002042 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002043 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002044 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002045 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002046 }
2047
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002048 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2049 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002050 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002051 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002052 }
2053
Willy Tarreau927b88b2019-03-04 08:03:25 +01002054 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002055 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002056 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 +02002057 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002058 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002059 h2s_close(h2s);
2060
2061 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002062}
2063
Willy Tarreau454f9052017-10-26 19:40:35 +02002064/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2065 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2066 */
2067static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2068{
2069 int error;
2070
2071 /* note that empty DATA frames are perfectly valid and sometimes used
2072 * to signal an end of stream (with the ES flag).
2073 */
2074
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002075 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002076 return 0; // empty buffer
2077
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002078 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002079 return 0; // incomplete frame
2080
2081 /* now either the frame is complete or the buffer is complete */
2082
Willy Tarreau454f9052017-10-26 19:40:35 +02002083 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2084 /* RFC7540#6.1 */
2085 error = H2_ERR_STREAM_CLOSED;
2086 goto strm_err;
2087 }
2088
Willy Tarreau1915ca22019-01-24 11:49:37 +01002089 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2090 /* RFC7540#8.1.2 */
2091 error = H2_ERR_PROTOCOL_ERROR;
2092 goto strm_err;
2093 }
2094
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002095 if (!h2_frt_transfer_data(h2s))
2096 return 0;
2097
Willy Tarreau454f9052017-10-26 19:40:35 +02002098 /* call the upper layers to process the frame, then let the upper layer
2099 * notify the stream about any change.
2100 */
2101 if (!h2s->cs) {
2102 error = H2_ERR_STREAM_CLOSED;
2103 goto strm_err;
2104 }
2105
Willy Tarreau8f650c32017-11-21 19:36:21 +01002106 if (h2c->st0 >= H2_CS_ERROR)
2107 return 0;
2108
Willy Tarreau721c9742017-11-07 11:05:42 +01002109 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002110 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002111 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002112 }
2113
2114 /* check for completion : the callee will change this to FRAME_A or
2115 * FRAME_H once done.
2116 */
2117 if (h2c->st0 == H2_CS_FRAME_P)
2118 return 0;
2119
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002120 /* last frame */
2121 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002122 if (h2s->cs)
2123 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002124 if (h2s->st == H2_SS_OPEN)
2125 h2s->st = H2_SS_HREM;
2126 else
2127 h2s_close(h2s);
2128
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002129 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002130
2131 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2132 /* RFC7540#8.1.2 */
2133 error = H2_ERR_PROTOCOL_ERROR;
2134 goto strm_err;
2135 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002136 }
2137
Willy Tarreau454f9052017-10-26 19:40:35 +02002138 return 1;
2139
Willy Tarreau454f9052017-10-26 19:40:35 +02002140 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002141 h2s_error(h2s, error);
2142 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002143 return 0;
2144}
2145
Willy Tarreaubc933932017-10-09 16:21:43 +02002146/* process Rx frames to be demultiplexed */
2147static void h2_process_demux(struct h2c *h2c)
2148{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002149 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002150 struct h2_fh hdr;
2151 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002152
Willy Tarreau081d4722017-05-16 21:51:05 +02002153 if (h2c->st0 >= H2_CS_ERROR)
2154 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002155
2156 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2157 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002158 if (h2c->flags & H2_CF_IS_BACK)
2159 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002160 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2161 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002162 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002163 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002164 sess_log(h2c->conn->owner);
2165 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002166 goto fail;
2167 }
2168
2169 h2c->max_id = 0;
2170 h2c->st0 = H2_CS_SETTINGS1;
2171 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002172
2173 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002174 /* ensure that what is pending is a valid SETTINGS frame
2175 * without an ACK.
2176 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002177 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002178 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002179 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002180 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002181 sess_log(h2c->conn->owner);
2182 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002183 goto fail;
2184 }
2185
2186 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2187 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2188 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2189 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002190 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002191 goto fail;
2192 }
2193
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002194 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002195 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2196 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2197 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002198 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002199 goto fail;
2200 }
2201
Willy Tarreau3bf69182018-12-21 15:34:50 +01002202 /* that's OK, switch to FRAME_P to process it. This is
2203 * a SETTINGS frame whose header has already been
2204 * deleted above.
2205 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002206 padlen = 0;
2207 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002208 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002209 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002210
2211 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002212 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002213 int ret = 0;
2214
2215 if (h2c->st0 >= H2_CS_ERROR)
2216 break;
2217
2218 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002219 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002220 break;
2221
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002222 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002223 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002224 if (!h2c->nb_streams) {
2225 /* only log if no other stream can report the error */
2226 sess_log(h2c->conn->owner);
2227 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002228 break;
2229 }
2230
Willy Tarreau3bf69182018-12-21 15:34:50 +01002231 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2232 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2233 * we read the pad length and drop it from the remaining
2234 * payload (one byte + the 9 remaining ones = 10 total
2235 * removed), so we have a frame payload starting after the
2236 * pad len. Flow controlled frames (DATA) also count the
2237 * padlen in the flow control, so it must be adjusted.
2238 */
2239 if (hdr.len < 1) {
2240 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2241 sess_log(h2c->conn->owner);
2242 goto fail;
2243 }
2244 hdr.len--;
2245
2246 if (b_data(&h2c->dbuf) < 10)
2247 break; // missing padlen
2248
2249 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2250
2251 if (padlen > hdr.len) {
2252 /* RFC7540#6.1 : pad length = length of
2253 * frame payload or greater => error.
2254 */
2255 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2256 sess_log(h2c->conn->owner);
2257 goto fail;
2258 }
2259
2260 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2261 h2c->rcvd_c++;
2262 h2c->rcvd_s++;
2263 }
2264 b_del(&h2c->dbuf, 1);
2265 }
2266 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002267
2268 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002269 h2c->dfl = hdr.len;
2270 h2c->dsi = hdr.sid;
2271 h2c->dft = hdr.ft;
2272 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002273 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002274 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002275
2276 /* check for minimum basic frame format validity */
2277 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2278 if (ret != H2_ERR_NO_ERROR) {
2279 h2c_error(h2c, ret);
2280 sess_log(h2c->conn->owner);
2281 goto fail;
2282 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002283 }
2284
2285 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002286 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2287
Willy Tarreau567beb82018-12-18 16:52:44 +01002288 if (tmp_h2s != h2s && h2s && h2s->cs &&
2289 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002290 (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 +01002291 /* we may have to signal the upper layers */
2292 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002293 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002294 }
2295 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002296
Willy Tarreaud7901432017-12-29 11:34:40 +01002297 if (h2c->st0 == H2_CS_FRAME_E)
2298 goto strm_err;
2299
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002300 if (h2s->st == H2_SS_IDLE &&
2301 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2302 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2303 * this state MUST be treated as a connection error
2304 */
2305 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002306 if (!h2c->nb_streams) {
2307 /* only log if no other stream can report the error */
2308 sess_log(h2c->conn->owner);
2309 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002310 break;
2311 }
2312
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002313 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2314 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2315 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002316 * this state MUST be treated as a stream error.
2317 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2318 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002319 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002320 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2321 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2322 else
2323 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002324 goto strm_err;
2325 }
2326
Willy Tarreauab837502017-12-27 15:07:30 +01002327 /* Below the management of frames received in closed state is a
2328 * bit hackish because the spec makes strong differences between
2329 * streams closed by receiving RST, sending RST, and seeing ES
2330 * in both directions. In addition to this, the creation of a
2331 * new stream reusing the identifier of a closed one will be
2332 * detected here. Given that we cannot keep track of all closed
2333 * streams forever, we consider that unknown closed streams were
2334 * closed on RST received, which allows us to respond with an
2335 * RST without breaking the connection (eg: to abort a transfer).
2336 * Some frames have to be silently ignored as well.
2337 */
2338 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002339 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002340 /* #5.1.1: The identifier of a newly
2341 * established stream MUST be numerically
2342 * greater than all streams that the initiating
2343 * endpoint has opened or reserved. This
2344 * governs streams that are opened using a
2345 * HEADERS frame and streams that are reserved
2346 * using PUSH_PROMISE. An endpoint that
2347 * receives an unexpected stream identifier
2348 * MUST respond with a connection error.
2349 */
2350 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2351 goto strm_err;
2352 }
2353
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002354 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002355 /* RFC7540#5.1:closed: an endpoint that
2356 * receives any frame other than PRIORITY after
2357 * receiving a RST_STREAM MUST treat that as a
2358 * stream error of type STREAM_CLOSED.
2359 *
2360 * Note that old streams fall into this category
2361 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002362 *
2363 * However, we cannot generalize this to all frame types. Those
2364 * carrying compression state must still be processed before
2365 * being dropped or we'll desynchronize the decoder. This can
2366 * happen with request trailers received after sending an
2367 * RST_STREAM, or with header/trailers responses received after
2368 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002369 */
2370 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2371 h2c->st0 = H2_CS_FRAME_E;
2372 goto strm_err;
2373 }
2374
2375 /* RFC7540#5.1:closed: if this state is reached as a
2376 * result of sending a RST_STREAM frame, the peer that
2377 * receives the RST_STREAM might have already sent
2378 * frames on the stream that cannot be withdrawn. An
2379 * endpoint MUST ignore frames that it receives on
2380 * closed streams after it has sent a RST_STREAM
2381 * frame. An endpoint MAY choose to limit the period
2382 * over which it ignores frames and treat frames that
2383 * arrive after this time as being in error.
2384 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002385 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002386 /* RFC7540#5.1:closed: any frame other than
2387 * PRIO/WU/RST in this state MUST be treated as
2388 * a connection error
2389 */
2390 if (h2c->dft != H2_FT_RST_STREAM &&
2391 h2c->dft != H2_FT_PRIORITY &&
2392 h2c->dft != H2_FT_WINDOW_UPDATE) {
2393 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2394 goto strm_err;
2395 }
2396 }
2397 }
2398
Willy Tarreauc0da1962017-10-30 18:38:00 +01002399#if 0
2400 // problem below: it is not possible to completely ignore such
2401 // streams as we need to maintain the compression state as well
2402 // and for this we need to completely process these frames (eg:
2403 // HEADERS frames) as well as counting DATA frames to emit
2404 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2405 // This is a typical case of layer violation where the
2406 // transported contents are critical to the connection's
2407 // validity and must be ignored at the same time :-(
2408
2409 /* graceful shutdown, ignore streams whose ID is higher than
2410 * the one advertised in GOAWAY. RFC7540#6.8.
2411 */
2412 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002413 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2414 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002415 h2c->dfl -= ret;
2416 ret = h2c->dfl == 0;
2417 goto strm_err;
2418 }
2419#endif
2420
Willy Tarreau7e98c052017-10-10 15:56:59 +02002421 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002422 case H2_FT_SETTINGS:
2423 if (h2c->st0 == H2_CS_FRAME_P)
2424 ret = h2c_handle_settings(h2c);
2425
2426 if (h2c->st0 == H2_CS_FRAME_A)
2427 ret = h2c_ack_settings(h2c);
2428 break;
2429
Willy Tarreaucf68c782017-10-10 17:11:41 +02002430 case H2_FT_PING:
2431 if (h2c->st0 == H2_CS_FRAME_P)
2432 ret = h2c_handle_ping(h2c);
2433
2434 if (h2c->st0 == H2_CS_FRAME_A)
2435 ret = h2c_ack_ping(h2c);
2436 break;
2437
Willy Tarreau26f95952017-07-27 17:18:30 +02002438 case H2_FT_WINDOW_UPDATE:
2439 if (h2c->st0 == H2_CS_FRAME_P)
2440 ret = h2c_handle_window_update(h2c, h2s);
2441 break;
2442
Willy Tarreau61290ec2017-10-17 08:19:21 +02002443 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002444 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2445 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2446 * frames' parsers consume all following CONTINUATION
2447 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002448 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002449 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2450 sess_log(h2c->conn->owner);
2451 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002452
Willy Tarreau13278b42017-10-13 19:23:14 +02002453 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002454 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002455 if (h2c->flags & H2_CF_IS_BACK)
2456 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2457 else
2458 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002459 if (tmp_h2s) {
2460 h2s = tmp_h2s;
2461 ret = 1;
2462 }
2463 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002464 break;
2465
Willy Tarreau454f9052017-10-26 19:40:35 +02002466 case H2_FT_DATA:
2467 if (h2c->st0 == H2_CS_FRAME_P)
2468 ret = h2c_frt_handle_data(h2c, h2s);
2469
2470 if (h2c->st0 == H2_CS_FRAME_A)
2471 ret = h2c_send_strm_wu(h2c);
2472 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002473
Willy Tarreau92153fc2017-12-03 19:46:19 +01002474 case H2_FT_PRIORITY:
2475 if (h2c->st0 == H2_CS_FRAME_P)
2476 ret = h2c_handle_priority(h2c);
2477 break;
2478
Willy Tarreaucd234e92017-08-18 10:59:39 +02002479 case H2_FT_RST_STREAM:
2480 if (h2c->st0 == H2_CS_FRAME_P)
2481 ret = h2c_handle_rst_stream(h2c, h2s);
2482 break;
2483
Willy Tarreaue96b0922017-10-30 00:28:29 +01002484 case H2_FT_GOAWAY:
2485 if (h2c->st0 == H2_CS_FRAME_P)
2486 ret = h2c_handle_goaway(h2c);
2487 break;
2488
Willy Tarreau1c661982017-10-30 13:52:01 +01002489 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002490 default:
2491 /* drop frames that we ignore. They may be larger than
2492 * the buffer so we drain all of their contents until
2493 * we reach the end.
2494 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002495 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2496 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002497 h2c->dfl -= ret;
2498 ret = h2c->dfl == 0;
2499 }
2500
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002501 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002502 /* We may have to send an RST if not done yet */
2503 if (h2s->st == H2_SS_ERROR)
2504 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002505
Willy Tarreaua20a5192017-12-27 11:02:06 +01002506 if (h2c->st0 == H2_CS_FRAME_E)
2507 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002508
Willy Tarreau7e98c052017-10-10 15:56:59 +02002509 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002510 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002511 break;
2512
2513 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002514 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002515 h2c->st0 = H2_CS_FRAME_H;
2516 }
2517 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002518
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002519 if (h2c->rcvd_c > 0 &&
2520 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2521 h2c_send_conn_wu(h2c);
2522
Willy Tarreau52eed752017-09-22 15:05:09 +02002523 fail:
2524 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002525 if (h2s && h2s->cs &&
2526 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002527 (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 +01002528 /* we may have to signal the upper layers */
2529 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002530 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002531 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002532
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002533 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002534}
2535
2536/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2537 * the end.
2538 */
2539static int h2_process_mux(struct h2c *h2c)
2540{
Olivier Houchardd360ac62019-03-22 17:37:16 +01002541 struct h2s *h2s;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002542
Willy Tarreau01b44822018-10-03 14:26:37 +02002543 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2544 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2545 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2546 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2547 if (h2c->st0 == H2_CS_ERROR) {
2548 h2c->st0 = H2_CS_ERROR2;
2549 sess_log(h2c->conn->owner);
2550 }
2551 goto fail;
2552 }
2553 h2c->st0 = H2_CS_SETTINGS1;
2554 }
2555 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002556 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002557 return 1;
2558 }
2559
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002560 /* start by sending possibly pending window updates */
2561 if (h2c->rcvd_c > 0 &&
2562 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2563 h2c_send_conn_wu(h2c) < 0)
2564 goto fail;
2565
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002566 /* First we always process the flow control list because the streams
2567 * waiting there were already elected for immediate emission but were
2568 * blocked just on this.
2569 */
2570
Olivier Houchardd360ac62019-03-22 17:37:16 +01002571 list_for_each_entry(h2s, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002572 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2573 h2c->st0 >= H2_CS_ERROR)
2574 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002575 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2576 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002577
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002578 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002579 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2580 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002581 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002582 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002583 }
2584
Olivier Houchardd360ac62019-03-22 17:37:16 +01002585 list_for_each_entry(h2s, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002586 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2587 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002588 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2589 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002590
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002591 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002592 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2593 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002594 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002595 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002596 }
2597
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002598 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002599 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002600 if (h2c->st0 == H2_CS_ERROR) {
2601 if (h2c->max_id >= 0) {
2602 h2c_send_goaway_error(h2c, NULL);
2603 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2604 return 0;
2605 }
2606
2607 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2608 }
2609 return 1;
2610 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002611 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002612}
2613
Willy Tarreau62f52692017-10-08 23:01:42 +02002614
Willy Tarreau479998a2018-11-18 06:30:59 +01002615/* Attempt to read data, and subscribe if none available.
2616 * The function returns 1 if data has been received, otherwise zero.
2617 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002618static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002619{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002620 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002621 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002622 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002623 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002624
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002625 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002626 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002627
Willy Tarreau315d8072017-12-10 22:17:57 +01002628 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002629 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002630
Willy Tarreau44e973f2018-03-01 17:49:30 +01002631 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002632 if (!buf) {
2633 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002634 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002635 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002636
Olivier Houchard7505f942018-08-21 18:10:44 +02002637 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002638 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002639 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2640 /* HTX in use : try to pre-align the buffer like the
2641 * rxbufs will be to optimize memory copies. We'll make
2642 * sure that the frame header lands at the end of the
2643 * HTX block to alias it upon recv. We cannot use the
2644 * head because rcv_buf() will realign the buffer if
2645 * it's empty. Thus we cheat and pretend we already
2646 * have a few bytes there.
2647 */
2648 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002649 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002650 }
2651 else
2652 max = b_room(buf);
2653
Olivier Houchard7505f942018-08-21 18:10:44 +02002654 if (max)
2655 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2656 else
2657 ret = 0;
2658 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002659
Olivier Houchard53216e72018-10-10 15:46:36 +02002660 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002661 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002662
Olivier Houcharda1411e62018-08-17 18:42:48 +02002663 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002664 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002665 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002666 }
2667
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002668 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002669 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002670 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002671}
2672
Willy Tarreau479998a2018-11-18 06:30:59 +01002673/* Try to send data if possible.
2674 * The function returns 1 if data have been sent, otherwise zero.
2675 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002676static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002677{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002678 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002679 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002680 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002681
2682 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002683 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002684
Olivier Houchard7505f942018-08-21 18:10:44 +02002685
Willy Tarreaua2af5122017-10-09 11:56:46 +02002686 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2687 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002688 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002689 }
2690
Willy Tarreaubc933932017-10-09 16:21:43 +02002691 /* This loop is quite simple : it tries to fill as much as it can from
2692 * pending streams into the existing buffer until it's reportedly full
2693 * or the end of send requests is reached. Then it tries to send this
2694 * buffer's contents out, marks it not full if at least one byte could
2695 * be sent, and tries again.
2696 *
2697 * The snd_buf() function normally takes a "flags" argument which may
2698 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2699 * data immediately comes and CO_SFL_STREAMER to indicate that the
2700 * connection is streaming lots of data (used to increase TLS record
2701 * size at the expense of latency). The former can be sent any time
2702 * there's a buffer full flag, as it indicates at least one stream
2703 * attempted to send and failed so there are pending data. An
2704 * alternative would be to set it as long as there's an active stream
2705 * but that would be problematic for ACKs until we have an absolute
2706 * guarantee that all waiters have at least one byte to send. The
2707 * latter should possibly not be set for now.
2708 */
2709
2710 done = 0;
2711 while (!done) {
2712 unsigned int flags = 0;
2713
2714 /* fill as much as we can into the current buffer */
2715 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2716 done = h2_process_mux(h2c);
2717
Olivier Houchard2b094432019-01-29 18:28:36 +01002718 if (h2c->flags & H2_CF_MUX_MALLOC)
2719 break;
2720
Willy Tarreaubc933932017-10-09 16:21:43 +02002721 if (conn->flags & CO_FL_ERROR)
2722 break;
2723
2724 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2725 flags |= CO_SFL_MSG_MORE;
2726
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002727 if (b_data(&h2c->mbuf)) {
2728 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002729 if (!ret)
2730 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002731 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002732 b_del(&h2c->mbuf, ret);
2733 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002734 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002735
2736 /* wrote at least one byte, the buffer is not full anymore */
2737 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2738 }
2739
Willy Tarreaua2af5122017-10-09 11:56:46 +02002740 if (conn->flags & CO_FL_SOCK_WR_SH) {
2741 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002742 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002743 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002744 /* We're not full anymore, so we can wake any task that are waiting
2745 * for us.
2746 */
2747 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002748 struct h2s *h2s;
2749
2750 list_for_each_entry(h2s, &h2c->send_list, list) {
2751 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2752 break;
2753 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2754 continue;
2755
2756 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002757 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2758 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002759 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002760 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002761 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002762 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002763 /* We're done, no more to send */
2764 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002765 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002766schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002767 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2768 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002769 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002770}
2771
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002772/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002773static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2774{
2775 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002776 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002777
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002778 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002779 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002780 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002781 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002782 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002783 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002784 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002785}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002786
Willy Tarreau62f52692017-10-08 23:01:42 +02002787/* callback called on any event by the connection handler.
2788 * It applies changes and returns zero, or < 0 if it wants immediate
2789 * destruction of the connection (which normally doesn not happen in h2).
2790 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002791static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002792{
Olivier Houchard7505f942018-08-21 18:10:44 +02002793 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002794
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002795 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002796 h2_process_demux(h2c);
2797
2798 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002799 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002800
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002801 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002802 h2c->flags &= ~H2_CF_DEM_DFULL;
2803 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002804 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002805
Willy Tarreau0b37d652018-10-03 10:33:02 +02002806 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002807 /* frontend is stopping, reload likely in progress, let's try
2808 * to announce a graceful shutdown if not yet done. We don't
2809 * care if it fails, it will be tried again later.
2810 */
2811 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2812 if (h2c->last_sid < 0)
2813 h2c->last_sid = (1U << 31) - 1;
2814 h2c_send_goaway_error(h2c, NULL);
2815 }
2816 }
2817
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002818 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002819 * If we received early data, and the handshake is done, wake
2820 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002821 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002822 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2823 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2824 struct eb32_node *node;
2825 struct h2s *h2s;
2826
2827 h2c->flags |= H2_CF_WAIT_FOR_HS;
2828 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2829
2830 while (node) {
2831 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002832 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002833 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002834 node = eb32_next(node);
2835 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002836 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002837
Willy Tarreau26bd7612017-10-09 16:47:04 +02002838 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002839 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2840 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2841 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002842 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002843
2844 if (eb_is_empty(&h2c->streams_by_id)) {
2845 /* no more stream, kill the connection now */
2846 h2_release(conn);
2847 return -1;
2848 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002849 }
2850
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002851 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002852 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002853
Olivier Houchard53216e72018-10-10 15:46:36 +02002854 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2855 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2856 (h2c->st0 != H2_CS_ERROR &&
2857 !b_data(&h2c->mbuf) &&
2858 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2859 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002860 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002861
Willy Tarreau3f133572017-10-31 19:21:06 +01002862 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002863 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002864 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002865 task_queue(h2c->task);
2866 }
2867 else
2868 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002869 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002870
Olivier Houchard7505f942018-08-21 18:10:44 +02002871 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002872 return 0;
2873}
2874
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002875/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002876static int h2_wake(struct connection *conn)
2877{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002878 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002879
2880 return (h2_process(h2c));
2881}
2882
Willy Tarreauea392822017-10-31 10:02:25 +01002883/* Connection timeout management. The principle is that if there's no receipt
2884 * nor sending for a certain amount of time, the connection is closed. If the
2885 * MUX buffer still has lying data or is not allocatable, the connection is
2886 * immediately killed. If it's allocatable and empty, we attempt to send a
2887 * GOAWAY frame.
2888 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002889static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002890{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002891 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002892 int expired = tick_is_expired(t->expire, now_ms);
2893
Willy Tarreau0975f112018-03-29 15:22:59 +02002894 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002895 return t;
2896
Willy Tarreau0975f112018-03-29 15:22:59 +02002897 task_delete(t);
2898 task_free(t);
2899
2900 if (!h2c) {
2901 /* resources were already deleted */
2902 return NULL;
2903 }
2904
2905 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002906 h2c_error(h2c, H2_ERR_NO_ERROR);
2907 h2_wake_some_streams(h2c, 0, 0);
2908
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002909 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002910 /* don't even try to send a GOAWAY, the buffer is stuck */
2911 h2c->flags |= H2_CF_GOAWAY_FAILED;
2912 }
2913
2914 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002915 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002916 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2917 h2c->flags |= H2_CF_GOAWAY_FAILED;
2918
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002919 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2920 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002921 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002922 b_del(&h2c->mbuf, ret);
2923 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002924 }
2925 }
Willy Tarreauea392822017-10-31 10:02:25 +01002926
Willy Tarreau0975f112018-03-29 15:22:59 +02002927 /* either we can release everything now or it will be done later once
2928 * the last stream closes.
2929 */
2930 if (eb_is_empty(&h2c->streams_by_id))
2931 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002932
Willy Tarreauea392822017-10-31 10:02:25 +01002933 return NULL;
2934}
2935
2936
Willy Tarreau62f52692017-10-08 23:01:42 +02002937/*******************************************/
2938/* functions below are used by the streams */
2939/*******************************************/
2940
2941/*
2942 * Attach a new stream to a connection
2943 * (Used for outgoing connections)
2944 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002945static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002946{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002947 struct conn_stream *cs;
2948 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002949 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002950
2951 cs = cs_new(conn);
2952 if (!cs)
2953 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002954 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002955 if (!h2s) {
2956 cs_free(cs);
2957 return NULL;
2958 }
2959 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002960}
2961
Willy Tarreaufafd3982018-11-18 21:29:20 +01002962/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2963 * We have to scan because we may have some orphan streams. It might be
2964 * beneficial to scan backwards from the end to reduce the likeliness to find
2965 * orphans.
2966 */
2967static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2968{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002969 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002970 struct h2s *h2s;
2971 struct eb32_node *node;
2972
2973 node = eb32_first(&h2c->streams_by_id);
2974 while (node) {
2975 h2s = container_of(node, struct h2s, by_id);
2976 if (h2s->cs)
2977 return h2s->cs;
2978 node = eb32_next(node);
2979 }
2980 return NULL;
2981}
2982
Willy Tarreau62f52692017-10-08 23:01:42 +02002983/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002984 * Destroy the mux and the associated connection, if it is no longer used
2985 */
2986static void h2_destroy(struct connection *conn)
2987{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002988 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002989
2990 if (eb_is_empty(&h2c->streams_by_id))
2991 h2_release(h2c->conn);
2992}
2993
2994/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002995 * Detach the stream from the connection and possibly release the connection.
2996 */
2997static void h2_detach(struct conn_stream *cs)
2998{
Willy Tarreau60935142017-10-16 18:11:19 +02002999 struct h2s *h2s = cs->ctx;
3000 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003001 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003002
3003 cs->ctx = NULL;
3004 if (!h2s)
3005 return;
3006
Olivier Houchardf502aca2018-12-14 19:42:40 +01003007 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003008 h2c = h2s->h2c;
3009 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003010 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003011 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3012 !h2_frt_has_too_many_cs(h2c)) {
3013 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003014 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003015 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003016 }
Willy Tarreau60935142017-10-16 18:11:19 +02003017
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003018 /* this stream may be blocked waiting for some data to leave (possibly
3019 * an ES or RST frame), so orphan it in this case.
3020 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003021 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003022 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003023 (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 +01003024 return;
3025
Willy Tarreau45f752e2017-10-30 15:44:59 +01003026 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3027 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3028 /* unblock the connection if it was blocked on this
3029 * stream.
3030 */
3031 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3032 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003033 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003034 }
3035
Willy Tarreau71049cc2018-03-28 13:56:39 +02003036 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003037
Olivier Houchard8a786902018-12-15 16:05:40 +01003038 if (h2c->flags & H2_CF_IS_BACK &&
3039 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003040 if (!(h2c->conn->flags &
3041 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3042 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003043 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003044 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3045 h2c->conn->owner = NULL;
3046 if (eb_is_empty(&h2c->streams_by_id)) {
3047 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3048 /* The server doesn't want it, let's kill the connection right away */
3049 h2c->conn->mux->destroy(h2c->conn);
3050 return;
3051 }
3052 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003053 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003054 if (eb_is_empty(&h2c->streams_by_id)) {
3055 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3056 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3057 return;
3058 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003059 /* Never ever allow to reuse a connection from a non-reuse backend */
3060 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3061 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003062 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003063 struct server *srv = objt_server(h2c->conn->target);
3064
3065 if (srv) {
3066 if (h2c->conn->flags & CO_FL_PRIVATE)
3067 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3068 else
3069 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3070 }
3071
3072 }
3073 }
3074 }
3075
Willy Tarreaue323f342018-03-28 13:51:45 +02003076 /* We don't want to close right now unless we're removing the
3077 * last stream, and either the connection is in error, or it
3078 * reached the ID already specified in a GOAWAY frame received
3079 * or sent (as seen by last_sid >= 0).
3080 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003081 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003082 /* no more stream will come, kill it now */
3083 h2_release(h2c->conn);
3084 }
3085 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003086 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003087 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3088 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003089 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003090 else
3091 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003092 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003093}
3094
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003095/* Performs a synchronous or asynchronous shutr().
3096 * FIXME: guess what the return code tries to indicate!
3097 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003098static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003099{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003100 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003101 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003102
Willy Tarreau721c9742017-11-07 11:05:42 +01003103 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003104 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003105
Willy Tarreau18059042019-01-31 19:12:48 +01003106 /* a connstream may require us to immediately kill the whole connection
3107 * for example because of a "tcp-request content reject" rule that is
3108 * normally used to limit abuse. In this case we schedule a goaway to
3109 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003110 */
Willy Tarreau18059042019-01-31 19:12:48 +01003111 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3112 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3113 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3114 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3115 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003116 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3117 /* Nothing was never sent for this stream, so reset with
3118 * REFUSED_STREAM error to let the client retry the
3119 * request.
3120 */
3121 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3122 }
Willy Tarreau18059042019-01-31 19:12:48 +01003123
Willy Tarreau90c32322017-11-24 08:00:30 +01003124 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003125 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003126 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003127
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003128 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003129 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003130 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003131
Olivier Houchard7a977432019-03-21 15:47:13 +01003132 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003133add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003134 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003135 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003136 if (h2s->flags & H2_SF_BLK_MFCTL) {
3137 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3138 h2s->send_wait = sw;
3139 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3140 h2s->send_wait = sw;
3141 LIST_ADDQ(&h2c->send_list, &h2s->list);
3142 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003143 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003144 /* Let the handler know we want shutr */
3145 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003146 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003147}
3148
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003149/* Performs a synchronous or asynchronous shutw().
3150 * FIXME: guess what the return code tries to indicate!
3151 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003152static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003153{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003154 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003155 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003156
Willy Tarreau721c9742017-11-07 11:05:42 +01003157 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003158 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003159
Willy Tarreau67434202017-11-06 20:20:51 +01003160 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003161 /* we can cleanly close using an empty data frame only after headers */
3162
3163 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3164 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003165 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003166
3167 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003168 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003169 else
3170 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003171 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003172 /* a connstream may require us to immediately kill the whole connection
3173 * for example because of a "tcp-request content reject" rule that is
3174 * normally used to limit abuse. In this case we schedule a goaway to
3175 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003176 */
Willy Tarreau18059042019-01-31 19:12:48 +01003177 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3178 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3179 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3180 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3181 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003182 else {
3183 /* Nothing was never sent for this stream, so reset with
3184 * REFUSED_STREAM error to let the client retry the
3185 * request.
3186 */
3187 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3188 }
Willy Tarreau18059042019-01-31 19:12:48 +01003189
Willy Tarreau90c32322017-11-24 08:00:30 +01003190 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003191 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003192 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003193
Willy Tarreau00dd0782018-03-01 16:31:34 +01003194 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003195 }
3196
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003197 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003198 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003199 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003200
3201 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003202 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003203 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003204 if (h2s->flags & H2_SF_BLK_MFCTL) {
3205 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3206 h2s->send_wait = sw;
3207 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3208 h2s->send_wait = sw;
3209 LIST_ADDQ(&h2c->send_list, &h2s->list);
3210 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003211 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003212 /* let the handler know we want to shutw */
3213 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003214 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003215}
3216
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003217/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3218 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3219 * and prevented the last frame from being emitted.
3220 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003221static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3222{
3223 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003224 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003225 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003226
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003227 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003228 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003229 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003230 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003231 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003232 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003233 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003234 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003235 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003236
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003237 if (!ret) {
3238 /* We're done trying to send, remove ourself from the send_list */
3239 h2s->send_wait->events &= ~SUB_RETRY_SEND;
3240 h2s->send_wait = NULL;
3241 LIST_DEL_INIT(&h2s->list);
3242 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003243 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003244 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003245 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003246 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003247
3248 if (h2c_is_dead(h2c))
3249 h2_release(h2c->conn);
3250 }
3251
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003252 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003253}
3254
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003255/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003256static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3257{
3258 struct h2s *h2s = cs->ctx;
3259
3260 if (!mode)
3261 return;
3262
3263 h2_do_shutr(h2s);
3264}
3265
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003266/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003267static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3268{
3269 struct h2s *h2s = cs->ctx;
3270
3271 h2_do_shutw(h2s);
3272}
3273
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003274/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003275 * HTX request or response depending on the connection's side. Returns a
3276 * positive value on success, a negative value on failure, or 0 if it couldn't
3277 * proceed. May report connection errors in h2c->errcode if the frame is
3278 * non-decodable and the connection unrecoverable. In absence of connection
3279 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003280 *
3281 * The function may fold CONTINUATION frames into the initial HEADERS frame
3282 * by removing padding and next frame header, then moving the CONTINUATION
3283 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3284 * leaving a hole between the main frame and the beginning of the next one.
3285 * The possibly remaining incomplete or next frame at the end may be moved
3286 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3287 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3288 *
3289 * A buffer at the beginning of processing may look like this :
3290 *
3291 * ,---.---------.-----.--------------.--------------.------.---.
3292 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3293 * `---^---------^-----^--------------^--------------^------^---'
3294 * | | <-----> | |
3295 * area | dpl | wrap
3296 * |<--------------> |
3297 * | dfl |
3298 * |<-------------------------------------------------->|
3299 * head data
3300 *
3301 * Padding is automatically overwritten when folding, participating to the
3302 * hole size after dfl :
3303 *
3304 * ,---.------------------------.-----.--------------.------.---.
3305 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3306 * `---^------------------------^-----^--------------^------^---'
3307 * | | <-----> | |
3308 * area | hole | wrap
3309 * |<-----------------------> |
3310 * | dfl |
3311 * |<-------------------------------------------------->|
3312 * head data
3313 *
3314 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3315 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3316 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003317 *
3318 * The <flags> field must point to either the stream's flags or to a copy of it
3319 * so that the function can update the following flags :
3320 * - H2_SF_DATA_CLEN when content-length is seen
3321 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3322 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003323 *
3324 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3325 * decoding, in order to detect if we're dealing with a headers or a trailers
3326 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003327 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003328static 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 +02003329{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003330 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003331 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003332 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003333 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003334 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003335 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003336 int flen; // header frame len
3337 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003338 int ret = 0;
3339 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003340 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003341 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003342
Willy Tarreauea18f862018-12-22 20:19:26 +01003343next_frame:
3344 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3345 goto leave; // incomplete input frame
3346
3347 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3348 * this case, we'll try to paste it immediately after the initial
3349 * HEADERS frame payload and kill any possible padding. The initial
3350 * frame's length will be increased to represent the concatenation
3351 * of the two frames. The next frame is read from position <tlen>
3352 * and written at position <flen> (minus padding if some is present).
3353 */
3354 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3355 struct h2_fh hdr;
3356 int clen; // CONTINUATION frame's payload length
3357
3358 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3359 /* no more data, the buffer may be full, either due to
3360 * too large a frame or because of too large a hole that
3361 * we're going to compact at the end.
3362 */
3363 goto leave;
3364 }
3365
3366 if (hdr.ft != H2_FT_CONTINUATION) {
3367 /* RFC7540#6.10: frame of unexpected type */
3368 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3369 goto fail;
3370 }
3371
3372 if (hdr.sid != h2c->dsi) {
3373 /* RFC7540#6.10: frame of different stream */
3374 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3375 goto fail;
3376 }
3377
3378 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3379 /* RFC7540#4.2: invalid frame length */
3380 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3381 goto fail;
3382 }
3383
3384 /* detect when we must stop aggragating frames */
3385 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3386
3387 /* Take as much as we can of the CONTINUATION frame's payload */
3388 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3389 if (clen > hdr.len)
3390 clen = hdr.len;
3391
3392 /* Move the frame's payload over the padding, hole and frame
3393 * header. At least one of hole or dpl is null (see diagrams
3394 * above). The hole moves after the new aggragated frame.
3395 */
3396 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3397 h2c->dfl += clen - h2c->dpl;
3398 hole += h2c->dpl + 9;
3399 h2c->dpl = 0;
3400 goto next_frame;
3401 }
3402
3403 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003404
Willy Tarreau13278b42017-10-13 19:23:14 +02003405 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003406 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003407 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003408 copy = alloc_trash_chunk();
3409 if (!copy) {
3410 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3411 goto fail;
3412 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003413 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3414 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3415 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003416 }
3417
Willy Tarreau13278b42017-10-13 19:23:14 +02003418 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3419 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003420 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003421 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3422 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003423 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003424 }
3425
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003426 if (flen < 5) {
3427 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3428 goto fail;
3429 }
3430
Willy Tarreau13278b42017-10-13 19:23:14 +02003431 hdrs += 5; // stream dep = 4, weight = 1
3432 flen -= 5;
3433 }
3434
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003435 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003436 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003437 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003438 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003439
Willy Tarreau937f7602018-02-26 15:22:17 +01003440 /* we can't retry a failed decompression operation so we must be very
3441 * careful not to take any risks. In practice the output buffer is
3442 * always empty except maybe for trailers, in which case we simply have
3443 * to wait for the upper layer to finish consuming what is available.
3444 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003445
3446 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003447 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003448 if (!htx_is_empty(htx)) {
3449 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003450 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003451 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003452 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003453 if (b_data(rxbuf)) {
3454 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003455 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003456 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003457
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003458 rxbuf->head = 0;
3459 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003460 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003461
Willy Tarreau25919232019-01-03 14:48:18 +01003462 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003463 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3464 sizeof(list)/sizeof(list[0]), tmp);
3465 if (outlen < 0) {
3466 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3467 goto fail;
3468 }
3469
Willy Tarreau25919232019-01-03 14:48:18 +01003470 /* The PACK decompressor was updated, let's update the input buffer and
3471 * the parser's state to commit these changes and allow us to later
3472 * fail solely on the stream if needed.
3473 */
3474 b_del(&h2c->dbuf, h2c->dfl + hole);
3475 h2c->dfl = hole = 0;
3476 h2c->st0 = H2_CS_FRAME_H;
3477
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003478 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003479 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003480
Willy Tarreau88d138e2019-01-02 19:38:14 +01003481 if (*flags & H2_SF_HEADERS_RCVD)
3482 goto trailers;
3483
3484 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003485 if (htx) {
3486 /* HTX mode */
3487 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003488 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003489 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003490 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003491 } else {
3492 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003493 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003494 if (outlen > 0)
3495 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003496 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003497
3498 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003499 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003500 goto fail;
3501 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003502
Willy Tarreau174b06a2018-04-25 18:13:58 +02003503 if (msgf & H2_MSGF_BODY) {
3504 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003505 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003506 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003507 if (htx)
3508 htx->extra = *body_len;
3509 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003510 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003511 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003512 }
3513
Willy Tarreau88d138e2019-01-02 19:38:14 +01003514 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003515 /* indicate that a HEADERS frame was received for this stream, except
3516 * for 1xx responses. For 1xx responses, another HEADERS frame is
3517 * expected.
3518 */
3519 if (!(msgf & H2_MSGF_RSP_1XX))
3520 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003521
Christopher Faulet0b465482019-02-19 15:14:23 +01003522 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003523 /* Mark the end of message, either using EOM in HTX or with the
3524 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3525 * is not set during headers with END_STREAM.
3526 */
3527 if (htx) {
3528 if (!htx_add_endof(htx, HTX_BLK_EOM))
3529 goto fail;
3530 }
3531 else if (*flags & H2_SF_DATA_CHNK) {
3532 if (!b_putblk(rxbuf, "\r\n", 2))
3533 goto fail;
3534 }
3535 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003536
Willy Tarreau86277d42019-01-02 15:36:11 +01003537 /* success */
3538 ret = 1;
3539
Willy Tarreau68dd9852017-07-03 14:44:26 +02003540 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003541 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003542 * move the remaining data over it.
3543 */
3544 if (hole) {
3545 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3546 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3547 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3548 b_sub(&h2c->dbuf, hole);
3549 }
3550
3551 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3552 /* too large frames */
3553 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003554 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003555 }
3556
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003557 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003558 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003559 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003560 return ret;
3561
Willy Tarreau68dd9852017-07-03 14:44:26 +02003562 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003563 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003564 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003565
3566 trailers:
3567 /* This is the last HEADERS frame hence a trailer */
3568
3569 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3570 /* It's a trailer but it's missing ES flag */
3571 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3572 goto fail;
3573 }
3574
3575 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3576 * block, and when using chunks we must send the 0 CRLF marker. For
3577 * other modes, the trailers are silently dropped.
3578 */
3579 if (htx) {
3580 if (!htx_add_endof(htx, HTX_BLK_EOD))
3581 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003582 if (h2_make_htx_trailers(list, htx) <= 0)
3583 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003584 }
3585 else if (*flags & H2_SF_DATA_CHNK) {
3586 /* Legacy mode with chunked encoding : we must finalize the
3587 * data block message emit the trailing CRLF */
3588 if (!b_putblk(rxbuf, "0\r\n", 3))
3589 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003590
3591 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3592 if (outlen > 0)
3593 b_add(rxbuf, outlen);
3594 else
3595 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003596 }
3597
3598 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003599}
3600
Willy Tarreau454f9052017-10-26 19:40:35 +02003601/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3602 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3603 * in use, a new chunk is emitted for each frame. This is supposed to fit
3604 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3605 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3606 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003607 * parser state is automatically updated. Returns > 0 if it could completely
3608 * send the current frame, 0 if it couldn't complete, in which case
3609 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3610 * DATA frame can return 0 as a valid result). Stream errors are reported in
3611 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3612 * have checked the frame header and ensured that the frame was complete or the
3613 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003614 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003615static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003616{
3617 struct h2c *h2c = h2s->h2c;
3618 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003619 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003620 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003621 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003622 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003623
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003624 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003625
Olivier Houchard638b7992018-08-16 15:41:52 +02003626 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003627 if (!csbuf) {
3628 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003629 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003630 }
3631
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003632try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003633 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003634 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3635 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003636 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003637 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003638
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003639 if (flen > b_data(&h2c->dbuf)) {
3640 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003641 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003642 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003643 }
3644
Willy Tarreaua9b77962019-01-31 07:23:00 +01003645 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003646 block1 = htx_free_data_space(htx);
3647 if (!block1) {
3648 h2c->flags |= H2_CF_DEM_SFULL;
3649 goto fail;
3650 }
3651 if (flen > block1)
3652 flen = block1;
3653
3654 /* here, flen is the max we can copy into the output buffer */
3655 block1 = b_contig_data(&h2c->dbuf, 0);
3656 if (flen > block1)
3657 flen = block1;
3658
3659 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3660 h2c->flags |= H2_CF_DEM_SFULL;
3661 goto fail;
3662 }
3663
3664 b_del(&h2c->dbuf, flen);
3665 h2c->dfl -= flen;
3666 h2c->rcvd_c += flen;
3667 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003668
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003669 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003670 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003671 htx->extra = h2s->body_len;
3672 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003673 goto try_again;
3674 }
3675 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003676 /* it doesn't fit and the buffer is fragmented,
3677 * so let's defragment it and try again.
3678 */
3679 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003680 }
3681
Willy Tarreaueba10f22018-04-25 20:44:22 +02003682 /* chunked-encoding requires more room */
3683 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003684 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003685 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3686 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3687 (chklen < 1048576) ? 4 : 8;
3688 chklen += 4; // CRLF, CRLF
3689 }
3690
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003691 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003692 if (flen + chklen > b_room(csbuf)) {
3693 if (chklen >= b_room(csbuf)) {
3694 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003695 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003696 }
3697 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003698 }
3699
3700 if (h2s->flags & H2_SF_DATA_CHNK) {
3701 /* emit the chunk size */
3702 unsigned int chksz = flen;
3703 char str[10];
3704 char *beg;
3705
3706 beg = str + sizeof(str);
3707 *--beg = '\n';
3708 *--beg = '\r';
3709 do {
3710 *--beg = hextab[chksz & 0xF];
3711 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003712 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003713 }
3714
Willy Tarreau454f9052017-10-26 19:40:35 +02003715 /* Block1 is the length of the first block before the buffer wraps,
3716 * block2 is the optional second block to reach the end of the frame.
3717 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003718 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003719 if (block1 > flen)
3720 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003721 block2 = flen - block1;
3722
3723 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003724 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003725
3726 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003727 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003728
Willy Tarreaueba10f22018-04-25 20:44:22 +02003729 if (h2s->flags & H2_SF_DATA_CHNK) {
3730 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003731 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003732 }
3733
Willy Tarreau454f9052017-10-26 19:40:35 +02003734 /* now mark the input data as consumed (will be deleted from the buffer
3735 * by the caller when seeing FRAME_A after sending the window update).
3736 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003737 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003738 h2c->dfl -= flen;
3739 h2c->rcvd_c += flen;
3740 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3741
Willy Tarreau1915ca22019-01-24 11:49:37 +01003742 if (h2s->flags & H2_SF_DATA_CLEN)
3743 h2s->body_len -= flen;
3744
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003745 if (h2c->dfl > h2c->dpl) {
3746 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003747 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003748 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003749 }
3750
Willy Tarreau4a28da12018-01-04 14:41:00 +01003751 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003752 /* here we're done with the frame, all the payload (except padding) was
3753 * transferred.
3754 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003755
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003756 if (h2c->dff & H2_F_DATA_END_STREAM) {
3757 if (htx) {
3758 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3759 h2c->flags |= H2_CF_DEM_SFULL;
3760 goto fail;
3761 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003762 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003763 else if (h2s->flags & H2_SF_DATA_CHNK) {
3764 /* emit the trailing 0 CRLF CRLF */
3765 if (b_room(csbuf) < 5) {
3766 h2c->flags |= H2_CF_DEM_SFULL;
3767 goto fail;
3768 }
3769 chklen += 5;
3770 b_putblk(csbuf, "0\r\n\r\n", 5);
3771 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003772 }
3773
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003774 h2c->rcvd_c += h2c->dpl;
3775 h2c->rcvd_s += h2c->dpl;
3776 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003777 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003778 if (htx)
3779 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003780 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003781 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003782 if (htx)
3783 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003784 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003785}
3786
Willy Tarreau5dd17352018-06-14 13:33:30 +02003787/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3788 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3789 * number of bytes sent. The caller must check the stream's status to detect
3790 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003791 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003792static 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 +02003793{
3794 struct http_hdr list[MAX_HTTP_HDR];
3795 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003796 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003797 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003798 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003799 int es_now = 0;
3800 int ret = 0;
3801 int hdr;
3802
3803 if (h2c_mux_busy(h2c, h2s)) {
3804 h2s->flags |= H2_SF_BLK_MBUSY;
3805 return 0;
3806 }
3807
Willy Tarreau44e973f2018-03-01 17:49:30 +01003808 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003809 h2c->flags |= H2_CF_MUX_MALLOC;
3810 h2s->flags |= H2_SF_BLK_MROOM;
3811 return 0;
3812 }
3813
3814 /* First, try to parse the H1 response and index it into <list>.
3815 * NOTE! Since it comes from haproxy, we *know* that a response header
3816 * block does not wrap and we can safely read it this way without
3817 * having to realign the buffer.
3818 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003819 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003820 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003821 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003822 /* incomplete or invalid response, this is abnormal coming from
3823 * haproxy and may only result in a bad errorfile or bad Lua code
3824 * so that won't be fixed, raise an error now.
3825 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003826 * FIXME: we should instead add the ability to only return a
3827 * 502 bad gateway. But in theory this is not supposed to
3828 * happen.
3829 */
3830 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3831 ret = 0;
3832 goto end;
3833 }
3834
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003835 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003836
3837 /* certain statuses have no body or an empty one, regardless of
3838 * what the headers say.
3839 */
3840 if (sl.st.status >= 100 && sl.st.status < 200) {
3841 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3842 h1m->curr_len = h1m->body_len = 0;
3843 }
3844 else if (sl.st.status == 204 || sl.st.status == 304) {
3845 /* no contents, claim c-len is present and set to zero */
3846 h1m->flags &= ~H1_MF_CHNK;
3847 h1m->flags |= H1_MF_CLEN;
3848 h1m->curr_len = h1m->body_len = 0;
3849 }
3850
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003851 chunk_reset(&outbuf);
3852
3853 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003854 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003855 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003856 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003857
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003858 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003859 break;
3860 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003861 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003862 }
3863
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003864 if (outbuf.size < 9)
3865 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003866
3867 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003868 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3869 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3870 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003871
3872 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003873 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003874 /* this is an unparsable response */
3875 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3876 ret = 0;
3877 goto end;
3878 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003879
3880 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003881 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003882 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003883 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003884 }
3885
3886 /* encode all headers, stop at empty name */
3887 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003888 /* these ones do not exist in H2 and must be dropped. */
3889 if (isteq(list[hdr].n, ist("connection")) ||
3890 isteq(list[hdr].n, ist("proxy-connection")) ||
3891 isteq(list[hdr].n, ist("keep-alive")) ||
3892 isteq(list[hdr].n, ist("upgrade")) ||
3893 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003894 continue;
3895
3896 if (isteq(list[hdr].n, ist("")))
3897 break; // end
3898
3899 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3900 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003901 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003902 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003903 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003904 }
3905 }
3906
3907 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003908 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003909 es_now = 1;
3910
3911 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003912 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003913
3914 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003915 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003916
3917 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003918 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003919
3920 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003921 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003922 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003923
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003924 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003925 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003926 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003927
Willy Tarreau801250e2018-09-11 11:45:04 +02003928 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003929 h2s->flags |= H2_SF_ES_SENT;
3930 if (h2s->st == H2_SS_OPEN)
3931 h2s->st = H2_SS_HLOC;
3932 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003933 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003934 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003935 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003936 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003937 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003938 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003939 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003940 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003941 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003942
3943 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003944
3945 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003946 //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 +02003947 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003948 full:
3949 h1m_init_res(h1m);
3950 h1m->err_pos = -1; // don't care about errors on the response path
3951 h2c->flags |= H2_CF_MUX_MFULL;
3952 h2s->flags |= H2_SF_BLK_MROOM;
3953 ret = 0;
3954 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003955}
3956
Willy Tarreau5dd17352018-06-14 13:33:30 +02003957/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3958 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3959 * the number of bytes sent. The caller must check the stream's status to
3960 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003961 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003962static 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 +02003963{
3964 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003965 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003966 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003967 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003968 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003969 int es_now = 0;
3970 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003971 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003972 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003973
3974 if (h2c_mux_busy(h2c, h2s)) {
3975 h2s->flags |= H2_SF_BLK_MBUSY;
3976 goto end;
3977 }
3978
Willy Tarreau44e973f2018-03-01 17:49:30 +01003979 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003980 h2c->flags |= H2_CF_MUX_MALLOC;
3981 h2s->flags |= H2_SF_BLK_MROOM;
3982 goto end;
3983 }
3984
3985 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003986 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003987 goto end;
3988
3989 chunk_reset(&outbuf);
3990
3991 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003992 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003993 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003994 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003995
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003996 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003997 break;
3998 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003999 /* If there are pending data in the output buffer, and we have
4000 * less than 1/4 of the mbuf's size and everything fits, we'll
4001 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4002 * is full and wait, to save some slow realign calls.
4003 */
4004 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4005 h2c->flags |= H2_CF_MUX_MFULL;
4006 h2s->flags |= H2_SF_BLK_MROOM;
4007 goto end;
4008 }
4009
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004010 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004011 }
4012
4013 if (outbuf.size < 9) {
4014 h2c->flags |= H2_CF_MUX_MFULL;
4015 h2s->flags |= H2_SF_BLK_MROOM;
4016 goto end;
4017 }
4018
4019 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004020 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4021 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4022 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004023
4024 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4025 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004026 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004027 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004028 break;
4029 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004030 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004031 if ((long long)size > h1m->curr_len)
4032 size = h1m->curr_len;
4033 break;
4034 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004035 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004036 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004037 if (!ret)
4038 goto end;
4039
4040 if (ret < 0) {
4041 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004042 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004043 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4044 goto end;
4045 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004046 max -= ret;
4047 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004048 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004049 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004050 }
4051
Willy Tarreau801250e2018-09-11 11:45:04 +02004052 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004053 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004054 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004055 if (!ret)
4056 goto end;
4057
4058 if (ret < 0) {
4059 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004060 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004061 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4062 goto end;
4063 }
4064
4065 size = chunk;
4066 h1m->curr_len = chunk;
4067 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004068 max -= ret;
4069 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004070 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004071 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004072 if (!size)
4073 goto send_empty;
4074 }
4075
4076 /* in MSG_DATA state, continue below */
4077 size = h1m->curr_len;
4078 break;
4079 }
4080
4081 /* we have in <size> the exact number of bytes we need to copy from
4082 * the H1 buffer. We need to check this against the connection's and
4083 * the stream's send windows, and to ensure that this fits in the max
4084 * frame size and in the buffer's available space minus 9 bytes (for
4085 * the frame header). The connection's flow control is applied last so
4086 * that we can use a separate list of streams which are immediately
4087 * unblocked on window opening. Note: we don't implement padding.
4088 */
4089
Willy Tarreau5dd17352018-06-14 13:33:30 +02004090 if (size > max)
4091 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004092
4093 if (size > h2s->mws)
4094 size = h2s->mws;
4095
4096 if (size <= 0) {
4097 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004098 if (h2s->send_wait) {
4099 LIST_DEL(&h2s->list);
4100 LIST_INIT(&h2s->list);
4101 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004102 goto end;
4103 }
4104
4105 if (h2c->mfs && size > h2c->mfs)
4106 size = h2c->mfs;
4107
4108 if (size + 9 > outbuf.size) {
4109 /* we have an opportunity for enlarging the too small
4110 * available space, let's try.
4111 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004112 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004113 goto realign_again;
4114 size = outbuf.size - 9;
4115 }
4116
4117 if (size <= 0) {
4118 h2c->flags |= H2_CF_MUX_MFULL;
4119 h2s->flags |= H2_SF_BLK_MROOM;
4120 goto end;
4121 }
4122
4123 if (size > h2c->mws)
4124 size = h2c->mws;
4125
4126 if (size <= 0) {
4127 h2s->flags |= H2_SF_BLK_MFCTL;
4128 goto end;
4129 }
4130
4131 /* copy whatever we can */
4132 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004133 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004134 if (ret == 1)
4135 len2 = 0;
4136
4137 if (!ret || len1 + len2 < size) {
4138 /* FIXME: must normally never happen */
4139 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4140 goto end;
4141 }
4142
4143 /* limit len1/len2 to size */
4144 if (len1 + len2 > size) {
4145 int sub = len1 + len2 - size;
4146
4147 if (len2 > sub)
4148 len2 -= sub;
4149 else {
4150 sub -= len2;
4151 len2 = 0;
4152 len1 -= sub;
4153 }
4154 }
4155
4156 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004157 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004158 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004159 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004160
4161 send_empty:
4162 /* we may need to add END_STREAM */
4163 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4164 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004165 *
4166 * FIXME: what we do here is not correct because we send end_stream
4167 * before knowing if we'll have to send a HEADERS frame for the
4168 * trailers. More importantly we're not consuming the trailing CRLF
4169 * after the end of trailers, so it will be left to the caller to
4170 * eat it. The right way to do it would be to measure trailers here
4171 * and to send ES only if there are no trailers.
4172 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004173 */
4174 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004175 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004176 es_now = 1;
4177
4178 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004179 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004180
4181 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004182 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004183
4184 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004185 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004186
4187 /* consume incoming H1 response */
4188 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004189 max -= size;
4190 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004191 total += size;
4192 h1m->curr_len -= size;
4193 h2s->mws -= size;
4194 h2c->mws -= size;
4195
4196 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004197 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004198 goto new_frame;
4199 }
4200 }
4201
4202 if (es_now) {
4203 if (h2s->st == H2_SS_OPEN)
4204 h2s->st = H2_SS_HLOC;
4205 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004206 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004207
Willy Tarreau35a62702018-02-27 15:37:25 +01004208 if (!(h1m->flags & H1_MF_CHNK)) {
4209 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004210 total += max;
4211 ofs += max;
4212 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004213
Willy Tarreau801250e2018-09-11 11:45:04 +02004214 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004215 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004216
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004217 h2s->flags |= H2_SF_ES_SENT;
4218 }
4219
4220 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004221 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 +02004222 return total;
4223}
4224
Willy Tarreau115e83b2018-12-01 19:17:53 +01004225/* Try to send a HEADERS frame matching HTX response present in HTX message
4226 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4227 * must check the stream's status to detect any error which might have happened
4228 * subsequently to a successful send. The htx blocks are automatically removed
4229 * from the message. The htx message is assumed to be valid since produced from
4230 * the internal code, hence it contains a start line, an optional series of
4231 * header blocks and an end of header, otherwise an invalid frame could be
4232 * emitted and the resulting htx message could be left in an inconsistent state.
4233 */
4234static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4235{
4236 struct http_hdr list[MAX_HTTP_HDR];
4237 struct h2c *h2c = h2s->h2c;
4238 struct htx_blk *blk;
4239 struct htx_blk *blk_end;
4240 struct buffer outbuf;
4241 struct htx_sl *sl;
4242 enum htx_blk_type type;
4243 int es_now = 0;
4244 int ret = 0;
4245 int hdr;
4246 int idx;
4247
4248 if (h2c_mux_busy(h2c, h2s)) {
4249 h2s->flags |= H2_SF_BLK_MBUSY;
4250 return 0;
4251 }
4252
4253 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4254 h2c->flags |= H2_CF_MUX_MALLOC;
4255 h2s->flags |= H2_SF_BLK_MROOM;
4256 return 0;
4257 }
4258
4259 /* determine the first block which must not be deleted, blk_end may
4260 * be NULL if all blocks have to be deleted.
4261 */
4262 idx = htx_get_head(htx);
4263 blk_end = NULL;
4264 while (idx != -1) {
4265 type = htx_get_blk_type(htx_get_blk(htx, idx));
4266 idx = htx_get_next(htx, idx);
4267 if (type == HTX_BLK_EOH) {
4268 if (idx != -1)
4269 blk_end = htx_get_blk(htx, idx);
4270 break;
4271 }
4272 }
4273
4274 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004275 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004276 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004277 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004278 if (h2s->status < 100 || h2s->status > 999)
4279 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004280
4281 /* and the rest of the headers, that we dump starting at header 0 */
4282 hdr = 0;
4283
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004284 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004285 while ((idx = htx_get_next(htx, idx)) != -1) {
4286 blk = htx_get_blk(htx, idx);
4287 type = htx_get_blk_type(blk);
4288
4289 if (type == HTX_BLK_UNUSED)
4290 continue;
4291
4292 if (type != HTX_BLK_HDR)
4293 break;
4294
4295 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4296 goto fail;
4297
4298 list[hdr].n = htx_get_blk_name(htx, blk);
4299 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004300 hdr++;
4301 }
4302
4303 /* marker for end of headers */
4304 list[hdr].n = ist("");
4305
4306 if (h2s->status == 204 || h2s->status == 304) {
4307 /* no contents, claim c-len is present and set to zero */
4308 es_now = 1;
4309 }
4310
4311 chunk_reset(&outbuf);
4312
4313 while (1) {
4314 outbuf.area = b_tail(&h2c->mbuf);
4315 outbuf.size = b_contig_space(&h2c->mbuf);
4316 outbuf.data = 0;
4317
4318 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4319 break;
4320 realign_again:
4321 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4322 }
4323
4324 if (outbuf.size < 9)
4325 goto full;
4326
4327 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4328 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4329 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4330 outbuf.data = 9;
4331
4332 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004333 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004334 if (b_space_wraps(&h2c->mbuf))
4335 goto realign_again;
4336 goto full;
4337 }
4338
4339 /* encode all headers, stop at empty name */
4340 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4341 /* these ones do not exist in H2 and must be dropped. */
4342 if (isteq(list[hdr].n, ist("connection")) ||
4343 isteq(list[hdr].n, ist("proxy-connection")) ||
4344 isteq(list[hdr].n, ist("keep-alive")) ||
4345 isteq(list[hdr].n, ist("upgrade")) ||
4346 isteq(list[hdr].n, ist("transfer-encoding")))
4347 continue;
4348
4349 if (isteq(list[hdr].n, ist("")))
4350 break; // end
4351
4352 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4353 /* output full */
4354 if (b_space_wraps(&h2c->mbuf))
4355 goto realign_again;
4356 goto full;
4357 }
4358 }
4359
Christopher Faulet0b465482019-02-19 15:14:23 +01004360 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004361 * FIXME: we should also set it when we know for sure that the
4362 * content-length is zero as well as on 204/304
4363 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004364 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4365 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004366 es_now = 1;
4367
Willy Tarreau927b88b2019-03-04 08:03:25 +01004368 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004369 es_now = 1;
4370
4371 /* update the frame's size */
4372 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4373
4374 if (es_now)
4375 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4376
4377 /* commit the H2 response */
4378 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004379
4380 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4381 * 1xx responses, another HEADERS frame is expected.
4382 */
4383 if (h2s->status >= 200 || h2s->status == 101)
4384 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004385
Willy Tarreau115e83b2018-12-01 19:17:53 +01004386 if (es_now) {
4387 h2s->flags |= H2_SF_ES_SENT;
4388 if (h2s->st == H2_SS_OPEN)
4389 h2s->st = H2_SS_HLOC;
4390 else
4391 h2s_close(h2s);
4392 }
4393
4394 /* OK we could properly deliver the response */
4395
4396 /* remove all header blocks including the EOH and compute the
4397 * corresponding size.
4398 *
4399 * FIXME: We should remove everything when es_now is set.
4400 */
4401 ret = 0;
4402 idx = htx_get_head(htx);
4403 blk = htx_get_blk(htx, idx);
4404 while (blk != blk_end) {
4405 ret += htx_get_blksz(blk);
4406 blk = htx_remove_blk(htx, blk);
4407 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004408
4409 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4410 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004411 end:
4412 return ret;
4413 full:
4414 h2c->flags |= H2_CF_MUX_MFULL;
4415 h2s->flags |= H2_SF_BLK_MROOM;
4416 ret = 0;
4417 goto end;
4418 fail:
4419 /* unparsable HTX messages, too large ones to be produced in the local
4420 * list etc go here (unrecoverable errors).
4421 */
4422 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4423 ret = 0;
4424 goto end;
4425}
4426
Willy Tarreau80739692018-10-05 11:35:57 +02004427/* Try to send a HEADERS frame matching HTX request present in HTX message
4428 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4429 * must check the stream's status to detect any error which might have happened
4430 * subsequently to a successful send. The htx blocks are automatically removed
4431 * from the message. The htx message is assumed to be valid since produced from
4432 * the internal code, hence it contains a start line, an optional series of
4433 * header blocks and an end of header, otherwise an invalid frame could be
4434 * emitted and the resulting htx message could be left in an inconsistent state.
4435 */
4436static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4437{
4438 struct http_hdr list[MAX_HTTP_HDR];
4439 struct h2c *h2c = h2s->h2c;
4440 struct htx_blk *blk;
4441 struct htx_blk *blk_end;
4442 struct buffer outbuf;
4443 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004444 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004445 enum htx_blk_type type;
4446 int es_now = 0;
4447 int ret = 0;
4448 int hdr;
4449 int idx;
4450
4451 if (h2c_mux_busy(h2c, h2s)) {
4452 h2s->flags |= H2_SF_BLK_MBUSY;
4453 return 0;
4454 }
4455
4456 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4457 h2c->flags |= H2_CF_MUX_MALLOC;
4458 h2s->flags |= H2_SF_BLK_MROOM;
4459 return 0;
4460 }
4461
4462 /* determine the first block which must not be deleted, blk_end may
4463 * be NULL if all blocks have to be deleted.
4464 */
4465 idx = htx_get_head(htx);
4466 blk_end = NULL;
4467 while (idx != -1) {
4468 type = htx_get_blk_type(htx_get_blk(htx, idx));
4469 idx = htx_get_next(htx, idx);
4470 if (type == HTX_BLK_EOH) {
4471 if (idx != -1)
4472 blk_end = htx_get_blk(htx, idx);
4473 break;
4474 }
4475 }
4476
4477 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004478 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004479 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004480 meth = htx_sl_req_meth(sl);
4481 path = htx_sl_req_uri(sl);
4482
4483 /* and the rest of the headers, that we dump starting at header 0 */
4484 hdr = 0;
4485
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004486 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004487 while ((idx = htx_get_next(htx, idx)) != -1) {
4488 blk = htx_get_blk(htx, idx);
4489 type = htx_get_blk_type(blk);
4490
4491 if (type == HTX_BLK_UNUSED)
4492 continue;
4493
4494 if (type != HTX_BLK_HDR)
4495 break;
4496
4497 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4498 goto fail;
4499
4500 list[hdr].n = htx_get_blk_name(htx, blk);
4501 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004502 hdr++;
4503 }
4504
4505 /* marker for end of headers */
4506 list[hdr].n = ist("");
4507
4508 chunk_reset(&outbuf);
4509
4510 while (1) {
4511 outbuf.area = b_tail(&h2c->mbuf);
4512 outbuf.size = b_contig_space(&h2c->mbuf);
4513 outbuf.data = 0;
4514
4515 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4516 break;
4517 realign_again:
4518 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4519 }
4520
4521 if (outbuf.size < 9)
4522 goto full;
4523
4524 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4525 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4526 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4527 outbuf.data = 9;
4528
4529 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004530 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004531 if (b_space_wraps(&h2c->mbuf))
4532 goto realign_again;
4533 goto full;
4534 }
4535
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004536 /* RFC7540 #8.3: the CONNECT method must have :
4537 * - :authority set to the URI part (host:port)
4538 * - :method set to CONNECT
4539 * - :scheme and :path omitted
4540 */
4541 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4542 /* encode the scheme which is always "https" (or 0x86 for "http") */
4543 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4544 /* output full */
4545 if (b_space_wraps(&h2c->mbuf))
4546 goto realign_again;
4547 goto full;
4548 }
Willy Tarreau80739692018-10-05 11:35:57 +02004549
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004550 /* encode the path, which necessarily is the second one */
4551 if (!hpack_encode_path(&outbuf, path)) {
4552 /* output full */
4553 if (b_space_wraps(&h2c->mbuf))
4554 goto realign_again;
4555 goto full;
4556 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004557
4558 /* look for the Host header and place it in :authority */
4559 auth = ist2(NULL, 0);
4560 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4561 if (isteq(list[hdr].n, ist("")))
4562 break; // end
4563
4564 if (isteq(list[hdr].n, ist("host"))) {
4565 auth = list[hdr].v;
4566 break;
4567 }
4568 }
4569 }
4570 else {
4571 /* for CONNECT, :authority is taken from the path */
4572 auth = path;
4573 }
4574
4575 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4576 /* output full */
4577 if (b_space_wraps(&h2c->mbuf))
4578 goto realign_again;
4579 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004580 }
4581
4582 /* encode all headers, stop at empty name */
4583 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4584 /* these ones do not exist in H2 and must be dropped. */
4585 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004586 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004587 isteq(list[hdr].n, ist("proxy-connection")) ||
4588 isteq(list[hdr].n, ist("keep-alive")) ||
4589 isteq(list[hdr].n, ist("upgrade")) ||
4590 isteq(list[hdr].n, ist("transfer-encoding")))
4591 continue;
4592
4593 if (isteq(list[hdr].n, ist("")))
4594 break; // end
4595
4596 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4597 /* output full */
4598 if (b_space_wraps(&h2c->mbuf))
4599 goto realign_again;
4600 goto full;
4601 }
4602 }
4603
4604 /* we may need to add END_STREAM if we have no body :
4605 * - request already closed, or :
4606 * - no transfer-encoding, and :
4607 * - no content-length or content-length:0
4608 * Fixme: this doesn't take into account CONNECT requests.
4609 */
4610 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4611 es_now = 1;
4612
4613 if (sl->flags & HTX_SL_F_BODYLESS)
4614 es_now = 1;
4615
Willy Tarreau927b88b2019-03-04 08:03:25 +01004616 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004617 es_now = 1;
4618
4619 /* update the frame's size */
4620 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4621
4622 if (es_now)
4623 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4624
4625 /* commit the H2 response */
4626 b_add(&h2c->mbuf, outbuf.data);
4627 h2s->flags |= H2_SF_HEADERS_SENT;
4628 h2s->st = H2_SS_OPEN;
4629
Willy Tarreau80739692018-10-05 11:35:57 +02004630 if (es_now) {
4631 // trim any possibly pending data (eg: inconsistent content-length)
4632 h2s->flags |= H2_SF_ES_SENT;
4633 h2s->st = H2_SS_HLOC;
4634 }
4635
4636 /* remove all header blocks including the EOH and compute the
4637 * corresponding size.
4638 *
4639 * FIXME: We should remove everything when es_now is set.
4640 */
4641 ret = 0;
4642 idx = htx_get_head(htx);
4643 blk = htx_get_blk(htx, idx);
4644 while (blk != blk_end) {
4645 ret += htx_get_blksz(blk);
4646 blk = htx_remove_blk(htx, blk);
4647 }
4648
4649 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4650 htx_remove_blk(htx, blk_end);
4651
4652 end:
4653 return ret;
4654 full:
4655 h2c->flags |= H2_CF_MUX_MFULL;
4656 h2s->flags |= H2_SF_BLK_MROOM;
4657 ret = 0;
4658 goto end;
4659 fail:
4660 /* unparsable HTX messages, too large ones to be produced in the local
4661 * list etc go here (unrecoverable errors).
4662 */
4663 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4664 ret = 0;
4665 goto end;
4666}
4667
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004668/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004669 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4670 * caller must check the stream's status to detect any error which might have
4671 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004672 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4673 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004674static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004675{
4676 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004677 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004678 struct buffer outbuf;
4679 size_t total = 0;
4680 int es_now = 0;
4681 int bsize; /* htx block size */
4682 int fsize; /* h2 frame size */
4683 struct htx_blk *blk;
4684 enum htx_blk_type type;
4685 int idx;
4686
4687 if (h2c_mux_busy(h2c, h2s)) {
4688 h2s->flags |= H2_SF_BLK_MBUSY;
4689 goto end;
4690 }
4691
4692 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4693 h2c->flags |= H2_CF_MUX_MALLOC;
4694 h2s->flags |= H2_SF_BLK_MROOM;
4695 goto end;
4696 }
4697
Willy Tarreau98de12a2018-12-12 07:03:00 +01004698 htx = htx_from_buf(buf);
4699
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004700 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4701 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4702 * the caller to handle.
4703 */
4704
4705 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004706 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004707 goto end;
4708
4709 idx = htx_get_head(htx);
4710 blk = htx_get_blk(htx, idx);
4711 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4712 bsize = htx_get_blksz(blk);
4713 fsize = bsize;
4714
4715 if (type == HTX_BLK_EOD) {
4716 /* if we have an EOD, we're dealing with chunked data. We may
4717 * have a set of trailers after us that the caller will want to
4718 * deal with. Let's simply remove the EOD and return.
4719 */
4720 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004721 total++; // EOD counts as one byte
4722 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004723 goto end;
4724 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004725 else if (type == HTX_BLK_EOM) {
4726 if (h2s->flags & H2_SF_ES_SENT) {
4727 /* ES already sent */
4728 htx_remove_blk(htx, blk);
4729 total++; // EOM counts as one byte
4730 count--;
4731 goto end;
4732 }
4733 }
4734 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004735 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004736
4737 /* Perform some optimizations to reduce the number of buffer copies.
4738 * First, if the mux's buffer is empty and the htx area contains
4739 * exactly one data block of the same size as the requested count, and
4740 * this count fits within the frame size, the stream's window size, and
4741 * the connection's window size, then it's possible to simply swap the
4742 * caller's buffer with the mux's output buffer and adjust offsets and
4743 * length to match the entire DATA HTX block in the middle. In this
4744 * case we perform a true zero-copy operation from end-to-end. This is
4745 * the situation that happens all the time with large files. Second, if
4746 * this is not possible, but the mux's output buffer is empty, we still
4747 * have an opportunity to avoid the copy to the intermediary buffer, by
4748 * making the intermediary buffer's area point to the output buffer's
4749 * area. In this case we want to skip the HTX header to make sure that
4750 * copies remain aligned and that this operation remains possible all
4751 * the time. This goes for headers, data blocks and any data extracted
4752 * from the HTX blocks.
4753 */
4754 if (unlikely(fsize == count &&
4755 htx->used == 1 && type == HTX_BLK_DATA &&
4756 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4757 void *old_area = h2c->mbuf.area;
4758
4759 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004760 /* Too bad there are data left there. We're willing to memcpy/memmove
4761 * up to 1/4 of the buffer, which means that it's OK to copy a large
4762 * frame into a buffer containing few data if it needs to be realigned,
4763 * and that it's also OK to copy few data without realigning. Otherwise
4764 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004765 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004766 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4767 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4768 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004769 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004770
Willy Tarreau98de12a2018-12-12 07:03:00 +01004771 h2c->flags |= H2_CF_MUX_MFULL;
4772 h2s->flags |= H2_SF_BLK_MROOM;
4773 goto end;
4774 }
4775
4776 /* map an H2 frame to the HTX block so that we can put the
4777 * frame header there.
4778 */
4779 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004780 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004781 h2c->mbuf.data = fsize + 9;
4782 outbuf.area = b_head(&h2c->mbuf);
4783
4784 /* prepend an H2 DATA frame header just before the DATA block */
4785 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4786 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4787 h2_set_frame_size(outbuf.area, fsize);
4788
4789 /* update windows */
4790 h2s->mws -= fsize;
4791 h2c->mws -= fsize;
4792
4793 /* and exchange with our old area */
4794 buf->area = old_area;
4795 buf->data = buf->head = 0;
4796 total += fsize;
4797 goto end;
4798 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004799
Willy Tarreau98de12a2018-12-12 07:03:00 +01004800 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004801 /* for DATA and EOM we'll have to emit a frame, even if empty */
4802
4803 while (1) {
4804 outbuf.area = b_tail(&h2c->mbuf);
4805 outbuf.size = b_contig_space(&h2c->mbuf);
4806 outbuf.data = 0;
4807
4808 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4809 break;
4810 realign_again:
4811 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4812 }
4813
4814 if (outbuf.size < 9) {
4815 h2c->flags |= H2_CF_MUX_MFULL;
4816 h2s->flags |= H2_SF_BLK_MROOM;
4817 goto end;
4818 }
4819
4820 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4821 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4822 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4823 outbuf.data = 9;
4824
4825 /* we have in <fsize> the exact number of bytes we need to copy from
4826 * the HTX buffer. We need to check this against the connection's and
4827 * the stream's send windows, and to ensure that this fits in the max
4828 * frame size and in the buffer's available space minus 9 bytes (for
4829 * the frame header). The connection's flow control is applied last so
4830 * that we can use a separate list of streams which are immediately
4831 * unblocked on window opening. Note: we don't implement padding.
4832 */
4833
4834 /* EOM is presented with bsize==1 but would lead to the emission of an
4835 * empty frame, thus we force it to zero here.
4836 */
4837 if (type == HTX_BLK_EOM)
4838 bsize = fsize = 0;
4839
4840 if (!fsize)
4841 goto send_empty;
4842
4843 if (h2s->mws <= 0) {
4844 h2s->flags |= H2_SF_BLK_SFCTL;
4845 if (h2s->send_wait) {
4846 LIST_DEL(&h2s->list);
4847 LIST_INIT(&h2s->list);
4848 }
4849 goto end;
4850 }
4851
Willy Tarreauee573762018-12-04 15:25:57 +01004852 if (fsize > count)
4853 fsize = count;
4854
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004855 if (fsize > h2s->mws)
4856 fsize = h2s->mws; // >0
4857
4858 if (h2c->mfs && fsize > h2c->mfs)
4859 fsize = h2c->mfs; // >0
4860
4861 if (fsize + 9 > outbuf.size) {
4862 /* we have an opportunity for enlarging the too small
4863 * available space, let's try.
4864 * FIXME: is this really interesting to do? Maybe we'll
4865 * spend lots of time realigning instead of using two
4866 * frames.
4867 */
4868 if (b_space_wraps(&h2c->mbuf))
4869 goto realign_again;
4870 fsize = outbuf.size - 9;
4871
4872 if (fsize <= 0) {
4873 /* no need to send an empty frame here */
4874 h2c->flags |= H2_CF_MUX_MFULL;
4875 h2s->flags |= H2_SF_BLK_MROOM;
4876 goto end;
4877 }
4878 }
4879
4880 if (h2c->mws <= 0) {
4881 h2s->flags |= H2_SF_BLK_MFCTL;
4882 goto end;
4883 }
4884
4885 if (fsize > h2c->mws)
4886 fsize = h2c->mws;
4887
4888 /* now let's copy this this into the output buffer */
4889 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004890 h2s->mws -= fsize;
4891 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004892 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004893
4894 send_empty:
4895 /* update the frame's size */
4896 h2_set_frame_size(outbuf.area, fsize);
4897
4898 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4899 * meeting EOM. We should optimize this later.
4900 */
4901 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004902 total++; // EOM counts as one byte
4903 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004904 es_now = 1;
4905 }
4906
4907 if (es_now)
4908 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4909
4910 /* commit the H2 response */
4911 b_add(&h2c->mbuf, fsize + 9);
4912
4913 /* consume incoming HTX block, including EOM */
4914 total += fsize;
4915 if (fsize == bsize) {
4916 htx_remove_blk(htx, blk);
4917 if (fsize)
4918 goto new_frame;
4919 } else {
4920 /* we've truncated this block */
4921 htx_cut_data_blk(htx, blk, fsize);
4922 }
4923
4924 if (es_now) {
4925 if (h2s->st == H2_SS_OPEN)
4926 h2s->st = H2_SS_HLOC;
4927 else
4928 h2s_close(h2s);
4929
4930 h2s->flags |= H2_SF_ES_SENT;
4931 }
4932
4933 end:
4934 return total;
4935}
4936
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004937/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4938 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4939 * processed. The caller must check the stream's status to detect any error
4940 * which might have happened subsequently to a successful send. The htx blocks
4941 * are automatically removed from the message. The htx message is assumed to be
4942 * valid since produced from the internal code. Processing stops when meeting
4943 * the EOM, which is also removed. All trailers are processed at once and sent
4944 * as a single frame. The ES flag is always set.
4945 */
4946static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4947{
4948 struct http_hdr list[MAX_HTTP_HDR];
4949 struct h2c *h2c = h2s->h2c;
4950 struct htx_blk *blk;
4951 struct htx_blk *blk_end;
4952 struct buffer outbuf;
4953 struct h1m h1m;
4954 enum htx_blk_type type;
4955 uint32_t size;
4956 int ret = 0;
4957 int hdr;
4958 int idx;
4959 void *start;
4960
4961 if (h2c_mux_busy(h2c, h2s)) {
4962 h2s->flags |= H2_SF_BLK_MBUSY;
4963 goto end;
4964 }
4965
4966 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4967 h2c->flags |= H2_CF_MUX_MALLOC;
4968 h2s->flags |= H2_SF_BLK_MROOM;
4969 goto end;
4970 }
4971
4972 /* The principle is that we parse each and every trailers block using
4973 * the H1 headers parser, and append it to the list. We don't proceed
4974 * until EOM is met. blk_end will point to the EOM block.
4975 */
4976 hdr = 0;
4977 memset(list, 0, sizeof(list));
4978 blk_end = NULL;
4979
4980 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4981 blk = htx_get_blk(htx, idx);
4982 type = htx_get_blk_type(blk);
4983
4984 if (type == HTX_BLK_UNUSED)
4985 continue;
4986
4987 if (type != HTX_BLK_TLR) {
4988 if (type == HTX_BLK_EOM)
4989 blk_end = blk;
4990 break;
4991 }
4992
4993 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4994 goto fail;
4995
4996 size = htx_get_blksz(blk);
4997 start = htx_get_blk_ptr(htx, blk);
4998
4999 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5000 h1m.err_pos = 0;
5001 ret = h1_headers_to_hdr_list(start, start + size,
5002 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5003 &h1m, NULL);
5004 if (ret < 0)
5005 goto fail;
5006
5007 /* ret == 0 if an incomplete trailers block was found (missing
5008 * empty line), or > 0 if it was found. We have to continue on
5009 * incomplete messages because the trailers block might be
5010 * incomplete.
5011 */
5012
5013 /* search the new end */
5014 while (hdr <= sizeof(list)/sizeof(list[0])) {
5015 if (!list[hdr].n.len)
5016 break;
5017 hdr++;
5018 }
5019 }
5020
5021 if (!blk_end)
5022 goto end; // end not found yet
5023
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005024 chunk_reset(&outbuf);
5025
5026 while (1) {
5027 outbuf.area = b_tail(&h2c->mbuf);
5028 outbuf.size = b_contig_space(&h2c->mbuf);
5029 outbuf.data = 0;
5030
5031 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5032 break;
5033 realign_again:
5034 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5035 }
5036
5037 if (outbuf.size < 9)
5038 goto full;
5039
5040 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5041 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5042 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5043 outbuf.data = 9;
5044
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005045 /* encode all headers */
5046 for (idx = 0; idx < hdr; idx++) {
5047 /* these ones do not exist in H2 or must not appear in
5048 * trailers and must be dropped.
5049 */
5050 if (isteq(list[idx].n, ist("host")) ||
5051 isteq(list[idx].n, ist("content-length")) ||
5052 isteq(list[idx].n, ist("connection")) ||
5053 isteq(list[idx].n, ist("proxy-connection")) ||
5054 isteq(list[idx].n, ist("keep-alive")) ||
5055 isteq(list[idx].n, ist("upgrade")) ||
5056 isteq(list[idx].n, ist("te")) ||
5057 isteq(list[idx].n, ist("transfer-encoding")))
5058 continue;
5059
5060 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5061 /* output full */
5062 if (b_space_wraps(&h2c->mbuf))
5063 goto realign_again;
5064 goto full;
5065 }
5066 }
5067
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005068 if (!hdr) {
5069 /* here we have a problem, we've received an empty trailers
5070 * block followed by an EOM. Because of this we can't send a
5071 * HEADERS frame, so we have to cheat and instead send an empty
5072 * DATA frame conveying the ES flag.
5073 */
5074 outbuf.area[3] = H2_FT_DATA;
5075 outbuf.area[4] = H2_F_DATA_END_STREAM;
5076 }
5077
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005078 /* update the frame's size */
5079 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5080
5081 /* commit the H2 response */
5082 b_add(&h2c->mbuf, outbuf.data);
5083 h2s->flags |= H2_SF_ES_SENT;
5084
5085 if (h2s->st == H2_SS_OPEN)
5086 h2s->st = H2_SS_HLOC;
5087 else
5088 h2s_close(h2s);
5089
5090 /* OK we could properly deliver the response */
5091 done:
5092 /* remove all header blocks including EOM and compute the corresponding size. */
5093 ret = 0;
5094 idx = htx_get_head(htx);
5095 blk = htx_get_blk(htx, idx);
5096 while (blk != blk_end) {
5097 ret += htx_get_blksz(blk);
5098 blk = htx_remove_blk(htx, blk);
5099 }
5100 blk = htx_remove_blk(htx, blk);
5101 end:
5102 return ret;
5103 full:
5104 h2c->flags |= H2_CF_MUX_MFULL;
5105 h2s->flags |= H2_SF_BLK_MROOM;
5106 ret = 0;
5107 goto end;
5108 fail:
5109 /* unparsable HTX messages, too large ones to be produced in the local
5110 * list etc go here (unrecoverable errors).
5111 */
5112 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5113 ret = 0;
5114 goto end;
5115}
5116
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005117/* Called from the upper layer, to subscribe to events, such as being able to send.
5118 * The <param> argument here is supposed to be a pointer to a wait_event struct
5119 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5120 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5121 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5122 * returns 0 except for the error above.
5123 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005124static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5125{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005126 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005127 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005128 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005129
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005130 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005131 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005132 if (!(sw->events & SUB_RETRY_RECV)) {
5133 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005134 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005135 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005136 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005137 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005138 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005139 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005140 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005141 if (!(sw->events & SUB_RETRY_SEND)) {
5142 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005143 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005144 h2s->send_wait = sw;
5145 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5146 if (h2s->flags & H2_SF_BLK_MFCTL)
5147 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5148 else
5149 LIST_ADDQ(&h2c->send_list, &h2s->list);
5150 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005151 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005152 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005153 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005154 if (event_type != 0)
5155 return -1;
5156 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005157}
5158
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005159/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5160 * The <param> argument here is supposed to be a pointer to the same wait_event
5161 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5162 * It always returns zero.
5163 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005164static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5165{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005166 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005167 struct h2s *h2s = cs->ctx;
5168
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005169 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005170 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005171 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005172 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005173 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005174 }
5175 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005176 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005177 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005178 if (h2s->send_wait == sw) {
5179 LIST_DEL(&h2s->list);
5180 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005181 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005182 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005183 }
5184 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005185 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5186 sw = param;
5187 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005188 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Willy Tarreaue73256f2019-03-25 18:02:54 +01005189 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005190 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005191 LIST_DEL(&h2s->list);
5192 LIST_INIT(&h2s->list);
Olivier Houchard3ea35132019-03-25 14:10:42 +01005193 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005194 }
5195 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005196 return 0;
5197}
5198
5199
Olivier Houchard511efea2018-08-16 15:30:32 +02005200/* Called from the upper layer, to receive data */
5201static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5202{
Olivier Houchard638b7992018-08-16 15:41:52 +02005203 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005204 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005205 struct htx *h2s_htx = NULL;
5206 struct htx *buf_htx = NULL;
5207 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005208 size_t ret = 0;
5209
5210 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005211 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5212 /* in HTX mode we ignore the count argument */
5213 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005214 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005215 /* Here htx_to_buf() will set buffer data to 0 because
5216 * the HTX is empty.
5217 */
5218 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005219 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005220 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005221
5222 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005223 count = htx_free_data_space(buf_htx);
5224 if (flags & CO_RFL_KEEP_RSV) {
5225 if (count <= global.tune.maxrewrite)
5226 goto end;
5227 count -= global.tune.maxrewrite;
5228 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005229
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005230 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005231
5232 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5233 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5234
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005235 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005236 htx_to_buf(buf_htx, buf);
5237 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005238 ret = htx_ret.ret;
5239 }
5240 else {
5241 ret = b_xfer(buf, &h2s->rxbuf, count);
5242 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005243
Christopher Faulet37070b22019-02-14 15:12:14 +01005244 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005245 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005246 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005247 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005248 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005249 if (cs->flags & CS_FL_REOS)
5250 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005251 if (cs->flags & CS_FL_ERR_PENDING)
5252 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005253 if (b_size(&h2s->rxbuf)) {
5254 b_free(&h2s->rxbuf);
5255 offer_buffers(NULL, tasks_run_queue);
5256 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005257 }
5258
Willy Tarreau082f5592018-11-25 08:03:32 +01005259 if (ret && h2c->dsi == h2s->id) {
5260 /* demux is blocking on this stream's buffer */
5261 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005262 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005263 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005264
Olivier Houchard511efea2018-08-16 15:30:32 +02005265 return ret;
5266}
5267
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005268/* stops all senders of this connection for example when the mux buffer is full.
5269 * They are moved from the sending_list to either fctl_list or send_list.
5270 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005271static void h2_stop_senders(struct h2c *h2c)
5272{
5273 struct h2s *h2s, *h2s_back;
5274
Olivier Houchardd360ac62019-03-22 17:37:16 +01005275 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5276 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005277 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005278 h2s->send_wait->events |= SUB_RETRY_SEND;
5279 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005280 }
5281}
5282
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005283/* Called from the upper layer, to send data from buffer <buf> for no more than
5284 * <count> bytes. Returns the number of bytes effectively sent. Some status
5285 * flags may be updated on the conn_stream.
5286 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005287static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005288{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005289 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005290 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005291 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005292 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005293 struct htx *htx;
5294 struct htx_blk *blk;
5295 enum htx_blk_type btype;
5296 uint32_t bsize;
5297 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005298
Olivier Houchardd360ac62019-03-22 17:37:16 +01005299 /* If we were not just woken because we wanted to send but couldn't,
5300 * and there's somebody else that is waiting to send, do nothing,
5301 * we will subscribe later and be put at the end of the list
5302 */
5303 LIST_DEL_INIT(&h2s->sending_list);
5304 if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) &&
5305 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5306 return 0;
5307
Olivier Houchardd846c262018-10-19 17:24:29 +02005308 if (h2s->send_wait) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005309 /* We want to stay in the send_list, so prepare ourself to be
5310 * eventually recalled if needed, and only remove ourself from
5311 * the list if we managed to send anything.
5312 */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005313 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01005314 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005315 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005316 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5317 return 0;
5318
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005319 /* htx will be enough to decide if we're using HTX or legacy */
5320 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5321
Willy Tarreau0bad0432018-06-14 16:54:01 +02005322 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005323 h2s->flags |= H2_SF_OUTGOING_DATA;
5324
Willy Tarreau751f2d02018-10-05 09:35:00 +02005325 if (h2s->id == 0) {
5326 int32_t id = h2c_get_next_sid(h2s->h2c);
5327
5328 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005329 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005330 return 0;
5331 }
5332
5333 eb32_delete(&h2s->by_id);
5334 h2s->by_id.key = h2s->id = id;
5335 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005336 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005337 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5338 }
5339
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005340 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005341 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5342 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005343 idx = htx_get_head(htx);
5344 blk = htx_get_blk(htx, idx);
5345 btype = htx_get_blk_type(blk);
5346 bsize = htx_get_blksz(blk);
5347
5348 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005349 case HTX_BLK_REQ_SL:
5350 /* start-line before headers */
5351 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5352 if (ret > 0) {
5353 total += ret;
5354 count -= ret;
5355 if (ret < bsize)
5356 goto done;
5357 }
5358 break;
5359
Willy Tarreau115e83b2018-12-01 19:17:53 +01005360 case HTX_BLK_RES_SL:
5361 /* start-line before headers */
5362 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5363 if (ret > 0) {
5364 total += ret;
5365 count -= ret;
5366 if (ret < bsize)
5367 goto done;
5368 }
5369 break;
5370
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005371 case HTX_BLK_DATA:
5372 case HTX_BLK_EOD:
5373 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005374 /* all these cause the emission of a DATA frame (possibly empty).
5375 * This EOM necessarily is one before trailers, as the EOM following
5376 * trailers would have been consumed by the trailers parser.
5377 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005378 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005379 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005380 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005381 total += ret;
5382 count -= ret;
5383 if (ret < bsize)
5384 goto done;
5385 }
5386 break;
5387
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005388 case HTX_BLK_TLR:
5389 /* This is the first trailers block, all the subsequent ones AND
5390 * the EOM will be swallowed by the parser.
5391 */
5392 ret = h2s_htx_make_trailers(h2s, htx);
5393 if (ret > 0) {
5394 total += ret;
5395 count -= ret;
5396 if (ret < bsize)
5397 goto done;
5398 }
5399 break;
5400
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005401 default:
5402 htx_remove_blk(htx, blk);
5403 total += bsize;
5404 count -= bsize;
5405 break;
5406 }
5407 }
5408 goto done;
5409 }
5410
5411 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005412 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005413 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005414 if (h2s->h2c->flags & H2_CF_IS_BACK)
5415 ret = -1;
5416 else
5417 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005418 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005419 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005420 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005421 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005422 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005423 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005424 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005425
Willy Tarreau5dd17352018-06-14 13:33:30 +02005426 if (unlikely((int)ret <= 0)) {
5427 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005428 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5429 break;
5430 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005431 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005432 total += count;
5433 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005434 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005435 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005436 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005437 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005438 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005439 break;
5440 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005441
5442 total += ret;
5443 count -= ret;
5444
5445 if (h2s->st >= H2_SS_ERROR)
5446 break;
5447
5448 if (h2s->flags & H2_SF_BLK_ANY)
5449 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005450 }
5451
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005452 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005453 if (h2s->st >= H2_SS_ERROR) {
5454 /* trim any possibly pending data after we close (extra CR-LF,
5455 * unprocessed trailers, abnormal extra data, ...)
5456 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005457 total += count;
5458 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005459 }
5460
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005461 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005462 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005463 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005464 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005465 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005466 }
5467
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005468 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005469 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005470 } else {
5471 b_del(buf, total);
5472 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005473
5474 /* The mux is full, cancel the pending tasks */
5475 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5476 (h2s->flags & H2_SF_BLK_MBUSY))
5477 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005478
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005479 /* If we're running HTX, and we read the whole buffer, then pretend
5480 * we read exactly what the caller specified, as with HTX the caller
5481 * will always give the buffer size, instead of the amount of data
5482 * available.
5483 */
5484 if (htx && !b_data(buf))
5485 total = orig_count;
5486
Olivier Houchard7505f942018-08-21 18:10:44 +02005487 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005488 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005489 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005490
Olivier Houchard7505f942018-08-21 18:10:44 +02005491 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005492 /* If we're waiting for flow control, and we got a shutr on the
5493 * connection, we will never be unlocked, so add an error on
5494 * the conn_stream.
5495 */
5496 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5497 !b_data(&h2s->h2c->dbuf) &&
5498 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5499 if (cs->flags & CS_FL_EOS)
5500 cs->flags |= CS_FL_ERROR;
5501 else
5502 cs->flags |= CS_FL_ERR_PENDING;
5503 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01005504 if (total > 0 && h2s->send_wait) {
5505 /* Ok we managed to send something, leave the send_list */
5506 h2s->send_wait->events &= ~SUB_RETRY_SEND;
5507 h2s->send_wait = NULL;
5508 LIST_DEL_INIT(&h2s->list);
5509 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005510 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005511}
5512
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005513/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005514static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005515{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005516 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005517 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005518 struct eb32_node *node;
5519 int fctl_cnt = 0;
5520 int send_cnt = 0;
5521 int tree_cnt = 0;
5522 int orph_cnt = 0;
5523
5524 if (!h2c)
5525 return;
5526
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005527 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005528 fctl_cnt++;
5529
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005530 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005531 send_cnt++;
5532
Willy Tarreau3af37712018-12-18 14:34:41 +01005533 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005534 node = eb32_first(&h2c->streams_by_id);
5535 while (node) {
5536 h2s = container_of(node, struct h2s, by_id);
5537 tree_cnt++;
5538 if (!h2s->cs)
5539 orph_cnt++;
5540 node = eb32_next(node);
5541 }
5542
Willy Tarreau987c0632018-12-18 10:32:05 +01005543 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5544 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5545 " .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 +02005546 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5547 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005548 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005549 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5550 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5551 h2c->msi,
5552 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5553 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5554
5555 if (h2s) {
5556 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5557 h2s, h2s->id, h2s->flags,
5558 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5559 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5560 h2s->cs);
5561 if (h2s->cs)
5562 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5563 h2s->cs->flags, h2s->cs->data);
5564 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005565}
Willy Tarreau62f52692017-10-08 23:01:42 +02005566
5567/*******************************************************/
5568/* functions below are dedicated to the config parsers */
5569/*******************************************************/
5570
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005571/* config parser for global "tune.h2.header-table-size" */
5572static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5573 struct proxy *defpx, const char *file, int line,
5574 char **err)
5575{
5576 if (too_many_args(1, args, err, NULL))
5577 return -1;
5578
5579 h2_settings_header_table_size = atoi(args[1]);
5580 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5581 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5582 return -1;
5583 }
5584 return 0;
5585}
Willy Tarreau62f52692017-10-08 23:01:42 +02005586
Willy Tarreaue6baec02017-07-27 11:45:11 +02005587/* config parser for global "tune.h2.initial-window-size" */
5588static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5589 struct proxy *defpx, const char *file, int line,
5590 char **err)
5591{
5592 if (too_many_args(1, args, err, NULL))
5593 return -1;
5594
5595 h2_settings_initial_window_size = atoi(args[1]);
5596 if (h2_settings_initial_window_size < 0) {
5597 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5598 return -1;
5599 }
5600 return 0;
5601}
5602
Willy Tarreau5242ef82017-07-27 11:47:28 +02005603/* config parser for global "tune.h2.max-concurrent-streams" */
5604static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5605 struct proxy *defpx, const char *file, int line,
5606 char **err)
5607{
5608 if (too_many_args(1, args, err, NULL))
5609 return -1;
5610
5611 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005612 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005613 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5614 return -1;
5615 }
5616 return 0;
5617}
5618
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005619/* config parser for global "tune.h2.max-frame-size" */
5620static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5621 struct proxy *defpx, const char *file, int line,
5622 char **err)
5623{
5624 if (too_many_args(1, args, err, NULL))
5625 return -1;
5626
5627 h2_settings_max_frame_size = atoi(args[1]);
5628 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5629 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5630 return -1;
5631 }
5632 return 0;
5633}
5634
Willy Tarreau62f52692017-10-08 23:01:42 +02005635
5636/****************************************/
5637/* MUX initialization and instanciation */
5638/***************************************/
5639
5640/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005641static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005642 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005643 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005644 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005645 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005646 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005647 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005648 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005649 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005650 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005651 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005652 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005653 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005654 .shutr = h2_shutr,
5655 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005656 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005657 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005658 .name = "H2",
5659};
5660
Christopher Faulet32f61c02018-04-10 14:33:41 +02005661/* PROTO selection : this mux registers PROTO token "h2" */
5662static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005663 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005664
Willy Tarreau0108d902018-11-25 19:14:37 +01005665INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5666
Christopher Faulet9b579102019-04-11 22:52:25 +02005667
5668/* The mux operations */
5669static const struct mux_ops h2_htx_ops = {
5670 .init = h2_init,
5671 .wake = h2_wake,
5672 .snd_buf = h2_snd_buf,
5673 .rcv_buf = h2_rcv_buf,
5674 .subscribe = h2_subscribe,
5675 .unsubscribe = h2_unsubscribe,
5676 .attach = h2_attach,
5677 .get_first_cs = h2_get_first_cs,
5678 .detach = h2_detach,
5679 .destroy = h2_destroy,
5680 .avail_streams = h2_avail_streams,
5681 .used_streams = h2_used_streams,
5682 .shutr = h2_shutr,
5683 .shutw = h2_shutw,
5684 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005685 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005686 .name = "H2",
5687};
5688
Willy Tarreauf8957272018-10-03 10:25:20 +02005689static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005690 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005691
5692INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5693
Willy Tarreau62f52692017-10-08 23:01:42 +02005694/* config keyword parsers */
5695static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005696 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005697 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005698 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005699 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005700 { 0, NULL, NULL }
5701}};
5702
Willy Tarreau0108d902018-11-25 19:14:37 +01005703INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);