blob: 9894592e58a02b24776146bd610e45ff937863d4 [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 */
335static inline void h2c_restart_reading(const struct h2c *h2c)
336{
337 if (!h2_recv_allowed(h2c))
338 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100339 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100340 return;
341 tasklet_wakeup(h2c->wait_event.task);
342}
343
344
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100345/* returns true if the front connection has too many conn_streams attached */
346static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200347{
Willy Tarreaua8754662018-12-23 20:43:58 +0100348 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200349}
350
Willy Tarreau44e973f2018-03-01 17:49:30 +0100351/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
352 * flags are used to figure what buffer was requested. It returns 1 if the
353 * allocation succeeds, in which case the connection is woken up, or 0 if it's
354 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200355 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100356static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200357{
358 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100359 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200360
Willy Tarreau44e973f2018-03-01 17:49:30 +0100361 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200362 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100363 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200364 return 1;
365 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200366
Willy Tarreau44e973f2018-03-01 17:49:30 +0100367 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
368 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200369
370 if (h2c->flags & H2_CF_DEM_MROOM) {
371 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100372 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200373 }
Willy Tarreau14398122017-09-22 14:26:04 +0200374 return 1;
375 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100376
377 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
378 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200379 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100380 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100381 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100382 return 1;
383 }
384
Willy Tarreau14398122017-09-22 14:26:04 +0200385 return 0;
386}
387
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200388static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200389{
390 struct buffer *buf = NULL;
391
Willy Tarreau44e973f2018-03-01 17:49:30 +0100392 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
393 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
394 h2c->buf_wait.target = h2c;
395 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100396 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100397 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100398 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200399 __conn_xprt_stop_recv(h2c->conn);
400 }
401 return buf;
402}
403
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200404static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200405{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200406 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100407 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200408 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200409 }
410}
411
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100412/* returns the number of allocatable outgoing streams for the connection taking
413 * the last_sid and the reserved ones into account.
414 */
415static inline int h2_streams_left(const struct h2c *h2c)
416{
417 int ret;
418
419 /* consider the number of outgoing streams we're allowed to create before
420 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
421 * nb_reserved is the number of streams which don't yet have an ID.
422 */
423 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
424 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
425 if (ret < 0)
426 ret = 0;
427 return ret;
428}
429
Willy Tarreau00f18a32019-01-26 12:19:01 +0100430/* returns the number of streams in use on a connection to figure if it's
431 * idle or not. We check nb_cs and not nb_streams as the caller will want
432 * to know if it was the last one after a detach().
433 */
434static int h2_used_streams(struct connection *conn)
435{
436 struct h2c *h2c = conn->ctx;
437
438 return h2c->nb_cs;
439}
440
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100441/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100442static int h2_avail_streams(struct connection *conn)
443{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100444 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100445 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100446 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100447
Willy Tarreau6afec462019-01-28 06:40:19 +0100448 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
449 * streams on the connection.
450 */
451 if (h2c->last_sid >= 0)
452 return 0;
453
Willy Tarreau86949782019-01-31 10:42:05 +0100454 /* note: may be negative if a SETTINGS frame changes the limit */
455 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100456
457 /* we must also consider the limit imposed by stream IDs */
458 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100459 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100460 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100461 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
462 ret1 = MIN(ret1, ret2);
463 }
464 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100465}
466
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200467
Willy Tarreau62f52692017-10-08 23:01:42 +0200468/*****************************************************************/
469/* functions below are dedicated to the mux setup and management */
470/*****************************************************************/
471
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200472/* Initialize the mux once it's attached. For outgoing connections, the context
473 * is already initialized before installing the mux, so we detect incoming
474 * connections from the fact that the context is still NULL. Returns < 0 on
475 * error.
476 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100477static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200478{
479 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100480 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481
Willy Tarreaubafbe012017-11-24 17:34:44 +0100482 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200483 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200484 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200485
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100486 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200487 h2c->flags = H2_CF_IS_BACK;
488 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
489 if (tick_isset(prx->timeout.serverfin))
490 h2c->shut_timeout = prx->timeout.serverfin;
491 } else {
492 h2c->flags = H2_CF_NONE;
493 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
494 if (tick_isset(prx->timeout.clientfin))
495 h2c->shut_timeout = prx->timeout.clientfin;
496 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100497
Willy Tarreau0b37d652018-10-03 10:33:02 +0200498 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100499 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100500 if (tick_isset(h2c->timeout)) {
501 t = task_new(tid_bit);
502 if (!t)
503 goto fail;
504
505 h2c->task = t;
506 t->process = h2_timeout_task;
507 t->context = h2c;
508 t->expire = tick_add(now_ms, h2c->timeout);
509 }
Willy Tarreauea392822017-10-31 10:02:25 +0100510
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200511 h2c->wait_event.task = tasklet_new();
512 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200513 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200514 h2c->wait_event.task->process = h2_io_cb;
515 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100516 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200517
Willy Tarreau32218eb2017-09-22 08:07:25 +0200518 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
519 if (!h2c->ddht)
520 goto fail;
521
522 /* Initialise the context. */
523 h2c->st0 = H2_CS_PREFACE;
524 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100525 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200526 h2c->max_id = -1;
527 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100528 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100530 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200531 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100532 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100533 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200534
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200535 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200536 h2c->dsi = -1;
537 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100538
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539 h2c->last_sid = -1;
540
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200541 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 h2c->miw = 65535; /* mux initial window size */
543 h2c->mws = 65535; /* mux window size */
544 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200545 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200546 LIST_INIT(&h2c->send_list);
547 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200548 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100549 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200550
Willy Tarreau3f133572017-10-31 19:21:06 +0100551 if (t)
552 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100553
Willy Tarreau01b44822018-10-03 14:26:37 +0200554 if (h2c->flags & H2_CF_IS_BACK) {
555 /* FIXME: this is temporary, for outgoing connections we need
556 * to immediately allocate a stream until the code is modified
557 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100558 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200559 */
560 struct h2s *h2s;
561
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100562 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200563 if (!h2s)
564 goto fail_stream;
565 }
566
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100567 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200568
Willy Tarreau0f383582018-10-03 14:22:21 +0200569 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100570 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200571 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200572 fail_stream:
573 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200574 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100575 if (t)
576 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200577 if (h2c->wait_event.task)
578 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100579 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200580 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200581 return -1;
582}
583
Willy Tarreau751f2d02018-10-05 09:35:00 +0200584/* returns the next allocatable outgoing stream ID for the H2 connection, or
585 * -1 if no more is allocatable.
586 */
587static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
588{
589 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100590
591 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200592 id = -1;
593 return id;
594}
595
Willy Tarreau2373acc2017-10-12 17:35:14 +0200596/* returns the stream associated with id <id> or NULL if not found */
597static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
598{
599 struct eb32_node *node;
600
Willy Tarreau751f2d02018-10-05 09:35:00 +0200601 if (id == 0)
602 return (struct h2s *)h2_closed_stream;
603
Willy Tarreau2a856182017-05-16 15:20:39 +0200604 if (id > h2c->max_id)
605 return (struct h2s *)h2_idle_stream;
606
Willy Tarreau2373acc2017-10-12 17:35:14 +0200607 node = eb32_lookup(&h2c->streams_by_id, id);
608 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200609 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200610
611 return container_of(node, struct h2s, by_id);
612}
613
Willy Tarreau62f52692017-10-08 23:01:42 +0200614/* release function for a connection. This one should be called to free all
615 * resources allocated to the mux.
616 */
617static void h2_release(struct connection *conn)
618{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100619 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200620
Willy Tarreau32218eb2017-09-22 08:07:25 +0200621 if (h2c) {
622 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200623
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100624 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100625 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100626 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200627
Willy Tarreau44e973f2018-03-01 17:49:30 +0100628 h2_release_buf(h2c, &h2c->dbuf);
629 h2_release_buf(h2c, &h2c->mbuf);
630
Willy Tarreauea392822017-10-31 10:02:25 +0100631 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200632 h2c->task->context = NULL;
633 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100634 h2c->task = NULL;
635 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200636 if (h2c->wait_event.task)
637 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100638 if (h2c->wait_event.events != 0)
639 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200640 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100641
Willy Tarreaubafbe012017-11-24 17:34:44 +0100642 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200643 }
644
645 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100646 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200647
648 conn_stop_tracking(conn);
649 conn_full_close(conn);
650 if (conn->destroy_cb)
651 conn->destroy_cb(conn);
652 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200653}
654
655
Willy Tarreau71681172017-10-23 14:39:06 +0200656/******************************************************/
657/* functions below are for the H2 protocol processing */
658/******************************************************/
659
660/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100661static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200662{
663 return h2s ? h2s->id : 0;
664}
665
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200666/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100667static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200668{
669 if (h2c->msi < 0)
670 return 0;
671
672 if (h2c->msi == h2s_id(h2s))
673 return 0;
674
675 return 1;
676}
677
Willy Tarreau741d6df2017-10-17 08:00:59 +0200678/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100679static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200680{
681 h2c->errcode = err;
682 h2c->st0 = H2_CS_ERROR;
683}
684
Willy Tarreau175cebb2019-01-24 10:02:24 +0100685/* marks an error on the stream. It may also update an already closed stream
686 * (e.g. to report an error after an RST was received).
687 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100688static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200689{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100690 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200691 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100692 if (h2s->st < H2_SS_ERROR)
693 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100694 if (h2s->cs)
695 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200696 }
697}
698
Willy Tarreau7e094452018-12-19 18:08:52 +0100699/* attempt to notify the data layer of recv availability */
700static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
701{
702 struct wait_event *sw;
703
704 if (h2s->recv_wait) {
705 sw = h2s->recv_wait;
706 sw->events &= ~SUB_RETRY_RECV;
707 tasklet_wakeup(sw->task);
708 h2s->recv_wait = NULL;
709 }
710}
711
712/* attempt to notify the data layer of send availability */
713static void __maybe_unused h2s_notify_send(struct h2s *h2s)
714{
715 struct wait_event *sw;
716
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100717 if (h2s->send_wait && !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100718 sw = h2s->send_wait;
719 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100720 sw->events |= SUB_CALL_UNSUBSCRIBE;
721 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100722 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100723 }
724}
725
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100726/* alerts the data layer, trying to wake it up by all means, following
727 * this sequence :
728 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
729 * - if its subscribed to send, then it's woken up for send
730 * - if it was subscribed to neither, its ->wake() callback is called
731 * It is safe to call this function with a closed stream which doesn't have a
732 * conn_stream anymore.
733 */
734static void __maybe_unused h2s_alert(struct h2s *h2s)
735{
736 if (h2s->recv_wait || h2s->send_wait) {
737 h2s_notify_recv(h2s);
738 h2s_notify_send(h2s);
739 }
740 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
741 h2s->cs->data_cb->wake(h2s->cs);
742}
743
Willy Tarreaue4820742017-07-27 13:37:23 +0200744/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100745static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200746{
747 uint8_t *out = frame;
748
749 *out = len >> 16;
750 write_n16(out + 1, len);
751}
752
Willy Tarreau54c15062017-10-10 17:10:03 +0200753/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
754 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
755 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200756 * available in the buffer's input prior to calling this function. The buffer
757 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200758 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100759static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200760 const struct buffer *b, int o)
761{
Willy Tarreau591d4452018-06-15 17:21:00 +0200762 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 +0200763}
764
Willy Tarreau1f094672017-11-20 21:27:45 +0100765static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200766{
Willy Tarreau591d4452018-06-15 17:21:00 +0200767 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200768}
769
Willy Tarreau1f094672017-11-20 21:27:45 +0100770static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200771{
Willy Tarreau591d4452018-06-15 17:21:00 +0200772 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200773}
774
Willy Tarreau1f094672017-11-20 21:27:45 +0100775static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200776{
Willy Tarreau591d4452018-06-15 17:21:00 +0200777 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200778}
779
780
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100781/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
782 * The algorithm is not obvious. It turns out that H2 headers are neither
783 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
784 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200785 *
786 * b0 b1 b2 b3 b4 b5..b8
787 * +----------+---------+--------+----+----+----------------------+
788 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
789 * +----------+---------+--------+----+----+----------------------+
790 *
791 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
792 * we get the sid properly aligned and ordered, and 16 bits of len properly
793 * ordered as well. The type and flags can be extracted using bit shifts from
794 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200795 * Returns zero if some bytes are missing, otherwise non-zero on success. The
796 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200797 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100798static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200799{
800 uint64_t w;
801
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100802 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200803 return 0;
804
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100805 w = h2_get_n64(b, o + 1);
806 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200807 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
808 h->ff = w >> 32;
809 h->ft = w >> 40;
810 h->len += w >> 48;
811 return 1;
812}
813
814/* skip the next 9 bytes corresponding to the frame header possibly parsed by
815 * h2_peek_frame_hdr() above.
816 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100817static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200818{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200819 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200820}
821
822/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100823static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200824{
825 int ret;
826
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100827 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200828 if (ret > 0)
829 h2_skip_frame_hdr(b);
830 return ret;
831}
832
Willy Tarreau00dd0782018-03-01 16:31:34 +0100833/* marks stream <h2s> as CLOSED and decrement the number of active streams for
834 * its connection if the stream was not yet closed. Please use this exclusively
835 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100836 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100837static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100838{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100839 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100840 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100841 if (!h2s->id)
842 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100843 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100844 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100845 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
846 h2s_notify_recv(h2s);
847 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100848 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100849 h2s->st = H2_SS_CLOSED;
850}
851
Willy Tarreau71049cc2018-03-28 13:56:39 +0200852/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
853static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100854{
855 h2s_close(h2s);
856 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200857 if (b_size(&h2s->rxbuf)) {
858 b_free(&h2s->rxbuf);
859 offer_buffers(NULL, tasks_run_queue);
860 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200861 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100862 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200863 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100864 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800865 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200866 * reference left would be in the h2c send_list/fctl_list, and if
867 * we're in it, we're getting out anyway
868 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100869 LIST_DEL_INIT(&h2s->list);
870 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200871 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100872 pool_free(pool_head_h2s, h2s);
873}
874
Willy Tarreaua8e49542018-10-03 18:53:55 +0200875/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
876 * stream tree. In case of error, nothing is added and NULL is returned. The
877 * causes of errors can be any failed memory allocation. The caller is
878 * responsible for checking if the connection may support an extra stream
879 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200880 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200881static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200882{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200883 struct h2s *h2s;
884
Willy Tarreaubafbe012017-11-24 17:34:44 +0100885 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200886 if (!h2s)
887 goto out;
888
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200889 h2s->wait_event.task = tasklet_new();
890 if (!h2s->wait_event.task) {
891 pool_free(pool_head_h2s, h2s);
892 goto out;
893 }
894 h2s->send_wait = NULL;
895 h2s->recv_wait = NULL;
896 h2s->wait_event.task->process = h2_deferred_shut;
897 h2s->wait_event.task->context = h2s;
898 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100899 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200900 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100901 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200902 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200903 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200904 h2s->mws = h2c->miw;
905 h2s->flags = H2_SF_NONE;
906 h2s->errcode = H2_ERR_NO_ERROR;
907 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200908 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100909 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200910 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200911
912 if (h2c->flags & H2_CF_IS_BACK) {
913 h1m_init_req(&h2s->h1m);
914 h2s->h1m.err_pos = -1; // don't care about errors on the request path
915 h2s->h1m.flags |= H1_MF_TOLOWER;
916 } else {
917 h1m_init_res(&h2s->h1m);
918 h2s->h1m.err_pos = -1; // don't care about errors on the response path
919 h2s->h1m.flags |= H1_MF_TOLOWER;
920 }
921
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200922 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200923 if (id > 0)
924 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100925 else
926 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200927
928 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100929 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100930 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200931
932 return h2s;
933
934 out_free_h2s:
935 pool_free(pool_head_h2s, h2s);
936 out:
937 return NULL;
938}
939
940/* creates a new stream <id> on the h2c connection and returns it, or NULL in
941 * case of memory allocation error.
942 */
943static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
944{
945 struct session *sess = h2c->conn->owner;
946 struct conn_stream *cs;
947 struct h2s *h2s;
948
949 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
950 goto out;
951
952 h2s = h2s_new(h2c, id);
953 if (!h2s)
954 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200955
956 cs = cs_new(h2c->conn);
957 if (!cs)
958 goto out_close;
959
Olivier Houchard746fb772018-12-15 19:42:00 +0100960 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200961 h2s->cs = cs;
962 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200963 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200964
965 if (stream_create_from_cs(cs) < 0)
966 goto out_free_cs;
967
Willy Tarreau590a0512018-09-05 11:56:48 +0200968 /* We want the accept date presented to the next stream to be the one
969 * we have now, the handshake time to be null (since the next stream
970 * is not delayed by a handshake), and the idle time to count since
971 * right now.
972 */
973 sess->accept_date = date;
974 sess->tv_accept = now;
975 sess->t_handshake = 0;
976
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200977 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100978 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200979 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200980 return h2s;
981
982 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200983 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200984 cs_free(cs);
985 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200986 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200987 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200988 sess_log(sess);
989 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200990}
991
Willy Tarreau751f2d02018-10-05 09:35:00 +0200992/* allocates a new stream associated to conn_stream <cs> on the h2c connection
993 * and returns it, or NULL in case of memory allocation error or if the highest
994 * possible stream ID was reached.
995 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100996static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200997{
998 struct h2s *h2s = NULL;
999
Willy Tarreau86949782019-01-31 10:42:05 +01001000 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001001 goto out;
1002
Willy Tarreaua80dca82019-01-24 17:08:28 +01001003 if (h2_streams_left(h2c) < 1)
1004 goto out;
1005
Willy Tarreau751f2d02018-10-05 09:35:00 +02001006 /* Defer choosing the ID until we send the first message to create the stream */
1007 h2s = h2s_new(h2c, 0);
1008 if (!h2s)
1009 goto out;
1010
1011 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001012 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001013 cs->ctx = h2s;
1014 h2c->nb_cs++;
1015
Willy Tarreau751f2d02018-10-05 09:35:00 +02001016 out:
1017 return h2s;
1018}
1019
Willy Tarreaube5b7152017-09-25 16:25:39 +02001020/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1021 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1022 * the various settings codes.
1023 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001024static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001025{
1026 struct buffer *res;
1027 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001028 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001029 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001030 int ret;
1031
1032 if (h2c_mux_busy(h2c, NULL)) {
1033 h2c->flags |= H2_CF_DEM_MBUSY;
1034 return 0;
1035 }
1036
Willy Tarreau44e973f2018-03-01 17:49:30 +01001037 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001038 if (!res) {
1039 h2c->flags |= H2_CF_MUX_MALLOC;
1040 h2c->flags |= H2_CF_DEM_MROOM;
1041 return 0;
1042 }
1043
1044 chunk_init(&buf, buf_data, sizeof(buf_data));
1045 chunk_memcpy(&buf,
1046 "\x00\x00\x00" /* length : 0 for now */
1047 "\x04\x00" /* type : 4 (settings), flags : 0 */
1048 "\x00\x00\x00\x00", /* stream ID : 0 */
1049 9);
1050
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001051 if (h2c->flags & H2_CF_IS_BACK) {
1052 /* send settings_enable_push=0 */
1053 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1054 }
1055
Willy Tarreaube5b7152017-09-25 16:25:39 +02001056 if (h2_settings_header_table_size != 4096) {
1057 char str[6] = "\x00\x01"; /* header_table_size */
1058
1059 write_n32(str + 2, h2_settings_header_table_size);
1060 chunk_memcat(&buf, str, 6);
1061 }
1062
1063 if (h2_settings_initial_window_size != 65535) {
1064 char str[6] = "\x00\x04"; /* initial_window_size */
1065
1066 write_n32(str + 2, h2_settings_initial_window_size);
1067 chunk_memcat(&buf, str, 6);
1068 }
1069
1070 if (h2_settings_max_concurrent_streams != 0) {
1071 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1072
1073 /* Note: 0 means "unlimited" for haproxy's config but not for
1074 * the protocol, so never send this value!
1075 */
1076 write_n32(str + 2, h2_settings_max_concurrent_streams);
1077 chunk_memcat(&buf, str, 6);
1078 }
1079
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001080 mfs = h2_settings_max_frame_size;
1081 if (mfs > global.tune.bufsize)
1082 mfs = global.tune.bufsize;
1083
1084 if (!mfs)
1085 mfs = global.tune.bufsize;
1086
1087 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001088 char str[6] = "\x00\x05"; /* max_frame_size */
1089
1090 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1091 * match bufsize - rewrite size, but at the moment it seems
1092 * that clients don't take care of it.
1093 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001094 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001095 chunk_memcat(&buf, str, 6);
1096 }
1097
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001098 h2_set_frame_size(buf.area, buf.data - 9);
1099 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001100 if (unlikely(ret <= 0)) {
1101 if (!ret) {
1102 h2c->flags |= H2_CF_MUX_MFULL;
1103 h2c->flags |= H2_CF_DEM_MROOM;
1104 return 0;
1105 }
1106 else {
1107 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1108 return 0;
1109 }
1110 }
1111 return ret;
1112}
1113
Willy Tarreau52eed752017-09-22 15:05:09 +02001114/* Try to receive a connection preface, then upon success try to send our
1115 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1116 * missing data. It may return an error in h2c.
1117 */
1118static int h2c_frt_recv_preface(struct h2c *h2c)
1119{
1120 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001121 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001122
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001123 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001124
1125 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001126 if (ret1 < 0)
1127 sess_log(h2c->conn->owner);
1128
Willy Tarreau52eed752017-09-22 15:05:09 +02001129 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1130 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1131 return 0;
1132 }
1133
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001134 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001135 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001136 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001137
Willy Tarreaube5b7152017-09-25 16:25:39 +02001138 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001139}
1140
Willy Tarreau01b44822018-10-03 14:26:37 +02001141/* Try to send a connection preface, then upon success try to send our
1142 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1143 * missing data. It may return an error in h2c.
1144 */
1145static int h2c_bck_send_preface(struct h2c *h2c)
1146{
1147 struct buffer *res;
1148
1149 if (h2c_mux_busy(h2c, NULL)) {
1150 h2c->flags |= H2_CF_DEM_MBUSY;
1151 return 0;
1152 }
1153
1154 res = h2_get_buf(h2c, &h2c->mbuf);
1155 if (!res) {
1156 h2c->flags |= H2_CF_MUX_MALLOC;
1157 h2c->flags |= H2_CF_DEM_MROOM;
1158 return 0;
1159 }
1160
1161 if (!b_data(res)) {
1162 /* preface not yet sent */
1163 b_istput(res, ist(H2_CONN_PREFACE));
1164 }
1165
1166 return h2c_send_settings(h2c);
1167}
1168
Willy Tarreau081d4722017-05-16 21:51:05 +02001169/* try to send a GOAWAY frame on the connection to report an error or a graceful
1170 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1171 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1172 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1173 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1174 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1175 * on unrecoverable failure. It will not attempt to send one again in this last
1176 * case so that it is safe to use h2c_error() to report such errors.
1177 */
1178static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1179{
1180 struct buffer *res;
1181 char str[17];
1182 int ret;
1183
1184 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1185 return 1; // claim that it worked
1186
1187 if (h2c_mux_busy(h2c, h2s)) {
1188 if (h2s)
1189 h2s->flags |= H2_SF_BLK_MBUSY;
1190 else
1191 h2c->flags |= H2_CF_DEM_MBUSY;
1192 return 0;
1193 }
1194
Willy Tarreau44e973f2018-03-01 17:49:30 +01001195 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001196 if (!res) {
1197 h2c->flags |= H2_CF_MUX_MALLOC;
1198 if (h2s)
1199 h2s->flags |= H2_SF_BLK_MROOM;
1200 else
1201 h2c->flags |= H2_CF_DEM_MROOM;
1202 return 0;
1203 }
1204
1205 /* len: 8, type: 7, flags: none, sid: 0 */
1206 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1207
1208 if (h2c->last_sid < 0)
1209 h2c->last_sid = h2c->max_id;
1210
1211 write_n32(str + 9, h2c->last_sid);
1212 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001213 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001214 if (unlikely(ret <= 0)) {
1215 if (!ret) {
1216 h2c->flags |= H2_CF_MUX_MFULL;
1217 if (h2s)
1218 h2s->flags |= H2_SF_BLK_MROOM;
1219 else
1220 h2c->flags |= H2_CF_DEM_MROOM;
1221 return 0;
1222 }
1223 else {
1224 /* we cannot report this error using GOAWAY, so we mark
1225 * it and claim a success.
1226 */
1227 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1228 h2c->flags |= H2_CF_GOAWAY_FAILED;
1229 return 1;
1230 }
1231 }
1232 h2c->flags |= H2_CF_GOAWAY_SENT;
1233 return ret;
1234}
1235
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001236/* Try to send an RST_STREAM frame on the connection for the indicated stream
1237 * during mux operations. This stream must be valid and cannot be closed
1238 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1239 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1240 * not yet.
1241 *
1242 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1243 * to write the message, it subscribes the stream to future notifications.
1244 */
1245static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1246{
1247 struct buffer *res;
1248 char str[13];
1249 int ret;
1250
1251 if (!h2s || h2s->st == H2_SS_CLOSED)
1252 return 1;
1253
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001254 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1255 * RST_STREAM in response to a RST_STREAM frame.
1256 */
1257 if (h2c->dft == H2_FT_RST_STREAM) {
1258 ret = 1;
1259 goto ignore;
1260 }
1261
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001262 if (h2c_mux_busy(h2c, h2s)) {
1263 h2s->flags |= H2_SF_BLK_MBUSY;
1264 return 0;
1265 }
1266
Willy Tarreau44e973f2018-03-01 17:49:30 +01001267 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001268 if (!res) {
1269 h2c->flags |= H2_CF_MUX_MALLOC;
1270 h2s->flags |= H2_SF_BLK_MROOM;
1271 return 0;
1272 }
1273
1274 /* len: 4, type: 3, flags: none */
1275 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1276 write_n32(str + 5, h2s->id);
1277 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001278 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001279
1280 if (unlikely(ret <= 0)) {
1281 if (!ret) {
1282 h2c->flags |= H2_CF_MUX_MFULL;
1283 h2s->flags |= H2_SF_BLK_MROOM;
1284 return 0;
1285 }
1286 else {
1287 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1288 return 0;
1289 }
1290 }
1291
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001292 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001293 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001294 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001295 return ret;
1296}
1297
1298/* Try to send an RST_STREAM frame on the connection for the stream being
1299 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001300 * error code, even if the stream is one of the dummy ones, and will update
1301 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001302 *
1303 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1304 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001305 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001306 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001307 */
1308static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1309{
1310 struct buffer *res;
1311 char str[13];
1312 int ret;
1313
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001314 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1315 * RST_STREAM in response to a RST_STREAM frame.
1316 */
1317 if (h2c->dft == H2_FT_RST_STREAM) {
1318 ret = 1;
1319 goto ignore;
1320 }
1321
Willy Tarreau27a84c92017-10-17 08:10:17 +02001322 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001323 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001324 return 0;
1325 }
1326
Willy Tarreau44e973f2018-03-01 17:49:30 +01001327 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001328 if (!res) {
1329 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001330 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001331 return 0;
1332 }
1333
1334 /* len: 4, type: 3, flags: none */
1335 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001336
Willy Tarreau27a84c92017-10-17 08:10:17 +02001337 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001338 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001339 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001340
Willy Tarreau27a84c92017-10-17 08:10:17 +02001341 if (unlikely(ret <= 0)) {
1342 if (!ret) {
1343 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001344 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001345 return 0;
1346 }
1347 else {
1348 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1349 return 0;
1350 }
1351 }
1352
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001353 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001354 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001355 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001356 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001357 }
1358
Willy Tarreau27a84c92017-10-17 08:10:17 +02001359 return ret;
1360}
1361
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001362/* try to send an empty DATA frame with the ES flag set to notify about the
1363 * end of stream and match a shutdown(write). If an ES was already sent as
1364 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1365 * on success or zero if nothing was done. In case of lack of room to write the
1366 * message, it subscribes the requesting stream to future notifications.
1367 */
1368static int h2_send_empty_data_es(struct h2s *h2s)
1369{
1370 struct h2c *h2c = h2s->h2c;
1371 struct buffer *res;
1372 char str[9];
1373 int ret;
1374
Willy Tarreau721c9742017-11-07 11:05:42 +01001375 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001376 return 1;
1377
1378 if (h2c_mux_busy(h2c, h2s)) {
1379 h2s->flags |= H2_SF_BLK_MBUSY;
1380 return 0;
1381 }
1382
Willy Tarreau44e973f2018-03-01 17:49:30 +01001383 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001384 if (!res) {
1385 h2c->flags |= H2_CF_MUX_MALLOC;
1386 h2s->flags |= H2_SF_BLK_MROOM;
1387 return 0;
1388 }
1389
1390 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1391 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1392 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001393 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001394 if (likely(ret > 0)) {
1395 h2s->flags |= H2_SF_ES_SENT;
1396 }
1397 else if (!ret) {
1398 h2c->flags |= H2_CF_MUX_MFULL;
1399 h2s->flags |= H2_SF_BLK_MROOM;
1400 return 0;
1401 }
1402 else {
1403 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1404 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001405 }
1406 return ret;
1407}
1408
Christopher Fauletf02ca002019-03-07 16:21:34 +01001409/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1410 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1411 * connection. The stream's state is automatically updated accordingly. If the
1412 * stream is orphaned, it is destroyed.
1413 */
1414static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1415{
1416 if (!h2s->cs) {
1417 /* this stream was already orphaned */
1418 h2s_destroy(h2s);
1419 return;
1420 }
1421
1422 h2s->cs->flags |= flags;
1423 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1424 h2s->cs->flags |= CS_FL_ERROR;
1425
1426 h2s_alert(h2s);
1427
1428 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1429 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001430 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001431 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001432 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001433 h2s_close(h2s);
1434}
1435
1436/* wake the streams attached to the connection, whose id is greater than <last>
1437 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001438 */
1439static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1440{
1441 struct eb32_node *node;
1442 struct h2s *h2s;
1443
1444 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001445 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001446
1447 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001448 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001449
Christopher Fauletf02ca002019-03-07 16:21:34 +01001450 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001451 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1452 while (node) {
1453 h2s = container_of(node, struct h2s, by_id);
1454 if (h2s->id <= last)
1455 break;
1456 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001457 h2s_wake_one_stream(h2s, flags);
1458 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001459
Christopher Fauletf02ca002019-03-07 16:21:34 +01001460 /* Wake all streams with unassigned ID (ID == 0) */
1461 node = eb32_lookup(&h2c->streams_by_id, 0);
1462 while (node) {
1463 h2s = container_of(node, struct h2s, by_id);
1464 if (h2s->id > 0)
1465 break;
1466 node = eb32_next(node);
1467 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001468 }
1469}
1470
Willy Tarreau3421aba2017-07-27 15:41:03 +02001471/* Increase all streams' outgoing window size by the difference passed in
1472 * argument. This is needed upon receipt of the settings frame if the initial
1473 * window size is different. The difference may be negative and the resulting
1474 * window size as well, for the time it takes to receive some window updates.
1475 */
1476static void h2c_update_all_ws(struct h2c *h2c, int diff)
1477{
1478 struct h2s *h2s;
1479 struct eb32_node *node;
1480
1481 if (!diff)
1482 return;
1483
1484 node = eb32_first(&h2c->streams_by_id);
1485 while (node) {
1486 h2s = container_of(node, struct h2s, by_id);
1487 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001488
1489 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1490 h2s->flags &= ~H2_SF_BLK_SFCTL;
1491 if (h2s->send_wait)
1492 LIST_ADDQ(&h2c->send_list, &h2s->list);
1493
1494 }
1495
Willy Tarreau3421aba2017-07-27 15:41:03 +02001496 node = eb32_next(node);
1497 }
1498}
1499
1500/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1501 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001502 * return an error in h2c. The caller must have already verified frame length
1503 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001504 */
1505static int h2c_handle_settings(struct h2c *h2c)
1506{
1507 unsigned int offset;
1508 int error;
1509
1510 if (h2c->dff & H2_F_SETTINGS_ACK) {
1511 if (h2c->dfl) {
1512 error = H2_ERR_FRAME_SIZE_ERROR;
1513 goto fail;
1514 }
1515 return 1;
1516 }
1517
Willy Tarreau3421aba2017-07-27 15:41:03 +02001518 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001519 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001520 return 0;
1521
1522 /* parse the frame */
1523 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001524 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1525 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001526
1527 switch (type) {
1528 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1529 /* we need to update all existing streams with the
1530 * difference from the previous iws.
1531 */
1532 if (arg < 0) { // RFC7540#6.5.2
1533 error = H2_ERR_FLOW_CONTROL_ERROR;
1534 goto fail;
1535 }
1536 h2c_update_all_ws(h2c, arg - h2c->miw);
1537 h2c->miw = arg;
1538 break;
1539 case H2_SETTINGS_MAX_FRAME_SIZE:
1540 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1541 error = H2_ERR_PROTOCOL_ERROR;
1542 goto fail;
1543 }
1544 h2c->mfs = arg;
1545 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001546 case H2_SETTINGS_ENABLE_PUSH:
1547 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1548 error = H2_ERR_PROTOCOL_ERROR;
1549 goto fail;
1550 }
1551 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001552 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1553 if (h2c->flags & H2_CF_IS_BACK) {
1554 /* the limit is only for the backend; for the frontend it is our limit */
1555 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1556 arg = h2_settings_max_concurrent_streams;
1557 h2c->streams_limit = arg;
1558 }
1559 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001560 }
1561 }
1562
1563 /* need to ACK this frame now */
1564 h2c->st0 = H2_CS_FRAME_A;
1565 return 1;
1566 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001567 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001568 h2c_error(h2c, error);
1569 return 0;
1570}
1571
1572/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1573 * success or one of the h2_status values.
1574 */
1575static int h2c_ack_settings(struct h2c *h2c)
1576{
1577 struct buffer *res;
1578 char str[9];
1579 int ret = -1;
1580
1581 if (h2c_mux_busy(h2c, NULL)) {
1582 h2c->flags |= H2_CF_DEM_MBUSY;
1583 return 0;
1584 }
1585
Willy Tarreau44e973f2018-03-01 17:49:30 +01001586 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001587 if (!res) {
1588 h2c->flags |= H2_CF_MUX_MALLOC;
1589 h2c->flags |= H2_CF_DEM_MROOM;
1590 return 0;
1591 }
1592
1593 memcpy(str,
1594 "\x00\x00\x00" /* length : 0 (no data) */
1595 "\x04" "\x01" /* type : 4, flags : ACK */
1596 "\x00\x00\x00\x00" /* stream ID */, 9);
1597
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001598 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001599 if (unlikely(ret <= 0)) {
1600 if (!ret) {
1601 h2c->flags |= H2_CF_MUX_MFULL;
1602 h2c->flags |= H2_CF_DEM_MROOM;
1603 return 0;
1604 }
1605 else {
1606 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1607 return 0;
1608 }
1609 }
1610 return ret;
1611}
1612
Willy Tarreaucf68c782017-10-10 17:11:41 +02001613/* processes a PING frame and schedules an ACK if needed. The caller must pass
1614 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001615 * missing data. The caller must have already verified frame length
1616 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001617 */
1618static int h2c_handle_ping(struct h2c *h2c)
1619{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001620 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001621 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001622 h2c->st0 = H2_CS_FRAME_A;
1623 return 1;
1624}
1625
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001626/* Try to send a window update for stream id <sid> and value <increment>.
1627 * Returns > 0 on success or zero on missing room or failure. It may return an
1628 * error in h2c.
1629 */
1630static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1631{
1632 struct buffer *res;
1633 char str[13];
1634 int ret = -1;
1635
1636 if (h2c_mux_busy(h2c, NULL)) {
1637 h2c->flags |= H2_CF_DEM_MBUSY;
1638 return 0;
1639 }
1640
Willy Tarreau44e973f2018-03-01 17:49:30 +01001641 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001642 if (!res) {
1643 h2c->flags |= H2_CF_MUX_MALLOC;
1644 h2c->flags |= H2_CF_DEM_MROOM;
1645 return 0;
1646 }
1647
1648 /* length: 4, type: 8, flags: none */
1649 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1650 write_n32(str + 5, sid);
1651 write_n32(str + 9, increment);
1652
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001653 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001654
1655 if (unlikely(ret <= 0)) {
1656 if (!ret) {
1657 h2c->flags |= H2_CF_MUX_MFULL;
1658 h2c->flags |= H2_CF_DEM_MROOM;
1659 return 0;
1660 }
1661 else {
1662 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1663 return 0;
1664 }
1665 }
1666 return ret;
1667}
1668
1669/* try to send pending window update for the connection. It's safe to call it
1670 * with no pending updates. Returns > 0 on success or zero on missing room or
1671 * failure. It may return an error in h2c.
1672 */
1673static int h2c_send_conn_wu(struct h2c *h2c)
1674{
1675 int ret = 1;
1676
1677 if (h2c->rcvd_c <= 0)
1678 return 1;
1679
Willy Tarreau97aaa672018-12-23 09:49:04 +01001680 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1681 /* increase the advertised connection window to 2G on
1682 * first update.
1683 */
1684 h2c->flags |= H2_CF_WINDOW_OPENED;
1685 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1686 }
1687
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001688 /* send WU for the connection */
1689 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1690 if (ret > 0)
1691 h2c->rcvd_c = 0;
1692
1693 return ret;
1694}
1695
1696/* try to send pending window update for the current dmux stream. It's safe to
1697 * call it with no pending updates. Returns > 0 on success or zero on missing
1698 * room or failure. It may return an error in h2c.
1699 */
1700static int h2c_send_strm_wu(struct h2c *h2c)
1701{
1702 int ret = 1;
1703
1704 if (h2c->rcvd_s <= 0)
1705 return 1;
1706
1707 /* send WU for the stream */
1708 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1709 if (ret > 0)
1710 h2c->rcvd_s = 0;
1711
1712 return ret;
1713}
1714
Willy Tarreaucf68c782017-10-10 17:11:41 +02001715/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1716 * success, 0 on missing data or one of the h2_status values.
1717 */
1718static int h2c_ack_ping(struct h2c *h2c)
1719{
1720 struct buffer *res;
1721 char str[17];
1722 int ret = -1;
1723
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001724 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001725 return 0;
1726
1727 if (h2c_mux_busy(h2c, NULL)) {
1728 h2c->flags |= H2_CF_DEM_MBUSY;
1729 return 0;
1730 }
1731
Willy Tarreau44e973f2018-03-01 17:49:30 +01001732 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001733 if (!res) {
1734 h2c->flags |= H2_CF_MUX_MALLOC;
1735 h2c->flags |= H2_CF_DEM_MROOM;
1736 return 0;
1737 }
1738
1739 memcpy(str,
1740 "\x00\x00\x08" /* length : 8 (same payload) */
1741 "\x06" "\x01" /* type : 6, flags : ACK */
1742 "\x00\x00\x00\x00" /* stream ID */, 9);
1743
1744 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001745 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001746
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001747 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001748 if (unlikely(ret <= 0)) {
1749 if (!ret) {
1750 h2c->flags |= H2_CF_MUX_MFULL;
1751 h2c->flags |= H2_CF_DEM_MROOM;
1752 return 0;
1753 }
1754 else {
1755 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1756 return 0;
1757 }
1758 }
1759 return ret;
1760}
1761
Willy Tarreau26f95952017-07-27 17:18:30 +02001762/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1763 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001764 * h2c or h2s. The caller must have already verified frame length and stream ID
1765 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001766 */
1767static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1768{
1769 int32_t inc;
1770 int error;
1771
Willy Tarreau26f95952017-07-27 17:18:30 +02001772 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001773 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001774 return 0;
1775
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001776 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001777
1778 if (h2c->dsi != 0) {
1779 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001780
1781 /* it's not an error to receive WU on a closed stream */
1782 if (h2s->st == H2_SS_CLOSED)
1783 return 1;
1784
1785 if (!inc) {
1786 error = H2_ERR_PROTOCOL_ERROR;
1787 goto strm_err;
1788 }
1789
1790 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1791 error = H2_ERR_FLOW_CONTROL_ERROR;
1792 goto strm_err;
1793 }
1794
1795 h2s->mws += inc;
1796 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1797 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001798 if (h2s->send_wait)
1799 LIST_ADDQ(&h2c->send_list, &h2s->list);
1800
Willy Tarreau26f95952017-07-27 17:18:30 +02001801 }
1802 }
1803 else {
1804 /* connection window update */
1805 if (!inc) {
1806 error = H2_ERR_PROTOCOL_ERROR;
1807 goto conn_err;
1808 }
1809
1810 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1811 error = H2_ERR_FLOW_CONTROL_ERROR;
1812 goto conn_err;
1813 }
1814
1815 h2c->mws += inc;
1816 }
1817
1818 return 1;
1819
1820 conn_err:
1821 h2c_error(h2c, error);
1822 return 0;
1823
1824 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001825 h2s_error(h2s, error);
1826 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001827 return 0;
1828}
1829
Willy Tarreaue96b0922017-10-30 00:28:29 +01001830/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001831 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1832 * have already verified frame length and stream ID validity. Described in
1833 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001834 */
1835static int h2c_handle_goaway(struct h2c *h2c)
1836{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001837 int last;
1838
Willy Tarreaue96b0922017-10-30 00:28:29 +01001839 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001840 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001841 return 0;
1842
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001843 last = h2_get_n32(&h2c->dbuf, 0);
1844 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001845 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001846 if (h2c->last_sid < 0)
1847 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001848 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001849}
1850
Willy Tarreau92153fc2017-12-03 19:46:19 +01001851/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001852 * invalid. Returns > 0 on success or zero on missing data. It may return an
1853 * error in h2c. The caller must have already verified frame length and stream
1854 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001855 */
1856static int h2c_handle_priority(struct h2c *h2c)
1857{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001858 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001859 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001860 return 0;
1861
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001862 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001863 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001864 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1865 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001866 }
1867 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001868}
1869
Willy Tarreaucd234e92017-08-18 10:59:39 +02001870/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001871 * Returns > 0 on success or zero on missing data. The caller must have already
1872 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001873 */
1874static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1875{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001876 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001877 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001878 return 0;
1879
1880 /* late RST, already handled */
1881 if (h2s->st == H2_SS_CLOSED)
1882 return 1;
1883
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001884 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001885 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001886
1887 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001888 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001889 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001890 }
1891
1892 h2s->flags |= H2_SF_RST_RCVD;
1893 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001894}
1895
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001896/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1897 * It may return an error in h2c or h2s. The caller must consider that the
1898 * return value is the new h2s in case one was allocated (most common case).
1899 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001900 * errors here are reported as connection errors since it's impossible to
1901 * recover from such errors after the compression context has been altered.
1902 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001903static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001904{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001905 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001906 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001907 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001908 int error;
1909
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001910 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001911 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001912
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001913 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001914 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001915
1916 /* now either the frame is complete or the buffer is complete */
1917 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001918 /* The stream exists/existed, this must be a trailers frame */
1919 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001920 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001921 goto out;
1922 goto done;
1923 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001924 /* the connection was already killed by an RST, let's consume
1925 * the data and send another RST.
1926 */
1927 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1928 h2s = (struct h2s*)h2_error_stream;
1929 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001930 }
1931 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1932 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1933 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001934 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001935 goto conn_err;
1936 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001937 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1938 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001939
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001940 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001941
Willy Tarreau25919232019-01-03 14:48:18 +01001942 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001943 if (h2c->st0 >= H2_CS_ERROR)
1944 goto out;
1945
Willy Tarreau25919232019-01-03 14:48:18 +01001946 if (error <= 0) {
1947 if (error == 0)
1948 goto out; // missing data
1949
1950 /* Failed to decode this stream (e.g. too large request)
1951 * but the HPACK decompressor is still synchronized.
1952 */
1953 h2s = (struct h2s*)h2_error_stream;
1954 goto send_rst;
1955 }
1956
Willy Tarreau22de8d32018-09-05 19:55:58 +02001957 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001958 * positively from h2c_frt_stream_new(), the stream will report the error,
1959 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001960 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001961 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001962 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001963 h2s = (struct h2s*)h2_refused_stream;
1964 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001965 }
1966
1967 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001968 h2s->rxbuf = rxbuf;
1969 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001970 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001971
Willy Tarreau88d138e2019-01-02 19:38:14 +01001972 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001973 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001974 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001975
1976 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001977 if (h2s->cs)
1978 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01001979 if (h2s->st == H2_SS_OPEN)
1980 h2s->st = H2_SS_HREM;
1981 else
1982 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02001983 }
1984
Willy Tarreau3a429f02019-01-03 11:41:50 +01001985 /* update the max stream ID if the request is being processed */
1986 if (h2s->id > h2c->max_id)
1987 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001988
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001989 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001990
1991 conn_err:
1992 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001993 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001994
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001995 out:
1996 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001997 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001998
1999 send_rst:
2000 /* make the demux send an RST for the current stream. We may only
2001 * do this if we're certain that the HEADERS frame was properly
2002 * decompressed so that the HPACK decoder is still kept up to date.
2003 */
2004 h2_release_buf(h2c, &rxbuf);
2005 h2c->st0 = H2_CS_FRAME_E;
2006 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002007}
2008
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002009/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2010 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2011 * errors here are reported as connection errors since it's impossible to
2012 * recover from such errors after the compression context has been altered.
2013 */
2014static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2015{
2016 int error;
2017
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002018 if (!b_size(&h2c->dbuf))
2019 return NULL; // empty buffer
2020
2021 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2022 return NULL; // incomplete frame
2023
Willy Tarreau1915ca22019-01-24 11:49:37 +01002024 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002025
Willy Tarreau25919232019-01-03 14:48:18 +01002026 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002027 if (h2c->st0 >= H2_CS_ERROR)
2028 return NULL;
2029
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002030 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2031 /* RFC7540#5.1 */
2032 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2033 h2c->st0 = H2_CS_FRAME_E;
2034 return NULL;
2035 }
2036
Willy Tarreau25919232019-01-03 14:48:18 +01002037 if (error <= 0) {
2038 if (error == 0)
2039 return NULL; // missing data
2040
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002041 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002042 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002043 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002044 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002045 }
2046
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002047 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2048 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002049 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002050 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002051 }
2052
Willy Tarreau927b88b2019-03-04 08:03:25 +01002053 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002054 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002055 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 +02002056 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002057 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002058 h2s_close(h2s);
2059
2060 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002061}
2062
Willy Tarreau454f9052017-10-26 19:40:35 +02002063/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2064 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2065 */
2066static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2067{
2068 int error;
2069
2070 /* note that empty DATA frames are perfectly valid and sometimes used
2071 * to signal an end of stream (with the ES flag).
2072 */
2073
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002074 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002075 return 0; // empty buffer
2076
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002077 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002078 return 0; // incomplete frame
2079
2080 /* now either the frame is complete or the buffer is complete */
2081
Willy Tarreau454f9052017-10-26 19:40:35 +02002082 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2083 /* RFC7540#6.1 */
2084 error = H2_ERR_STREAM_CLOSED;
2085 goto strm_err;
2086 }
2087
Willy Tarreau1915ca22019-01-24 11:49:37 +01002088 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2089 /* RFC7540#8.1.2 */
2090 error = H2_ERR_PROTOCOL_ERROR;
2091 goto strm_err;
2092 }
2093
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002094 if (!h2_frt_transfer_data(h2s))
2095 return 0;
2096
Willy Tarreau454f9052017-10-26 19:40:35 +02002097 /* call the upper layers to process the frame, then let the upper layer
2098 * notify the stream about any change.
2099 */
2100 if (!h2s->cs) {
2101 error = H2_ERR_STREAM_CLOSED;
2102 goto strm_err;
2103 }
2104
Willy Tarreau8f650c32017-11-21 19:36:21 +01002105 if (h2c->st0 >= H2_CS_ERROR)
2106 return 0;
2107
Willy Tarreau721c9742017-11-07 11:05:42 +01002108 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002109 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002110 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002111 }
2112
2113 /* check for completion : the callee will change this to FRAME_A or
2114 * FRAME_H once done.
2115 */
2116 if (h2c->st0 == H2_CS_FRAME_P)
2117 return 0;
2118
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002119 /* last frame */
2120 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002121 if (h2s->cs)
2122 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002123 if (h2s->st == H2_SS_OPEN)
2124 h2s->st = H2_SS_HREM;
2125 else
2126 h2s_close(h2s);
2127
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002128 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002129
2130 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2131 /* RFC7540#8.1.2 */
2132 error = H2_ERR_PROTOCOL_ERROR;
2133 goto strm_err;
2134 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002135 }
2136
Willy Tarreau454f9052017-10-26 19:40:35 +02002137 return 1;
2138
Willy Tarreau454f9052017-10-26 19:40:35 +02002139 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002140 h2s_error(h2s, error);
2141 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002142 return 0;
2143}
2144
Willy Tarreaubc933932017-10-09 16:21:43 +02002145/* process Rx frames to be demultiplexed */
2146static void h2_process_demux(struct h2c *h2c)
2147{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002148 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002149 struct h2_fh hdr;
2150 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002151
Willy Tarreau081d4722017-05-16 21:51:05 +02002152 if (h2c->st0 >= H2_CS_ERROR)
2153 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002154
2155 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2156 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002157 if (h2c->flags & H2_CF_IS_BACK)
2158 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002159 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2160 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002161 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002162 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002163 sess_log(h2c->conn->owner);
2164 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002165 goto fail;
2166 }
2167
2168 h2c->max_id = 0;
2169 h2c->st0 = H2_CS_SETTINGS1;
2170 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002171
2172 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002173 /* ensure that what is pending is a valid SETTINGS frame
2174 * without an ACK.
2175 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002176 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002177 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002178 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002179 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002180 sess_log(h2c->conn->owner);
2181 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002182 goto fail;
2183 }
2184
2185 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2186 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2187 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2188 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002189 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002190 goto fail;
2191 }
2192
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002193 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002194 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2195 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2196 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002197 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002198 goto fail;
2199 }
2200
Willy Tarreau3bf69182018-12-21 15:34:50 +01002201 /* that's OK, switch to FRAME_P to process it. This is
2202 * a SETTINGS frame whose header has already been
2203 * deleted above.
2204 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002205 padlen = 0;
2206 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002207 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002208 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002209
2210 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002211 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002212 int ret = 0;
2213
2214 if (h2c->st0 >= H2_CS_ERROR)
2215 break;
2216
2217 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002218 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002219 break;
2220
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002221 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002222 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002223 if (!h2c->nb_streams) {
2224 /* only log if no other stream can report the error */
2225 sess_log(h2c->conn->owner);
2226 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002227 break;
2228 }
2229
Willy Tarreau3bf69182018-12-21 15:34:50 +01002230 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2231 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2232 * we read the pad length and drop it from the remaining
2233 * payload (one byte + the 9 remaining ones = 10 total
2234 * removed), so we have a frame payload starting after the
2235 * pad len. Flow controlled frames (DATA) also count the
2236 * padlen in the flow control, so it must be adjusted.
2237 */
2238 if (hdr.len < 1) {
2239 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2240 sess_log(h2c->conn->owner);
2241 goto fail;
2242 }
2243 hdr.len--;
2244
2245 if (b_data(&h2c->dbuf) < 10)
2246 break; // missing padlen
2247
2248 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2249
2250 if (padlen > hdr.len) {
2251 /* RFC7540#6.1 : pad length = length of
2252 * frame payload or greater => error.
2253 */
2254 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2255 sess_log(h2c->conn->owner);
2256 goto fail;
2257 }
2258
2259 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2260 h2c->rcvd_c++;
2261 h2c->rcvd_s++;
2262 }
2263 b_del(&h2c->dbuf, 1);
2264 }
2265 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002266
2267 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002268 h2c->dfl = hdr.len;
2269 h2c->dsi = hdr.sid;
2270 h2c->dft = hdr.ft;
2271 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002272 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002273 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002274
2275 /* check for minimum basic frame format validity */
2276 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2277 if (ret != H2_ERR_NO_ERROR) {
2278 h2c_error(h2c, ret);
2279 sess_log(h2c->conn->owner);
2280 goto fail;
2281 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002282 }
2283
2284 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002285 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2286
Willy Tarreau567beb82018-12-18 16:52:44 +01002287 if (tmp_h2s != h2s && h2s && h2s->cs &&
2288 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002289 (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 +01002290 /* we may have to signal the upper layers */
2291 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002292 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002293 }
2294 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002295
Willy Tarreaud7901432017-12-29 11:34:40 +01002296 if (h2c->st0 == H2_CS_FRAME_E)
2297 goto strm_err;
2298
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002299 if (h2s->st == H2_SS_IDLE &&
2300 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2301 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2302 * this state MUST be treated as a connection error
2303 */
2304 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002305 if (!h2c->nb_streams) {
2306 /* only log if no other stream can report the error */
2307 sess_log(h2c->conn->owner);
2308 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002309 break;
2310 }
2311
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002312 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2313 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2314 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002315 * this state MUST be treated as a stream error.
2316 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2317 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002318 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002319 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2320 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2321 else
2322 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002323 goto strm_err;
2324 }
2325
Willy Tarreauab837502017-12-27 15:07:30 +01002326 /* Below the management of frames received in closed state is a
2327 * bit hackish because the spec makes strong differences between
2328 * streams closed by receiving RST, sending RST, and seeing ES
2329 * in both directions. In addition to this, the creation of a
2330 * new stream reusing the identifier of a closed one will be
2331 * detected here. Given that we cannot keep track of all closed
2332 * streams forever, we consider that unknown closed streams were
2333 * closed on RST received, which allows us to respond with an
2334 * RST without breaking the connection (eg: to abort a transfer).
2335 * Some frames have to be silently ignored as well.
2336 */
2337 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002338 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002339 /* #5.1.1: The identifier of a newly
2340 * established stream MUST be numerically
2341 * greater than all streams that the initiating
2342 * endpoint has opened or reserved. This
2343 * governs streams that are opened using a
2344 * HEADERS frame and streams that are reserved
2345 * using PUSH_PROMISE. An endpoint that
2346 * receives an unexpected stream identifier
2347 * MUST respond with a connection error.
2348 */
2349 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2350 goto strm_err;
2351 }
2352
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002353 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002354 /* RFC7540#5.1:closed: an endpoint that
2355 * receives any frame other than PRIORITY after
2356 * receiving a RST_STREAM MUST treat that as a
2357 * stream error of type STREAM_CLOSED.
2358 *
2359 * Note that old streams fall into this category
2360 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002361 *
2362 * However, we cannot generalize this to all frame types. Those
2363 * carrying compression state must still be processed before
2364 * being dropped or we'll desynchronize the decoder. This can
2365 * happen with request trailers received after sending an
2366 * RST_STREAM, or with header/trailers responses received after
2367 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002368 */
2369 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2370 h2c->st0 = H2_CS_FRAME_E;
2371 goto strm_err;
2372 }
2373
2374 /* RFC7540#5.1:closed: if this state is reached as a
2375 * result of sending a RST_STREAM frame, the peer that
2376 * receives the RST_STREAM might have already sent
2377 * frames on the stream that cannot be withdrawn. An
2378 * endpoint MUST ignore frames that it receives on
2379 * closed streams after it has sent a RST_STREAM
2380 * frame. An endpoint MAY choose to limit the period
2381 * over which it ignores frames and treat frames that
2382 * arrive after this time as being in error.
2383 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002384 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002385 /* RFC7540#5.1:closed: any frame other than
2386 * PRIO/WU/RST in this state MUST be treated as
2387 * a connection error
2388 */
2389 if (h2c->dft != H2_FT_RST_STREAM &&
2390 h2c->dft != H2_FT_PRIORITY &&
2391 h2c->dft != H2_FT_WINDOW_UPDATE) {
2392 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2393 goto strm_err;
2394 }
2395 }
2396 }
2397
Willy Tarreauc0da1962017-10-30 18:38:00 +01002398#if 0
2399 // problem below: it is not possible to completely ignore such
2400 // streams as we need to maintain the compression state as well
2401 // and for this we need to completely process these frames (eg:
2402 // HEADERS frames) as well as counting DATA frames to emit
2403 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2404 // This is a typical case of layer violation where the
2405 // transported contents are critical to the connection's
2406 // validity and must be ignored at the same time :-(
2407
2408 /* graceful shutdown, ignore streams whose ID is higher than
2409 * the one advertised in GOAWAY. RFC7540#6.8.
2410 */
2411 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002412 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2413 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002414 h2c->dfl -= ret;
2415 ret = h2c->dfl == 0;
2416 goto strm_err;
2417 }
2418#endif
2419
Willy Tarreau7e98c052017-10-10 15:56:59 +02002420 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002421 case H2_FT_SETTINGS:
2422 if (h2c->st0 == H2_CS_FRAME_P)
2423 ret = h2c_handle_settings(h2c);
2424
2425 if (h2c->st0 == H2_CS_FRAME_A)
2426 ret = h2c_ack_settings(h2c);
2427 break;
2428
Willy Tarreaucf68c782017-10-10 17:11:41 +02002429 case H2_FT_PING:
2430 if (h2c->st0 == H2_CS_FRAME_P)
2431 ret = h2c_handle_ping(h2c);
2432
2433 if (h2c->st0 == H2_CS_FRAME_A)
2434 ret = h2c_ack_ping(h2c);
2435 break;
2436
Willy Tarreau26f95952017-07-27 17:18:30 +02002437 case H2_FT_WINDOW_UPDATE:
2438 if (h2c->st0 == H2_CS_FRAME_P)
2439 ret = h2c_handle_window_update(h2c, h2s);
2440 break;
2441
Willy Tarreau61290ec2017-10-17 08:19:21 +02002442 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002443 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2444 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2445 * frames' parsers consume all following CONTINUATION
2446 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002447 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002448 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2449 sess_log(h2c->conn->owner);
2450 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002451
Willy Tarreau13278b42017-10-13 19:23:14 +02002452 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002453 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002454 if (h2c->flags & H2_CF_IS_BACK)
2455 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2456 else
2457 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002458 if (tmp_h2s) {
2459 h2s = tmp_h2s;
2460 ret = 1;
2461 }
2462 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002463 break;
2464
Willy Tarreau454f9052017-10-26 19:40:35 +02002465 case H2_FT_DATA:
2466 if (h2c->st0 == H2_CS_FRAME_P)
2467 ret = h2c_frt_handle_data(h2c, h2s);
2468
2469 if (h2c->st0 == H2_CS_FRAME_A)
2470 ret = h2c_send_strm_wu(h2c);
2471 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002472
Willy Tarreau92153fc2017-12-03 19:46:19 +01002473 case H2_FT_PRIORITY:
2474 if (h2c->st0 == H2_CS_FRAME_P)
2475 ret = h2c_handle_priority(h2c);
2476 break;
2477
Willy Tarreaucd234e92017-08-18 10:59:39 +02002478 case H2_FT_RST_STREAM:
2479 if (h2c->st0 == H2_CS_FRAME_P)
2480 ret = h2c_handle_rst_stream(h2c, h2s);
2481 break;
2482
Willy Tarreaue96b0922017-10-30 00:28:29 +01002483 case H2_FT_GOAWAY:
2484 if (h2c->st0 == H2_CS_FRAME_P)
2485 ret = h2c_handle_goaway(h2c);
2486 break;
2487
Willy Tarreau1c661982017-10-30 13:52:01 +01002488 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002489 default:
2490 /* drop frames that we ignore. They may be larger than
2491 * the buffer so we drain all of their contents until
2492 * we reach the end.
2493 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002494 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2495 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002496 h2c->dfl -= ret;
2497 ret = h2c->dfl == 0;
2498 }
2499
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002500 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002501 /* We may have to send an RST if not done yet */
2502 if (h2s->st == H2_SS_ERROR)
2503 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002504
Willy Tarreaua20a5192017-12-27 11:02:06 +01002505 if (h2c->st0 == H2_CS_FRAME_E)
2506 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002507
Willy Tarreau7e98c052017-10-10 15:56:59 +02002508 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002509 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002510 break;
2511
2512 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002513 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002514 h2c->st0 = H2_CS_FRAME_H;
2515 }
2516 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002517
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002518 if (h2c->rcvd_c > 0 &&
2519 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2520 h2c_send_conn_wu(h2c);
2521
Willy Tarreau52eed752017-09-22 15:05:09 +02002522 fail:
2523 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002524 if (h2s && h2s->cs &&
2525 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002526 (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 +01002527 /* we may have to signal the upper layers */
2528 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002529 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002530 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002531
Willy Tarreau47b515a2018-12-21 16:09:41 +01002532 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002533}
2534
2535/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2536 * the end.
2537 */
2538static int h2_process_mux(struct h2c *h2c)
2539{
Olivier Houchardd360ac62019-03-22 17:37:16 +01002540 struct h2s *h2s;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002541
Willy Tarreau01b44822018-10-03 14:26:37 +02002542 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2543 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2544 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2545 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2546 if (h2c->st0 == H2_CS_ERROR) {
2547 h2c->st0 = H2_CS_ERROR2;
2548 sess_log(h2c->conn->owner);
2549 }
2550 goto fail;
2551 }
2552 h2c->st0 = H2_CS_SETTINGS1;
2553 }
2554 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002555 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002556 return 1;
2557 }
2558
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002559 /* start by sending possibly pending window updates */
2560 if (h2c->rcvd_c > 0 &&
2561 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2562 h2c_send_conn_wu(h2c) < 0)
2563 goto fail;
2564
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002565 /* First we always process the flow control list because the streams
2566 * waiting there were already elected for immediate emission but were
2567 * blocked just on this.
2568 */
2569
Olivier Houchardd360ac62019-03-22 17:37:16 +01002570 list_for_each_entry(h2s, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002571 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2572 h2c->st0 >= H2_CS_ERROR)
2573 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002574 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2575 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002576
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002577 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002578 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2579 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002580 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002581 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002582 }
2583
Olivier Houchardd360ac62019-03-22 17:37:16 +01002584 list_for_each_entry(h2s, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002585 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2586 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002587 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2588 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002589
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002590 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002591 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2592 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002593 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002594 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002595 }
2596
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002597 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002598 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002599 if (h2c->st0 == H2_CS_ERROR) {
2600 if (h2c->max_id >= 0) {
2601 h2c_send_goaway_error(h2c, NULL);
2602 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2603 return 0;
2604 }
2605
2606 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2607 }
2608 return 1;
2609 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002610 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002611}
2612
Willy Tarreau62f52692017-10-08 23:01:42 +02002613
Willy Tarreau479998a2018-11-18 06:30:59 +01002614/* Attempt to read data, and subscribe if none available.
2615 * The function returns 1 if data has been received, otherwise zero.
2616 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002617static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002618{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002619 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002620 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002621 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002622 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002623
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002624 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002625 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002626
Willy Tarreau315d8072017-12-10 22:17:57 +01002627 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002628 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002629
Willy Tarreau44e973f2018-03-01 17:49:30 +01002630 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002631 if (!buf) {
2632 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002633 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002634 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002635
Olivier Houchard7505f942018-08-21 18:10:44 +02002636 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002637 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002638 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2639 /* HTX in use : try to pre-align the buffer like the
2640 * rxbufs will be to optimize memory copies. We'll make
2641 * sure that the frame header lands at the end of the
2642 * HTX block to alias it upon recv. We cannot use the
2643 * head because rcv_buf() will realign the buffer if
2644 * it's empty. Thus we cheat and pretend we already
2645 * have a few bytes there.
2646 */
2647 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002648 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002649 }
2650 else
2651 max = b_room(buf);
2652
Olivier Houchard7505f942018-08-21 18:10:44 +02002653 if (max)
2654 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2655 else
2656 ret = 0;
2657 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002658
Olivier Houchard53216e72018-10-10 15:46:36 +02002659 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002660 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002661
Olivier Houcharda1411e62018-08-17 18:42:48 +02002662 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002663 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002664 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002665 }
2666
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002667 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002668 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002669 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002670}
2671
Willy Tarreau479998a2018-11-18 06:30:59 +01002672/* Try to send data if possible.
2673 * The function returns 1 if data have been sent, otherwise zero.
2674 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002675static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002676{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002677 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002678 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002679 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002680
2681 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002682 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002683
Olivier Houchard7505f942018-08-21 18:10:44 +02002684
Willy Tarreaua2af5122017-10-09 11:56:46 +02002685 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2686 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002687 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002688 }
2689
Willy Tarreaubc933932017-10-09 16:21:43 +02002690 /* This loop is quite simple : it tries to fill as much as it can from
2691 * pending streams into the existing buffer until it's reportedly full
2692 * or the end of send requests is reached. Then it tries to send this
2693 * buffer's contents out, marks it not full if at least one byte could
2694 * be sent, and tries again.
2695 *
2696 * The snd_buf() function normally takes a "flags" argument which may
2697 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2698 * data immediately comes and CO_SFL_STREAMER to indicate that the
2699 * connection is streaming lots of data (used to increase TLS record
2700 * size at the expense of latency). The former can be sent any time
2701 * there's a buffer full flag, as it indicates at least one stream
2702 * attempted to send and failed so there are pending data. An
2703 * alternative would be to set it as long as there's an active stream
2704 * but that would be problematic for ACKs until we have an absolute
2705 * guarantee that all waiters have at least one byte to send. The
2706 * latter should possibly not be set for now.
2707 */
2708
2709 done = 0;
2710 while (!done) {
2711 unsigned int flags = 0;
2712
2713 /* fill as much as we can into the current buffer */
2714 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2715 done = h2_process_mux(h2c);
2716
Olivier Houchard2b094432019-01-29 18:28:36 +01002717 if (h2c->flags & H2_CF_MUX_MALLOC)
2718 break;
2719
Willy Tarreaubc933932017-10-09 16:21:43 +02002720 if (conn->flags & CO_FL_ERROR)
2721 break;
2722
2723 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2724 flags |= CO_SFL_MSG_MORE;
2725
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002726 if (b_data(&h2c->mbuf)) {
2727 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002728 if (!ret)
2729 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002730 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002731 b_del(&h2c->mbuf, ret);
2732 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002733 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002734
2735 /* wrote at least one byte, the buffer is not full anymore */
2736 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2737 }
2738
Willy Tarreaua2af5122017-10-09 11:56:46 +02002739 if (conn->flags & CO_FL_SOCK_WR_SH) {
2740 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002741 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002742 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002743 /* We're not full anymore, so we can wake any task that are waiting
2744 * for us.
2745 */
2746 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002747 struct h2s *h2s;
2748
2749 list_for_each_entry(h2s, &h2c->send_list, list) {
2750 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2751 break;
2752 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2753 continue;
2754
2755 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002756 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2757 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002758 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002759 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002760 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002761 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002762 /* We're done, no more to send */
2763 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002764 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002765schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002766 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2767 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002768 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002769}
2770
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002771/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002772static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2773{
2774 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002775 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002776
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002777 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002778 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002779 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002780 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002781 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002782 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002783 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002784}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002785
Willy Tarreau62f52692017-10-08 23:01:42 +02002786/* callback called on any event by the connection handler.
2787 * It applies changes and returns zero, or < 0 if it wants immediate
2788 * destruction of the connection (which normally doesn not happen in h2).
2789 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002790static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002791{
Olivier Houchard7505f942018-08-21 18:10:44 +02002792 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002793
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002794 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002795 h2_process_demux(h2c);
2796
2797 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002798 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002799
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002800 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002801 h2c->flags &= ~H2_CF_DEM_DFULL;
2802 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002803 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002804
Willy Tarreau0b37d652018-10-03 10:33:02 +02002805 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002806 /* frontend is stopping, reload likely in progress, let's try
2807 * to announce a graceful shutdown if not yet done. We don't
2808 * care if it fails, it will be tried again later.
2809 */
2810 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2811 if (h2c->last_sid < 0)
2812 h2c->last_sid = (1U << 31) - 1;
2813 h2c_send_goaway_error(h2c, NULL);
2814 }
2815 }
2816
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002817 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002818 * If we received early data, and the handshake is done, wake
2819 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002820 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002821 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2822 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2823 struct eb32_node *node;
2824 struct h2s *h2s;
2825
2826 h2c->flags |= H2_CF_WAIT_FOR_HS;
2827 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2828
2829 while (node) {
2830 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002831 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002832 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002833 node = eb32_next(node);
2834 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002835 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002836
Willy Tarreau26bd7612017-10-09 16:47:04 +02002837 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002838 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2839 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2840 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002841 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002842
2843 if (eb_is_empty(&h2c->streams_by_id)) {
2844 /* no more stream, kill the connection now */
2845 h2_release(conn);
2846 return -1;
2847 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002848 }
2849
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002850 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002851 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002852
Olivier Houchard53216e72018-10-10 15:46:36 +02002853 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2854 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2855 (h2c->st0 != H2_CS_ERROR &&
2856 !b_data(&h2c->mbuf) &&
2857 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2858 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002859 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002860
Willy Tarreau3f133572017-10-31 19:21:06 +01002861 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002862 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002863 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002864 task_queue(h2c->task);
2865 }
2866 else
2867 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002868 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002869
Olivier Houchard7505f942018-08-21 18:10:44 +02002870 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002871 return 0;
2872}
2873
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002874/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002875static int h2_wake(struct connection *conn)
2876{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002877 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002878
2879 return (h2_process(h2c));
2880}
2881
Willy Tarreauea392822017-10-31 10:02:25 +01002882/* Connection timeout management. The principle is that if there's no receipt
2883 * nor sending for a certain amount of time, the connection is closed. If the
2884 * MUX buffer still has lying data or is not allocatable, the connection is
2885 * immediately killed. If it's allocatable and empty, we attempt to send a
2886 * GOAWAY frame.
2887 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002888static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002889{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002890 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002891 int expired = tick_is_expired(t->expire, now_ms);
2892
Willy Tarreau0975f112018-03-29 15:22:59 +02002893 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002894 return t;
2895
Willy Tarreau0975f112018-03-29 15:22:59 +02002896 task_delete(t);
2897 task_free(t);
2898
2899 if (!h2c) {
2900 /* resources were already deleted */
2901 return NULL;
2902 }
2903
2904 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002905 h2c_error(h2c, H2_ERR_NO_ERROR);
2906 h2_wake_some_streams(h2c, 0, 0);
2907
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002908 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002909 /* don't even try to send a GOAWAY, the buffer is stuck */
2910 h2c->flags |= H2_CF_GOAWAY_FAILED;
2911 }
2912
2913 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002914 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002915 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2916 h2c->flags |= H2_CF_GOAWAY_FAILED;
2917
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002918 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2919 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002920 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002921 b_del(&h2c->mbuf, ret);
2922 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002923 }
2924 }
Willy Tarreauea392822017-10-31 10:02:25 +01002925
Willy Tarreau0975f112018-03-29 15:22:59 +02002926 /* either we can release everything now or it will be done later once
2927 * the last stream closes.
2928 */
2929 if (eb_is_empty(&h2c->streams_by_id))
2930 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002931
Willy Tarreauea392822017-10-31 10:02:25 +01002932 return NULL;
2933}
2934
2935
Willy Tarreau62f52692017-10-08 23:01:42 +02002936/*******************************************/
2937/* functions below are used by the streams */
2938/*******************************************/
2939
2940/*
2941 * Attach a new stream to a connection
2942 * (Used for outgoing connections)
2943 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002944static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002945{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002946 struct conn_stream *cs;
2947 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002948 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002949
2950 cs = cs_new(conn);
2951 if (!cs)
2952 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002953 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002954 if (!h2s) {
2955 cs_free(cs);
2956 return NULL;
2957 }
2958 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002959}
2960
Willy Tarreaufafd3982018-11-18 21:29:20 +01002961/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2962 * We have to scan because we may have some orphan streams. It might be
2963 * beneficial to scan backwards from the end to reduce the likeliness to find
2964 * orphans.
2965 */
2966static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2967{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002968 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002969 struct h2s *h2s;
2970 struct eb32_node *node;
2971
2972 node = eb32_first(&h2c->streams_by_id);
2973 while (node) {
2974 h2s = container_of(node, struct h2s, by_id);
2975 if (h2s->cs)
2976 return h2s->cs;
2977 node = eb32_next(node);
2978 }
2979 return NULL;
2980}
2981
Willy Tarreau62f52692017-10-08 23:01:42 +02002982/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002983 * Destroy the mux and the associated connection, if it is no longer used
2984 */
2985static void h2_destroy(struct connection *conn)
2986{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002987 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002988
2989 if (eb_is_empty(&h2c->streams_by_id))
2990 h2_release(h2c->conn);
2991}
2992
2993/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002994 * Detach the stream from the connection and possibly release the connection.
2995 */
2996static void h2_detach(struct conn_stream *cs)
2997{
Willy Tarreau60935142017-10-16 18:11:19 +02002998 struct h2s *h2s = cs->ctx;
2999 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003000 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003001
3002 cs->ctx = NULL;
3003 if (!h2s)
3004 return;
3005
Olivier Houchardf502aca2018-12-14 19:42:40 +01003006 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003007 h2c = h2s->h2c;
3008 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003009 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003010 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3011 !h2_frt_has_too_many_cs(h2c)) {
3012 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003013 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003014 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02003015 }
Willy Tarreau60935142017-10-16 18:11:19 +02003016
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003017 /* this stream may be blocked waiting for some data to leave (possibly
3018 * an ES or RST frame), so orphan it in this case.
3019 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003020 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003021 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003022 (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 +01003023 return;
3024
Willy Tarreau45f752e2017-10-30 15:44:59 +01003025 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3026 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3027 /* unblock the connection if it was blocked on this
3028 * stream.
3029 */
3030 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3031 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003032 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003033 }
3034
Willy Tarreau71049cc2018-03-28 13:56:39 +02003035 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003036
Olivier Houchard8a786902018-12-15 16:05:40 +01003037 if (h2c->flags & H2_CF_IS_BACK &&
3038 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003039 if (!(h2c->conn->flags &
3040 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3041 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003042 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003043 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3044 h2c->conn->owner = NULL;
3045 if (eb_is_empty(&h2c->streams_by_id)) {
3046 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3047 /* The server doesn't want it, let's kill the connection right away */
3048 h2c->conn->mux->destroy(h2c->conn);
3049 return;
3050 }
3051 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003052 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003053 if (eb_is_empty(&h2c->streams_by_id)) {
3054 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3055 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3056 return;
3057 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003058 /* Never ever allow to reuse a connection from a non-reuse backend */
3059 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3060 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003061 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003062 struct server *srv = objt_server(h2c->conn->target);
3063
3064 if (srv) {
3065 if (h2c->conn->flags & CO_FL_PRIVATE)
3066 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3067 else
3068 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3069 }
3070
3071 }
3072 }
3073 }
3074
Willy Tarreaue323f342018-03-28 13:51:45 +02003075 /* We don't want to close right now unless we're removing the
3076 * last stream, and either the connection is in error, or it
3077 * reached the ID already specified in a GOAWAY frame received
3078 * or sent (as seen by last_sid >= 0).
3079 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003080 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003081 /* no more stream will come, kill it now */
3082 h2_release(h2c->conn);
3083 }
3084 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003085 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003086 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3087 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003088 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003089 else
3090 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003091 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003092}
3093
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003094/* Performs a synchronous or asynchronous shutr().
3095 * FIXME: guess what the return code tries to indicate!
3096 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003097static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003098{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003099 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003100 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003101
Willy Tarreau721c9742017-11-07 11:05:42 +01003102 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003103 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003104
Willy Tarreau18059042019-01-31 19:12:48 +01003105 /* a connstream may require us to immediately kill the whole connection
3106 * for example because of a "tcp-request content reject" rule that is
3107 * normally used to limit abuse. In this case we schedule a goaway to
3108 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003109 */
Willy Tarreau18059042019-01-31 19:12:48 +01003110 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3111 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3112 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3113 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3114 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003115 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3116 /* Nothing was never sent for this stream, so reset with
3117 * REFUSED_STREAM error to let the client retry the
3118 * request.
3119 */
3120 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3121 }
Willy Tarreau18059042019-01-31 19:12:48 +01003122
Willy Tarreau90c32322017-11-24 08:00:30 +01003123 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003124 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003125 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003126
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003127 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003128 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003129 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003130
Olivier Houchard7a977432019-03-21 15:47:13 +01003131 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003132add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003133 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003134 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003135 if (h2s->flags & H2_SF_BLK_MFCTL) {
3136 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3137 h2s->send_wait = sw;
3138 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3139 h2s->send_wait = sw;
3140 LIST_ADDQ(&h2c->send_list, &h2s->list);
3141 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003142 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003143 /* Let the handler know we want shutr */
3144 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003145 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003146}
3147
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003148/* Performs a synchronous or asynchronous shutw().
3149 * FIXME: guess what the return code tries to indicate!
3150 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003151static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003152{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003153 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003154 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003155
Willy Tarreau721c9742017-11-07 11:05:42 +01003156 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003157 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003158
Willy Tarreau67434202017-11-06 20:20:51 +01003159 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003160 /* we can cleanly close using an empty data frame only after headers */
3161
3162 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3163 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003164 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003165
3166 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003167 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003168 else
3169 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003170 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003171 /* a connstream may require us to immediately kill the whole connection
3172 * for example because of a "tcp-request content reject" rule that is
3173 * normally used to limit abuse. In this case we schedule a goaway to
3174 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003175 */
Willy Tarreau18059042019-01-31 19:12:48 +01003176 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3177 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3178 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3179 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3180 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003181 else {
3182 /* Nothing was never sent for this stream, so reset with
3183 * REFUSED_STREAM error to let the client retry the
3184 * request.
3185 */
3186 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3187 }
Willy Tarreau18059042019-01-31 19:12:48 +01003188
Willy Tarreau90c32322017-11-24 08:00:30 +01003189 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003190 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003191 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003192
Willy Tarreau00dd0782018-03-01 16:31:34 +01003193 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003194 }
3195
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003196 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003197 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003198 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003199
3200 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003201 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003202 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003203 if (h2s->flags & H2_SF_BLK_MFCTL) {
3204 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3205 h2s->send_wait = sw;
3206 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3207 h2s->send_wait = sw;
3208 LIST_ADDQ(&h2c->send_list, &h2s->list);
3209 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003210 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003211 /* let the handler know we want to shutw */
3212 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003213 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003214}
3215
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003216/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3217 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3218 * and prevented the last frame from being emitted.
3219 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003220static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3221{
3222 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003223 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003224 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003225
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003226 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003227 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003228 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003229 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003230 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003231 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003232 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003233 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003234 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003235
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003236 if (!ret) {
3237 /* We're done trying to send, remove ourself from the send_list */
3238 h2s->send_wait->events &= ~SUB_RETRY_SEND;
3239 h2s->send_wait = NULL;
3240 LIST_DEL_INIT(&h2s->list);
3241 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003242 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003243 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003244 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003245 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003246
3247 if (h2c_is_dead(h2c))
3248 h2_release(h2c->conn);
3249 }
3250
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003251 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003252}
3253
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003254/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003255static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3256{
3257 struct h2s *h2s = cs->ctx;
3258
3259 if (!mode)
3260 return;
3261
3262 h2_do_shutr(h2s);
3263}
3264
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003265/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003266static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3267{
3268 struct h2s *h2s = cs->ctx;
3269
3270 h2_do_shutw(h2s);
3271}
3272
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003273/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003274 * HTX request or response depending on the connection's side. Returns a
3275 * positive value on success, a negative value on failure, or 0 if it couldn't
3276 * proceed. May report connection errors in h2c->errcode if the frame is
3277 * non-decodable and the connection unrecoverable. In absence of connection
3278 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003279 *
3280 * The function may fold CONTINUATION frames into the initial HEADERS frame
3281 * by removing padding and next frame header, then moving the CONTINUATION
3282 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3283 * leaving a hole between the main frame and the beginning of the next one.
3284 * The possibly remaining incomplete or next frame at the end may be moved
3285 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3286 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3287 *
3288 * A buffer at the beginning of processing may look like this :
3289 *
3290 * ,---.---------.-----.--------------.--------------.------.---.
3291 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3292 * `---^---------^-----^--------------^--------------^------^---'
3293 * | | <-----> | |
3294 * area | dpl | wrap
3295 * |<--------------> |
3296 * | dfl |
3297 * |<-------------------------------------------------->|
3298 * head data
3299 *
3300 * Padding is automatically overwritten when folding, participating to the
3301 * hole size after dfl :
3302 *
3303 * ,---.------------------------.-----.--------------.------.---.
3304 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3305 * `---^------------------------^-----^--------------^------^---'
3306 * | | <-----> | |
3307 * area | hole | wrap
3308 * |<-----------------------> |
3309 * | dfl |
3310 * |<-------------------------------------------------->|
3311 * head data
3312 *
3313 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3314 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3315 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003316 *
3317 * The <flags> field must point to either the stream's flags or to a copy of it
3318 * so that the function can update the following flags :
3319 * - H2_SF_DATA_CLEN when content-length is seen
3320 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3321 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003322 *
3323 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3324 * decoding, in order to detect if we're dealing with a headers or a trailers
3325 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003326 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003327static 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 +02003328{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003329 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003330 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003331 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003332 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003333 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003334 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003335 int flen; // header frame len
3336 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003337 int ret = 0;
3338 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003339 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003340 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003341
Willy Tarreauea18f862018-12-22 20:19:26 +01003342next_frame:
3343 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3344 goto leave; // incomplete input frame
3345
3346 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3347 * this case, we'll try to paste it immediately after the initial
3348 * HEADERS frame payload and kill any possible padding. The initial
3349 * frame's length will be increased to represent the concatenation
3350 * of the two frames. The next frame is read from position <tlen>
3351 * and written at position <flen> (minus padding if some is present).
3352 */
3353 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3354 struct h2_fh hdr;
3355 int clen; // CONTINUATION frame's payload length
3356
3357 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3358 /* no more data, the buffer may be full, either due to
3359 * too large a frame or because of too large a hole that
3360 * we're going to compact at the end.
3361 */
3362 goto leave;
3363 }
3364
3365 if (hdr.ft != H2_FT_CONTINUATION) {
3366 /* RFC7540#6.10: frame of unexpected type */
3367 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3368 goto fail;
3369 }
3370
3371 if (hdr.sid != h2c->dsi) {
3372 /* RFC7540#6.10: frame of different stream */
3373 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3374 goto fail;
3375 }
3376
3377 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3378 /* RFC7540#4.2: invalid frame length */
3379 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3380 goto fail;
3381 }
3382
3383 /* detect when we must stop aggragating frames */
3384 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3385
3386 /* Take as much as we can of the CONTINUATION frame's payload */
3387 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3388 if (clen > hdr.len)
3389 clen = hdr.len;
3390
3391 /* Move the frame's payload over the padding, hole and frame
3392 * header. At least one of hole or dpl is null (see diagrams
3393 * above). The hole moves after the new aggragated frame.
3394 */
3395 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3396 h2c->dfl += clen - h2c->dpl;
3397 hole += h2c->dpl + 9;
3398 h2c->dpl = 0;
3399 goto next_frame;
3400 }
3401
3402 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003403
Willy Tarreau13278b42017-10-13 19:23:14 +02003404 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003405 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003406 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003407 copy = alloc_trash_chunk();
3408 if (!copy) {
3409 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3410 goto fail;
3411 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003412 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3413 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3414 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003415 }
3416
Willy Tarreau13278b42017-10-13 19:23:14 +02003417 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3418 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003419 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003420 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3421 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003422 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003423 }
3424
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003425 if (flen < 5) {
3426 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3427 goto fail;
3428 }
3429
Willy Tarreau13278b42017-10-13 19:23:14 +02003430 hdrs += 5; // stream dep = 4, weight = 1
3431 flen -= 5;
3432 }
3433
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003434 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003435 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003436 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003437 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003438
Willy Tarreau937f7602018-02-26 15:22:17 +01003439 /* we can't retry a failed decompression operation so we must be very
3440 * careful not to take any risks. In practice the output buffer is
3441 * always empty except maybe for trailers, in which case we simply have
3442 * to wait for the upper layer to finish consuming what is available.
3443 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003444
3445 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003446 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003447 if (!htx_is_empty(htx)) {
3448 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003449 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003450 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003451 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003452 if (b_data(rxbuf)) {
3453 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003454 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003455 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003456
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003457 rxbuf->head = 0;
3458 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003459 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003460
Willy Tarreau25919232019-01-03 14:48:18 +01003461 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003462 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3463 sizeof(list)/sizeof(list[0]), tmp);
3464 if (outlen < 0) {
3465 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3466 goto fail;
3467 }
3468
Willy Tarreau25919232019-01-03 14:48:18 +01003469 /* The PACK decompressor was updated, let's update the input buffer and
3470 * the parser's state to commit these changes and allow us to later
3471 * fail solely on the stream if needed.
3472 */
3473 b_del(&h2c->dbuf, h2c->dfl + hole);
3474 h2c->dfl = hole = 0;
3475 h2c->st0 = H2_CS_FRAME_H;
3476
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003477 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003478 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003479
Willy Tarreau88d138e2019-01-02 19:38:14 +01003480 if (*flags & H2_SF_HEADERS_RCVD)
3481 goto trailers;
3482
3483 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003484 if (htx) {
3485 /* HTX mode */
3486 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003487 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003488 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003489 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003490 } else {
3491 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003492 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003493 if (outlen > 0)
3494 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003495 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003496
3497 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003498 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003499 goto fail;
3500 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003501
Willy Tarreau174b06a2018-04-25 18:13:58 +02003502 if (msgf & H2_MSGF_BODY) {
3503 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003504 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003505 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003506 if (htx)
3507 htx->extra = *body_len;
3508 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003509 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003510 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003511 }
3512
Willy Tarreau88d138e2019-01-02 19:38:14 +01003513 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003514 /* indicate that a HEADERS frame was received for this stream, except
3515 * for 1xx responses. For 1xx responses, another HEADERS frame is
3516 * expected.
3517 */
3518 if (!(msgf & H2_MSGF_RSP_1XX))
3519 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003520
Christopher Faulet0b465482019-02-19 15:14:23 +01003521 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003522 /* Mark the end of message, either using EOM in HTX or with the
3523 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3524 * is not set during headers with END_STREAM.
3525 */
3526 if (htx) {
3527 if (!htx_add_endof(htx, HTX_BLK_EOM))
3528 goto fail;
3529 }
3530 else if (*flags & H2_SF_DATA_CHNK) {
3531 if (!b_putblk(rxbuf, "\r\n", 2))
3532 goto fail;
3533 }
3534 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003535
Willy Tarreau86277d42019-01-02 15:36:11 +01003536 /* success */
3537 ret = 1;
3538
Willy Tarreau68dd9852017-07-03 14:44:26 +02003539 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003540 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003541 * move the remaining data over it.
3542 */
3543 if (hole) {
3544 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3545 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3546 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3547 b_sub(&h2c->dbuf, hole);
3548 }
3549
3550 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3551 /* too large frames */
3552 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003553 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003554 }
3555
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003556 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003557 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003558 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003559 return ret;
3560
Willy Tarreau68dd9852017-07-03 14:44:26 +02003561 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003562 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003563 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003564
3565 trailers:
3566 /* This is the last HEADERS frame hence a trailer */
3567
3568 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3569 /* It's a trailer but it's missing ES flag */
3570 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3571 goto fail;
3572 }
3573
3574 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3575 * block, and when using chunks we must send the 0 CRLF marker. For
3576 * other modes, the trailers are silently dropped.
3577 */
3578 if (htx) {
3579 if (!htx_add_endof(htx, HTX_BLK_EOD))
3580 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003581 if (h2_make_htx_trailers(list, htx) <= 0)
3582 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003583 }
3584 else if (*flags & H2_SF_DATA_CHNK) {
3585 /* Legacy mode with chunked encoding : we must finalize the
3586 * data block message emit the trailing CRLF */
3587 if (!b_putblk(rxbuf, "0\r\n", 3))
3588 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003589
3590 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3591 if (outlen > 0)
3592 b_add(rxbuf, outlen);
3593 else
3594 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003595 }
3596
3597 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003598}
3599
Willy Tarreau454f9052017-10-26 19:40:35 +02003600/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3601 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3602 * in use, a new chunk is emitted for each frame. This is supposed to fit
3603 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3604 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3605 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003606 * parser state is automatically updated. Returns > 0 if it could completely
3607 * send the current frame, 0 if it couldn't complete, in which case
3608 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3609 * DATA frame can return 0 as a valid result). Stream errors are reported in
3610 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3611 * have checked the frame header and ensured that the frame was complete or the
3612 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003613 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003614static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003615{
3616 struct h2c *h2c = h2s->h2c;
3617 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003618 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003619 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003620 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003621 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003622
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003623 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003624
Olivier Houchard638b7992018-08-16 15:41:52 +02003625 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003626 if (!csbuf) {
3627 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003628 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003629 }
3630
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003631try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003632 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003633 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3634 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003635 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003636 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003637
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003638 if (flen > b_data(&h2c->dbuf)) {
3639 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003640 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003641 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003642 }
3643
Willy Tarreaua9b77962019-01-31 07:23:00 +01003644 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003645 block1 = htx_free_data_space(htx);
3646 if (!block1) {
3647 h2c->flags |= H2_CF_DEM_SFULL;
3648 goto fail;
3649 }
3650 if (flen > block1)
3651 flen = block1;
3652
3653 /* here, flen is the max we can copy into the output buffer */
3654 block1 = b_contig_data(&h2c->dbuf, 0);
3655 if (flen > block1)
3656 flen = block1;
3657
3658 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3659 h2c->flags |= H2_CF_DEM_SFULL;
3660 goto fail;
3661 }
3662
3663 b_del(&h2c->dbuf, flen);
3664 h2c->dfl -= flen;
3665 h2c->rcvd_c += flen;
3666 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003667
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003668 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003669 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003670 htx->extra = h2s->body_len;
3671 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003672 goto try_again;
3673 }
3674 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003675 /* it doesn't fit and the buffer is fragmented,
3676 * so let's defragment it and try again.
3677 */
3678 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003679 }
3680
Willy Tarreaueba10f22018-04-25 20:44:22 +02003681 /* chunked-encoding requires more room */
3682 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003683 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003684 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3685 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3686 (chklen < 1048576) ? 4 : 8;
3687 chklen += 4; // CRLF, CRLF
3688 }
3689
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003690 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003691 if (flen + chklen > b_room(csbuf)) {
3692 if (chklen >= b_room(csbuf)) {
3693 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003694 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003695 }
3696 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003697 }
3698
3699 if (h2s->flags & H2_SF_DATA_CHNK) {
3700 /* emit the chunk size */
3701 unsigned int chksz = flen;
3702 char str[10];
3703 char *beg;
3704
3705 beg = str + sizeof(str);
3706 *--beg = '\n';
3707 *--beg = '\r';
3708 do {
3709 *--beg = hextab[chksz & 0xF];
3710 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003711 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003712 }
3713
Willy Tarreau454f9052017-10-26 19:40:35 +02003714 /* Block1 is the length of the first block before the buffer wraps,
3715 * block2 is the optional second block to reach the end of the frame.
3716 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003717 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003718 if (block1 > flen)
3719 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003720 block2 = flen - block1;
3721
3722 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003723 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003724
3725 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003726 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003727
Willy Tarreaueba10f22018-04-25 20:44:22 +02003728 if (h2s->flags & H2_SF_DATA_CHNK) {
3729 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003730 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003731 }
3732
Willy Tarreau454f9052017-10-26 19:40:35 +02003733 /* now mark the input data as consumed (will be deleted from the buffer
3734 * by the caller when seeing FRAME_A after sending the window update).
3735 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003736 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003737 h2c->dfl -= flen;
3738 h2c->rcvd_c += flen;
3739 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3740
Willy Tarreau1915ca22019-01-24 11:49:37 +01003741 if (h2s->flags & H2_SF_DATA_CLEN)
3742 h2s->body_len -= flen;
3743
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003744 if (h2c->dfl > h2c->dpl) {
3745 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003746 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003747 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003748 }
3749
Willy Tarreau4a28da12018-01-04 14:41:00 +01003750 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003751 /* here we're done with the frame, all the payload (except padding) was
3752 * transferred.
3753 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003754
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003755 if (h2c->dff & H2_F_DATA_END_STREAM) {
3756 if (htx) {
3757 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3758 h2c->flags |= H2_CF_DEM_SFULL;
3759 goto fail;
3760 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003761 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003762 else if (h2s->flags & H2_SF_DATA_CHNK) {
3763 /* emit the trailing 0 CRLF CRLF */
3764 if (b_room(csbuf) < 5) {
3765 h2c->flags |= H2_CF_DEM_SFULL;
3766 goto fail;
3767 }
3768 chklen += 5;
3769 b_putblk(csbuf, "0\r\n\r\n", 5);
3770 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003771 }
3772
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003773 h2c->rcvd_c += h2c->dpl;
3774 h2c->rcvd_s += h2c->dpl;
3775 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003776 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003777 if (htx)
3778 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003779 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003780 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003781 if (htx)
3782 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003783 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003784}
3785
Willy Tarreau5dd17352018-06-14 13:33:30 +02003786/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3787 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3788 * number of bytes sent. The caller must check the stream's status to detect
3789 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003790 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003791static 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 +02003792{
3793 struct http_hdr list[MAX_HTTP_HDR];
3794 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003795 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003796 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003797 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003798 int es_now = 0;
3799 int ret = 0;
3800 int hdr;
3801
3802 if (h2c_mux_busy(h2c, h2s)) {
3803 h2s->flags |= H2_SF_BLK_MBUSY;
3804 return 0;
3805 }
3806
Willy Tarreau44e973f2018-03-01 17:49:30 +01003807 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003808 h2c->flags |= H2_CF_MUX_MALLOC;
3809 h2s->flags |= H2_SF_BLK_MROOM;
3810 return 0;
3811 }
3812
3813 /* First, try to parse the H1 response and index it into <list>.
3814 * NOTE! Since it comes from haproxy, we *know* that a response header
3815 * block does not wrap and we can safely read it this way without
3816 * having to realign the buffer.
3817 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003818 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003819 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003820 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003821 /* incomplete or invalid response, this is abnormal coming from
3822 * haproxy and may only result in a bad errorfile or bad Lua code
3823 * so that won't be fixed, raise an error now.
3824 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003825 * FIXME: we should instead add the ability to only return a
3826 * 502 bad gateway. But in theory this is not supposed to
3827 * happen.
3828 */
3829 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3830 ret = 0;
3831 goto end;
3832 }
3833
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003834 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003835
3836 /* certain statuses have no body or an empty one, regardless of
3837 * what the headers say.
3838 */
3839 if (sl.st.status >= 100 && sl.st.status < 200) {
3840 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3841 h1m->curr_len = h1m->body_len = 0;
3842 }
3843 else if (sl.st.status == 204 || sl.st.status == 304) {
3844 /* no contents, claim c-len is present and set to zero */
3845 h1m->flags &= ~H1_MF_CHNK;
3846 h1m->flags |= H1_MF_CLEN;
3847 h1m->curr_len = h1m->body_len = 0;
3848 }
3849
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003850 chunk_reset(&outbuf);
3851
3852 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003853 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003854 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003855 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003856
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003857 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003858 break;
3859 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003860 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003861 }
3862
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003863 if (outbuf.size < 9)
3864 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003865
3866 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003867 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3868 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3869 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003870
3871 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003872 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003873 /* this is an unparsable response */
3874 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3875 ret = 0;
3876 goto end;
3877 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003878
3879 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003880 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003881 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003882 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003883 }
3884
3885 /* encode all headers, stop at empty name */
3886 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003887 /* these ones do not exist in H2 and must be dropped. */
3888 if (isteq(list[hdr].n, ist("connection")) ||
3889 isteq(list[hdr].n, ist("proxy-connection")) ||
3890 isteq(list[hdr].n, ist("keep-alive")) ||
3891 isteq(list[hdr].n, ist("upgrade")) ||
3892 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003893 continue;
3894
3895 if (isteq(list[hdr].n, ist("")))
3896 break; // end
3897
3898 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3899 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003900 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003901 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003902 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003903 }
3904 }
3905
3906 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003907 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003908 es_now = 1;
3909
3910 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003911 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003912
3913 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003914 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003915
3916 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003917 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003918
3919 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003920 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003921 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003922
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003923 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003924 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003925 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003926
Willy Tarreau801250e2018-09-11 11:45:04 +02003927 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003928 h2s->flags |= H2_SF_ES_SENT;
3929 if (h2s->st == H2_SS_OPEN)
3930 h2s->st = H2_SS_HLOC;
3931 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003932 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003933 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003934 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003935 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003936 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003937 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003938 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003939 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003940 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003941
3942 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003943
3944 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003945 //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 +02003946 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003947 full:
3948 h1m_init_res(h1m);
3949 h1m->err_pos = -1; // don't care about errors on the response path
3950 h2c->flags |= H2_CF_MUX_MFULL;
3951 h2s->flags |= H2_SF_BLK_MROOM;
3952 ret = 0;
3953 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003954}
3955
Willy Tarreau5dd17352018-06-14 13:33:30 +02003956/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3957 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3958 * the number of bytes sent. The caller must check the stream's status to
3959 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003960 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003961static 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 +02003962{
3963 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003964 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003965 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003966 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003967 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003968 int es_now = 0;
3969 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003970 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003971 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003972
3973 if (h2c_mux_busy(h2c, h2s)) {
3974 h2s->flags |= H2_SF_BLK_MBUSY;
3975 goto end;
3976 }
3977
Willy Tarreau44e973f2018-03-01 17:49:30 +01003978 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003979 h2c->flags |= H2_CF_MUX_MALLOC;
3980 h2s->flags |= H2_SF_BLK_MROOM;
3981 goto end;
3982 }
3983
3984 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003985 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003986 goto end;
3987
3988 chunk_reset(&outbuf);
3989
3990 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003991 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003992 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003993 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003994
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003995 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003996 break;
3997 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003998 /* If there are pending data in the output buffer, and we have
3999 * less than 1/4 of the mbuf's size and everything fits, we'll
4000 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4001 * is full and wait, to save some slow realign calls.
4002 */
4003 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4004 h2c->flags |= H2_CF_MUX_MFULL;
4005 h2s->flags |= H2_SF_BLK_MROOM;
4006 goto end;
4007 }
4008
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004009 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004010 }
4011
4012 if (outbuf.size < 9) {
4013 h2c->flags |= H2_CF_MUX_MFULL;
4014 h2s->flags |= H2_SF_BLK_MROOM;
4015 goto end;
4016 }
4017
4018 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004019 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4020 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4021 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004022
4023 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4024 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004025 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004026 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004027 break;
4028 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004029 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004030 if ((long long)size > h1m->curr_len)
4031 size = h1m->curr_len;
4032 break;
4033 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004034 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004035 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004036 if (!ret)
4037 goto end;
4038
4039 if (ret < 0) {
4040 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004041 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004042 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4043 goto end;
4044 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004045 max -= ret;
4046 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004047 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004048 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004049 }
4050
Willy Tarreau801250e2018-09-11 11:45:04 +02004051 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004052 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004053 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004054 if (!ret)
4055 goto end;
4056
4057 if (ret < 0) {
4058 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004059 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004060 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4061 goto end;
4062 }
4063
4064 size = chunk;
4065 h1m->curr_len = chunk;
4066 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004067 max -= ret;
4068 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004069 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004070 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004071 if (!size)
4072 goto send_empty;
4073 }
4074
4075 /* in MSG_DATA state, continue below */
4076 size = h1m->curr_len;
4077 break;
4078 }
4079
4080 /* we have in <size> the exact number of bytes we need to copy from
4081 * the H1 buffer. We need to check this against the connection's and
4082 * the stream's send windows, and to ensure that this fits in the max
4083 * frame size and in the buffer's available space minus 9 bytes (for
4084 * the frame header). The connection's flow control is applied last so
4085 * that we can use a separate list of streams which are immediately
4086 * unblocked on window opening. Note: we don't implement padding.
4087 */
4088
Willy Tarreau5dd17352018-06-14 13:33:30 +02004089 if (size > max)
4090 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004091
4092 if (size > h2s->mws)
4093 size = h2s->mws;
4094
4095 if (size <= 0) {
4096 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004097 if (h2s->send_wait) {
4098 LIST_DEL(&h2s->list);
4099 LIST_INIT(&h2s->list);
4100 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004101 goto end;
4102 }
4103
4104 if (h2c->mfs && size > h2c->mfs)
4105 size = h2c->mfs;
4106
4107 if (size + 9 > outbuf.size) {
4108 /* we have an opportunity for enlarging the too small
4109 * available space, let's try.
4110 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004111 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004112 goto realign_again;
4113 size = outbuf.size - 9;
4114 }
4115
4116 if (size <= 0) {
4117 h2c->flags |= H2_CF_MUX_MFULL;
4118 h2s->flags |= H2_SF_BLK_MROOM;
4119 goto end;
4120 }
4121
4122 if (size > h2c->mws)
4123 size = h2c->mws;
4124
4125 if (size <= 0) {
4126 h2s->flags |= H2_SF_BLK_MFCTL;
4127 goto end;
4128 }
4129
4130 /* copy whatever we can */
4131 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004132 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004133 if (ret == 1)
4134 len2 = 0;
4135
4136 if (!ret || len1 + len2 < size) {
4137 /* FIXME: must normally never happen */
4138 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4139 goto end;
4140 }
4141
4142 /* limit len1/len2 to size */
4143 if (len1 + len2 > size) {
4144 int sub = len1 + len2 - size;
4145
4146 if (len2 > sub)
4147 len2 -= sub;
4148 else {
4149 sub -= len2;
4150 len2 = 0;
4151 len1 -= sub;
4152 }
4153 }
4154
4155 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004156 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004157 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004158 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004159
4160 send_empty:
4161 /* we may need to add END_STREAM */
4162 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4163 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004164 *
4165 * FIXME: what we do here is not correct because we send end_stream
4166 * before knowing if we'll have to send a HEADERS frame for the
4167 * trailers. More importantly we're not consuming the trailing CRLF
4168 * after the end of trailers, so it will be left to the caller to
4169 * eat it. The right way to do it would be to measure trailers here
4170 * and to send ES only if there are no trailers.
4171 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004172 */
4173 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004174 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004175 es_now = 1;
4176
4177 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004178 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004179
4180 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004181 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004182
4183 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004184 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004185
4186 /* consume incoming H1 response */
4187 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004188 max -= size;
4189 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004190 total += size;
4191 h1m->curr_len -= size;
4192 h2s->mws -= size;
4193 h2c->mws -= size;
4194
4195 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004196 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004197 goto new_frame;
4198 }
4199 }
4200
4201 if (es_now) {
4202 if (h2s->st == H2_SS_OPEN)
4203 h2s->st = H2_SS_HLOC;
4204 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004205 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004206
Willy Tarreau35a62702018-02-27 15:37:25 +01004207 if (!(h1m->flags & H1_MF_CHNK)) {
4208 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004209 total += max;
4210 ofs += max;
4211 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004212
Willy Tarreau801250e2018-09-11 11:45:04 +02004213 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004214 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004215
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004216 h2s->flags |= H2_SF_ES_SENT;
4217 }
4218
4219 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004220 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 +02004221 return total;
4222}
4223
Willy Tarreau115e83b2018-12-01 19:17:53 +01004224/* Try to send a HEADERS frame matching HTX response present in HTX message
4225 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4226 * must check the stream's status to detect any error which might have happened
4227 * subsequently to a successful send. The htx blocks are automatically removed
4228 * from the message. The htx message is assumed to be valid since produced from
4229 * the internal code, hence it contains a start line, an optional series of
4230 * header blocks and an end of header, otherwise an invalid frame could be
4231 * emitted and the resulting htx message could be left in an inconsistent state.
4232 */
4233static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4234{
4235 struct http_hdr list[MAX_HTTP_HDR];
4236 struct h2c *h2c = h2s->h2c;
4237 struct htx_blk *blk;
4238 struct htx_blk *blk_end;
4239 struct buffer outbuf;
4240 struct htx_sl *sl;
4241 enum htx_blk_type type;
4242 int es_now = 0;
4243 int ret = 0;
4244 int hdr;
4245 int idx;
4246
4247 if (h2c_mux_busy(h2c, h2s)) {
4248 h2s->flags |= H2_SF_BLK_MBUSY;
4249 return 0;
4250 }
4251
4252 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4253 h2c->flags |= H2_CF_MUX_MALLOC;
4254 h2s->flags |= H2_SF_BLK_MROOM;
4255 return 0;
4256 }
4257
4258 /* determine the first block which must not be deleted, blk_end may
4259 * be NULL if all blocks have to be deleted.
4260 */
4261 idx = htx_get_head(htx);
4262 blk_end = NULL;
4263 while (idx != -1) {
4264 type = htx_get_blk_type(htx_get_blk(htx, idx));
4265 idx = htx_get_next(htx, idx);
4266 if (type == HTX_BLK_EOH) {
4267 if (idx != -1)
4268 blk_end = htx_get_blk(htx, idx);
4269 break;
4270 }
4271 }
4272
4273 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004274 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004275 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004276 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004277 if (h2s->status < 100 || h2s->status > 999)
4278 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004279
4280 /* and the rest of the headers, that we dump starting at header 0 */
4281 hdr = 0;
4282
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004283 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004284 while ((idx = htx_get_next(htx, idx)) != -1) {
4285 blk = htx_get_blk(htx, idx);
4286 type = htx_get_blk_type(blk);
4287
4288 if (type == HTX_BLK_UNUSED)
4289 continue;
4290
4291 if (type != HTX_BLK_HDR)
4292 break;
4293
4294 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4295 goto fail;
4296
4297 list[hdr].n = htx_get_blk_name(htx, blk);
4298 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004299 hdr++;
4300 }
4301
4302 /* marker for end of headers */
4303 list[hdr].n = ist("");
4304
4305 if (h2s->status == 204 || h2s->status == 304) {
4306 /* no contents, claim c-len is present and set to zero */
4307 es_now = 1;
4308 }
4309
4310 chunk_reset(&outbuf);
4311
4312 while (1) {
4313 outbuf.area = b_tail(&h2c->mbuf);
4314 outbuf.size = b_contig_space(&h2c->mbuf);
4315 outbuf.data = 0;
4316
4317 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4318 break;
4319 realign_again:
4320 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4321 }
4322
4323 if (outbuf.size < 9)
4324 goto full;
4325
4326 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4327 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4328 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4329 outbuf.data = 9;
4330
4331 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004332 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004333 if (b_space_wraps(&h2c->mbuf))
4334 goto realign_again;
4335 goto full;
4336 }
4337
4338 /* encode all headers, stop at empty name */
4339 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4340 /* these ones do not exist in H2 and must be dropped. */
4341 if (isteq(list[hdr].n, ist("connection")) ||
4342 isteq(list[hdr].n, ist("proxy-connection")) ||
4343 isteq(list[hdr].n, ist("keep-alive")) ||
4344 isteq(list[hdr].n, ist("upgrade")) ||
4345 isteq(list[hdr].n, ist("transfer-encoding")))
4346 continue;
4347
4348 if (isteq(list[hdr].n, ist("")))
4349 break; // end
4350
4351 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4352 /* output full */
4353 if (b_space_wraps(&h2c->mbuf))
4354 goto realign_again;
4355 goto full;
4356 }
4357 }
4358
Christopher Faulet0b465482019-02-19 15:14:23 +01004359 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004360 * FIXME: we should also set it when we know for sure that the
4361 * content-length is zero as well as on 204/304
4362 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004363 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4364 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004365 es_now = 1;
4366
Willy Tarreau927b88b2019-03-04 08:03:25 +01004367 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004368 es_now = 1;
4369
4370 /* update the frame's size */
4371 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4372
4373 if (es_now)
4374 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4375
4376 /* commit the H2 response */
4377 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004378
4379 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4380 * 1xx responses, another HEADERS frame is expected.
4381 */
4382 if (h2s->status >= 200 || h2s->status == 101)
4383 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004384
Willy Tarreau115e83b2018-12-01 19:17:53 +01004385 if (es_now) {
4386 h2s->flags |= H2_SF_ES_SENT;
4387 if (h2s->st == H2_SS_OPEN)
4388 h2s->st = H2_SS_HLOC;
4389 else
4390 h2s_close(h2s);
4391 }
4392
4393 /* OK we could properly deliver the response */
4394
4395 /* remove all header blocks including the EOH and compute the
4396 * corresponding size.
4397 *
4398 * FIXME: We should remove everything when es_now is set.
4399 */
4400 ret = 0;
4401 idx = htx_get_head(htx);
4402 blk = htx_get_blk(htx, idx);
4403 while (blk != blk_end) {
4404 ret += htx_get_blksz(blk);
4405 blk = htx_remove_blk(htx, blk);
4406 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004407
4408 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4409 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004410 end:
4411 return ret;
4412 full:
4413 h2c->flags |= H2_CF_MUX_MFULL;
4414 h2s->flags |= H2_SF_BLK_MROOM;
4415 ret = 0;
4416 goto end;
4417 fail:
4418 /* unparsable HTX messages, too large ones to be produced in the local
4419 * list etc go here (unrecoverable errors).
4420 */
4421 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4422 ret = 0;
4423 goto end;
4424}
4425
Willy Tarreau80739692018-10-05 11:35:57 +02004426/* Try to send a HEADERS frame matching HTX request present in HTX message
4427 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4428 * must check the stream's status to detect any error which might have happened
4429 * subsequently to a successful send. The htx blocks are automatically removed
4430 * from the message. The htx message is assumed to be valid since produced from
4431 * the internal code, hence it contains a start line, an optional series of
4432 * header blocks and an end of header, otherwise an invalid frame could be
4433 * emitted and the resulting htx message could be left in an inconsistent state.
4434 */
4435static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4436{
4437 struct http_hdr list[MAX_HTTP_HDR];
4438 struct h2c *h2c = h2s->h2c;
4439 struct htx_blk *blk;
4440 struct htx_blk *blk_end;
4441 struct buffer outbuf;
4442 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004443 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004444 enum htx_blk_type type;
4445 int es_now = 0;
4446 int ret = 0;
4447 int hdr;
4448 int idx;
4449
4450 if (h2c_mux_busy(h2c, h2s)) {
4451 h2s->flags |= H2_SF_BLK_MBUSY;
4452 return 0;
4453 }
4454
4455 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4456 h2c->flags |= H2_CF_MUX_MALLOC;
4457 h2s->flags |= H2_SF_BLK_MROOM;
4458 return 0;
4459 }
4460
4461 /* determine the first block which must not be deleted, blk_end may
4462 * be NULL if all blocks have to be deleted.
4463 */
4464 idx = htx_get_head(htx);
4465 blk_end = NULL;
4466 while (idx != -1) {
4467 type = htx_get_blk_type(htx_get_blk(htx, idx));
4468 idx = htx_get_next(htx, idx);
4469 if (type == HTX_BLK_EOH) {
4470 if (idx != -1)
4471 blk_end = htx_get_blk(htx, idx);
4472 break;
4473 }
4474 }
4475
4476 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004477 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004478 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004479 meth = htx_sl_req_meth(sl);
4480 path = htx_sl_req_uri(sl);
4481
4482 /* and the rest of the headers, that we dump starting at header 0 */
4483 hdr = 0;
4484
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004485 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004486 while ((idx = htx_get_next(htx, idx)) != -1) {
4487 blk = htx_get_blk(htx, idx);
4488 type = htx_get_blk_type(blk);
4489
4490 if (type == HTX_BLK_UNUSED)
4491 continue;
4492
4493 if (type != HTX_BLK_HDR)
4494 break;
4495
4496 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4497 goto fail;
4498
4499 list[hdr].n = htx_get_blk_name(htx, blk);
4500 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004501 hdr++;
4502 }
4503
4504 /* marker for end of headers */
4505 list[hdr].n = ist("");
4506
4507 chunk_reset(&outbuf);
4508
4509 while (1) {
4510 outbuf.area = b_tail(&h2c->mbuf);
4511 outbuf.size = b_contig_space(&h2c->mbuf);
4512 outbuf.data = 0;
4513
4514 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4515 break;
4516 realign_again:
4517 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4518 }
4519
4520 if (outbuf.size < 9)
4521 goto full;
4522
4523 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4524 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4525 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4526 outbuf.data = 9;
4527
4528 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004529 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004530 if (b_space_wraps(&h2c->mbuf))
4531 goto realign_again;
4532 goto full;
4533 }
4534
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004535 /* RFC7540 #8.3: the CONNECT method must have :
4536 * - :authority set to the URI part (host:port)
4537 * - :method set to CONNECT
4538 * - :scheme and :path omitted
4539 */
4540 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4541 /* encode the scheme which is always "https" (or 0x86 for "http") */
4542 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4543 /* output full */
4544 if (b_space_wraps(&h2c->mbuf))
4545 goto realign_again;
4546 goto full;
4547 }
Willy Tarreau80739692018-10-05 11:35:57 +02004548
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004549 /* encode the path, which necessarily is the second one */
4550 if (!hpack_encode_path(&outbuf, path)) {
4551 /* output full */
4552 if (b_space_wraps(&h2c->mbuf))
4553 goto realign_again;
4554 goto full;
4555 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004556
4557 /* look for the Host header and place it in :authority */
4558 auth = ist2(NULL, 0);
4559 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4560 if (isteq(list[hdr].n, ist("")))
4561 break; // end
4562
4563 if (isteq(list[hdr].n, ist("host"))) {
4564 auth = list[hdr].v;
4565 break;
4566 }
4567 }
4568 }
4569 else {
4570 /* for CONNECT, :authority is taken from the path */
4571 auth = path;
4572 }
4573
4574 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4575 /* output full */
4576 if (b_space_wraps(&h2c->mbuf))
4577 goto realign_again;
4578 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004579 }
4580
4581 /* encode all headers, stop at empty name */
4582 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4583 /* these ones do not exist in H2 and must be dropped. */
4584 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004585 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004586 isteq(list[hdr].n, ist("proxy-connection")) ||
4587 isteq(list[hdr].n, ist("keep-alive")) ||
4588 isteq(list[hdr].n, ist("upgrade")) ||
4589 isteq(list[hdr].n, ist("transfer-encoding")))
4590 continue;
4591
4592 if (isteq(list[hdr].n, ist("")))
4593 break; // end
4594
4595 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4596 /* output full */
4597 if (b_space_wraps(&h2c->mbuf))
4598 goto realign_again;
4599 goto full;
4600 }
4601 }
4602
4603 /* we may need to add END_STREAM if we have no body :
4604 * - request already closed, or :
4605 * - no transfer-encoding, and :
4606 * - no content-length or content-length:0
4607 * Fixme: this doesn't take into account CONNECT requests.
4608 */
4609 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4610 es_now = 1;
4611
4612 if (sl->flags & HTX_SL_F_BODYLESS)
4613 es_now = 1;
4614
Willy Tarreau927b88b2019-03-04 08:03:25 +01004615 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004616 es_now = 1;
4617
4618 /* update the frame's size */
4619 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4620
4621 if (es_now)
4622 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4623
4624 /* commit the H2 response */
4625 b_add(&h2c->mbuf, outbuf.data);
4626 h2s->flags |= H2_SF_HEADERS_SENT;
4627 h2s->st = H2_SS_OPEN;
4628
Willy Tarreau80739692018-10-05 11:35:57 +02004629 if (es_now) {
4630 // trim any possibly pending data (eg: inconsistent content-length)
4631 h2s->flags |= H2_SF_ES_SENT;
4632 h2s->st = H2_SS_HLOC;
4633 }
4634
4635 /* remove all header blocks including the EOH and compute the
4636 * corresponding size.
4637 *
4638 * FIXME: We should remove everything when es_now is set.
4639 */
4640 ret = 0;
4641 idx = htx_get_head(htx);
4642 blk = htx_get_blk(htx, idx);
4643 while (blk != blk_end) {
4644 ret += htx_get_blksz(blk);
4645 blk = htx_remove_blk(htx, blk);
4646 }
4647
4648 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4649 htx_remove_blk(htx, blk_end);
4650
4651 end:
4652 return ret;
4653 full:
4654 h2c->flags |= H2_CF_MUX_MFULL;
4655 h2s->flags |= H2_SF_BLK_MROOM;
4656 ret = 0;
4657 goto end;
4658 fail:
4659 /* unparsable HTX messages, too large ones to be produced in the local
4660 * list etc go here (unrecoverable errors).
4661 */
4662 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4663 ret = 0;
4664 goto end;
4665}
4666
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004667/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004668 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4669 * caller must check the stream's status to detect any error which might have
4670 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004671 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4672 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004673static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004674{
4675 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004676 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004677 struct buffer outbuf;
4678 size_t total = 0;
4679 int es_now = 0;
4680 int bsize; /* htx block size */
4681 int fsize; /* h2 frame size */
4682 struct htx_blk *blk;
4683 enum htx_blk_type type;
4684 int idx;
4685
4686 if (h2c_mux_busy(h2c, h2s)) {
4687 h2s->flags |= H2_SF_BLK_MBUSY;
4688 goto end;
4689 }
4690
4691 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4692 h2c->flags |= H2_CF_MUX_MALLOC;
4693 h2s->flags |= H2_SF_BLK_MROOM;
4694 goto end;
4695 }
4696
Willy Tarreau98de12a2018-12-12 07:03:00 +01004697 htx = htx_from_buf(buf);
4698
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004699 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4700 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4701 * the caller to handle.
4702 */
4703
4704 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004705 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004706 goto end;
4707
4708 idx = htx_get_head(htx);
4709 blk = htx_get_blk(htx, idx);
4710 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4711 bsize = htx_get_blksz(blk);
4712 fsize = bsize;
4713
4714 if (type == HTX_BLK_EOD) {
4715 /* if we have an EOD, we're dealing with chunked data. We may
4716 * have a set of trailers after us that the caller will want to
4717 * deal with. Let's simply remove the EOD and return.
4718 */
4719 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004720 total++; // EOD counts as one byte
4721 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004722 goto end;
4723 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004724 else if (type == HTX_BLK_EOM) {
4725 if (h2s->flags & H2_SF_ES_SENT) {
4726 /* ES already sent */
4727 htx_remove_blk(htx, blk);
4728 total++; // EOM counts as one byte
4729 count--;
4730 goto end;
4731 }
4732 }
4733 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004734 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004735
4736 /* Perform some optimizations to reduce the number of buffer copies.
4737 * First, if the mux's buffer is empty and the htx area contains
4738 * exactly one data block of the same size as the requested count, and
4739 * this count fits within the frame size, the stream's window size, and
4740 * the connection's window size, then it's possible to simply swap the
4741 * caller's buffer with the mux's output buffer and adjust offsets and
4742 * length to match the entire DATA HTX block in the middle. In this
4743 * case we perform a true zero-copy operation from end-to-end. This is
4744 * the situation that happens all the time with large files. Second, if
4745 * this is not possible, but the mux's output buffer is empty, we still
4746 * have an opportunity to avoid the copy to the intermediary buffer, by
4747 * making the intermediary buffer's area point to the output buffer's
4748 * area. In this case we want to skip the HTX header to make sure that
4749 * copies remain aligned and that this operation remains possible all
4750 * the time. This goes for headers, data blocks and any data extracted
4751 * from the HTX blocks.
4752 */
4753 if (unlikely(fsize == count &&
4754 htx->used == 1 && type == HTX_BLK_DATA &&
4755 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4756 void *old_area = h2c->mbuf.area;
4757
4758 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004759 /* Too bad there are data left there. We're willing to memcpy/memmove
4760 * up to 1/4 of the buffer, which means that it's OK to copy a large
4761 * frame into a buffer containing few data if it needs to be realigned,
4762 * and that it's also OK to copy few data without realigning. Otherwise
4763 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004764 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004765 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4766 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4767 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004768 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004769
Willy Tarreau98de12a2018-12-12 07:03:00 +01004770 h2c->flags |= H2_CF_MUX_MFULL;
4771 h2s->flags |= H2_SF_BLK_MROOM;
4772 goto end;
4773 }
4774
4775 /* map an H2 frame to the HTX block so that we can put the
4776 * frame header there.
4777 */
4778 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004779 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004780 h2c->mbuf.data = fsize + 9;
4781 outbuf.area = b_head(&h2c->mbuf);
4782
4783 /* prepend an H2 DATA frame header just before the DATA block */
4784 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4785 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4786 h2_set_frame_size(outbuf.area, fsize);
4787
4788 /* update windows */
4789 h2s->mws -= fsize;
4790 h2c->mws -= fsize;
4791
4792 /* and exchange with our old area */
4793 buf->area = old_area;
4794 buf->data = buf->head = 0;
4795 total += fsize;
4796 goto end;
4797 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004798
Willy Tarreau98de12a2018-12-12 07:03:00 +01004799 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004800 /* for DATA and EOM we'll have to emit a frame, even if empty */
4801
4802 while (1) {
4803 outbuf.area = b_tail(&h2c->mbuf);
4804 outbuf.size = b_contig_space(&h2c->mbuf);
4805 outbuf.data = 0;
4806
4807 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4808 break;
4809 realign_again:
4810 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4811 }
4812
4813 if (outbuf.size < 9) {
4814 h2c->flags |= H2_CF_MUX_MFULL;
4815 h2s->flags |= H2_SF_BLK_MROOM;
4816 goto end;
4817 }
4818
4819 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4820 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4821 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4822 outbuf.data = 9;
4823
4824 /* we have in <fsize> the exact number of bytes we need to copy from
4825 * the HTX buffer. We need to check this against the connection's and
4826 * the stream's send windows, and to ensure that this fits in the max
4827 * frame size and in the buffer's available space minus 9 bytes (for
4828 * the frame header). The connection's flow control is applied last so
4829 * that we can use a separate list of streams which are immediately
4830 * unblocked on window opening. Note: we don't implement padding.
4831 */
4832
4833 /* EOM is presented with bsize==1 but would lead to the emission of an
4834 * empty frame, thus we force it to zero here.
4835 */
4836 if (type == HTX_BLK_EOM)
4837 bsize = fsize = 0;
4838
4839 if (!fsize)
4840 goto send_empty;
4841
4842 if (h2s->mws <= 0) {
4843 h2s->flags |= H2_SF_BLK_SFCTL;
4844 if (h2s->send_wait) {
4845 LIST_DEL(&h2s->list);
4846 LIST_INIT(&h2s->list);
4847 }
4848 goto end;
4849 }
4850
Willy Tarreauee573762018-12-04 15:25:57 +01004851 if (fsize > count)
4852 fsize = count;
4853
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004854 if (fsize > h2s->mws)
4855 fsize = h2s->mws; // >0
4856
4857 if (h2c->mfs && fsize > h2c->mfs)
4858 fsize = h2c->mfs; // >0
4859
4860 if (fsize + 9 > outbuf.size) {
4861 /* we have an opportunity for enlarging the too small
4862 * available space, let's try.
4863 * FIXME: is this really interesting to do? Maybe we'll
4864 * spend lots of time realigning instead of using two
4865 * frames.
4866 */
4867 if (b_space_wraps(&h2c->mbuf))
4868 goto realign_again;
4869 fsize = outbuf.size - 9;
4870
4871 if (fsize <= 0) {
4872 /* no need to send an empty frame here */
4873 h2c->flags |= H2_CF_MUX_MFULL;
4874 h2s->flags |= H2_SF_BLK_MROOM;
4875 goto end;
4876 }
4877 }
4878
4879 if (h2c->mws <= 0) {
4880 h2s->flags |= H2_SF_BLK_MFCTL;
4881 goto end;
4882 }
4883
4884 if (fsize > h2c->mws)
4885 fsize = h2c->mws;
4886
4887 /* now let's copy this this into the output buffer */
4888 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004889 h2s->mws -= fsize;
4890 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004891 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004892
4893 send_empty:
4894 /* update the frame's size */
4895 h2_set_frame_size(outbuf.area, fsize);
4896
4897 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4898 * meeting EOM. We should optimize this later.
4899 */
4900 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004901 total++; // EOM counts as one byte
4902 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004903 es_now = 1;
4904 }
4905
4906 if (es_now)
4907 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4908
4909 /* commit the H2 response */
4910 b_add(&h2c->mbuf, fsize + 9);
4911
4912 /* consume incoming HTX block, including EOM */
4913 total += fsize;
4914 if (fsize == bsize) {
4915 htx_remove_blk(htx, blk);
4916 if (fsize)
4917 goto new_frame;
4918 } else {
4919 /* we've truncated this block */
4920 htx_cut_data_blk(htx, blk, fsize);
4921 }
4922
4923 if (es_now) {
4924 if (h2s->st == H2_SS_OPEN)
4925 h2s->st = H2_SS_HLOC;
4926 else
4927 h2s_close(h2s);
4928
4929 h2s->flags |= H2_SF_ES_SENT;
4930 }
4931
4932 end:
4933 return total;
4934}
4935
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004936/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4937 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4938 * processed. The caller must check the stream's status to detect any error
4939 * which might have happened subsequently to a successful send. The htx blocks
4940 * are automatically removed from the message. The htx message is assumed to be
4941 * valid since produced from the internal code. Processing stops when meeting
4942 * the EOM, which is also removed. All trailers are processed at once and sent
4943 * as a single frame. The ES flag is always set.
4944 */
4945static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4946{
4947 struct http_hdr list[MAX_HTTP_HDR];
4948 struct h2c *h2c = h2s->h2c;
4949 struct htx_blk *blk;
4950 struct htx_blk *blk_end;
4951 struct buffer outbuf;
4952 struct h1m h1m;
4953 enum htx_blk_type type;
4954 uint32_t size;
4955 int ret = 0;
4956 int hdr;
4957 int idx;
4958 void *start;
4959
4960 if (h2c_mux_busy(h2c, h2s)) {
4961 h2s->flags |= H2_SF_BLK_MBUSY;
4962 goto end;
4963 }
4964
4965 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4966 h2c->flags |= H2_CF_MUX_MALLOC;
4967 h2s->flags |= H2_SF_BLK_MROOM;
4968 goto end;
4969 }
4970
4971 /* The principle is that we parse each and every trailers block using
4972 * the H1 headers parser, and append it to the list. We don't proceed
4973 * until EOM is met. blk_end will point to the EOM block.
4974 */
4975 hdr = 0;
4976 memset(list, 0, sizeof(list));
4977 blk_end = NULL;
4978
4979 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4980 blk = htx_get_blk(htx, idx);
4981 type = htx_get_blk_type(blk);
4982
4983 if (type == HTX_BLK_UNUSED)
4984 continue;
4985
4986 if (type != HTX_BLK_TLR) {
4987 if (type == HTX_BLK_EOM)
4988 blk_end = blk;
4989 break;
4990 }
4991
4992 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4993 goto fail;
4994
4995 size = htx_get_blksz(blk);
4996 start = htx_get_blk_ptr(htx, blk);
4997
4998 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4999 h1m.err_pos = 0;
5000 ret = h1_headers_to_hdr_list(start, start + size,
5001 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5002 &h1m, NULL);
5003 if (ret < 0)
5004 goto fail;
5005
5006 /* ret == 0 if an incomplete trailers block was found (missing
5007 * empty line), or > 0 if it was found. We have to continue on
5008 * incomplete messages because the trailers block might be
5009 * incomplete.
5010 */
5011
5012 /* search the new end */
5013 while (hdr <= sizeof(list)/sizeof(list[0])) {
5014 if (!list[hdr].n.len)
5015 break;
5016 hdr++;
5017 }
5018 }
5019
5020 if (!blk_end)
5021 goto end; // end not found yet
5022
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005023 chunk_reset(&outbuf);
5024
5025 while (1) {
5026 outbuf.area = b_tail(&h2c->mbuf);
5027 outbuf.size = b_contig_space(&h2c->mbuf);
5028 outbuf.data = 0;
5029
5030 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5031 break;
5032 realign_again:
5033 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5034 }
5035
5036 if (outbuf.size < 9)
5037 goto full;
5038
5039 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5040 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5041 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5042 outbuf.data = 9;
5043
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005044 /* encode all headers */
5045 for (idx = 0; idx < hdr; idx++) {
5046 /* these ones do not exist in H2 or must not appear in
5047 * trailers and must be dropped.
5048 */
5049 if (isteq(list[idx].n, ist("host")) ||
5050 isteq(list[idx].n, ist("content-length")) ||
5051 isteq(list[idx].n, ist("connection")) ||
5052 isteq(list[idx].n, ist("proxy-connection")) ||
5053 isteq(list[idx].n, ist("keep-alive")) ||
5054 isteq(list[idx].n, ist("upgrade")) ||
5055 isteq(list[idx].n, ist("te")) ||
5056 isteq(list[idx].n, ist("transfer-encoding")))
5057 continue;
5058
5059 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5060 /* output full */
5061 if (b_space_wraps(&h2c->mbuf))
5062 goto realign_again;
5063 goto full;
5064 }
5065 }
5066
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005067 if (!hdr) {
5068 /* here we have a problem, we've received an empty trailers
5069 * block followed by an EOM. Because of this we can't send a
5070 * HEADERS frame, so we have to cheat and instead send an empty
5071 * DATA frame conveying the ES flag.
5072 */
5073 outbuf.area[3] = H2_FT_DATA;
5074 outbuf.area[4] = H2_F_DATA_END_STREAM;
5075 }
5076
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005077 /* update the frame's size */
5078 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5079
5080 /* commit the H2 response */
5081 b_add(&h2c->mbuf, outbuf.data);
5082 h2s->flags |= H2_SF_ES_SENT;
5083
5084 if (h2s->st == H2_SS_OPEN)
5085 h2s->st = H2_SS_HLOC;
5086 else
5087 h2s_close(h2s);
5088
5089 /* OK we could properly deliver the response */
5090 done:
5091 /* remove all header blocks including EOM and compute the corresponding size. */
5092 ret = 0;
5093 idx = htx_get_head(htx);
5094 blk = htx_get_blk(htx, idx);
5095 while (blk != blk_end) {
5096 ret += htx_get_blksz(blk);
5097 blk = htx_remove_blk(htx, blk);
5098 }
5099 blk = htx_remove_blk(htx, blk);
5100 end:
5101 return ret;
5102 full:
5103 h2c->flags |= H2_CF_MUX_MFULL;
5104 h2s->flags |= H2_SF_BLK_MROOM;
5105 ret = 0;
5106 goto end;
5107 fail:
5108 /* unparsable HTX messages, too large ones to be produced in the local
5109 * list etc go here (unrecoverable errors).
5110 */
5111 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5112 ret = 0;
5113 goto end;
5114}
5115
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005116/* Called from the upper layer, to subscribe to events, such as being able to send.
5117 * The <param> argument here is supposed to be a pointer to a wait_event struct
5118 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5119 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5120 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5121 * returns 0 except for the error above.
5122 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005123static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5124{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005125 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005126 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005127 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005128
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005129 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005130 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005131 if (!(sw->events & SUB_RETRY_RECV)) {
5132 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005133 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005134 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005135 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005136 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005137 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005138 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005139 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005140 if (!(sw->events & SUB_RETRY_SEND)) {
5141 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005142 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005143 h2s->send_wait = sw;
5144 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5145 if (h2s->flags & H2_SF_BLK_MFCTL)
5146 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5147 else
5148 LIST_ADDQ(&h2c->send_list, &h2s->list);
5149 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005150 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005151 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005152 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005153 if (event_type != 0)
5154 return -1;
5155 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005156}
5157
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005158/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5159 * The <param> argument here is supposed to be a pointer to the same wait_event
5160 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5161 * It always returns zero.
5162 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005163static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5164{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005165 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005166 struct h2s *h2s = cs->ctx;
5167
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005168 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005169 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005170 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005171 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005172 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005173 }
5174 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005175 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005176 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005177 if (h2s->send_wait == sw) {
5178 LIST_DEL(&h2s->list);
5179 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005180 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005181 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005182 }
5183 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005184 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5185 sw = param;
5186 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005187 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Willy Tarreaue73256f2019-03-25 18:02:54 +01005188 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005189 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005190 LIST_DEL(&h2s->list);
5191 LIST_INIT(&h2s->list);
Olivier Houchard3ea35132019-03-25 14:10:42 +01005192 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005193 }
5194 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005195 return 0;
5196}
5197
5198
Olivier Houchard511efea2018-08-16 15:30:32 +02005199/* Called from the upper layer, to receive data */
5200static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5201{
Olivier Houchard638b7992018-08-16 15:41:52 +02005202 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005203 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005204 struct htx *h2s_htx = NULL;
5205 struct htx *buf_htx = NULL;
5206 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005207 size_t ret = 0;
5208
5209 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005210 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5211 /* in HTX mode we ignore the count argument */
5212 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005213 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005214 /* Here htx_to_buf() will set buffer data to 0 because
5215 * the HTX is empty.
5216 */
5217 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005218 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005219 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005220
5221 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005222 count = htx_free_data_space(buf_htx);
5223 if (flags & CO_RFL_KEEP_RSV) {
5224 if (count <= global.tune.maxrewrite)
5225 goto end;
5226 count -= global.tune.maxrewrite;
5227 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005228
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005229 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005230
5231 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5232 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5233
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005234 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005235 htx_to_buf(buf_htx, buf);
5236 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005237 ret = htx_ret.ret;
5238 }
5239 else {
5240 ret = b_xfer(buf, &h2s->rxbuf, count);
5241 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005242
Christopher Faulet37070b22019-02-14 15:12:14 +01005243 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005244 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005245 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005246 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005247 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005248 if (cs->flags & CS_FL_REOS)
5249 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005250 if (cs->flags & CS_FL_ERR_PENDING)
5251 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005252 if (b_size(&h2s->rxbuf)) {
5253 b_free(&h2s->rxbuf);
5254 offer_buffers(NULL, tasks_run_queue);
5255 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005256 }
5257
Willy Tarreau082f5592018-11-25 08:03:32 +01005258 if (ret && h2c->dsi == h2s->id) {
5259 /* demux is blocking on this stream's buffer */
5260 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005261 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005262 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005263
Olivier Houchard511efea2018-08-16 15:30:32 +02005264 return ret;
5265}
5266
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005267/* stops all senders of this connection for example when the mux buffer is full.
5268 * They are moved from the sending_list to either fctl_list or send_list.
5269 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005270static void h2_stop_senders(struct h2c *h2c)
5271{
5272 struct h2s *h2s, *h2s_back;
5273
Olivier Houchardd360ac62019-03-22 17:37:16 +01005274 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5275 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005276 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005277 h2s->send_wait->events |= SUB_RETRY_SEND;
5278 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005279 }
5280}
5281
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005282/* Called from the upper layer, to send data from buffer <buf> for no more than
5283 * <count> bytes. Returns the number of bytes effectively sent. Some status
5284 * flags may be updated on the conn_stream.
5285 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005286static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005287{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005288 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005289 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005290 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005291 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005292 struct htx *htx;
5293 struct htx_blk *blk;
5294 enum htx_blk_type btype;
5295 uint32_t bsize;
5296 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005297
Olivier Houchardd360ac62019-03-22 17:37:16 +01005298 /* If we were not just woken because we wanted to send but couldn't,
5299 * and there's somebody else that is waiting to send, do nothing,
5300 * we will subscribe later and be put at the end of the list
5301 */
5302 LIST_DEL_INIT(&h2s->sending_list);
5303 if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) &&
5304 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5305 return 0;
5306
Olivier Houchardd846c262018-10-19 17:24:29 +02005307 if (h2s->send_wait) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005308 /* We want to stay in the send_list, so prepare ourself to be
5309 * eventually recalled if needed, and only remove ourself from
5310 * the list if we managed to send anything.
5311 */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005312 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01005313 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005314 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005315 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5316 return 0;
5317
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005318 /* htx will be enough to decide if we're using HTX or legacy */
5319 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5320
Willy Tarreau0bad0432018-06-14 16:54:01 +02005321 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005322 h2s->flags |= H2_SF_OUTGOING_DATA;
5323
Willy Tarreau751f2d02018-10-05 09:35:00 +02005324 if (h2s->id == 0) {
5325 int32_t id = h2c_get_next_sid(h2s->h2c);
5326
5327 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005328 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005329 return 0;
5330 }
5331
5332 eb32_delete(&h2s->by_id);
5333 h2s->by_id.key = h2s->id = id;
5334 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005335 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005336 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5337 }
5338
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005339 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005340 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5341 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005342 idx = htx_get_head(htx);
5343 blk = htx_get_blk(htx, idx);
5344 btype = htx_get_blk_type(blk);
5345 bsize = htx_get_blksz(blk);
5346
5347 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005348 case HTX_BLK_REQ_SL:
5349 /* start-line before headers */
5350 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5351 if (ret > 0) {
5352 total += ret;
5353 count -= ret;
5354 if (ret < bsize)
5355 goto done;
5356 }
5357 break;
5358
Willy Tarreau115e83b2018-12-01 19:17:53 +01005359 case HTX_BLK_RES_SL:
5360 /* start-line before headers */
5361 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5362 if (ret > 0) {
5363 total += ret;
5364 count -= ret;
5365 if (ret < bsize)
5366 goto done;
5367 }
5368 break;
5369
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005370 case HTX_BLK_DATA:
5371 case HTX_BLK_EOD:
5372 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005373 /* all these cause the emission of a DATA frame (possibly empty).
5374 * This EOM necessarily is one before trailers, as the EOM following
5375 * trailers would have been consumed by the trailers parser.
5376 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005377 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005378 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005379 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005380 total += ret;
5381 count -= ret;
5382 if (ret < bsize)
5383 goto done;
5384 }
5385 break;
5386
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005387 case HTX_BLK_TLR:
5388 /* This is the first trailers block, all the subsequent ones AND
5389 * the EOM will be swallowed by the parser.
5390 */
5391 ret = h2s_htx_make_trailers(h2s, htx);
5392 if (ret > 0) {
5393 total += ret;
5394 count -= ret;
5395 if (ret < bsize)
5396 goto done;
5397 }
5398 break;
5399
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005400 default:
5401 htx_remove_blk(htx, blk);
5402 total += bsize;
5403 count -= bsize;
5404 break;
5405 }
5406 }
5407 goto done;
5408 }
5409
5410 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005411 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005412 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005413 if (h2s->h2c->flags & H2_CF_IS_BACK)
5414 ret = -1;
5415 else
5416 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005417 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005418 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005419 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005420 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005421 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005422 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005423 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005424
Willy Tarreau5dd17352018-06-14 13:33:30 +02005425 if (unlikely((int)ret <= 0)) {
5426 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005427 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5428 break;
5429 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005430 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005431 total += count;
5432 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005433 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005434 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005435 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005436 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005437 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005438 break;
5439 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005440
5441 total += ret;
5442 count -= ret;
5443
5444 if (h2s->st >= H2_SS_ERROR)
5445 break;
5446
5447 if (h2s->flags & H2_SF_BLK_ANY)
5448 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005449 }
5450
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005451 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005452 if (h2s->st >= H2_SS_ERROR) {
5453 /* trim any possibly pending data after we close (extra CR-LF,
5454 * unprocessed trailers, abnormal extra data, ...)
5455 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005456 total += count;
5457 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005458 }
5459
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005460 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005461 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005462 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005463 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005464 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005465 }
5466
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005467 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005468 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005469 } else {
5470 b_del(buf, total);
5471 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005472
5473 /* The mux is full, cancel the pending tasks */
5474 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5475 (h2s->flags & H2_SF_BLK_MBUSY))
5476 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005477
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005478 /* If we're running HTX, and we read the whole buffer, then pretend
5479 * we read exactly what the caller specified, as with HTX the caller
5480 * will always give the buffer size, instead of the amount of data
5481 * available.
5482 */
5483 if (htx && !b_data(buf))
5484 total = orig_count;
5485
Olivier Houchard7505f942018-08-21 18:10:44 +02005486 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005487 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005488 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005489
Olivier Houchard7505f942018-08-21 18:10:44 +02005490 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005491 /* If we're waiting for flow control, and we got a shutr on the
5492 * connection, we will never be unlocked, so add an error on
5493 * the conn_stream.
5494 */
5495 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5496 !b_data(&h2s->h2c->dbuf) &&
5497 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5498 if (cs->flags & CS_FL_EOS)
5499 cs->flags |= CS_FL_ERROR;
5500 else
5501 cs->flags |= CS_FL_ERR_PENDING;
5502 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01005503 if (total > 0 && h2s->send_wait) {
5504 /* Ok we managed to send something, leave the send_list */
5505 h2s->send_wait->events &= ~SUB_RETRY_SEND;
5506 h2s->send_wait = NULL;
5507 LIST_DEL_INIT(&h2s->list);
5508 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005509 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005510}
5511
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005512/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005513static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005514{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005515 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005516 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005517 struct eb32_node *node;
5518 int fctl_cnt = 0;
5519 int send_cnt = 0;
5520 int tree_cnt = 0;
5521 int orph_cnt = 0;
5522
5523 if (!h2c)
5524 return;
5525
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005526 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005527 fctl_cnt++;
5528
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005529 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005530 send_cnt++;
5531
Willy Tarreau3af37712018-12-18 14:34:41 +01005532 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005533 node = eb32_first(&h2c->streams_by_id);
5534 while (node) {
5535 h2s = container_of(node, struct h2s, by_id);
5536 tree_cnt++;
5537 if (!h2s->cs)
5538 orph_cnt++;
5539 node = eb32_next(node);
5540 }
5541
Willy Tarreau987c0632018-12-18 10:32:05 +01005542 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5543 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5544 " .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 +02005545 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5546 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005547 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005548 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5549 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5550 h2c->msi,
5551 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5552 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5553
5554 if (h2s) {
5555 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5556 h2s, h2s->id, h2s->flags,
5557 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5558 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5559 h2s->cs);
5560 if (h2s->cs)
5561 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5562 h2s->cs->flags, h2s->cs->data);
5563 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005564}
Willy Tarreau62f52692017-10-08 23:01:42 +02005565
5566/*******************************************************/
5567/* functions below are dedicated to the config parsers */
5568/*******************************************************/
5569
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005570/* config parser for global "tune.h2.header-table-size" */
5571static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5572 struct proxy *defpx, const char *file, int line,
5573 char **err)
5574{
5575 if (too_many_args(1, args, err, NULL))
5576 return -1;
5577
5578 h2_settings_header_table_size = atoi(args[1]);
5579 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5580 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5581 return -1;
5582 }
5583 return 0;
5584}
Willy Tarreau62f52692017-10-08 23:01:42 +02005585
Willy Tarreaue6baec02017-07-27 11:45:11 +02005586/* config parser for global "tune.h2.initial-window-size" */
5587static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5588 struct proxy *defpx, const char *file, int line,
5589 char **err)
5590{
5591 if (too_many_args(1, args, err, NULL))
5592 return -1;
5593
5594 h2_settings_initial_window_size = atoi(args[1]);
5595 if (h2_settings_initial_window_size < 0) {
5596 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5597 return -1;
5598 }
5599 return 0;
5600}
5601
Willy Tarreau5242ef82017-07-27 11:47:28 +02005602/* config parser for global "tune.h2.max-concurrent-streams" */
5603static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5604 struct proxy *defpx, const char *file, int line,
5605 char **err)
5606{
5607 if (too_many_args(1, args, err, NULL))
5608 return -1;
5609
5610 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005611 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005612 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5613 return -1;
5614 }
5615 return 0;
5616}
5617
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005618/* config parser for global "tune.h2.max-frame-size" */
5619static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5620 struct proxy *defpx, const char *file, int line,
5621 char **err)
5622{
5623 if (too_many_args(1, args, err, NULL))
5624 return -1;
5625
5626 h2_settings_max_frame_size = atoi(args[1]);
5627 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5628 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5629 return -1;
5630 }
5631 return 0;
5632}
5633
Willy Tarreau62f52692017-10-08 23:01:42 +02005634
5635/****************************************/
5636/* MUX initialization and instanciation */
5637/***************************************/
5638
5639/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005640static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005641 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005642 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005643 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005644 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005645 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005646 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005647 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005648 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005649 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005650 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005651 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005652 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005653 .shutr = h2_shutr,
5654 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005655 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005656 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005657 .name = "H2",
5658};
5659
Christopher Faulet32f61c02018-04-10 14:33:41 +02005660/* PROTO selection : this mux registers PROTO token "h2" */
5661static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005662 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005663
Willy Tarreau0108d902018-11-25 19:14:37 +01005664INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5665
Willy Tarreauf8957272018-10-03 10:25:20 +02005666static struct mux_proto_list mux_proto_h2_htx =
5667 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5668
5669INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5670
Willy Tarreau62f52692017-10-08 23:01:42 +02005671/* config keyword parsers */
5672static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005673 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005674 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005675 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005676 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005677 { 0, NULL, NULL }
5678}};
5679
Willy Tarreau0108d902018-11-25 19:14:37 +01005680INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);