blob: 685d5838550671ada80789e9c36cbd237815df59 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
147/* HTTP/2 stream flags (32 bit), in h2s->flags */
148#define H2_SF_NONE 0x00000000
149#define H2_SF_ES_RCVD 0x00000001
150#define H2_SF_ES_SENT 0x00000002
151
152#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
153#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
154
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200155/* stream flags indicating the reason the stream is blocked */
156#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
157#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
158#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
159#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
160#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
161
Willy Tarreau454f9052017-10-26 19:40:35 +0200162/* stream flags indicating how data is supposed to be sent */
163#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
164#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
165
166/* step we're currently in when sending chunks. This is needed because we may
167 * have to transfer chunks as large as a full buffer so there's no room left
168 * for size nor crlf around.
169 */
170#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
171#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
172#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
173
174#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
175
Willy Tarreau67434202017-11-06 20:20:51 +0100176#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100177#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100178
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100179#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
180
Willy Tarreau18312642017-10-11 07:57:07 +0200181/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
182 * it is being processed in the internal HTTP representation (H1 for now).
183 */
184struct h2s {
185 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100186 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200187 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200188 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200190 int32_t id; /* stream ID */
191 uint32_t flags; /* H2_SF_* */
192 int mws; /* mux window size for this stream */
193 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
194 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200195 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100196 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200197 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200198 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100199 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
200 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100202 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200203};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200204
Willy Tarreauc6405142017-09-21 20:23:50 +0200205/* descriptor for an h2 frame header */
206struct h2_fh {
207 uint32_t len; /* length, host order, 24 bits */
208 uint32_t sid; /* stream id, host order, 31 bits */
209 uint8_t ft; /* frame type */
210 uint8_t ff; /* frame flags */
211};
212
Willy Tarreau8ceae722018-11-26 11:58:30 +0100213/* the h2c connection pool */
214DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
215
216/* the h2s stream pool */
217DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
218
Willy Tarreaudc572362018-12-12 08:08:05 +0100219/* The default connection window size is 65535, it may only be enlarged using
220 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
221 * we'll pretend we already received the difference between the two to send
222 * an equivalent window update to enlarge it to 2G-1.
223 */
224#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
225
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226/* a few settings from the global section */
227static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200228static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100229static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100230static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau2a856182017-05-16 15:20:39 +0200232/* a dmumy closed stream */
233static const struct h2s *h2_closed_stream = &(const struct h2s){
234 .cs = NULL,
235 .h2c = NULL,
236 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100237 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100238 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200239 .id = 0,
240};
241
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100242/* a dmumy closed stream returning a PROTOCOL_ERROR error */
243static const struct h2s *h2_error_stream = &(const struct h2s){
244 .cs = NULL,
245 .h2c = NULL,
246 .st = H2_SS_CLOSED,
247 .errcode = H2_ERR_PROTOCOL_ERROR,
248 .flags = 0,
249 .id = 0,
250};
251
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100252/* a dmumy closed stream returning a REFUSED_STREAM error */
253static const struct h2s *h2_refused_stream = &(const struct h2s){
254 .cs = NULL,
255 .h2c = NULL,
256 .st = H2_SS_CLOSED,
257 .errcode = H2_ERR_REFUSED_STREAM,
258 .flags = 0,
259 .id = 0,
260};
261
Willy Tarreau2a856182017-05-16 15:20:39 +0200262/* and a dummy idle stream for use with any unannounced stream */
263static const struct h2s *h2_idle_stream = &(const struct h2s){
264 .cs = NULL,
265 .h2c = NULL,
266 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100267 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200268 .id = 0,
269};
270
Olivier Houchard9f6af332018-05-25 14:04:04 +0200271static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200272static int h2_send(struct h2c *h2c);
273static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200274static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200275static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100276static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100277static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100278static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200279static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100280static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100281static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200282
Olivier Houchard7a977432019-03-21 15:47:13 +0100283static __inline int
284h2c_is_dead(struct h2c *h2c)
285{
286 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
287 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
288 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
289 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
290 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
291 (conn_xprt_read0_pending(h2c->conn) ||
292 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
293 return 1;
294
295 return 0;
296
297}
298
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200299/*****************************************************/
300/* functions below are for dynamic buffer management */
301/*****************************************************/
302
Willy Tarreau315d8072017-12-10 22:17:57 +0100303/* indicates whether or not the we may call the h2_recv() function to attempt
304 * to receive data into the buffer and/or demux pending data. The condition is
305 * a bit complex due to some API limits for now. The rules are the following :
306 * - if an error or a shutdown was detected on the connection and the buffer
307 * is empty, we must not attempt to receive
308 * - if the demux buf failed to be allocated, we must not try to receive and
309 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100310 * - if no flag indicates a blocking condition, we may attempt to receive,
311 * regardless of whether the demux buffer is full or not, so that only
312 * de demux part decides whether or not to block. This is needed because
313 * the connection API indeed prevents us from re-enabling receipt that is
314 * already enabled in a polled state, so we must always immediately stop
315 * as soon as the demux can't proceed so as never to hit an end of read
316 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100317 * - otherwise must may not attempt
318 */
319static inline int h2_recv_allowed(const struct h2c *h2c)
320{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100322 (h2c->st0 >= H2_CS_ERROR ||
323 h2c->conn->flags & CO_FL_ERROR ||
324 conn_xprt_read0_pending(h2c->conn)))
325 return 0;
326
327 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100328 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100329 return 1;
330
331 return 0;
332}
333
Willy Tarreau47b515a2018-12-21 16:09:41 +0100334/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200335static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100336{
337 if (!h2_recv_allowed(h2c))
338 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200339 if ((!consider_buffer || !b_data(&h2c->dbuf))
340 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100341 return;
342 tasklet_wakeup(h2c->wait_event.task);
343}
344
345
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100346/* returns true if the front connection has too many conn_streams attached */
347static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200348{
Willy Tarreaua8754662018-12-23 20:43:58 +0100349 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200350}
351
Willy Tarreau44e973f2018-03-01 17:49:30 +0100352/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
353 * flags are used to figure what buffer was requested. It returns 1 if the
354 * allocation succeeds, in which case the connection is woken up, or 0 if it's
355 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200356 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100357static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200358{
359 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100360 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200361
Willy Tarreau44e973f2018-03-01 17:49:30 +0100362 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200363 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200364 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200365 return 1;
366 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200367
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
369 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200370
371 if (h2c->flags & H2_CF_DEM_MROOM) {
372 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200373 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200374 }
Willy Tarreau14398122017-09-22 14:26:04 +0200375 return 1;
376 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100377
378 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
379 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200380 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100381 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200382 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100383 return 1;
384 }
385
Willy Tarreau14398122017-09-22 14:26:04 +0200386 return 0;
387}
388
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200389static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200390{
391 struct buffer *buf = NULL;
392
Willy Tarreau44e973f2018-03-01 17:49:30 +0100393 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
394 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
395 h2c->buf_wait.target = h2c;
396 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100397 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100398 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100399 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200400 __conn_xprt_stop_recv(h2c->conn);
401 }
402 return buf;
403}
404
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200405static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200406{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200407 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100408 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200409 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200410 }
411}
412
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100413/* returns the number of allocatable outgoing streams for the connection taking
414 * the last_sid and the reserved ones into account.
415 */
416static inline int h2_streams_left(const struct h2c *h2c)
417{
418 int ret;
419
420 /* consider the number of outgoing streams we're allowed to create before
421 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
422 * nb_reserved is the number of streams which don't yet have an ID.
423 */
424 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
425 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
426 if (ret < 0)
427 ret = 0;
428 return ret;
429}
430
Willy Tarreau00f18a32019-01-26 12:19:01 +0100431/* returns the number of streams in use on a connection to figure if it's
432 * idle or not. We check nb_cs and not nb_streams as the caller will want
433 * to know if it was the last one after a detach().
434 */
435static int h2_used_streams(struct connection *conn)
436{
437 struct h2c *h2c = conn->ctx;
438
439 return h2c->nb_cs;
440}
441
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100442/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100443static int h2_avail_streams(struct connection *conn)
444{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100445 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100446 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100447 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100448
Willy Tarreau6afec462019-01-28 06:40:19 +0100449 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
450 * streams on the connection.
451 */
452 if (h2c->last_sid >= 0)
453 return 0;
454
Willy Tarreau86949782019-01-31 10:42:05 +0100455 /* note: may be negative if a SETTINGS frame changes the limit */
456 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100457
458 /* we must also consider the limit imposed by stream IDs */
459 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100460 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100461 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100462 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
463 ret1 = MIN(ret1, ret2);
464 }
465 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100466}
467
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200468
Willy Tarreau62f52692017-10-08 23:01:42 +0200469/*****************************************************************/
470/* functions below are dedicated to the mux setup and management */
471/*****************************************************************/
472
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200473/* Initialize the mux once it's attached. For outgoing connections, the context
474 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200475 * connections from the fact that the context is still NULL (even during mux
476 * upgrades). <input> is always used as Input buffer and may contain data. It is
477 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200478 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200479static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
480 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481{
482 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100483 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200484
Willy Tarreaubafbe012017-11-24 17:34:44 +0100485 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200486 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200487 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200488
Christopher Faulete9b70722019-04-08 10:46:02 +0200489 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200490 h2c->flags = H2_CF_IS_BACK;
491 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
492 if (tick_isset(prx->timeout.serverfin))
493 h2c->shut_timeout = prx->timeout.serverfin;
494 } else {
495 h2c->flags = H2_CF_NONE;
496 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
497 if (tick_isset(prx->timeout.clientfin))
498 h2c->shut_timeout = prx->timeout.clientfin;
499 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100500
Willy Tarreau0b37d652018-10-03 10:33:02 +0200501 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100502 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100503 if (tick_isset(h2c->timeout)) {
504 t = task_new(tid_bit);
505 if (!t)
506 goto fail;
507
508 h2c->task = t;
509 t->process = h2_timeout_task;
510 t->context = h2c;
511 t->expire = tick_add(now_ms, h2c->timeout);
512 }
Willy Tarreauea392822017-10-31 10:02:25 +0100513
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200514 h2c->wait_event.task = tasklet_new();
515 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200516 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200517 h2c->wait_event.task->process = h2_io_cb;
518 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100519 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200520
Willy Tarreau32218eb2017-09-22 08:07:25 +0200521 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
522 if (!h2c->ddht)
523 goto fail;
524
525 /* Initialise the context. */
526 h2c->st0 = H2_CS_PREFACE;
527 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100528 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 h2c->max_id = -1;
530 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100531 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200532 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100533 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200534 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100535 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100536 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200537
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200538 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539 h2c->dsi = -1;
540 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100541
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 h2c->last_sid = -1;
543
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200544 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200545 h2c->miw = 65535; /* mux initial window size */
546 h2c->mws = 65535; /* mux window size */
547 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200548 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200549 LIST_INIT(&h2c->send_list);
550 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200551 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100552 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553
Willy Tarreau3f133572017-10-31 19:21:06 +0100554 if (t)
555 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100556
Willy Tarreau01b44822018-10-03 14:26:37 +0200557 if (h2c->flags & H2_CF_IS_BACK) {
558 /* FIXME: this is temporary, for outgoing connections we need
559 * to immediately allocate a stream until the code is modified
560 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100561 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200562 */
563 struct h2s *h2s;
564
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100565 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200566 if (!h2s)
567 goto fail_stream;
568 }
569
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100570 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200571
Willy Tarreau0f383582018-10-03 14:22:21 +0200572 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200573 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200574 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200575 fail_stream:
576 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200577 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100578 if (t)
579 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200580 if (h2c->wait_event.task)
581 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100582 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200583 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200584 return -1;
585}
586
Willy Tarreau751f2d02018-10-05 09:35:00 +0200587/* returns the next allocatable outgoing stream ID for the H2 connection, or
588 * -1 if no more is allocatable.
589 */
590static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
591{
592 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100593
594 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200595 id = -1;
596 return id;
597}
598
Willy Tarreau2373acc2017-10-12 17:35:14 +0200599/* returns the stream associated with id <id> or NULL if not found */
600static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
601{
602 struct eb32_node *node;
603
Willy Tarreau751f2d02018-10-05 09:35:00 +0200604 if (id == 0)
605 return (struct h2s *)h2_closed_stream;
606
Willy Tarreau2a856182017-05-16 15:20:39 +0200607 if (id > h2c->max_id)
608 return (struct h2s *)h2_idle_stream;
609
Willy Tarreau2373acc2017-10-12 17:35:14 +0200610 node = eb32_lookup(&h2c->streams_by_id, id);
611 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200612 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200613
614 return container_of(node, struct h2s, by_id);
615}
616
Christopher Faulet73c12072019-04-08 11:23:22 +0200617/* release function. This one should be called to free all resources allocated
618 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200619 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200620static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200621{
Christopher Faulet61840e72019-04-15 09:33:32 +0200622 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200623
Willy Tarreau32218eb2017-09-22 08:07:25 +0200624 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200625 /* The connection must be aattached to this mux to be released */
626 if (h2c->conn && h2c->conn->ctx == h2c)
627 conn = h2c->conn;
628
Willy Tarreau32218eb2017-09-22 08:07:25 +0200629 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200630
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100631 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100632 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100633 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200634
Willy Tarreau44e973f2018-03-01 17:49:30 +0100635 h2_release_buf(h2c, &h2c->dbuf);
636 h2_release_buf(h2c, &h2c->mbuf);
637
Willy Tarreauea392822017-10-31 10:02:25 +0100638 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200639 h2c->task->context = NULL;
640 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100641 h2c->task = NULL;
642 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200643 if (h2c->wait_event.task)
644 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200645 if (h2c->wait_event.events != 0)
646 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
647 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100648
Willy Tarreaubafbe012017-11-24 17:34:44 +0100649 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200650 }
651
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200652 if (conn) {
653 conn->mux = NULL;
654 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200655
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200656 conn_stop_tracking(conn);
657 conn_full_close(conn);
658 if (conn->destroy_cb)
659 conn->destroy_cb(conn);
660 conn_free(conn);
661 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200662}
663
664
Willy Tarreau71681172017-10-23 14:39:06 +0200665/******************************************************/
666/* functions below are for the H2 protocol processing */
667/******************************************************/
668
669/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100670static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200671{
672 return h2s ? h2s->id : 0;
673}
674
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200675/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100676static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200677{
678 if (h2c->msi < 0)
679 return 0;
680
681 if (h2c->msi == h2s_id(h2s))
682 return 0;
683
684 return 1;
685}
686
Willy Tarreau741d6df2017-10-17 08:00:59 +0200687/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100688static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200689{
690 h2c->errcode = err;
691 h2c->st0 = H2_CS_ERROR;
692}
693
Willy Tarreau175cebb2019-01-24 10:02:24 +0100694/* marks an error on the stream. It may also update an already closed stream
695 * (e.g. to report an error after an RST was received).
696 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100697static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200698{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100699 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200700 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100701 if (h2s->st < H2_SS_ERROR)
702 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100703 if (h2s->cs)
704 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200705 }
706}
707
Willy Tarreau7e094452018-12-19 18:08:52 +0100708/* attempt to notify the data layer of recv availability */
709static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
710{
711 struct wait_event *sw;
712
713 if (h2s->recv_wait) {
714 sw = h2s->recv_wait;
715 sw->events &= ~SUB_RETRY_RECV;
716 tasklet_wakeup(sw->task);
717 h2s->recv_wait = NULL;
718 }
719}
720
721/* attempt to notify the data layer of send availability */
722static void __maybe_unused h2s_notify_send(struct h2s *h2s)
723{
724 struct wait_event *sw;
725
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100726 if (h2s->send_wait && !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100727 sw = h2s->send_wait;
728 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100729 sw->events |= SUB_CALL_UNSUBSCRIBE;
730 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100731 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100732 }
733}
734
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100735/* alerts the data layer, trying to wake it up by all means, following
736 * this sequence :
737 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
738 * - if its subscribed to send, then it's woken up for send
739 * - if it was subscribed to neither, its ->wake() callback is called
740 * It is safe to call this function with a closed stream which doesn't have a
741 * conn_stream anymore.
742 */
743static void __maybe_unused h2s_alert(struct h2s *h2s)
744{
745 if (h2s->recv_wait || h2s->send_wait) {
746 h2s_notify_recv(h2s);
747 h2s_notify_send(h2s);
748 }
749 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
750 h2s->cs->data_cb->wake(h2s->cs);
751}
752
Willy Tarreaue4820742017-07-27 13:37:23 +0200753/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100754static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200755{
756 uint8_t *out = frame;
757
758 *out = len >> 16;
759 write_n16(out + 1, len);
760}
761
Willy Tarreau54c15062017-10-10 17:10:03 +0200762/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
763 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
764 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200765 * available in the buffer's input prior to calling this function. The buffer
766 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200767 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100768static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200769 const struct buffer *b, int o)
770{
Willy Tarreau591d4452018-06-15 17:21:00 +0200771 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 +0200772}
773
Willy Tarreau1f094672017-11-20 21:27:45 +0100774static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200775{
Willy Tarreau591d4452018-06-15 17:21:00 +0200776 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200777}
778
Willy Tarreau1f094672017-11-20 21:27:45 +0100779static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200780{
Willy Tarreau591d4452018-06-15 17:21:00 +0200781 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200782}
783
Willy Tarreau1f094672017-11-20 21:27:45 +0100784static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200785{
Willy Tarreau591d4452018-06-15 17:21:00 +0200786 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200787}
788
789
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100790/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
791 * The algorithm is not obvious. It turns out that H2 headers are neither
792 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
793 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200794 *
795 * b0 b1 b2 b3 b4 b5..b8
796 * +----------+---------+--------+----+----+----------------------+
797 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
798 * +----------+---------+--------+----+----+----------------------+
799 *
800 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
801 * we get the sid properly aligned and ordered, and 16 bits of len properly
802 * ordered as well. The type and flags can be extracted using bit shifts from
803 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200804 * Returns zero if some bytes are missing, otherwise non-zero on success. The
805 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200806 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100807static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200808{
809 uint64_t w;
810
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100811 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200812 return 0;
813
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100814 w = h2_get_n64(b, o + 1);
815 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200816 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
817 h->ff = w >> 32;
818 h->ft = w >> 40;
819 h->len += w >> 48;
820 return 1;
821}
822
823/* skip the next 9 bytes corresponding to the frame header possibly parsed by
824 * h2_peek_frame_hdr() above.
825 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100826static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200827{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200828 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200829}
830
831/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100832static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200833{
834 int ret;
835
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100836 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200837 if (ret > 0)
838 h2_skip_frame_hdr(b);
839 return ret;
840}
841
Willy Tarreau00dd0782018-03-01 16:31:34 +0100842/* marks stream <h2s> as CLOSED and decrement the number of active streams for
843 * its connection if the stream was not yet closed. Please use this exclusively
844 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100845 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100846static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100847{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100848 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100849 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100850 if (!h2s->id)
851 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100852 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100853 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100854 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
855 h2s_notify_recv(h2s);
856 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100857 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100858 h2s->st = H2_SS_CLOSED;
859}
860
Willy Tarreau71049cc2018-03-28 13:56:39 +0200861/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
862static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100863{
864 h2s_close(h2s);
865 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200866 if (b_size(&h2s->rxbuf)) {
867 b_free(&h2s->rxbuf);
868 offer_buffers(NULL, tasks_run_queue);
869 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200870 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100871 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200872 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100873 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800874 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200875 * reference left would be in the h2c send_list/fctl_list, and if
876 * we're in it, we're getting out anyway
877 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100878 LIST_DEL_INIT(&h2s->list);
879 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200880 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100881 pool_free(pool_head_h2s, h2s);
882}
883
Willy Tarreaua8e49542018-10-03 18:53:55 +0200884/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
885 * stream tree. In case of error, nothing is added and NULL is returned. The
886 * causes of errors can be any failed memory allocation. The caller is
887 * responsible for checking if the connection may support an extra stream
888 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200889 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200890static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200891{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200892 struct h2s *h2s;
893
Willy Tarreaubafbe012017-11-24 17:34:44 +0100894 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200895 if (!h2s)
896 goto out;
897
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200898 h2s->wait_event.task = tasklet_new();
899 if (!h2s->wait_event.task) {
900 pool_free(pool_head_h2s, h2s);
901 goto out;
902 }
903 h2s->send_wait = NULL;
904 h2s->recv_wait = NULL;
905 h2s->wait_event.task->process = h2_deferred_shut;
906 h2s->wait_event.task->context = h2s;
907 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100908 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200909 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100910 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200911 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200912 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200913 h2s->mws = h2c->miw;
914 h2s->flags = H2_SF_NONE;
915 h2s->errcode = H2_ERR_NO_ERROR;
916 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200917 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100918 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200919 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200920
921 if (h2c->flags & H2_CF_IS_BACK) {
922 h1m_init_req(&h2s->h1m);
923 h2s->h1m.err_pos = -1; // don't care about errors on the request path
924 h2s->h1m.flags |= H1_MF_TOLOWER;
925 } else {
926 h1m_init_res(&h2s->h1m);
927 h2s->h1m.err_pos = -1; // don't care about errors on the response path
928 h2s->h1m.flags |= H1_MF_TOLOWER;
929 }
930
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200931 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200932 if (id > 0)
933 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100934 else
935 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200936
937 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100938 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100939 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200940
941 return h2s;
942
943 out_free_h2s:
944 pool_free(pool_head_h2s, h2s);
945 out:
946 return NULL;
947}
948
949/* creates a new stream <id> on the h2c connection and returns it, or NULL in
950 * case of memory allocation error.
951 */
952static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
953{
954 struct session *sess = h2c->conn->owner;
955 struct conn_stream *cs;
956 struct h2s *h2s;
957
958 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
959 goto out;
960
961 h2s = h2s_new(h2c, id);
962 if (!h2s)
963 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200964
965 cs = cs_new(h2c->conn);
966 if (!cs)
967 goto out_close;
968
Olivier Houchard746fb772018-12-15 19:42:00 +0100969 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200970 h2s->cs = cs;
971 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200972 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200973
974 if (stream_create_from_cs(cs) < 0)
975 goto out_free_cs;
976
Willy Tarreau590a0512018-09-05 11:56:48 +0200977 /* We want the accept date presented to the next stream to be the one
978 * we have now, the handshake time to be null (since the next stream
979 * is not delayed by a handshake), and the idle time to count since
980 * right now.
981 */
982 sess->accept_date = date;
983 sess->tv_accept = now;
984 sess->t_handshake = 0;
985
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200986 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100987 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200988 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200989 return h2s;
990
991 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200992 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200993 cs_free(cs);
994 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200995 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200996 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200997 sess_log(sess);
998 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200999}
1000
Willy Tarreau751f2d02018-10-05 09:35:00 +02001001/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1002 * and returns it, or NULL in case of memory allocation error or if the highest
1003 * possible stream ID was reached.
1004 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001005static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001006{
1007 struct h2s *h2s = NULL;
1008
Willy Tarreau86949782019-01-31 10:42:05 +01001009 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001010 goto out;
1011
Willy Tarreaua80dca82019-01-24 17:08:28 +01001012 if (h2_streams_left(h2c) < 1)
1013 goto out;
1014
Willy Tarreau751f2d02018-10-05 09:35:00 +02001015 /* Defer choosing the ID until we send the first message to create the stream */
1016 h2s = h2s_new(h2c, 0);
1017 if (!h2s)
1018 goto out;
1019
1020 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001021 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001022 cs->ctx = h2s;
1023 h2c->nb_cs++;
1024
Willy Tarreau751f2d02018-10-05 09:35:00 +02001025 out:
1026 return h2s;
1027}
1028
Willy Tarreaube5b7152017-09-25 16:25:39 +02001029/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1030 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1031 * the various settings codes.
1032 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001033static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001034{
1035 struct buffer *res;
1036 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001037 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001038 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001039 int ret;
1040
1041 if (h2c_mux_busy(h2c, NULL)) {
1042 h2c->flags |= H2_CF_DEM_MBUSY;
1043 return 0;
1044 }
1045
Willy Tarreau44e973f2018-03-01 17:49:30 +01001046 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001047 if (!res) {
1048 h2c->flags |= H2_CF_MUX_MALLOC;
1049 h2c->flags |= H2_CF_DEM_MROOM;
1050 return 0;
1051 }
1052
1053 chunk_init(&buf, buf_data, sizeof(buf_data));
1054 chunk_memcpy(&buf,
1055 "\x00\x00\x00" /* length : 0 for now */
1056 "\x04\x00" /* type : 4 (settings), flags : 0 */
1057 "\x00\x00\x00\x00", /* stream ID : 0 */
1058 9);
1059
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001060 if (h2c->flags & H2_CF_IS_BACK) {
1061 /* send settings_enable_push=0 */
1062 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1063 }
1064
Willy Tarreaube5b7152017-09-25 16:25:39 +02001065 if (h2_settings_header_table_size != 4096) {
1066 char str[6] = "\x00\x01"; /* header_table_size */
1067
1068 write_n32(str + 2, h2_settings_header_table_size);
1069 chunk_memcat(&buf, str, 6);
1070 }
1071
1072 if (h2_settings_initial_window_size != 65535) {
1073 char str[6] = "\x00\x04"; /* initial_window_size */
1074
1075 write_n32(str + 2, h2_settings_initial_window_size);
1076 chunk_memcat(&buf, str, 6);
1077 }
1078
1079 if (h2_settings_max_concurrent_streams != 0) {
1080 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1081
1082 /* Note: 0 means "unlimited" for haproxy's config but not for
1083 * the protocol, so never send this value!
1084 */
1085 write_n32(str + 2, h2_settings_max_concurrent_streams);
1086 chunk_memcat(&buf, str, 6);
1087 }
1088
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001089 mfs = h2_settings_max_frame_size;
1090 if (mfs > global.tune.bufsize)
1091 mfs = global.tune.bufsize;
1092
1093 if (!mfs)
1094 mfs = global.tune.bufsize;
1095
1096 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001097 char str[6] = "\x00\x05"; /* max_frame_size */
1098
1099 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1100 * match bufsize - rewrite size, but at the moment it seems
1101 * that clients don't take care of it.
1102 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001103 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001104 chunk_memcat(&buf, str, 6);
1105 }
1106
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001107 h2_set_frame_size(buf.area, buf.data - 9);
1108 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001109 if (unlikely(ret <= 0)) {
1110 if (!ret) {
1111 h2c->flags |= H2_CF_MUX_MFULL;
1112 h2c->flags |= H2_CF_DEM_MROOM;
1113 return 0;
1114 }
1115 else {
1116 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1117 return 0;
1118 }
1119 }
1120 return ret;
1121}
1122
Willy Tarreau52eed752017-09-22 15:05:09 +02001123/* Try to receive a connection preface, then upon success try to send our
1124 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1125 * missing data. It may return an error in h2c.
1126 */
1127static int h2c_frt_recv_preface(struct h2c *h2c)
1128{
1129 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001130 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001131
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001132 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001133
1134 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001135 if (ret1 < 0)
1136 sess_log(h2c->conn->owner);
1137
Willy Tarreau52eed752017-09-22 15:05:09 +02001138 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1139 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1140 return 0;
1141 }
1142
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001143 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001144 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001145 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001146
Willy Tarreaube5b7152017-09-25 16:25:39 +02001147 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001148}
1149
Willy Tarreau01b44822018-10-03 14:26:37 +02001150/* Try to send a connection preface, then upon success try to send our
1151 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1152 * missing data. It may return an error in h2c.
1153 */
1154static int h2c_bck_send_preface(struct h2c *h2c)
1155{
1156 struct buffer *res;
1157
1158 if (h2c_mux_busy(h2c, NULL)) {
1159 h2c->flags |= H2_CF_DEM_MBUSY;
1160 return 0;
1161 }
1162
1163 res = h2_get_buf(h2c, &h2c->mbuf);
1164 if (!res) {
1165 h2c->flags |= H2_CF_MUX_MALLOC;
1166 h2c->flags |= H2_CF_DEM_MROOM;
1167 return 0;
1168 }
1169
1170 if (!b_data(res)) {
1171 /* preface not yet sent */
1172 b_istput(res, ist(H2_CONN_PREFACE));
1173 }
1174
1175 return h2c_send_settings(h2c);
1176}
1177
Willy Tarreau081d4722017-05-16 21:51:05 +02001178/* try to send a GOAWAY frame on the connection to report an error or a graceful
1179 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1180 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1181 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1182 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1183 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1184 * on unrecoverable failure. It will not attempt to send one again in this last
1185 * case so that it is safe to use h2c_error() to report such errors.
1186 */
1187static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1188{
1189 struct buffer *res;
1190 char str[17];
1191 int ret;
1192
1193 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1194 return 1; // claim that it worked
1195
1196 if (h2c_mux_busy(h2c, h2s)) {
1197 if (h2s)
1198 h2s->flags |= H2_SF_BLK_MBUSY;
1199 else
1200 h2c->flags |= H2_CF_DEM_MBUSY;
1201 return 0;
1202 }
1203
Willy Tarreau44e973f2018-03-01 17:49:30 +01001204 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001205 if (!res) {
1206 h2c->flags |= H2_CF_MUX_MALLOC;
1207 if (h2s)
1208 h2s->flags |= H2_SF_BLK_MROOM;
1209 else
1210 h2c->flags |= H2_CF_DEM_MROOM;
1211 return 0;
1212 }
1213
1214 /* len: 8, type: 7, flags: none, sid: 0 */
1215 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1216
1217 if (h2c->last_sid < 0)
1218 h2c->last_sid = h2c->max_id;
1219
1220 write_n32(str + 9, h2c->last_sid);
1221 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001222 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001223 if (unlikely(ret <= 0)) {
1224 if (!ret) {
1225 h2c->flags |= H2_CF_MUX_MFULL;
1226 if (h2s)
1227 h2s->flags |= H2_SF_BLK_MROOM;
1228 else
1229 h2c->flags |= H2_CF_DEM_MROOM;
1230 return 0;
1231 }
1232 else {
1233 /* we cannot report this error using GOAWAY, so we mark
1234 * it and claim a success.
1235 */
1236 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1237 h2c->flags |= H2_CF_GOAWAY_FAILED;
1238 return 1;
1239 }
1240 }
1241 h2c->flags |= H2_CF_GOAWAY_SENT;
1242 return ret;
1243}
1244
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001245/* Try to send an RST_STREAM frame on the connection for the indicated stream
1246 * during mux operations. This stream must be valid and cannot be closed
1247 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1248 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1249 * not yet.
1250 *
1251 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1252 * to write the message, it subscribes the stream to future notifications.
1253 */
1254static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1255{
1256 struct buffer *res;
1257 char str[13];
1258 int ret;
1259
1260 if (!h2s || h2s->st == H2_SS_CLOSED)
1261 return 1;
1262
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001263 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1264 * RST_STREAM in response to a RST_STREAM frame.
1265 */
1266 if (h2c->dft == H2_FT_RST_STREAM) {
1267 ret = 1;
1268 goto ignore;
1269 }
1270
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001271 if (h2c_mux_busy(h2c, h2s)) {
1272 h2s->flags |= H2_SF_BLK_MBUSY;
1273 return 0;
1274 }
1275
Willy Tarreau44e973f2018-03-01 17:49:30 +01001276 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001277 if (!res) {
1278 h2c->flags |= H2_CF_MUX_MALLOC;
1279 h2s->flags |= H2_SF_BLK_MROOM;
1280 return 0;
1281 }
1282
1283 /* len: 4, type: 3, flags: none */
1284 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1285 write_n32(str + 5, h2s->id);
1286 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001287 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001288
1289 if (unlikely(ret <= 0)) {
1290 if (!ret) {
1291 h2c->flags |= H2_CF_MUX_MFULL;
1292 h2s->flags |= H2_SF_BLK_MROOM;
1293 return 0;
1294 }
1295 else {
1296 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1297 return 0;
1298 }
1299 }
1300
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001301 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001302 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001303 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001304 return ret;
1305}
1306
1307/* Try to send an RST_STREAM frame on the connection for the stream being
1308 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001309 * error code, even if the stream is one of the dummy ones, and will update
1310 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001311 *
1312 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1313 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001314 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001315 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001316 */
1317static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1318{
1319 struct buffer *res;
1320 char str[13];
1321 int ret;
1322
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001323 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1324 * RST_STREAM in response to a RST_STREAM frame.
1325 */
1326 if (h2c->dft == H2_FT_RST_STREAM) {
1327 ret = 1;
1328 goto ignore;
1329 }
1330
Willy Tarreau27a84c92017-10-17 08:10:17 +02001331 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001332 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001333 return 0;
1334 }
1335
Willy Tarreau44e973f2018-03-01 17:49:30 +01001336 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001337 if (!res) {
1338 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001339 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001340 return 0;
1341 }
1342
1343 /* len: 4, type: 3, flags: none */
1344 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001345
Willy Tarreau27a84c92017-10-17 08:10:17 +02001346 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001347 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001348 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001349
Willy Tarreau27a84c92017-10-17 08:10:17 +02001350 if (unlikely(ret <= 0)) {
1351 if (!ret) {
1352 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001353 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001354 return 0;
1355 }
1356 else {
1357 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1358 return 0;
1359 }
1360 }
1361
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001362 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001363 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001364 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001365 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001366 }
1367
Willy Tarreau27a84c92017-10-17 08:10:17 +02001368 return ret;
1369}
1370
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001371/* try to send an empty DATA frame with the ES flag set to notify about the
1372 * end of stream and match a shutdown(write). If an ES was already sent as
1373 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1374 * on success or zero if nothing was done. In case of lack of room to write the
1375 * message, it subscribes the requesting stream to future notifications.
1376 */
1377static int h2_send_empty_data_es(struct h2s *h2s)
1378{
1379 struct h2c *h2c = h2s->h2c;
1380 struct buffer *res;
1381 char str[9];
1382 int ret;
1383
Willy Tarreau721c9742017-11-07 11:05:42 +01001384 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001385 return 1;
1386
1387 if (h2c_mux_busy(h2c, h2s)) {
1388 h2s->flags |= H2_SF_BLK_MBUSY;
1389 return 0;
1390 }
1391
Willy Tarreau44e973f2018-03-01 17:49:30 +01001392 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001393 if (!res) {
1394 h2c->flags |= H2_CF_MUX_MALLOC;
1395 h2s->flags |= H2_SF_BLK_MROOM;
1396 return 0;
1397 }
1398
1399 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1400 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1401 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001402 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001403 if (likely(ret > 0)) {
1404 h2s->flags |= H2_SF_ES_SENT;
1405 }
1406 else if (!ret) {
1407 h2c->flags |= H2_CF_MUX_MFULL;
1408 h2s->flags |= H2_SF_BLK_MROOM;
1409 return 0;
1410 }
1411 else {
1412 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1413 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001414 }
1415 return ret;
1416}
1417
Christopher Fauletf02ca002019-03-07 16:21:34 +01001418/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1419 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1420 * connection. The stream's state is automatically updated accordingly. If the
1421 * stream is orphaned, it is destroyed.
1422 */
1423static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1424{
1425 if (!h2s->cs) {
1426 /* this stream was already orphaned */
1427 h2s_destroy(h2s);
1428 return;
1429 }
1430
1431 h2s->cs->flags |= flags;
1432 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1433 h2s->cs->flags |= CS_FL_ERROR;
1434
1435 h2s_alert(h2s);
1436
1437 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1438 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001439 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001440 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001441 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001442 h2s_close(h2s);
1443}
1444
1445/* wake the streams attached to the connection, whose id is greater than <last>
1446 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001447 */
1448static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1449{
1450 struct eb32_node *node;
1451 struct h2s *h2s;
1452
1453 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001454 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001455
1456 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001457 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001458
Christopher Fauletf02ca002019-03-07 16:21:34 +01001459 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001460 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1461 while (node) {
1462 h2s = container_of(node, struct h2s, by_id);
1463 if (h2s->id <= last)
1464 break;
1465 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001466 h2s_wake_one_stream(h2s, flags);
1467 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001468
Christopher Fauletf02ca002019-03-07 16:21:34 +01001469 /* Wake all streams with unassigned ID (ID == 0) */
1470 node = eb32_lookup(&h2c->streams_by_id, 0);
1471 while (node) {
1472 h2s = container_of(node, struct h2s, by_id);
1473 if (h2s->id > 0)
1474 break;
1475 node = eb32_next(node);
1476 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001477 }
1478}
1479
Willy Tarreau3421aba2017-07-27 15:41:03 +02001480/* Increase all streams' outgoing window size by the difference passed in
1481 * argument. This is needed upon receipt of the settings frame if the initial
1482 * window size is different. The difference may be negative and the resulting
1483 * window size as well, for the time it takes to receive some window updates.
1484 */
1485static void h2c_update_all_ws(struct h2c *h2c, int diff)
1486{
1487 struct h2s *h2s;
1488 struct eb32_node *node;
1489
1490 if (!diff)
1491 return;
1492
1493 node = eb32_first(&h2c->streams_by_id);
1494 while (node) {
1495 h2s = container_of(node, struct h2s, by_id);
1496 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001497
1498 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1499 h2s->flags &= ~H2_SF_BLK_SFCTL;
1500 if (h2s->send_wait)
1501 LIST_ADDQ(&h2c->send_list, &h2s->list);
1502
1503 }
1504
Willy Tarreau3421aba2017-07-27 15:41:03 +02001505 node = eb32_next(node);
1506 }
1507}
1508
1509/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1510 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001511 * return an error in h2c. The caller must have already verified frame length
1512 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001513 */
1514static int h2c_handle_settings(struct h2c *h2c)
1515{
1516 unsigned int offset;
1517 int error;
1518
1519 if (h2c->dff & H2_F_SETTINGS_ACK) {
1520 if (h2c->dfl) {
1521 error = H2_ERR_FRAME_SIZE_ERROR;
1522 goto fail;
1523 }
1524 return 1;
1525 }
1526
Willy Tarreau3421aba2017-07-27 15:41:03 +02001527 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001528 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001529 return 0;
1530
1531 /* parse the frame */
1532 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001533 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1534 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001535
1536 switch (type) {
1537 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1538 /* we need to update all existing streams with the
1539 * difference from the previous iws.
1540 */
1541 if (arg < 0) { // RFC7540#6.5.2
1542 error = H2_ERR_FLOW_CONTROL_ERROR;
1543 goto fail;
1544 }
1545 h2c_update_all_ws(h2c, arg - h2c->miw);
1546 h2c->miw = arg;
1547 break;
1548 case H2_SETTINGS_MAX_FRAME_SIZE:
1549 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1550 error = H2_ERR_PROTOCOL_ERROR;
1551 goto fail;
1552 }
1553 h2c->mfs = arg;
1554 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001555 case H2_SETTINGS_ENABLE_PUSH:
1556 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1557 error = H2_ERR_PROTOCOL_ERROR;
1558 goto fail;
1559 }
1560 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001561 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1562 if (h2c->flags & H2_CF_IS_BACK) {
1563 /* the limit is only for the backend; for the frontend it is our limit */
1564 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1565 arg = h2_settings_max_concurrent_streams;
1566 h2c->streams_limit = arg;
1567 }
1568 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001569 }
1570 }
1571
1572 /* need to ACK this frame now */
1573 h2c->st0 = H2_CS_FRAME_A;
1574 return 1;
1575 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001576 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001577 h2c_error(h2c, error);
1578 return 0;
1579}
1580
1581/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1582 * success or one of the h2_status values.
1583 */
1584static int h2c_ack_settings(struct h2c *h2c)
1585{
1586 struct buffer *res;
1587 char str[9];
1588 int ret = -1;
1589
1590 if (h2c_mux_busy(h2c, NULL)) {
1591 h2c->flags |= H2_CF_DEM_MBUSY;
1592 return 0;
1593 }
1594
Willy Tarreau44e973f2018-03-01 17:49:30 +01001595 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001596 if (!res) {
1597 h2c->flags |= H2_CF_MUX_MALLOC;
1598 h2c->flags |= H2_CF_DEM_MROOM;
1599 return 0;
1600 }
1601
1602 memcpy(str,
1603 "\x00\x00\x00" /* length : 0 (no data) */
1604 "\x04" "\x01" /* type : 4, flags : ACK */
1605 "\x00\x00\x00\x00" /* stream ID */, 9);
1606
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001607 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001608 if (unlikely(ret <= 0)) {
1609 if (!ret) {
1610 h2c->flags |= H2_CF_MUX_MFULL;
1611 h2c->flags |= H2_CF_DEM_MROOM;
1612 return 0;
1613 }
1614 else {
1615 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1616 return 0;
1617 }
1618 }
1619 return ret;
1620}
1621
Willy Tarreaucf68c782017-10-10 17:11:41 +02001622/* processes a PING frame and schedules an ACK if needed. The caller must pass
1623 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001624 * missing data. The caller must have already verified frame length
1625 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001626 */
1627static int h2c_handle_ping(struct h2c *h2c)
1628{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001629 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001630 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001631 h2c->st0 = H2_CS_FRAME_A;
1632 return 1;
1633}
1634
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001635/* Try to send a window update for stream id <sid> and value <increment>.
1636 * Returns > 0 on success or zero on missing room or failure. It may return an
1637 * error in h2c.
1638 */
1639static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1640{
1641 struct buffer *res;
1642 char str[13];
1643 int ret = -1;
1644
1645 if (h2c_mux_busy(h2c, NULL)) {
1646 h2c->flags |= H2_CF_DEM_MBUSY;
1647 return 0;
1648 }
1649
Willy Tarreau44e973f2018-03-01 17:49:30 +01001650 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001651 if (!res) {
1652 h2c->flags |= H2_CF_MUX_MALLOC;
1653 h2c->flags |= H2_CF_DEM_MROOM;
1654 return 0;
1655 }
1656
1657 /* length: 4, type: 8, flags: none */
1658 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1659 write_n32(str + 5, sid);
1660 write_n32(str + 9, increment);
1661
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001662 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001663
1664 if (unlikely(ret <= 0)) {
1665 if (!ret) {
1666 h2c->flags |= H2_CF_MUX_MFULL;
1667 h2c->flags |= H2_CF_DEM_MROOM;
1668 return 0;
1669 }
1670 else {
1671 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1672 return 0;
1673 }
1674 }
1675 return ret;
1676}
1677
1678/* try to send pending window update for the connection. It's safe to call it
1679 * with no pending updates. Returns > 0 on success or zero on missing room or
1680 * failure. It may return an error in h2c.
1681 */
1682static int h2c_send_conn_wu(struct h2c *h2c)
1683{
1684 int ret = 1;
1685
1686 if (h2c->rcvd_c <= 0)
1687 return 1;
1688
Willy Tarreau97aaa672018-12-23 09:49:04 +01001689 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1690 /* increase the advertised connection window to 2G on
1691 * first update.
1692 */
1693 h2c->flags |= H2_CF_WINDOW_OPENED;
1694 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1695 }
1696
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001697 /* send WU for the connection */
1698 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1699 if (ret > 0)
1700 h2c->rcvd_c = 0;
1701
1702 return ret;
1703}
1704
1705/* try to send pending window update for the current dmux stream. It's safe to
1706 * call it with no pending updates. Returns > 0 on success or zero on missing
1707 * room or failure. It may return an error in h2c.
1708 */
1709static int h2c_send_strm_wu(struct h2c *h2c)
1710{
1711 int ret = 1;
1712
1713 if (h2c->rcvd_s <= 0)
1714 return 1;
1715
1716 /* send WU for the stream */
1717 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1718 if (ret > 0)
1719 h2c->rcvd_s = 0;
1720
1721 return ret;
1722}
1723
Willy Tarreaucf68c782017-10-10 17:11:41 +02001724/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1725 * success, 0 on missing data or one of the h2_status values.
1726 */
1727static int h2c_ack_ping(struct h2c *h2c)
1728{
1729 struct buffer *res;
1730 char str[17];
1731 int ret = -1;
1732
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001733 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001734 return 0;
1735
1736 if (h2c_mux_busy(h2c, NULL)) {
1737 h2c->flags |= H2_CF_DEM_MBUSY;
1738 return 0;
1739 }
1740
Willy Tarreau44e973f2018-03-01 17:49:30 +01001741 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001742 if (!res) {
1743 h2c->flags |= H2_CF_MUX_MALLOC;
1744 h2c->flags |= H2_CF_DEM_MROOM;
1745 return 0;
1746 }
1747
1748 memcpy(str,
1749 "\x00\x00\x08" /* length : 8 (same payload) */
1750 "\x06" "\x01" /* type : 6, flags : ACK */
1751 "\x00\x00\x00\x00" /* stream ID */, 9);
1752
1753 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001754 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001755
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001756 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001757 if (unlikely(ret <= 0)) {
1758 if (!ret) {
1759 h2c->flags |= H2_CF_MUX_MFULL;
1760 h2c->flags |= H2_CF_DEM_MROOM;
1761 return 0;
1762 }
1763 else {
1764 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1765 return 0;
1766 }
1767 }
1768 return ret;
1769}
1770
Willy Tarreau26f95952017-07-27 17:18:30 +02001771/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1772 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001773 * h2c or h2s. The caller must have already verified frame length and stream ID
1774 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001775 */
1776static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1777{
1778 int32_t inc;
1779 int error;
1780
Willy Tarreau26f95952017-07-27 17:18:30 +02001781 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001782 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001783 return 0;
1784
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001785 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001786
1787 if (h2c->dsi != 0) {
1788 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001789
1790 /* it's not an error to receive WU on a closed stream */
1791 if (h2s->st == H2_SS_CLOSED)
1792 return 1;
1793
1794 if (!inc) {
1795 error = H2_ERR_PROTOCOL_ERROR;
1796 goto strm_err;
1797 }
1798
1799 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1800 error = H2_ERR_FLOW_CONTROL_ERROR;
1801 goto strm_err;
1802 }
1803
1804 h2s->mws += inc;
1805 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1806 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001807 if (h2s->send_wait)
1808 LIST_ADDQ(&h2c->send_list, &h2s->list);
1809
Willy Tarreau26f95952017-07-27 17:18:30 +02001810 }
1811 }
1812 else {
1813 /* connection window update */
1814 if (!inc) {
1815 error = H2_ERR_PROTOCOL_ERROR;
1816 goto conn_err;
1817 }
1818
1819 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1820 error = H2_ERR_FLOW_CONTROL_ERROR;
1821 goto conn_err;
1822 }
1823
1824 h2c->mws += inc;
1825 }
1826
1827 return 1;
1828
1829 conn_err:
1830 h2c_error(h2c, error);
1831 return 0;
1832
1833 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001834 h2s_error(h2s, error);
1835 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001836 return 0;
1837}
1838
Willy Tarreaue96b0922017-10-30 00:28:29 +01001839/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001840 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1841 * have already verified frame length and stream ID validity. Described in
1842 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001843 */
1844static int h2c_handle_goaway(struct h2c *h2c)
1845{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001846 int last;
1847
Willy Tarreaue96b0922017-10-30 00:28:29 +01001848 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001849 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001850 return 0;
1851
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001852 last = h2_get_n32(&h2c->dbuf, 0);
1853 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001854 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001855 if (h2c->last_sid < 0)
1856 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001857 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001858}
1859
Willy Tarreau92153fc2017-12-03 19:46:19 +01001860/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001861 * invalid. Returns > 0 on success or zero on missing data. It may return an
1862 * error in h2c. The caller must have already verified frame length and stream
1863 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001864 */
1865static int h2c_handle_priority(struct h2c *h2c)
1866{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001867 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001868 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001869 return 0;
1870
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001871 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001872 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001873 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1874 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001875 }
1876 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001877}
1878
Willy Tarreaucd234e92017-08-18 10:59:39 +02001879/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001880 * Returns > 0 on success or zero on missing data. The caller must have already
1881 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001882 */
1883static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1884{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001885 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001886 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001887 return 0;
1888
1889 /* late RST, already handled */
1890 if (h2s->st == H2_SS_CLOSED)
1891 return 1;
1892
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001893 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001894 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001895
1896 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001897 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001898 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001899 }
1900
1901 h2s->flags |= H2_SF_RST_RCVD;
1902 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001903}
1904
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001905/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1906 * It may return an error in h2c or h2s. The caller must consider that the
1907 * return value is the new h2s in case one was allocated (most common case).
1908 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001909 * errors here are reported as connection errors since it's impossible to
1910 * recover from such errors after the compression context has been altered.
1911 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001912static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001913{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001914 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001915 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001916 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001917 int error;
1918
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001919 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001920 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001921
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001922 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001923 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001924
1925 /* now either the frame is complete or the buffer is complete */
1926 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001927 /* The stream exists/existed, this must be a trailers frame */
1928 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001929 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001930 goto out;
1931 goto done;
1932 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001933 /* the connection was already killed by an RST, let's consume
1934 * the data and send another RST.
1935 */
1936 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1937 h2s = (struct h2s*)h2_error_stream;
1938 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001939 }
1940 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1941 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1942 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001943 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001944 goto conn_err;
1945 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001946 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1947 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001948
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001949 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001950
Willy Tarreau25919232019-01-03 14:48:18 +01001951 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001952 if (h2c->st0 >= H2_CS_ERROR)
1953 goto out;
1954
Willy Tarreau25919232019-01-03 14:48:18 +01001955 if (error <= 0) {
1956 if (error == 0)
1957 goto out; // missing data
1958
1959 /* Failed to decode this stream (e.g. too large request)
1960 * but the HPACK decompressor is still synchronized.
1961 */
1962 h2s = (struct h2s*)h2_error_stream;
1963 goto send_rst;
1964 }
1965
Willy Tarreau22de8d32018-09-05 19:55:58 +02001966 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001967 * positively from h2c_frt_stream_new(), the stream will report the error,
1968 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001969 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001970 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001971 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001972 h2s = (struct h2s*)h2_refused_stream;
1973 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001974 }
1975
1976 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001977 h2s->rxbuf = rxbuf;
1978 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001979 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001980
Willy Tarreau88d138e2019-01-02 19:38:14 +01001981 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001982 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001983 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001984
1985 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001986 if (h2s->cs)
1987 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01001988 if (h2s->st == H2_SS_OPEN)
1989 h2s->st = H2_SS_HREM;
1990 else
1991 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02001992 }
1993
Willy Tarreau3a429f02019-01-03 11:41:50 +01001994 /* update the max stream ID if the request is being processed */
1995 if (h2s->id > h2c->max_id)
1996 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001997
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001998 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001999
2000 conn_err:
2001 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002002 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002003
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002004 out:
2005 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002006 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002007
2008 send_rst:
2009 /* make the demux send an RST for the current stream. We may only
2010 * do this if we're certain that the HEADERS frame was properly
2011 * decompressed so that the HPACK decoder is still kept up to date.
2012 */
2013 h2_release_buf(h2c, &rxbuf);
2014 h2c->st0 = H2_CS_FRAME_E;
2015 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002016}
2017
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002018/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2019 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2020 * errors here are reported as connection errors since it's impossible to
2021 * recover from such errors after the compression context has been altered.
2022 */
2023static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2024{
2025 int error;
2026
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002027 if (!b_size(&h2c->dbuf))
2028 return NULL; // empty buffer
2029
2030 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2031 return NULL; // incomplete frame
2032
Willy Tarreau1915ca22019-01-24 11:49:37 +01002033 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002034
Willy Tarreau25919232019-01-03 14:48:18 +01002035 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002036 if (h2c->st0 >= H2_CS_ERROR)
2037 return NULL;
2038
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002039 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2040 /* RFC7540#5.1 */
2041 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2042 h2c->st0 = H2_CS_FRAME_E;
2043 return NULL;
2044 }
2045
Willy Tarreau25919232019-01-03 14:48:18 +01002046 if (error <= 0) {
2047 if (error == 0)
2048 return NULL; // missing data
2049
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002050 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002051 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002052 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002053 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002054 }
2055
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002056 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2057 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002058 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002059 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002060 }
2061
Willy Tarreau927b88b2019-03-04 08:03:25 +01002062 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002063 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002064 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 +02002065 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002066 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002067 h2s_close(h2s);
2068
2069 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002070}
2071
Willy Tarreau454f9052017-10-26 19:40:35 +02002072/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2073 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2074 */
2075static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2076{
2077 int error;
2078
2079 /* note that empty DATA frames are perfectly valid and sometimes used
2080 * to signal an end of stream (with the ES flag).
2081 */
2082
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002083 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002084 return 0; // empty buffer
2085
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002086 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002087 return 0; // incomplete frame
2088
2089 /* now either the frame is complete or the buffer is complete */
2090
Willy Tarreau454f9052017-10-26 19:40:35 +02002091 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2092 /* RFC7540#6.1 */
2093 error = H2_ERR_STREAM_CLOSED;
2094 goto strm_err;
2095 }
2096
Willy Tarreau1915ca22019-01-24 11:49:37 +01002097 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2098 /* RFC7540#8.1.2 */
2099 error = H2_ERR_PROTOCOL_ERROR;
2100 goto strm_err;
2101 }
2102
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002103 if (!h2_frt_transfer_data(h2s))
2104 return 0;
2105
Willy Tarreau454f9052017-10-26 19:40:35 +02002106 /* call the upper layers to process the frame, then let the upper layer
2107 * notify the stream about any change.
2108 */
2109 if (!h2s->cs) {
2110 error = H2_ERR_STREAM_CLOSED;
2111 goto strm_err;
2112 }
2113
Willy Tarreau8f650c32017-11-21 19:36:21 +01002114 if (h2c->st0 >= H2_CS_ERROR)
2115 return 0;
2116
Willy Tarreau721c9742017-11-07 11:05:42 +01002117 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002118 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002119 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002120 }
2121
2122 /* check for completion : the callee will change this to FRAME_A or
2123 * FRAME_H once done.
2124 */
2125 if (h2c->st0 == H2_CS_FRAME_P)
2126 return 0;
2127
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002128 /* last frame */
2129 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002130 if (h2s->cs)
2131 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002132 if (h2s->st == H2_SS_OPEN)
2133 h2s->st = H2_SS_HREM;
2134 else
2135 h2s_close(h2s);
2136
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002137 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002138
2139 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2140 /* RFC7540#8.1.2 */
2141 error = H2_ERR_PROTOCOL_ERROR;
2142 goto strm_err;
2143 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002144 }
2145
Willy Tarreau454f9052017-10-26 19:40:35 +02002146 return 1;
2147
Willy Tarreau454f9052017-10-26 19:40:35 +02002148 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002149 h2s_error(h2s, error);
2150 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002151 return 0;
2152}
2153
Willy Tarreaubc933932017-10-09 16:21:43 +02002154/* process Rx frames to be demultiplexed */
2155static void h2_process_demux(struct h2c *h2c)
2156{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002157 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002158 struct h2_fh hdr;
2159 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002160
Willy Tarreau081d4722017-05-16 21:51:05 +02002161 if (h2c->st0 >= H2_CS_ERROR)
2162 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002163
2164 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2165 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002166 if (h2c->flags & H2_CF_IS_BACK)
2167 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002168 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2169 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002170 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002171 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002172 sess_log(h2c->conn->owner);
2173 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002174 goto fail;
2175 }
2176
2177 h2c->max_id = 0;
2178 h2c->st0 = H2_CS_SETTINGS1;
2179 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002180
2181 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002182 /* ensure that what is pending is a valid SETTINGS frame
2183 * without an ACK.
2184 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002185 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002186 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002187 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002188 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002189 sess_log(h2c->conn->owner);
2190 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002191 goto fail;
2192 }
2193
2194 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2195 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2196 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2197 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002198 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002199 goto fail;
2200 }
2201
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002202 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002203 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2204 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2205 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002206 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002207 goto fail;
2208 }
2209
Willy Tarreau3bf69182018-12-21 15:34:50 +01002210 /* that's OK, switch to FRAME_P to process it. This is
2211 * a SETTINGS frame whose header has already been
2212 * deleted above.
2213 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002214 padlen = 0;
2215 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002216 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002217 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002218
2219 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002220 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002221 int ret = 0;
2222
2223 if (h2c->st0 >= H2_CS_ERROR)
2224 break;
2225
2226 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002227 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002228 break;
2229
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002230 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002231 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002232 if (!h2c->nb_streams) {
2233 /* only log if no other stream can report the error */
2234 sess_log(h2c->conn->owner);
2235 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002236 break;
2237 }
2238
Willy Tarreau3bf69182018-12-21 15:34:50 +01002239 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2240 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2241 * we read the pad length and drop it from the remaining
2242 * payload (one byte + the 9 remaining ones = 10 total
2243 * removed), so we have a frame payload starting after the
2244 * pad len. Flow controlled frames (DATA) also count the
2245 * padlen in the flow control, so it must be adjusted.
2246 */
2247 if (hdr.len < 1) {
2248 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2249 sess_log(h2c->conn->owner);
2250 goto fail;
2251 }
2252 hdr.len--;
2253
2254 if (b_data(&h2c->dbuf) < 10)
2255 break; // missing padlen
2256
2257 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2258
2259 if (padlen > hdr.len) {
2260 /* RFC7540#6.1 : pad length = length of
2261 * frame payload or greater => error.
2262 */
2263 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2264 sess_log(h2c->conn->owner);
2265 goto fail;
2266 }
2267
2268 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2269 h2c->rcvd_c++;
2270 h2c->rcvd_s++;
2271 }
2272 b_del(&h2c->dbuf, 1);
2273 }
2274 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002275
2276 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002277 h2c->dfl = hdr.len;
2278 h2c->dsi = hdr.sid;
2279 h2c->dft = hdr.ft;
2280 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002281 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002282 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002283
2284 /* check for minimum basic frame format validity */
2285 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2286 if (ret != H2_ERR_NO_ERROR) {
2287 h2c_error(h2c, ret);
2288 sess_log(h2c->conn->owner);
2289 goto fail;
2290 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002291 }
2292
2293 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002294 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2295
Willy Tarreau567beb82018-12-18 16:52:44 +01002296 if (tmp_h2s != h2s && h2s && h2s->cs &&
2297 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002298 (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 +01002299 /* we may have to signal the upper layers */
2300 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002301 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002302 }
2303 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002304
Willy Tarreaud7901432017-12-29 11:34:40 +01002305 if (h2c->st0 == H2_CS_FRAME_E)
2306 goto strm_err;
2307
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002308 if (h2s->st == H2_SS_IDLE &&
2309 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2310 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2311 * this state MUST be treated as a connection error
2312 */
2313 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002314 if (!h2c->nb_streams) {
2315 /* only log if no other stream can report the error */
2316 sess_log(h2c->conn->owner);
2317 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002318 break;
2319 }
2320
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002321 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2322 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2323 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002324 * this state MUST be treated as a stream error.
2325 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2326 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002327 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002328 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2329 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2330 else
2331 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002332 goto strm_err;
2333 }
2334
Willy Tarreauab837502017-12-27 15:07:30 +01002335 /* Below the management of frames received in closed state is a
2336 * bit hackish because the spec makes strong differences between
2337 * streams closed by receiving RST, sending RST, and seeing ES
2338 * in both directions. In addition to this, the creation of a
2339 * new stream reusing the identifier of a closed one will be
2340 * detected here. Given that we cannot keep track of all closed
2341 * streams forever, we consider that unknown closed streams were
2342 * closed on RST received, which allows us to respond with an
2343 * RST without breaking the connection (eg: to abort a transfer).
2344 * Some frames have to be silently ignored as well.
2345 */
2346 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002347 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002348 /* #5.1.1: The identifier of a newly
2349 * established stream MUST be numerically
2350 * greater than all streams that the initiating
2351 * endpoint has opened or reserved. This
2352 * governs streams that are opened using a
2353 * HEADERS frame and streams that are reserved
2354 * using PUSH_PROMISE. An endpoint that
2355 * receives an unexpected stream identifier
2356 * MUST respond with a connection error.
2357 */
2358 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2359 goto strm_err;
2360 }
2361
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002362 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002363 /* RFC7540#5.1:closed: an endpoint that
2364 * receives any frame other than PRIORITY after
2365 * receiving a RST_STREAM MUST treat that as a
2366 * stream error of type STREAM_CLOSED.
2367 *
2368 * Note that old streams fall into this category
2369 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002370 *
2371 * However, we cannot generalize this to all frame types. Those
2372 * carrying compression state must still be processed before
2373 * being dropped or we'll desynchronize the decoder. This can
2374 * happen with request trailers received after sending an
2375 * RST_STREAM, or with header/trailers responses received after
2376 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002377 */
2378 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2379 h2c->st0 = H2_CS_FRAME_E;
2380 goto strm_err;
2381 }
2382
2383 /* RFC7540#5.1:closed: if this state is reached as a
2384 * result of sending a RST_STREAM frame, the peer that
2385 * receives the RST_STREAM might have already sent
2386 * frames on the stream that cannot be withdrawn. An
2387 * endpoint MUST ignore frames that it receives on
2388 * closed streams after it has sent a RST_STREAM
2389 * frame. An endpoint MAY choose to limit the period
2390 * over which it ignores frames and treat frames that
2391 * arrive after this time as being in error.
2392 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002393 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002394 /* RFC7540#5.1:closed: any frame other than
2395 * PRIO/WU/RST in this state MUST be treated as
2396 * a connection error
2397 */
2398 if (h2c->dft != H2_FT_RST_STREAM &&
2399 h2c->dft != H2_FT_PRIORITY &&
2400 h2c->dft != H2_FT_WINDOW_UPDATE) {
2401 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2402 goto strm_err;
2403 }
2404 }
2405 }
2406
Willy Tarreauc0da1962017-10-30 18:38:00 +01002407#if 0
2408 // problem below: it is not possible to completely ignore such
2409 // streams as we need to maintain the compression state as well
2410 // and for this we need to completely process these frames (eg:
2411 // HEADERS frames) as well as counting DATA frames to emit
2412 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2413 // This is a typical case of layer violation where the
2414 // transported contents are critical to the connection's
2415 // validity and must be ignored at the same time :-(
2416
2417 /* graceful shutdown, ignore streams whose ID is higher than
2418 * the one advertised in GOAWAY. RFC7540#6.8.
2419 */
2420 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002421 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2422 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002423 h2c->dfl -= ret;
2424 ret = h2c->dfl == 0;
2425 goto strm_err;
2426 }
2427#endif
2428
Willy Tarreau7e98c052017-10-10 15:56:59 +02002429 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002430 case H2_FT_SETTINGS:
2431 if (h2c->st0 == H2_CS_FRAME_P)
2432 ret = h2c_handle_settings(h2c);
2433
2434 if (h2c->st0 == H2_CS_FRAME_A)
2435 ret = h2c_ack_settings(h2c);
2436 break;
2437
Willy Tarreaucf68c782017-10-10 17:11:41 +02002438 case H2_FT_PING:
2439 if (h2c->st0 == H2_CS_FRAME_P)
2440 ret = h2c_handle_ping(h2c);
2441
2442 if (h2c->st0 == H2_CS_FRAME_A)
2443 ret = h2c_ack_ping(h2c);
2444 break;
2445
Willy Tarreau26f95952017-07-27 17:18:30 +02002446 case H2_FT_WINDOW_UPDATE:
2447 if (h2c->st0 == H2_CS_FRAME_P)
2448 ret = h2c_handle_window_update(h2c, h2s);
2449 break;
2450
Willy Tarreau61290ec2017-10-17 08:19:21 +02002451 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002452 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2453 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2454 * frames' parsers consume all following CONTINUATION
2455 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002456 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002457 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2458 sess_log(h2c->conn->owner);
2459 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002460
Willy Tarreau13278b42017-10-13 19:23:14 +02002461 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002462 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002463 if (h2c->flags & H2_CF_IS_BACK)
2464 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2465 else
2466 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002467 if (tmp_h2s) {
2468 h2s = tmp_h2s;
2469 ret = 1;
2470 }
2471 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002472 break;
2473
Willy Tarreau454f9052017-10-26 19:40:35 +02002474 case H2_FT_DATA:
2475 if (h2c->st0 == H2_CS_FRAME_P)
2476 ret = h2c_frt_handle_data(h2c, h2s);
2477
2478 if (h2c->st0 == H2_CS_FRAME_A)
2479 ret = h2c_send_strm_wu(h2c);
2480 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002481
Willy Tarreau92153fc2017-12-03 19:46:19 +01002482 case H2_FT_PRIORITY:
2483 if (h2c->st0 == H2_CS_FRAME_P)
2484 ret = h2c_handle_priority(h2c);
2485 break;
2486
Willy Tarreaucd234e92017-08-18 10:59:39 +02002487 case H2_FT_RST_STREAM:
2488 if (h2c->st0 == H2_CS_FRAME_P)
2489 ret = h2c_handle_rst_stream(h2c, h2s);
2490 break;
2491
Willy Tarreaue96b0922017-10-30 00:28:29 +01002492 case H2_FT_GOAWAY:
2493 if (h2c->st0 == H2_CS_FRAME_P)
2494 ret = h2c_handle_goaway(h2c);
2495 break;
2496
Willy Tarreau1c661982017-10-30 13:52:01 +01002497 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002498 default:
2499 /* drop frames that we ignore. They may be larger than
2500 * the buffer so we drain all of their contents until
2501 * we reach the end.
2502 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002503 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2504 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002505 h2c->dfl -= ret;
2506 ret = h2c->dfl == 0;
2507 }
2508
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002509 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002510 /* We may have to send an RST if not done yet */
2511 if (h2s->st == H2_SS_ERROR)
2512 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002513
Willy Tarreaua20a5192017-12-27 11:02:06 +01002514 if (h2c->st0 == H2_CS_FRAME_E)
2515 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002516
Willy Tarreau7e98c052017-10-10 15:56:59 +02002517 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002518 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002519 break;
2520
2521 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002522 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002523 h2c->st0 = H2_CS_FRAME_H;
2524 }
2525 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002526
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002527 if (h2c->rcvd_c > 0 &&
2528 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2529 h2c_send_conn_wu(h2c);
2530
Willy Tarreau52eed752017-09-22 15:05:09 +02002531 fail:
2532 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002533 if (h2s && h2s->cs &&
2534 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002535 (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 +01002536 /* we may have to signal the upper layers */
2537 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002538 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002539 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002540
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002541 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002542}
2543
2544/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2545 * the end.
2546 */
2547static int h2_process_mux(struct h2c *h2c)
2548{
Olivier Houchardd360ac62019-03-22 17:37:16 +01002549 struct h2s *h2s;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002550
Willy Tarreau01b44822018-10-03 14:26:37 +02002551 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2552 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2553 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2554 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2555 if (h2c->st0 == H2_CS_ERROR) {
2556 h2c->st0 = H2_CS_ERROR2;
2557 sess_log(h2c->conn->owner);
2558 }
2559 goto fail;
2560 }
2561 h2c->st0 = H2_CS_SETTINGS1;
2562 }
2563 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002564 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002565 return 1;
2566 }
2567
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002568 /* start by sending possibly pending window updates */
2569 if (h2c->rcvd_c > 0 &&
2570 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2571 h2c_send_conn_wu(h2c) < 0)
2572 goto fail;
2573
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002574 /* First we always process the flow control list because the streams
2575 * waiting there were already elected for immediate emission but were
2576 * blocked just on this.
2577 */
2578
Olivier Houchardd360ac62019-03-22 17:37:16 +01002579 list_for_each_entry(h2s, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002580 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2581 h2c->st0 >= H2_CS_ERROR)
2582 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002583 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2584 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002585
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002586 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002587 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2588 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002589 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002590 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002591 }
2592
Olivier Houchardd360ac62019-03-22 17:37:16 +01002593 list_for_each_entry(h2s, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002594 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2595 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002596 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2597 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002598
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002599 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002600 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2601 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002602 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002603 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002604 }
2605
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002606 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002607 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002608 if (h2c->st0 == H2_CS_ERROR) {
2609 if (h2c->max_id >= 0) {
2610 h2c_send_goaway_error(h2c, NULL);
2611 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2612 return 0;
2613 }
2614
2615 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2616 }
2617 return 1;
2618 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002619 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002620}
2621
Willy Tarreau62f52692017-10-08 23:01:42 +02002622
Willy Tarreau479998a2018-11-18 06:30:59 +01002623/* Attempt to read data, and subscribe if none available.
2624 * The function returns 1 if data has been received, otherwise zero.
2625 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002626static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002627{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002628 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002629 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002630 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002631 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002632
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002633 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002634 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002635
Willy Tarreau315d8072017-12-10 22:17:57 +01002636 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002637 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002638
Willy Tarreau44e973f2018-03-01 17:49:30 +01002639 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002640 if (!buf) {
2641 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002642 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002643 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002644
Olivier Houchard7505f942018-08-21 18:10:44 +02002645 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002646 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002647 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2648 /* HTX in use : try to pre-align the buffer like the
2649 * rxbufs will be to optimize memory copies. We'll make
2650 * sure that the frame header lands at the end of the
2651 * HTX block to alias it upon recv. We cannot use the
2652 * head because rcv_buf() will realign the buffer if
2653 * it's empty. Thus we cheat and pretend we already
2654 * have a few bytes there.
2655 */
2656 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002657 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002658 }
2659 else
2660 max = b_room(buf);
2661
Olivier Houchard7505f942018-08-21 18:10:44 +02002662 if (max)
2663 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2664 else
2665 ret = 0;
2666 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002667
Olivier Houchard53216e72018-10-10 15:46:36 +02002668 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002669 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002670
Olivier Houcharda1411e62018-08-17 18:42:48 +02002671 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002672 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002673 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002674 }
2675
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002676 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002677 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002678 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002679}
2680
Willy Tarreau479998a2018-11-18 06:30:59 +01002681/* Try to send data if possible.
2682 * The function returns 1 if data have been sent, otherwise zero.
2683 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002684static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002685{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002686 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002687 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002688 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002689
2690 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002691 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002692
Olivier Houchard7505f942018-08-21 18:10:44 +02002693
Willy Tarreaua2af5122017-10-09 11:56:46 +02002694 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2695 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002696 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002697 }
2698
Willy Tarreaubc933932017-10-09 16:21:43 +02002699 /* This loop is quite simple : it tries to fill as much as it can from
2700 * pending streams into the existing buffer until it's reportedly full
2701 * or the end of send requests is reached. Then it tries to send this
2702 * buffer's contents out, marks it not full if at least one byte could
2703 * be sent, and tries again.
2704 *
2705 * The snd_buf() function normally takes a "flags" argument which may
2706 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2707 * data immediately comes and CO_SFL_STREAMER to indicate that the
2708 * connection is streaming lots of data (used to increase TLS record
2709 * size at the expense of latency). The former can be sent any time
2710 * there's a buffer full flag, as it indicates at least one stream
2711 * attempted to send and failed so there are pending data. An
2712 * alternative would be to set it as long as there's an active stream
2713 * but that would be problematic for ACKs until we have an absolute
2714 * guarantee that all waiters have at least one byte to send. The
2715 * latter should possibly not be set for now.
2716 */
2717
2718 done = 0;
2719 while (!done) {
2720 unsigned int flags = 0;
2721
2722 /* fill as much as we can into the current buffer */
2723 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2724 done = h2_process_mux(h2c);
2725
Olivier Houchard2b094432019-01-29 18:28:36 +01002726 if (h2c->flags & H2_CF_MUX_MALLOC)
2727 break;
2728
Willy Tarreaubc933932017-10-09 16:21:43 +02002729 if (conn->flags & CO_FL_ERROR)
2730 break;
2731
2732 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2733 flags |= CO_SFL_MSG_MORE;
2734
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002735 if (b_data(&h2c->mbuf)) {
2736 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002737 if (!ret)
2738 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002739 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002740 b_del(&h2c->mbuf, ret);
2741 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002742 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002743
2744 /* wrote at least one byte, the buffer is not full anymore */
2745 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2746 }
2747
Willy Tarreaua2af5122017-10-09 11:56:46 +02002748 if (conn->flags & CO_FL_SOCK_WR_SH) {
2749 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002750 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002751 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002752 /* We're not full anymore, so we can wake any task that are waiting
2753 * for us.
2754 */
2755 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002756 struct h2s *h2s;
2757
2758 list_for_each_entry(h2s, &h2c->send_list, list) {
2759 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2760 break;
2761 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2762 continue;
2763
2764 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002765 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2766 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002767 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002768 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002769 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002770 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002771 /* We're done, no more to send */
2772 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002773 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002774schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002775 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2776 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002777 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002778}
2779
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002780/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002781static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2782{
2783 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002784 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002785
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002786 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002787 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002788 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002789 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002790 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002791 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002792 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002793}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002794
Willy Tarreau62f52692017-10-08 23:01:42 +02002795/* callback called on any event by the connection handler.
2796 * It applies changes and returns zero, or < 0 if it wants immediate
2797 * destruction of the connection (which normally doesn not happen in h2).
2798 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002799static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002800{
Olivier Houchard7505f942018-08-21 18:10:44 +02002801 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002802
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002803 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002804 h2_process_demux(h2c);
2805
2806 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002807 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002808
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002809 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002810 h2c->flags &= ~H2_CF_DEM_DFULL;
2811 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002812 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002813
Willy Tarreau0b37d652018-10-03 10:33:02 +02002814 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002815 /* frontend is stopping, reload likely in progress, let's try
2816 * to announce a graceful shutdown if not yet done. We don't
2817 * care if it fails, it will be tried again later.
2818 */
2819 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2820 if (h2c->last_sid < 0)
2821 h2c->last_sid = (1U << 31) - 1;
2822 h2c_send_goaway_error(h2c, NULL);
2823 }
2824 }
2825
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002826 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002827 * If we received early data, and the handshake is done, wake
2828 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002829 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002830 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2831 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2832 struct eb32_node *node;
2833 struct h2s *h2s;
2834
2835 h2c->flags |= H2_CF_WAIT_FOR_HS;
2836 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2837
2838 while (node) {
2839 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002840 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002841 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002842 node = eb32_next(node);
2843 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002844 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002845
Willy Tarreau26bd7612017-10-09 16:47:04 +02002846 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002847 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2848 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2849 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002850 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002851
2852 if (eb_is_empty(&h2c->streams_by_id)) {
2853 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002854 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002855 return -1;
2856 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002857 }
2858
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002859 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002860 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002861
Olivier Houchard53216e72018-10-10 15:46:36 +02002862 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2863 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2864 (h2c->st0 != H2_CS_ERROR &&
2865 !b_data(&h2c->mbuf) &&
2866 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2867 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002868 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002869
Willy Tarreau3f133572017-10-31 19:21:06 +01002870 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002871 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002872 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002873 task_queue(h2c->task);
2874 }
2875 else
2876 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002877 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002878
Olivier Houchard7505f942018-08-21 18:10:44 +02002879 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002880 return 0;
2881}
2882
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002883/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002884static int h2_wake(struct connection *conn)
2885{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002886 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002887
2888 return (h2_process(h2c));
2889}
2890
Willy Tarreauea392822017-10-31 10:02:25 +01002891/* Connection timeout management. The principle is that if there's no receipt
2892 * nor sending for a certain amount of time, the connection is closed. If the
2893 * MUX buffer still has lying data or is not allocatable, the connection is
2894 * immediately killed. If it's allocatable and empty, we attempt to send a
2895 * GOAWAY frame.
2896 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002897static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002898{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002899 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002900 int expired = tick_is_expired(t->expire, now_ms);
2901
Willy Tarreau0975f112018-03-29 15:22:59 +02002902 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002903 return t;
2904
Willy Tarreau0975f112018-03-29 15:22:59 +02002905 task_delete(t);
2906 task_free(t);
2907
2908 if (!h2c) {
2909 /* resources were already deleted */
2910 return NULL;
2911 }
2912
2913 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002914 h2c_error(h2c, H2_ERR_NO_ERROR);
2915 h2_wake_some_streams(h2c, 0, 0);
2916
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002917 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002918 /* don't even try to send a GOAWAY, the buffer is stuck */
2919 h2c->flags |= H2_CF_GOAWAY_FAILED;
2920 }
2921
2922 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002923 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002924 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2925 h2c->flags |= H2_CF_GOAWAY_FAILED;
2926
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002927 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2928 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002929 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002930 b_del(&h2c->mbuf, ret);
2931 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002932 }
2933 }
Willy Tarreauea392822017-10-31 10:02:25 +01002934
Willy Tarreau0975f112018-03-29 15:22:59 +02002935 /* either we can release everything now or it will be done later once
2936 * the last stream closes.
2937 */
2938 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002939 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002940
Willy Tarreauea392822017-10-31 10:02:25 +01002941 return NULL;
2942}
2943
2944
Willy Tarreau62f52692017-10-08 23:01:42 +02002945/*******************************************/
2946/* functions below are used by the streams */
2947/*******************************************/
2948
2949/*
2950 * Attach a new stream to a connection
2951 * (Used for outgoing connections)
2952 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002953static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002954{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002955 struct conn_stream *cs;
2956 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002957 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002958
2959 cs = cs_new(conn);
2960 if (!cs)
2961 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002962 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002963 if (!h2s) {
2964 cs_free(cs);
2965 return NULL;
2966 }
2967 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002968}
2969
Willy Tarreaufafd3982018-11-18 21:29:20 +01002970/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2971 * We have to scan because we may have some orphan streams. It might be
2972 * beneficial to scan backwards from the end to reduce the likeliness to find
2973 * orphans.
2974 */
2975static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2976{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002977 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002978 struct h2s *h2s;
2979 struct eb32_node *node;
2980
2981 node = eb32_first(&h2c->streams_by_id);
2982 while (node) {
2983 h2s = container_of(node, struct h2s, by_id);
2984 if (h2s->cs)
2985 return h2s->cs;
2986 node = eb32_next(node);
2987 }
2988 return NULL;
2989}
2990
Willy Tarreau62f52692017-10-08 23:01:42 +02002991/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002992 * Destroy the mux and the associated connection, if it is no longer used
2993 */
Christopher Faulet73c12072019-04-08 11:23:22 +02002994static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01002995{
Christopher Faulet73c12072019-04-08 11:23:22 +02002996 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002997
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002998 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002999 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003000}
3001
3002/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003003 * Detach the stream from the connection and possibly release the connection.
3004 */
3005static void h2_detach(struct conn_stream *cs)
3006{
Willy Tarreau60935142017-10-16 18:11:19 +02003007 struct h2s *h2s = cs->ctx;
3008 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003009 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003010
3011 cs->ctx = NULL;
3012 if (!h2s)
3013 return;
3014
Olivier Houchardf502aca2018-12-14 19:42:40 +01003015 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003016 h2c = h2s->h2c;
3017 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003018 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003019 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3020 !h2_frt_has_too_many_cs(h2c)) {
3021 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003022 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003023 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003024 }
Willy Tarreau60935142017-10-16 18:11:19 +02003025
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003026 /* this stream may be blocked waiting for some data to leave (possibly
3027 * an ES or RST frame), so orphan it in this case.
3028 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003029 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003030 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003031 (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 +01003032 return;
3033
Willy Tarreau45f752e2017-10-30 15:44:59 +01003034 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3035 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3036 /* unblock the connection if it was blocked on this
3037 * stream.
3038 */
3039 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3040 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003041 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003042 }
3043
Willy Tarreau71049cc2018-03-28 13:56:39 +02003044 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003045
Olivier Houchard8a786902018-12-15 16:05:40 +01003046 if (h2c->flags & H2_CF_IS_BACK &&
3047 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003048 if (!(h2c->conn->flags &
3049 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3050 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003051 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003052 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3053 h2c->conn->owner = NULL;
3054 if (eb_is_empty(&h2c->streams_by_id)) {
3055 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3056 /* The server doesn't want it, let's kill the connection right away */
3057 h2c->conn->mux->destroy(h2c->conn);
3058 return;
3059 }
3060 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003061 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003062 if (eb_is_empty(&h2c->streams_by_id)) {
3063 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3064 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3065 return;
3066 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003067 /* Never ever allow to reuse a connection from a non-reuse backend */
3068 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3069 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003070 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003071 struct server *srv = objt_server(h2c->conn->target);
3072
3073 if (srv) {
3074 if (h2c->conn->flags & CO_FL_PRIVATE)
3075 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3076 else
3077 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3078 }
3079
3080 }
3081 }
3082 }
3083
Willy Tarreaue323f342018-03-28 13:51:45 +02003084 /* We don't want to close right now unless we're removing the
3085 * last stream, and either the connection is in error, or it
3086 * reached the ID already specified in a GOAWAY frame received
3087 * or sent (as seen by last_sid >= 0).
3088 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003089 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003090 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003091 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003092 }
3093 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003094 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003095 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3096 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003097 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003098 else
3099 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003100 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003101}
3102
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003103/* Performs a synchronous or asynchronous shutr().
3104 * FIXME: guess what the return code tries to indicate!
3105 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003106static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003107{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003108 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003109 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003110
Willy Tarreau721c9742017-11-07 11:05:42 +01003111 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003112 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003113
Willy Tarreau18059042019-01-31 19:12:48 +01003114 /* a connstream may require us to immediately kill the whole connection
3115 * for example because of a "tcp-request content reject" rule that is
3116 * normally used to limit abuse. In this case we schedule a goaway to
3117 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003118 */
Willy Tarreau18059042019-01-31 19:12:48 +01003119 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3120 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3121 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3122 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3123 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003124 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3125 /* Nothing was never sent for this stream, so reset with
3126 * REFUSED_STREAM error to let the client retry the
3127 * request.
3128 */
3129 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3130 }
Willy Tarreau18059042019-01-31 19:12:48 +01003131
Willy Tarreau90c32322017-11-24 08:00:30 +01003132 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003133 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003134 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003135
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003136 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003137 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003138 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003139
Olivier Houchard7a977432019-03-21 15:47:13 +01003140 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003141add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003142 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003143 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003144 if (h2s->flags & H2_SF_BLK_MFCTL) {
3145 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3146 h2s->send_wait = sw;
3147 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3148 h2s->send_wait = sw;
3149 LIST_ADDQ(&h2c->send_list, &h2s->list);
3150 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003151 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003152 /* Let the handler know we want shutr */
3153 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003154 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003155}
3156
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003157/* Performs a synchronous or asynchronous shutw().
3158 * FIXME: guess what the return code tries to indicate!
3159 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003160static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003161{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003162 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003163 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003164
Willy Tarreau721c9742017-11-07 11:05:42 +01003165 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003166 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003167
Willy Tarreau67434202017-11-06 20:20:51 +01003168 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003169 /* we can cleanly close using an empty data frame only after headers */
3170
3171 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3172 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003173 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003174
3175 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003176 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003177 else
3178 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003179 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003180 /* a connstream may require us to immediately kill the whole connection
3181 * for example because of a "tcp-request content reject" rule that is
3182 * normally used to limit abuse. In this case we schedule a goaway to
3183 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003184 */
Willy Tarreau18059042019-01-31 19:12:48 +01003185 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3186 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3187 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3188 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3189 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003190 else {
3191 /* Nothing was never sent for this stream, so reset with
3192 * REFUSED_STREAM error to let the client retry the
3193 * request.
3194 */
3195 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3196 }
Willy Tarreau18059042019-01-31 19:12:48 +01003197
Willy Tarreau90c32322017-11-24 08:00:30 +01003198 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003199 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003200 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003201
Willy Tarreau00dd0782018-03-01 16:31:34 +01003202 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003203 }
3204
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003205 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003206 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003207 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003208
3209 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003210 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003211 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003212 if (h2s->flags & H2_SF_BLK_MFCTL) {
3213 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3214 h2s->send_wait = sw;
3215 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3216 h2s->send_wait = sw;
3217 LIST_ADDQ(&h2c->send_list, &h2s->list);
3218 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003219 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003220 /* let the handler know we want to shutw */
3221 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003222 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003223}
3224
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003225/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3226 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3227 * and prevented the last frame from being emitted.
3228 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003229static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3230{
3231 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003232 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003233 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003234
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003235 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003236 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003237 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003238 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003239 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003240 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003241 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003242 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003243 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003244
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003245 if (!ret) {
3246 /* We're done trying to send, remove ourself from the send_list */
3247 h2s->send_wait->events &= ~SUB_RETRY_SEND;
3248 h2s->send_wait = NULL;
3249 LIST_DEL_INIT(&h2s->list);
3250 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003251 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003252 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003253 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003254 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003255
3256 if (h2c_is_dead(h2c))
Christopher Faulet73c12072019-04-08 11:23:22 +02003257 h2_release(h2c);
Olivier Houchard7a977432019-03-21 15:47:13 +01003258 }
3259
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003260 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003261}
3262
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003263/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003264static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3265{
3266 struct h2s *h2s = cs->ctx;
3267
3268 if (!mode)
3269 return;
3270
3271 h2_do_shutr(h2s);
3272}
3273
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003274/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003275static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3276{
3277 struct h2s *h2s = cs->ctx;
3278
3279 h2_do_shutw(h2s);
3280}
3281
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003282/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003283 * HTX request or response depending on the connection's side. Returns a
3284 * positive value on success, a negative value on failure, or 0 if it couldn't
3285 * proceed. May report connection errors in h2c->errcode if the frame is
3286 * non-decodable and the connection unrecoverable. In absence of connection
3287 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003288 *
3289 * The function may fold CONTINUATION frames into the initial HEADERS frame
3290 * by removing padding and next frame header, then moving the CONTINUATION
3291 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3292 * leaving a hole between the main frame and the beginning of the next one.
3293 * The possibly remaining incomplete or next frame at the end may be moved
3294 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3295 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3296 *
3297 * A buffer at the beginning of processing may look like this :
3298 *
3299 * ,---.---------.-----.--------------.--------------.------.---.
3300 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3301 * `---^---------^-----^--------------^--------------^------^---'
3302 * | | <-----> | |
3303 * area | dpl | wrap
3304 * |<--------------> |
3305 * | dfl |
3306 * |<-------------------------------------------------->|
3307 * head data
3308 *
3309 * Padding is automatically overwritten when folding, participating to the
3310 * hole size after dfl :
3311 *
3312 * ,---.------------------------.-----.--------------.------.---.
3313 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3314 * `---^------------------------^-----^--------------^------^---'
3315 * | | <-----> | |
3316 * area | hole | wrap
3317 * |<-----------------------> |
3318 * | dfl |
3319 * |<-------------------------------------------------->|
3320 * head data
3321 *
3322 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3323 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3324 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003325 *
3326 * The <flags> field must point to either the stream's flags or to a copy of it
3327 * so that the function can update the following flags :
3328 * - H2_SF_DATA_CLEN when content-length is seen
3329 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3330 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003331 *
3332 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3333 * decoding, in order to detect if we're dealing with a headers or a trailers
3334 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003335 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003336static 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 +02003337{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003338 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003339 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003340 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003341 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003342 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003343 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003344 int flen; // header frame len
3345 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003346 int ret = 0;
3347 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003348 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003349 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003350
Willy Tarreauea18f862018-12-22 20:19:26 +01003351next_frame:
3352 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3353 goto leave; // incomplete input frame
3354
3355 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3356 * this case, we'll try to paste it immediately after the initial
3357 * HEADERS frame payload and kill any possible padding. The initial
3358 * frame's length will be increased to represent the concatenation
3359 * of the two frames. The next frame is read from position <tlen>
3360 * and written at position <flen> (minus padding if some is present).
3361 */
3362 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3363 struct h2_fh hdr;
3364 int clen; // CONTINUATION frame's payload length
3365
3366 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3367 /* no more data, the buffer may be full, either due to
3368 * too large a frame or because of too large a hole that
3369 * we're going to compact at the end.
3370 */
3371 goto leave;
3372 }
3373
3374 if (hdr.ft != H2_FT_CONTINUATION) {
3375 /* RFC7540#6.10: frame of unexpected type */
3376 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3377 goto fail;
3378 }
3379
3380 if (hdr.sid != h2c->dsi) {
3381 /* RFC7540#6.10: frame of different stream */
3382 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3383 goto fail;
3384 }
3385
3386 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3387 /* RFC7540#4.2: invalid frame length */
3388 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3389 goto fail;
3390 }
3391
3392 /* detect when we must stop aggragating frames */
3393 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3394
3395 /* Take as much as we can of the CONTINUATION frame's payload */
3396 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3397 if (clen > hdr.len)
3398 clen = hdr.len;
3399
3400 /* Move the frame's payload over the padding, hole and frame
3401 * header. At least one of hole or dpl is null (see diagrams
3402 * above). The hole moves after the new aggragated frame.
3403 */
3404 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3405 h2c->dfl += clen - h2c->dpl;
3406 hole += h2c->dpl + 9;
3407 h2c->dpl = 0;
3408 goto next_frame;
3409 }
3410
3411 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003412
Willy Tarreau13278b42017-10-13 19:23:14 +02003413 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003414 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003415 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003416 copy = alloc_trash_chunk();
3417 if (!copy) {
3418 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3419 goto fail;
3420 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003421 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3422 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3423 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003424 }
3425
Willy Tarreau13278b42017-10-13 19:23:14 +02003426 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3427 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003428 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003429 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3430 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003431 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003432 }
3433
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003434 if (flen < 5) {
3435 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3436 goto fail;
3437 }
3438
Willy Tarreau13278b42017-10-13 19:23:14 +02003439 hdrs += 5; // stream dep = 4, weight = 1
3440 flen -= 5;
3441 }
3442
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003443 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003444 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003445 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003446 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003447
Willy Tarreau937f7602018-02-26 15:22:17 +01003448 /* we can't retry a failed decompression operation so we must be very
3449 * careful not to take any risks. In practice the output buffer is
3450 * always empty except maybe for trailers, in which case we simply have
3451 * to wait for the upper layer to finish consuming what is available.
3452 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003453
3454 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003455 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003456 if (!htx_is_empty(htx)) {
3457 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003458 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003459 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003460 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003461 if (b_data(rxbuf)) {
3462 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003463 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003464 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003465
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003466 rxbuf->head = 0;
3467 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003468 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003469
Willy Tarreau25919232019-01-03 14:48:18 +01003470 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003471 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3472 sizeof(list)/sizeof(list[0]), tmp);
3473 if (outlen < 0) {
3474 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3475 goto fail;
3476 }
3477
Willy Tarreau25919232019-01-03 14:48:18 +01003478 /* The PACK decompressor was updated, let's update the input buffer and
3479 * the parser's state to commit these changes and allow us to later
3480 * fail solely on the stream if needed.
3481 */
3482 b_del(&h2c->dbuf, h2c->dfl + hole);
3483 h2c->dfl = hole = 0;
3484 h2c->st0 = H2_CS_FRAME_H;
3485
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003486 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003487 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003488
Willy Tarreau88d138e2019-01-02 19:38:14 +01003489 if (*flags & H2_SF_HEADERS_RCVD)
3490 goto trailers;
3491
3492 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003493 if (htx) {
3494 /* HTX mode */
3495 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003496 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003497 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003498 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003499 } else {
3500 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003501 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003502 if (outlen > 0)
3503 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003504 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003505
3506 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003507 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003508 goto fail;
3509 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003510
Willy Tarreau174b06a2018-04-25 18:13:58 +02003511 if (msgf & H2_MSGF_BODY) {
3512 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003513 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003514 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003515 if (htx)
3516 htx->extra = *body_len;
3517 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003518 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003519 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003520 }
3521
Willy Tarreau88d138e2019-01-02 19:38:14 +01003522 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003523 /* indicate that a HEADERS frame was received for this stream, except
3524 * for 1xx responses. For 1xx responses, another HEADERS frame is
3525 * expected.
3526 */
3527 if (!(msgf & H2_MSGF_RSP_1XX))
3528 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003529
Christopher Faulet0b465482019-02-19 15:14:23 +01003530 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003531 /* Mark the end of message, either using EOM in HTX or with the
3532 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3533 * is not set during headers with END_STREAM.
3534 */
3535 if (htx) {
3536 if (!htx_add_endof(htx, HTX_BLK_EOM))
3537 goto fail;
3538 }
3539 else if (*flags & H2_SF_DATA_CHNK) {
3540 if (!b_putblk(rxbuf, "\r\n", 2))
3541 goto fail;
3542 }
3543 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003544
Willy Tarreau86277d42019-01-02 15:36:11 +01003545 /* success */
3546 ret = 1;
3547
Willy Tarreau68dd9852017-07-03 14:44:26 +02003548 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003549 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003550 * move the remaining data over it.
3551 */
3552 if (hole) {
3553 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3554 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3555 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3556 b_sub(&h2c->dbuf, hole);
3557 }
3558
3559 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3560 /* too large frames */
3561 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003562 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003563 }
3564
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003565 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003566 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003567 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003568 return ret;
3569
Willy Tarreau68dd9852017-07-03 14:44:26 +02003570 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003571 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003572 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003573
3574 trailers:
3575 /* This is the last HEADERS frame hence a trailer */
3576
3577 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3578 /* It's a trailer but it's missing ES flag */
3579 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3580 goto fail;
3581 }
3582
3583 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3584 * block, and when using chunks we must send the 0 CRLF marker. For
3585 * other modes, the trailers are silently dropped.
3586 */
3587 if (htx) {
3588 if (!htx_add_endof(htx, HTX_BLK_EOD))
3589 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003590 if (h2_make_htx_trailers(list, htx) <= 0)
3591 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003592 }
3593 else if (*flags & H2_SF_DATA_CHNK) {
3594 /* Legacy mode with chunked encoding : we must finalize the
3595 * data block message emit the trailing CRLF */
3596 if (!b_putblk(rxbuf, "0\r\n", 3))
3597 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003598
3599 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3600 if (outlen > 0)
3601 b_add(rxbuf, outlen);
3602 else
3603 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003604 }
3605
3606 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003607}
3608
Willy Tarreau454f9052017-10-26 19:40:35 +02003609/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3610 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3611 * in use, a new chunk is emitted for each frame. This is supposed to fit
3612 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3613 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3614 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003615 * parser state is automatically updated. Returns > 0 if it could completely
3616 * send the current frame, 0 if it couldn't complete, in which case
3617 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3618 * DATA frame can return 0 as a valid result). Stream errors are reported in
3619 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3620 * have checked the frame header and ensured that the frame was complete or the
3621 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003622 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003623static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003624{
3625 struct h2c *h2c = h2s->h2c;
3626 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003627 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003628 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003629 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003630 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003631
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003632 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003633
Olivier Houchard638b7992018-08-16 15:41:52 +02003634 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003635 if (!csbuf) {
3636 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003637 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003638 }
3639
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003640try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003641 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003642 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3643 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003644 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003645 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003646
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003647 if (flen > b_data(&h2c->dbuf)) {
3648 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003649 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003650 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003651 }
3652
Willy Tarreaua9b77962019-01-31 07:23:00 +01003653 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003654 block1 = htx_free_data_space(htx);
3655 if (!block1) {
3656 h2c->flags |= H2_CF_DEM_SFULL;
3657 goto fail;
3658 }
3659 if (flen > block1)
3660 flen = block1;
3661
3662 /* here, flen is the max we can copy into the output buffer */
3663 block1 = b_contig_data(&h2c->dbuf, 0);
3664 if (flen > block1)
3665 flen = block1;
3666
3667 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3668 h2c->flags |= H2_CF_DEM_SFULL;
3669 goto fail;
3670 }
3671
3672 b_del(&h2c->dbuf, flen);
3673 h2c->dfl -= flen;
3674 h2c->rcvd_c += flen;
3675 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003676
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003677 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003678 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003679 htx->extra = h2s->body_len;
3680 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003681 goto try_again;
3682 }
3683 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003684 /* it doesn't fit and the buffer is fragmented,
3685 * so let's defragment it and try again.
3686 */
3687 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003688 }
3689
Willy Tarreaueba10f22018-04-25 20:44:22 +02003690 /* chunked-encoding requires more room */
3691 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003692 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003693 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3694 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3695 (chklen < 1048576) ? 4 : 8;
3696 chklen += 4; // CRLF, CRLF
3697 }
3698
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003699 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003700 if (flen + chklen > b_room(csbuf)) {
3701 if (chklen >= b_room(csbuf)) {
3702 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003703 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003704 }
3705 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003706 }
3707
3708 if (h2s->flags & H2_SF_DATA_CHNK) {
3709 /* emit the chunk size */
3710 unsigned int chksz = flen;
3711 char str[10];
3712 char *beg;
3713
3714 beg = str + sizeof(str);
3715 *--beg = '\n';
3716 *--beg = '\r';
3717 do {
3718 *--beg = hextab[chksz & 0xF];
3719 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003720 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003721 }
3722
Willy Tarreau454f9052017-10-26 19:40:35 +02003723 /* Block1 is the length of the first block before the buffer wraps,
3724 * block2 is the optional second block to reach the end of the frame.
3725 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003726 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003727 if (block1 > flen)
3728 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003729 block2 = flen - block1;
3730
3731 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003732 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003733
3734 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003735 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003736
Willy Tarreaueba10f22018-04-25 20:44:22 +02003737 if (h2s->flags & H2_SF_DATA_CHNK) {
3738 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003739 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003740 }
3741
Willy Tarreau454f9052017-10-26 19:40:35 +02003742 /* now mark the input data as consumed (will be deleted from the buffer
3743 * by the caller when seeing FRAME_A after sending the window update).
3744 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003745 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003746 h2c->dfl -= flen;
3747 h2c->rcvd_c += flen;
3748 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3749
Willy Tarreau1915ca22019-01-24 11:49:37 +01003750 if (h2s->flags & H2_SF_DATA_CLEN)
3751 h2s->body_len -= flen;
3752
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003753 if (h2c->dfl > h2c->dpl) {
3754 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003755 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003756 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003757 }
3758
Willy Tarreau4a28da12018-01-04 14:41:00 +01003759 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003760 /* here we're done with the frame, all the payload (except padding) was
3761 * transferred.
3762 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003763
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003764 if (h2c->dff & H2_F_DATA_END_STREAM) {
3765 if (htx) {
3766 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3767 h2c->flags |= H2_CF_DEM_SFULL;
3768 goto fail;
3769 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003770 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003771 else if (h2s->flags & H2_SF_DATA_CHNK) {
3772 /* emit the trailing 0 CRLF CRLF */
3773 if (b_room(csbuf) < 5) {
3774 h2c->flags |= H2_CF_DEM_SFULL;
3775 goto fail;
3776 }
3777 chklen += 5;
3778 b_putblk(csbuf, "0\r\n\r\n", 5);
3779 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003780 }
3781
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003782 h2c->rcvd_c += h2c->dpl;
3783 h2c->rcvd_s += h2c->dpl;
3784 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003785 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003786 if (htx)
3787 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003788 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003789 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003790 if (htx)
3791 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003792 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003793}
3794
Willy Tarreau5dd17352018-06-14 13:33:30 +02003795/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3796 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3797 * number of bytes sent. The caller must check the stream's status to detect
3798 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003799 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003800static 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 +02003801{
3802 struct http_hdr list[MAX_HTTP_HDR];
3803 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003804 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003805 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003806 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003807 int es_now = 0;
3808 int ret = 0;
3809 int hdr;
3810
3811 if (h2c_mux_busy(h2c, h2s)) {
3812 h2s->flags |= H2_SF_BLK_MBUSY;
3813 return 0;
3814 }
3815
Willy Tarreau44e973f2018-03-01 17:49:30 +01003816 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003817 h2c->flags |= H2_CF_MUX_MALLOC;
3818 h2s->flags |= H2_SF_BLK_MROOM;
3819 return 0;
3820 }
3821
3822 /* First, try to parse the H1 response and index it into <list>.
3823 * NOTE! Since it comes from haproxy, we *know* that a response header
3824 * block does not wrap and we can safely read it this way without
3825 * having to realign the buffer.
3826 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003827 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003828 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003829 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003830 /* incomplete or invalid response, this is abnormal coming from
3831 * haproxy and may only result in a bad errorfile or bad Lua code
3832 * so that won't be fixed, raise an error now.
3833 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003834 * FIXME: we should instead add the ability to only return a
3835 * 502 bad gateway. But in theory this is not supposed to
3836 * happen.
3837 */
3838 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3839 ret = 0;
3840 goto end;
3841 }
3842
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003843 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003844
3845 /* certain statuses have no body or an empty one, regardless of
3846 * what the headers say.
3847 */
3848 if (sl.st.status >= 100 && sl.st.status < 200) {
3849 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3850 h1m->curr_len = h1m->body_len = 0;
3851 }
3852 else if (sl.st.status == 204 || sl.st.status == 304) {
3853 /* no contents, claim c-len is present and set to zero */
3854 h1m->flags &= ~H1_MF_CHNK;
3855 h1m->flags |= H1_MF_CLEN;
3856 h1m->curr_len = h1m->body_len = 0;
3857 }
3858
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003859 chunk_reset(&outbuf);
3860
3861 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003862 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003863 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003864 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003865
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003866 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003867 break;
3868 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003869 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003870 }
3871
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003872 if (outbuf.size < 9)
3873 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003874
3875 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003876 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3877 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3878 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003879
3880 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003881 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003882 /* this is an unparsable response */
3883 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3884 ret = 0;
3885 goto end;
3886 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003887
3888 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003889 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003890 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003891 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003892 }
3893
3894 /* encode all headers, stop at empty name */
3895 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003896 /* these ones do not exist in H2 and must be dropped. */
3897 if (isteq(list[hdr].n, ist("connection")) ||
3898 isteq(list[hdr].n, ist("proxy-connection")) ||
3899 isteq(list[hdr].n, ist("keep-alive")) ||
3900 isteq(list[hdr].n, ist("upgrade")) ||
3901 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003902 continue;
3903
3904 if (isteq(list[hdr].n, ist("")))
3905 break; // end
3906
3907 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3908 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003909 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003910 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003911 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003912 }
3913 }
3914
3915 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003916 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003917 es_now = 1;
3918
3919 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003920 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003921
3922 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003923 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003924
3925 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003926 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003927
3928 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003929 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003930 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003931
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003932 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003933 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003934 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003935
Willy Tarreau801250e2018-09-11 11:45:04 +02003936 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003937 h2s->flags |= H2_SF_ES_SENT;
3938 if (h2s->st == H2_SS_OPEN)
3939 h2s->st = H2_SS_HLOC;
3940 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003941 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003942 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003943 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003944 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003945 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003946 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003947 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003948 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003949 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003950
3951 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003952
3953 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003954 //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 +02003955 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003956 full:
3957 h1m_init_res(h1m);
3958 h1m->err_pos = -1; // don't care about errors on the response path
3959 h2c->flags |= H2_CF_MUX_MFULL;
3960 h2s->flags |= H2_SF_BLK_MROOM;
3961 ret = 0;
3962 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003963}
3964
Willy Tarreau5dd17352018-06-14 13:33:30 +02003965/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3966 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3967 * the number of bytes sent. The caller must check the stream's status to
3968 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003969 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003970static 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 +02003971{
3972 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003973 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003974 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003975 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003976 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003977 int es_now = 0;
3978 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003979 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003980 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003981
3982 if (h2c_mux_busy(h2c, h2s)) {
3983 h2s->flags |= H2_SF_BLK_MBUSY;
3984 goto end;
3985 }
3986
Willy Tarreau44e973f2018-03-01 17:49:30 +01003987 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003988 h2c->flags |= H2_CF_MUX_MALLOC;
3989 h2s->flags |= H2_SF_BLK_MROOM;
3990 goto end;
3991 }
3992
3993 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003994 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003995 goto end;
3996
3997 chunk_reset(&outbuf);
3998
3999 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004000 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004001 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004002 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004003
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004004 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004005 break;
4006 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004007 /* If there are pending data in the output buffer, and we have
4008 * less than 1/4 of the mbuf's size and everything fits, we'll
4009 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4010 * is full and wait, to save some slow realign calls.
4011 */
4012 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4013 h2c->flags |= H2_CF_MUX_MFULL;
4014 h2s->flags |= H2_SF_BLK_MROOM;
4015 goto end;
4016 }
4017
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004018 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004019 }
4020
4021 if (outbuf.size < 9) {
4022 h2c->flags |= H2_CF_MUX_MFULL;
4023 h2s->flags |= H2_SF_BLK_MROOM;
4024 goto end;
4025 }
4026
4027 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004028 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4029 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4030 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004031
4032 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4033 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004034 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004035 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004036 break;
4037 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004038 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004039 if ((long long)size > h1m->curr_len)
4040 size = h1m->curr_len;
4041 break;
4042 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004043 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004044 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004045 if (!ret)
4046 goto end;
4047
4048 if (ret < 0) {
4049 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004050 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004051 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4052 goto end;
4053 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004054 max -= ret;
4055 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004056 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004057 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004058 }
4059
Willy Tarreau801250e2018-09-11 11:45:04 +02004060 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004061 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004062 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004063 if (!ret)
4064 goto end;
4065
4066 if (ret < 0) {
4067 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004068 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004069 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4070 goto end;
4071 }
4072
4073 size = chunk;
4074 h1m->curr_len = chunk;
4075 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004076 max -= ret;
4077 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004078 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004079 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004080 if (!size)
4081 goto send_empty;
4082 }
4083
4084 /* in MSG_DATA state, continue below */
4085 size = h1m->curr_len;
4086 break;
4087 }
4088
4089 /* we have in <size> the exact number of bytes we need to copy from
4090 * the H1 buffer. We need to check this against the connection's and
4091 * the stream's send windows, and to ensure that this fits in the max
4092 * frame size and in the buffer's available space minus 9 bytes (for
4093 * the frame header). The connection's flow control is applied last so
4094 * that we can use a separate list of streams which are immediately
4095 * unblocked on window opening. Note: we don't implement padding.
4096 */
4097
Willy Tarreau5dd17352018-06-14 13:33:30 +02004098 if (size > max)
4099 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004100
4101 if (size > h2s->mws)
4102 size = h2s->mws;
4103
4104 if (size <= 0) {
4105 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004106 if (h2s->send_wait) {
4107 LIST_DEL(&h2s->list);
4108 LIST_INIT(&h2s->list);
4109 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004110 goto end;
4111 }
4112
4113 if (h2c->mfs && size > h2c->mfs)
4114 size = h2c->mfs;
4115
4116 if (size + 9 > outbuf.size) {
4117 /* we have an opportunity for enlarging the too small
4118 * available space, let's try.
4119 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004120 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004121 goto realign_again;
4122 size = outbuf.size - 9;
4123 }
4124
4125 if (size <= 0) {
4126 h2c->flags |= H2_CF_MUX_MFULL;
4127 h2s->flags |= H2_SF_BLK_MROOM;
4128 goto end;
4129 }
4130
4131 if (size > h2c->mws)
4132 size = h2c->mws;
4133
4134 if (size <= 0) {
4135 h2s->flags |= H2_SF_BLK_MFCTL;
4136 goto end;
4137 }
4138
4139 /* copy whatever we can */
4140 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004141 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004142 if (ret == 1)
4143 len2 = 0;
4144
4145 if (!ret || len1 + len2 < size) {
4146 /* FIXME: must normally never happen */
4147 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4148 goto end;
4149 }
4150
4151 /* limit len1/len2 to size */
4152 if (len1 + len2 > size) {
4153 int sub = len1 + len2 - size;
4154
4155 if (len2 > sub)
4156 len2 -= sub;
4157 else {
4158 sub -= len2;
4159 len2 = 0;
4160 len1 -= sub;
4161 }
4162 }
4163
4164 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004165 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004166 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004167 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004168
4169 send_empty:
4170 /* we may need to add END_STREAM */
4171 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4172 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004173 *
4174 * FIXME: what we do here is not correct because we send end_stream
4175 * before knowing if we'll have to send a HEADERS frame for the
4176 * trailers. More importantly we're not consuming the trailing CRLF
4177 * after the end of trailers, so it will be left to the caller to
4178 * eat it. The right way to do it would be to measure trailers here
4179 * and to send ES only if there are no trailers.
4180 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004181 */
4182 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004183 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004184 es_now = 1;
4185
4186 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004187 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004188
4189 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004190 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004191
4192 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004193 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004194
4195 /* consume incoming H1 response */
4196 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004197 max -= size;
4198 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004199 total += size;
4200 h1m->curr_len -= size;
4201 h2s->mws -= size;
4202 h2c->mws -= size;
4203
4204 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004205 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004206 goto new_frame;
4207 }
4208 }
4209
4210 if (es_now) {
4211 if (h2s->st == H2_SS_OPEN)
4212 h2s->st = H2_SS_HLOC;
4213 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004214 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004215
Willy Tarreau35a62702018-02-27 15:37:25 +01004216 if (!(h1m->flags & H1_MF_CHNK)) {
4217 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004218 total += max;
4219 ofs += max;
4220 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004221
Willy Tarreau801250e2018-09-11 11:45:04 +02004222 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004223 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004224
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004225 h2s->flags |= H2_SF_ES_SENT;
4226 }
4227
4228 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004229 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 +02004230 return total;
4231}
4232
Willy Tarreau115e83b2018-12-01 19:17:53 +01004233/* Try to send a HEADERS frame matching HTX response present in HTX message
4234 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4235 * must check the stream's status to detect any error which might have happened
4236 * subsequently to a successful send. The htx blocks are automatically removed
4237 * from the message. The htx message is assumed to be valid since produced from
4238 * the internal code, hence it contains a start line, an optional series of
4239 * header blocks and an end of header, otherwise an invalid frame could be
4240 * emitted and the resulting htx message could be left in an inconsistent state.
4241 */
4242static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4243{
4244 struct http_hdr list[MAX_HTTP_HDR];
4245 struct h2c *h2c = h2s->h2c;
4246 struct htx_blk *blk;
4247 struct htx_blk *blk_end;
4248 struct buffer outbuf;
4249 struct htx_sl *sl;
4250 enum htx_blk_type type;
4251 int es_now = 0;
4252 int ret = 0;
4253 int hdr;
4254 int idx;
4255
4256 if (h2c_mux_busy(h2c, h2s)) {
4257 h2s->flags |= H2_SF_BLK_MBUSY;
4258 return 0;
4259 }
4260
4261 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4262 h2c->flags |= H2_CF_MUX_MALLOC;
4263 h2s->flags |= H2_SF_BLK_MROOM;
4264 return 0;
4265 }
4266
4267 /* determine the first block which must not be deleted, blk_end may
4268 * be NULL if all blocks have to be deleted.
4269 */
4270 idx = htx_get_head(htx);
4271 blk_end = NULL;
4272 while (idx != -1) {
4273 type = htx_get_blk_type(htx_get_blk(htx, idx));
4274 idx = htx_get_next(htx, idx);
4275 if (type == HTX_BLK_EOH) {
4276 if (idx != -1)
4277 blk_end = htx_get_blk(htx, idx);
4278 break;
4279 }
4280 }
4281
4282 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004283 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004284 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004285 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004286 if (h2s->status < 100 || h2s->status > 999)
4287 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004288
4289 /* and the rest of the headers, that we dump starting at header 0 */
4290 hdr = 0;
4291
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004292 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004293 while ((idx = htx_get_next(htx, idx)) != -1) {
4294 blk = htx_get_blk(htx, idx);
4295 type = htx_get_blk_type(blk);
4296
4297 if (type == HTX_BLK_UNUSED)
4298 continue;
4299
4300 if (type != HTX_BLK_HDR)
4301 break;
4302
4303 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4304 goto fail;
4305
4306 list[hdr].n = htx_get_blk_name(htx, blk);
4307 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004308 hdr++;
4309 }
4310
4311 /* marker for end of headers */
4312 list[hdr].n = ist("");
4313
4314 if (h2s->status == 204 || h2s->status == 304) {
4315 /* no contents, claim c-len is present and set to zero */
4316 es_now = 1;
4317 }
4318
4319 chunk_reset(&outbuf);
4320
4321 while (1) {
4322 outbuf.area = b_tail(&h2c->mbuf);
4323 outbuf.size = b_contig_space(&h2c->mbuf);
4324 outbuf.data = 0;
4325
4326 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4327 break;
4328 realign_again:
4329 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4330 }
4331
4332 if (outbuf.size < 9)
4333 goto full;
4334
4335 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4336 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4337 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4338 outbuf.data = 9;
4339
4340 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004341 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004342 if (b_space_wraps(&h2c->mbuf))
4343 goto realign_again;
4344 goto full;
4345 }
4346
4347 /* encode all headers, stop at empty name */
4348 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4349 /* these ones do not exist in H2 and must be dropped. */
4350 if (isteq(list[hdr].n, ist("connection")) ||
4351 isteq(list[hdr].n, ist("proxy-connection")) ||
4352 isteq(list[hdr].n, ist("keep-alive")) ||
4353 isteq(list[hdr].n, ist("upgrade")) ||
4354 isteq(list[hdr].n, ist("transfer-encoding")))
4355 continue;
4356
4357 if (isteq(list[hdr].n, ist("")))
4358 break; // end
4359
4360 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4361 /* output full */
4362 if (b_space_wraps(&h2c->mbuf))
4363 goto realign_again;
4364 goto full;
4365 }
4366 }
4367
Christopher Faulet0b465482019-02-19 15:14:23 +01004368 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004369 * FIXME: we should also set it when we know for sure that the
4370 * content-length is zero as well as on 204/304
4371 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004372 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4373 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004374 es_now = 1;
4375
Willy Tarreau927b88b2019-03-04 08:03:25 +01004376 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004377 es_now = 1;
4378
4379 /* update the frame's size */
4380 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4381
4382 if (es_now)
4383 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4384
4385 /* commit the H2 response */
4386 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004387
4388 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4389 * 1xx responses, another HEADERS frame is expected.
4390 */
4391 if (h2s->status >= 200 || h2s->status == 101)
4392 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004393
Willy Tarreau115e83b2018-12-01 19:17:53 +01004394 if (es_now) {
4395 h2s->flags |= H2_SF_ES_SENT;
4396 if (h2s->st == H2_SS_OPEN)
4397 h2s->st = H2_SS_HLOC;
4398 else
4399 h2s_close(h2s);
4400 }
4401
4402 /* OK we could properly deliver the response */
4403
4404 /* remove all header blocks including the EOH and compute the
4405 * corresponding size.
4406 *
4407 * FIXME: We should remove everything when es_now is set.
4408 */
4409 ret = 0;
4410 idx = htx_get_head(htx);
4411 blk = htx_get_blk(htx, idx);
4412 while (blk != blk_end) {
4413 ret += htx_get_blksz(blk);
4414 blk = htx_remove_blk(htx, blk);
4415 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004416
4417 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4418 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004419 end:
4420 return ret;
4421 full:
4422 h2c->flags |= H2_CF_MUX_MFULL;
4423 h2s->flags |= H2_SF_BLK_MROOM;
4424 ret = 0;
4425 goto end;
4426 fail:
4427 /* unparsable HTX messages, too large ones to be produced in the local
4428 * list etc go here (unrecoverable errors).
4429 */
4430 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4431 ret = 0;
4432 goto end;
4433}
4434
Willy Tarreau80739692018-10-05 11:35:57 +02004435/* Try to send a HEADERS frame matching HTX request present in HTX message
4436 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4437 * must check the stream's status to detect any error which might have happened
4438 * subsequently to a successful send. The htx blocks are automatically removed
4439 * from the message. The htx message is assumed to be valid since produced from
4440 * the internal code, hence it contains a start line, an optional series of
4441 * header blocks and an end of header, otherwise an invalid frame could be
4442 * emitted and the resulting htx message could be left in an inconsistent state.
4443 */
4444static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4445{
4446 struct http_hdr list[MAX_HTTP_HDR];
4447 struct h2c *h2c = h2s->h2c;
4448 struct htx_blk *blk;
4449 struct htx_blk *blk_end;
4450 struct buffer outbuf;
4451 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004452 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004453 enum htx_blk_type type;
4454 int es_now = 0;
4455 int ret = 0;
4456 int hdr;
4457 int idx;
4458
4459 if (h2c_mux_busy(h2c, h2s)) {
4460 h2s->flags |= H2_SF_BLK_MBUSY;
4461 return 0;
4462 }
4463
4464 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4465 h2c->flags |= H2_CF_MUX_MALLOC;
4466 h2s->flags |= H2_SF_BLK_MROOM;
4467 return 0;
4468 }
4469
4470 /* determine the first block which must not be deleted, blk_end may
4471 * be NULL if all blocks have to be deleted.
4472 */
4473 idx = htx_get_head(htx);
4474 blk_end = NULL;
4475 while (idx != -1) {
4476 type = htx_get_blk_type(htx_get_blk(htx, idx));
4477 idx = htx_get_next(htx, idx);
4478 if (type == HTX_BLK_EOH) {
4479 if (idx != -1)
4480 blk_end = htx_get_blk(htx, idx);
4481 break;
4482 }
4483 }
4484
4485 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004486 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004487 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004488 meth = htx_sl_req_meth(sl);
4489 path = htx_sl_req_uri(sl);
4490
4491 /* and the rest of the headers, that we dump starting at header 0 */
4492 hdr = 0;
4493
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004494 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004495 while ((idx = htx_get_next(htx, idx)) != -1) {
4496 blk = htx_get_blk(htx, idx);
4497 type = htx_get_blk_type(blk);
4498
4499 if (type == HTX_BLK_UNUSED)
4500 continue;
4501
4502 if (type != HTX_BLK_HDR)
4503 break;
4504
4505 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4506 goto fail;
4507
4508 list[hdr].n = htx_get_blk_name(htx, blk);
4509 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004510 hdr++;
4511 }
4512
4513 /* marker for end of headers */
4514 list[hdr].n = ist("");
4515
4516 chunk_reset(&outbuf);
4517
4518 while (1) {
4519 outbuf.area = b_tail(&h2c->mbuf);
4520 outbuf.size = b_contig_space(&h2c->mbuf);
4521 outbuf.data = 0;
4522
4523 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4524 break;
4525 realign_again:
4526 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4527 }
4528
4529 if (outbuf.size < 9)
4530 goto full;
4531
4532 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4533 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4534 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4535 outbuf.data = 9;
4536
4537 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004538 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004539 if (b_space_wraps(&h2c->mbuf))
4540 goto realign_again;
4541 goto full;
4542 }
4543
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004544 /* RFC7540 #8.3: the CONNECT method must have :
4545 * - :authority set to the URI part (host:port)
4546 * - :method set to CONNECT
4547 * - :scheme and :path omitted
4548 */
4549 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4550 /* encode the scheme which is always "https" (or 0x86 for "http") */
4551 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4552 /* output full */
4553 if (b_space_wraps(&h2c->mbuf))
4554 goto realign_again;
4555 goto full;
4556 }
Willy Tarreau80739692018-10-05 11:35:57 +02004557
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004558 /* encode the path, which necessarily is the second one */
4559 if (!hpack_encode_path(&outbuf, path)) {
4560 /* output full */
4561 if (b_space_wraps(&h2c->mbuf))
4562 goto realign_again;
4563 goto full;
4564 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004565
4566 /* look for the Host header and place it in :authority */
4567 auth = ist2(NULL, 0);
4568 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4569 if (isteq(list[hdr].n, ist("")))
4570 break; // end
4571
4572 if (isteq(list[hdr].n, ist("host"))) {
4573 auth = list[hdr].v;
4574 break;
4575 }
4576 }
4577 }
4578 else {
4579 /* for CONNECT, :authority is taken from the path */
4580 auth = path;
4581 }
4582
4583 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4584 /* output full */
4585 if (b_space_wraps(&h2c->mbuf))
4586 goto realign_again;
4587 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004588 }
4589
4590 /* encode all headers, stop at empty name */
4591 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4592 /* these ones do not exist in H2 and must be dropped. */
4593 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004594 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004595 isteq(list[hdr].n, ist("proxy-connection")) ||
4596 isteq(list[hdr].n, ist("keep-alive")) ||
4597 isteq(list[hdr].n, ist("upgrade")) ||
4598 isteq(list[hdr].n, ist("transfer-encoding")))
4599 continue;
4600
4601 if (isteq(list[hdr].n, ist("")))
4602 break; // end
4603
4604 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4605 /* output full */
4606 if (b_space_wraps(&h2c->mbuf))
4607 goto realign_again;
4608 goto full;
4609 }
4610 }
4611
4612 /* we may need to add END_STREAM if we have no body :
4613 * - request already closed, or :
4614 * - no transfer-encoding, and :
4615 * - no content-length or content-length:0
4616 * Fixme: this doesn't take into account CONNECT requests.
4617 */
4618 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4619 es_now = 1;
4620
4621 if (sl->flags & HTX_SL_F_BODYLESS)
4622 es_now = 1;
4623
Willy Tarreau927b88b2019-03-04 08:03:25 +01004624 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004625 es_now = 1;
4626
4627 /* update the frame's size */
4628 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4629
4630 if (es_now)
4631 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4632
4633 /* commit the H2 response */
4634 b_add(&h2c->mbuf, outbuf.data);
4635 h2s->flags |= H2_SF_HEADERS_SENT;
4636 h2s->st = H2_SS_OPEN;
4637
Willy Tarreau80739692018-10-05 11:35:57 +02004638 if (es_now) {
4639 // trim any possibly pending data (eg: inconsistent content-length)
4640 h2s->flags |= H2_SF_ES_SENT;
4641 h2s->st = H2_SS_HLOC;
4642 }
4643
4644 /* remove all header blocks including the EOH and compute the
4645 * corresponding size.
4646 *
4647 * FIXME: We should remove everything when es_now is set.
4648 */
4649 ret = 0;
4650 idx = htx_get_head(htx);
4651 blk = htx_get_blk(htx, idx);
4652 while (blk != blk_end) {
4653 ret += htx_get_blksz(blk);
4654 blk = htx_remove_blk(htx, blk);
4655 }
4656
4657 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4658 htx_remove_blk(htx, blk_end);
4659
4660 end:
4661 return ret;
4662 full:
4663 h2c->flags |= H2_CF_MUX_MFULL;
4664 h2s->flags |= H2_SF_BLK_MROOM;
4665 ret = 0;
4666 goto end;
4667 fail:
4668 /* unparsable HTX messages, too large ones to be produced in the local
4669 * list etc go here (unrecoverable errors).
4670 */
4671 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4672 ret = 0;
4673 goto end;
4674}
4675
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004676/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004677 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4678 * caller must check the stream's status to detect any error which might have
4679 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004680 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4681 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004682static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004683{
4684 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004685 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004686 struct buffer outbuf;
4687 size_t total = 0;
4688 int es_now = 0;
4689 int bsize; /* htx block size */
4690 int fsize; /* h2 frame size */
4691 struct htx_blk *blk;
4692 enum htx_blk_type type;
4693 int idx;
4694
4695 if (h2c_mux_busy(h2c, h2s)) {
4696 h2s->flags |= H2_SF_BLK_MBUSY;
4697 goto end;
4698 }
4699
4700 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4701 h2c->flags |= H2_CF_MUX_MALLOC;
4702 h2s->flags |= H2_SF_BLK_MROOM;
4703 goto end;
4704 }
4705
Willy Tarreau98de12a2018-12-12 07:03:00 +01004706 htx = htx_from_buf(buf);
4707
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004708 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4709 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4710 * the caller to handle.
4711 */
4712
4713 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004714 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004715 goto end;
4716
4717 idx = htx_get_head(htx);
4718 blk = htx_get_blk(htx, idx);
4719 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4720 bsize = htx_get_blksz(blk);
4721 fsize = bsize;
4722
4723 if (type == HTX_BLK_EOD) {
4724 /* if we have an EOD, we're dealing with chunked data. We may
4725 * have a set of trailers after us that the caller will want to
4726 * deal with. Let's simply remove the EOD and return.
4727 */
4728 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004729 total++; // EOD counts as one byte
4730 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004731 goto end;
4732 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004733 else if (type == HTX_BLK_EOM) {
4734 if (h2s->flags & H2_SF_ES_SENT) {
4735 /* ES already sent */
4736 htx_remove_blk(htx, blk);
4737 total++; // EOM counts as one byte
4738 count--;
4739 goto end;
4740 }
4741 }
4742 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004743 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004744
4745 /* Perform some optimizations to reduce the number of buffer copies.
4746 * First, if the mux's buffer is empty and the htx area contains
4747 * exactly one data block of the same size as the requested count, and
4748 * this count fits within the frame size, the stream's window size, and
4749 * the connection's window size, then it's possible to simply swap the
4750 * caller's buffer with the mux's output buffer and adjust offsets and
4751 * length to match the entire DATA HTX block in the middle. In this
4752 * case we perform a true zero-copy operation from end-to-end. This is
4753 * the situation that happens all the time with large files. Second, if
4754 * this is not possible, but the mux's output buffer is empty, we still
4755 * have an opportunity to avoid the copy to the intermediary buffer, by
4756 * making the intermediary buffer's area point to the output buffer's
4757 * area. In this case we want to skip the HTX header to make sure that
4758 * copies remain aligned and that this operation remains possible all
4759 * the time. This goes for headers, data blocks and any data extracted
4760 * from the HTX blocks.
4761 */
4762 if (unlikely(fsize == count &&
4763 htx->used == 1 && type == HTX_BLK_DATA &&
4764 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4765 void *old_area = h2c->mbuf.area;
4766
4767 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004768 /* Too bad there are data left there. We're willing to memcpy/memmove
4769 * up to 1/4 of the buffer, which means that it's OK to copy a large
4770 * frame into a buffer containing few data if it needs to be realigned,
4771 * and that it's also OK to copy few data without realigning. Otherwise
4772 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004773 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004774 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4775 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4776 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004777 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004778
Willy Tarreau98de12a2018-12-12 07:03:00 +01004779 h2c->flags |= H2_CF_MUX_MFULL;
4780 h2s->flags |= H2_SF_BLK_MROOM;
4781 goto end;
4782 }
4783
4784 /* map an H2 frame to the HTX block so that we can put the
4785 * frame header there.
4786 */
4787 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004788 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004789 h2c->mbuf.data = fsize + 9;
4790 outbuf.area = b_head(&h2c->mbuf);
4791
4792 /* prepend an H2 DATA frame header just before the DATA block */
4793 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4794 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4795 h2_set_frame_size(outbuf.area, fsize);
4796
4797 /* update windows */
4798 h2s->mws -= fsize;
4799 h2c->mws -= fsize;
4800
4801 /* and exchange with our old area */
4802 buf->area = old_area;
4803 buf->data = buf->head = 0;
4804 total += fsize;
4805 goto end;
4806 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004807
Willy Tarreau98de12a2018-12-12 07:03:00 +01004808 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004809 /* for DATA and EOM we'll have to emit a frame, even if empty */
4810
4811 while (1) {
4812 outbuf.area = b_tail(&h2c->mbuf);
4813 outbuf.size = b_contig_space(&h2c->mbuf);
4814 outbuf.data = 0;
4815
4816 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4817 break;
4818 realign_again:
4819 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4820 }
4821
4822 if (outbuf.size < 9) {
4823 h2c->flags |= H2_CF_MUX_MFULL;
4824 h2s->flags |= H2_SF_BLK_MROOM;
4825 goto end;
4826 }
4827
4828 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4829 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4830 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4831 outbuf.data = 9;
4832
4833 /* we have in <fsize> the exact number of bytes we need to copy from
4834 * the HTX buffer. We need to check this against the connection's and
4835 * the stream's send windows, and to ensure that this fits in the max
4836 * frame size and in the buffer's available space minus 9 bytes (for
4837 * the frame header). The connection's flow control is applied last so
4838 * that we can use a separate list of streams which are immediately
4839 * unblocked on window opening. Note: we don't implement padding.
4840 */
4841
4842 /* EOM is presented with bsize==1 but would lead to the emission of an
4843 * empty frame, thus we force it to zero here.
4844 */
4845 if (type == HTX_BLK_EOM)
4846 bsize = fsize = 0;
4847
4848 if (!fsize)
4849 goto send_empty;
4850
4851 if (h2s->mws <= 0) {
4852 h2s->flags |= H2_SF_BLK_SFCTL;
4853 if (h2s->send_wait) {
4854 LIST_DEL(&h2s->list);
4855 LIST_INIT(&h2s->list);
4856 }
4857 goto end;
4858 }
4859
Willy Tarreauee573762018-12-04 15:25:57 +01004860 if (fsize > count)
4861 fsize = count;
4862
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004863 if (fsize > h2s->mws)
4864 fsize = h2s->mws; // >0
4865
4866 if (h2c->mfs && fsize > h2c->mfs)
4867 fsize = h2c->mfs; // >0
4868
4869 if (fsize + 9 > outbuf.size) {
4870 /* we have an opportunity for enlarging the too small
4871 * available space, let's try.
4872 * FIXME: is this really interesting to do? Maybe we'll
4873 * spend lots of time realigning instead of using two
4874 * frames.
4875 */
4876 if (b_space_wraps(&h2c->mbuf))
4877 goto realign_again;
4878 fsize = outbuf.size - 9;
4879
4880 if (fsize <= 0) {
4881 /* no need to send an empty frame here */
4882 h2c->flags |= H2_CF_MUX_MFULL;
4883 h2s->flags |= H2_SF_BLK_MROOM;
4884 goto end;
4885 }
4886 }
4887
4888 if (h2c->mws <= 0) {
4889 h2s->flags |= H2_SF_BLK_MFCTL;
4890 goto end;
4891 }
4892
4893 if (fsize > h2c->mws)
4894 fsize = h2c->mws;
4895
4896 /* now let's copy this this into the output buffer */
4897 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004898 h2s->mws -= fsize;
4899 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004900 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004901
4902 send_empty:
4903 /* update the frame's size */
4904 h2_set_frame_size(outbuf.area, fsize);
4905
4906 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4907 * meeting EOM. We should optimize this later.
4908 */
4909 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004910 total++; // EOM counts as one byte
4911 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004912 es_now = 1;
4913 }
4914
4915 if (es_now)
4916 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4917
4918 /* commit the H2 response */
4919 b_add(&h2c->mbuf, fsize + 9);
4920
4921 /* consume incoming HTX block, including EOM */
4922 total += fsize;
4923 if (fsize == bsize) {
4924 htx_remove_blk(htx, blk);
4925 if (fsize)
4926 goto new_frame;
4927 } else {
4928 /* we've truncated this block */
4929 htx_cut_data_blk(htx, blk, fsize);
4930 }
4931
4932 if (es_now) {
4933 if (h2s->st == H2_SS_OPEN)
4934 h2s->st = H2_SS_HLOC;
4935 else
4936 h2s_close(h2s);
4937
4938 h2s->flags |= H2_SF_ES_SENT;
4939 }
4940
4941 end:
4942 return total;
4943}
4944
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004945/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4946 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4947 * processed. The caller must check the stream's status to detect any error
4948 * which might have happened subsequently to a successful send. The htx blocks
4949 * are automatically removed from the message. The htx message is assumed to be
4950 * valid since produced from the internal code. Processing stops when meeting
4951 * the EOM, which is also removed. All trailers are processed at once and sent
4952 * as a single frame. The ES flag is always set.
4953 */
4954static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4955{
4956 struct http_hdr list[MAX_HTTP_HDR];
4957 struct h2c *h2c = h2s->h2c;
4958 struct htx_blk *blk;
4959 struct htx_blk *blk_end;
4960 struct buffer outbuf;
4961 struct h1m h1m;
4962 enum htx_blk_type type;
4963 uint32_t size;
4964 int ret = 0;
4965 int hdr;
4966 int idx;
4967 void *start;
4968
4969 if (h2c_mux_busy(h2c, h2s)) {
4970 h2s->flags |= H2_SF_BLK_MBUSY;
4971 goto end;
4972 }
4973
4974 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4975 h2c->flags |= H2_CF_MUX_MALLOC;
4976 h2s->flags |= H2_SF_BLK_MROOM;
4977 goto end;
4978 }
4979
4980 /* The principle is that we parse each and every trailers block using
4981 * the H1 headers parser, and append it to the list. We don't proceed
4982 * until EOM is met. blk_end will point to the EOM block.
4983 */
4984 hdr = 0;
4985 memset(list, 0, sizeof(list));
4986 blk_end = NULL;
4987
4988 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4989 blk = htx_get_blk(htx, idx);
4990 type = htx_get_blk_type(blk);
4991
4992 if (type == HTX_BLK_UNUSED)
4993 continue;
4994
4995 if (type != HTX_BLK_TLR) {
4996 if (type == HTX_BLK_EOM)
4997 blk_end = blk;
4998 break;
4999 }
5000
5001 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5002 goto fail;
5003
5004 size = htx_get_blksz(blk);
5005 start = htx_get_blk_ptr(htx, blk);
5006
5007 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5008 h1m.err_pos = 0;
5009 ret = h1_headers_to_hdr_list(start, start + size,
5010 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5011 &h1m, NULL);
5012 if (ret < 0)
5013 goto fail;
5014
5015 /* ret == 0 if an incomplete trailers block was found (missing
5016 * empty line), or > 0 if it was found. We have to continue on
5017 * incomplete messages because the trailers block might be
5018 * incomplete.
5019 */
5020
5021 /* search the new end */
5022 while (hdr <= sizeof(list)/sizeof(list[0])) {
5023 if (!list[hdr].n.len)
5024 break;
5025 hdr++;
5026 }
5027 }
5028
5029 if (!blk_end)
5030 goto end; // end not found yet
5031
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005032 chunk_reset(&outbuf);
5033
5034 while (1) {
5035 outbuf.area = b_tail(&h2c->mbuf);
5036 outbuf.size = b_contig_space(&h2c->mbuf);
5037 outbuf.data = 0;
5038
5039 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5040 break;
5041 realign_again:
5042 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5043 }
5044
5045 if (outbuf.size < 9)
5046 goto full;
5047
5048 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5049 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5050 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5051 outbuf.data = 9;
5052
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005053 /* encode all headers */
5054 for (idx = 0; idx < hdr; idx++) {
5055 /* these ones do not exist in H2 or must not appear in
5056 * trailers and must be dropped.
5057 */
5058 if (isteq(list[idx].n, ist("host")) ||
5059 isteq(list[idx].n, ist("content-length")) ||
5060 isteq(list[idx].n, ist("connection")) ||
5061 isteq(list[idx].n, ist("proxy-connection")) ||
5062 isteq(list[idx].n, ist("keep-alive")) ||
5063 isteq(list[idx].n, ist("upgrade")) ||
5064 isteq(list[idx].n, ist("te")) ||
5065 isteq(list[idx].n, ist("transfer-encoding")))
5066 continue;
5067
5068 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5069 /* output full */
5070 if (b_space_wraps(&h2c->mbuf))
5071 goto realign_again;
5072 goto full;
5073 }
5074 }
5075
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005076 if (!hdr) {
5077 /* here we have a problem, we've received an empty trailers
5078 * block followed by an EOM. Because of this we can't send a
5079 * HEADERS frame, so we have to cheat and instead send an empty
5080 * DATA frame conveying the ES flag.
5081 */
5082 outbuf.area[3] = H2_FT_DATA;
5083 outbuf.area[4] = H2_F_DATA_END_STREAM;
5084 }
5085
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005086 /* update the frame's size */
5087 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5088
5089 /* commit the H2 response */
5090 b_add(&h2c->mbuf, outbuf.data);
5091 h2s->flags |= H2_SF_ES_SENT;
5092
5093 if (h2s->st == H2_SS_OPEN)
5094 h2s->st = H2_SS_HLOC;
5095 else
5096 h2s_close(h2s);
5097
5098 /* OK we could properly deliver the response */
5099 done:
5100 /* remove all header blocks including EOM and compute the corresponding size. */
5101 ret = 0;
5102 idx = htx_get_head(htx);
5103 blk = htx_get_blk(htx, idx);
5104 while (blk != blk_end) {
5105 ret += htx_get_blksz(blk);
5106 blk = htx_remove_blk(htx, blk);
5107 }
5108 blk = htx_remove_blk(htx, blk);
5109 end:
5110 return ret;
5111 full:
5112 h2c->flags |= H2_CF_MUX_MFULL;
5113 h2s->flags |= H2_SF_BLK_MROOM;
5114 ret = 0;
5115 goto end;
5116 fail:
5117 /* unparsable HTX messages, too large ones to be produced in the local
5118 * list etc go here (unrecoverable errors).
5119 */
5120 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5121 ret = 0;
5122 goto end;
5123}
5124
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005125/* Called from the upper layer, to subscribe to events, such as being able to send.
5126 * The <param> argument here is supposed to be a pointer to a wait_event struct
5127 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5128 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5129 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5130 * returns 0 except for the error above.
5131 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005132static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5133{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005134 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005135 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005136 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005137
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005138 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005139 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005140 if (!(sw->events & SUB_RETRY_RECV)) {
5141 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005142 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005143 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005144 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005145 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005146 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005147 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005148 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005149 if (!(sw->events & SUB_RETRY_SEND)) {
5150 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005151 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005152 h2s->send_wait = sw;
Olivier Houchard9a0f5592019-04-15 19:22:24 +02005153 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5154 LIST_ISEMPTY(&h2s->list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005155 if (h2s->flags & H2_SF_BLK_MFCTL)
5156 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5157 else
5158 LIST_ADDQ(&h2c->send_list, &h2s->list);
5159 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005160 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005161 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005162 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005163 if (event_type != 0)
5164 return -1;
5165 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005166}
5167
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005168/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5169 * The <param> argument here is supposed to be a pointer to the same wait_event
5170 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5171 * It always returns zero.
5172 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005173static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5174{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005175 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005176 struct h2s *h2s = cs->ctx;
5177
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005178 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005179 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005180 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005181 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005182 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005183 }
5184 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005185 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005186 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005187 if (h2s->send_wait == sw) {
5188 LIST_DEL(&h2s->list);
5189 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005190 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005191 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005192 }
5193 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005194 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5195 sw = param;
5196 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005197 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Willy Tarreaue73256f2019-03-25 18:02:54 +01005198 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005199 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005200 LIST_DEL(&h2s->list);
5201 LIST_INIT(&h2s->list);
Olivier Houchard3ea35132019-03-25 14:10:42 +01005202 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005203 }
5204 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005205 return 0;
5206}
5207
5208
Olivier Houchard511efea2018-08-16 15:30:32 +02005209/* Called from the upper layer, to receive data */
5210static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5211{
Olivier Houchard638b7992018-08-16 15:41:52 +02005212 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005213 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005214 struct htx *h2s_htx = NULL;
5215 struct htx *buf_htx = NULL;
5216 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005217 size_t ret = 0;
5218
5219 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005220 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5221 /* in HTX mode we ignore the count argument */
5222 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005223 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005224 /* Here htx_to_buf() will set buffer data to 0 because
5225 * the HTX is empty.
5226 */
5227 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005228 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005229 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005230
5231 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005232 count = htx_free_data_space(buf_htx);
5233 if (flags & CO_RFL_KEEP_RSV) {
5234 if (count <= global.tune.maxrewrite)
5235 goto end;
5236 count -= global.tune.maxrewrite;
5237 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005238
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005239 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005240
5241 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5242 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5243
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005244 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005245 htx_to_buf(buf_htx, buf);
5246 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005247 ret = htx_ret.ret;
5248 }
5249 else {
5250 ret = b_xfer(buf, &h2s->rxbuf, count);
5251 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005252
Christopher Faulet37070b22019-02-14 15:12:14 +01005253 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005254 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005255 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005256 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005257 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005258 if (cs->flags & CS_FL_REOS)
5259 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005260 if (cs->flags & CS_FL_ERR_PENDING)
5261 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005262 if (b_size(&h2s->rxbuf)) {
5263 b_free(&h2s->rxbuf);
5264 offer_buffers(NULL, tasks_run_queue);
5265 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005266 }
5267
Willy Tarreau082f5592018-11-25 08:03:32 +01005268 if (ret && h2c->dsi == h2s->id) {
5269 /* demux is blocking on this stream's buffer */
5270 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005271 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005272 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005273
Olivier Houchard511efea2018-08-16 15:30:32 +02005274 return ret;
5275}
5276
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005277/* stops all senders of this connection for example when the mux buffer is full.
5278 * They are moved from the sending_list to either fctl_list or send_list.
5279 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005280static void h2_stop_senders(struct h2c *h2c)
5281{
5282 struct h2s *h2s, *h2s_back;
5283
Olivier Houchardd360ac62019-03-22 17:37:16 +01005284 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5285 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005286 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005287 h2s->send_wait->events |= SUB_RETRY_SEND;
5288 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005289 }
5290}
5291
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005292/* Called from the upper layer, to send data from buffer <buf> for no more than
5293 * <count> bytes. Returns the number of bytes effectively sent. Some status
5294 * flags may be updated on the conn_stream.
5295 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005296static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005297{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005298 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005299 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005300 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005301 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005302 struct htx *htx;
5303 struct htx_blk *blk;
5304 enum htx_blk_type btype;
5305 uint32_t bsize;
5306 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005307
Olivier Houchardd360ac62019-03-22 17:37:16 +01005308 /* If we were not just woken because we wanted to send but couldn't,
5309 * and there's somebody else that is waiting to send, do nothing,
5310 * we will subscribe later and be put at the end of the list
5311 */
5312 LIST_DEL_INIT(&h2s->sending_list);
5313 if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) &&
5314 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5315 return 0;
5316
Olivier Houchardd846c262018-10-19 17:24:29 +02005317 if (h2s->send_wait) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005318 /* We want to stay in the send_list, so prepare ourself to be
5319 * eventually recalled if needed, and only remove ourself from
5320 * the list if we managed to send anything.
5321 */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005322 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01005323 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005324 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005325 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5326 return 0;
5327
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005328 /* htx will be enough to decide if we're using HTX or legacy */
5329 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5330
Willy Tarreau0bad0432018-06-14 16:54:01 +02005331 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005332 h2s->flags |= H2_SF_OUTGOING_DATA;
5333
Willy Tarreau751f2d02018-10-05 09:35:00 +02005334 if (h2s->id == 0) {
5335 int32_t id = h2c_get_next_sid(h2s->h2c);
5336
5337 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005338 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005339 return 0;
5340 }
5341
5342 eb32_delete(&h2s->by_id);
5343 h2s->by_id.key = h2s->id = id;
5344 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005345 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005346 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5347 }
5348
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005349 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005350 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5351 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005352 idx = htx_get_head(htx);
5353 blk = htx_get_blk(htx, idx);
5354 btype = htx_get_blk_type(blk);
5355 bsize = htx_get_blksz(blk);
5356
5357 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005358 case HTX_BLK_REQ_SL:
5359 /* start-line before headers */
5360 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5361 if (ret > 0) {
5362 total += ret;
5363 count -= ret;
5364 if (ret < bsize)
5365 goto done;
5366 }
5367 break;
5368
Willy Tarreau115e83b2018-12-01 19:17:53 +01005369 case HTX_BLK_RES_SL:
5370 /* start-line before headers */
5371 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5372 if (ret > 0) {
5373 total += ret;
5374 count -= ret;
5375 if (ret < bsize)
5376 goto done;
5377 }
5378 break;
5379
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005380 case HTX_BLK_DATA:
5381 case HTX_BLK_EOD:
5382 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005383 /* all these cause the emission of a DATA frame (possibly empty).
5384 * This EOM necessarily is one before trailers, as the EOM following
5385 * trailers would have been consumed by the trailers parser.
5386 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005387 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005388 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005389 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005390 total += ret;
5391 count -= ret;
5392 if (ret < bsize)
5393 goto done;
5394 }
5395 break;
5396
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005397 case HTX_BLK_TLR:
5398 /* This is the first trailers block, all the subsequent ones AND
5399 * the EOM will be swallowed by the parser.
5400 */
5401 ret = h2s_htx_make_trailers(h2s, htx);
5402 if (ret > 0) {
5403 total += ret;
5404 count -= ret;
5405 if (ret < bsize)
5406 goto done;
5407 }
5408 break;
5409
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005410 default:
5411 htx_remove_blk(htx, blk);
5412 total += bsize;
5413 count -= bsize;
5414 break;
5415 }
5416 }
5417 goto done;
5418 }
5419
5420 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005421 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005422 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005423 if (h2s->h2c->flags & H2_CF_IS_BACK)
5424 ret = -1;
5425 else
5426 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005427 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005428 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005429 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005430 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005431 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005432 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005433 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005434
Willy Tarreau5dd17352018-06-14 13:33:30 +02005435 if (unlikely((int)ret <= 0)) {
5436 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005437 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5438 break;
5439 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005440 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005441 total += count;
5442 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005443 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005444 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005445 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005446 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005447 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005448 break;
5449 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005450
5451 total += ret;
5452 count -= ret;
5453
5454 if (h2s->st >= H2_SS_ERROR)
5455 break;
5456
5457 if (h2s->flags & H2_SF_BLK_ANY)
5458 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005459 }
5460
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005461 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005462 if (h2s->st >= H2_SS_ERROR) {
5463 /* trim any possibly pending data after we close (extra CR-LF,
5464 * unprocessed trailers, abnormal extra data, ...)
5465 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005466 total += count;
5467 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005468 }
5469
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005470 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005471 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005472 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005473 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005474 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005475 }
5476
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005477 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005478 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005479 } else {
5480 b_del(buf, total);
5481 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005482
5483 /* The mux is full, cancel the pending tasks */
5484 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5485 (h2s->flags & H2_SF_BLK_MBUSY))
5486 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005487
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005488 /* If we're running HTX, and we read the whole buffer, then pretend
5489 * we read exactly what the caller specified, as with HTX the caller
5490 * will always give the buffer size, instead of the amount of data
5491 * available.
5492 */
5493 if (htx && !b_data(buf))
5494 total = orig_count;
5495
Olivier Houchard7505f942018-08-21 18:10:44 +02005496 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005497 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005498 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005499
Olivier Houchard7505f942018-08-21 18:10:44 +02005500 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005501 /* If we're waiting for flow control, and we got a shutr on the
5502 * connection, we will never be unlocked, so add an error on
5503 * the conn_stream.
5504 */
5505 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5506 !b_data(&h2s->h2c->dbuf) &&
5507 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5508 if (cs->flags & CS_FL_EOS)
5509 cs->flags |= CS_FL_ERROR;
5510 else
5511 cs->flags |= CS_FL_ERR_PENDING;
5512 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01005513 if (total > 0 && h2s->send_wait) {
5514 /* Ok we managed to send something, leave the send_list */
5515 h2s->send_wait->events &= ~SUB_RETRY_SEND;
5516 h2s->send_wait = NULL;
5517 LIST_DEL_INIT(&h2s->list);
5518 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005519 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005520}
5521
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005522/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005523static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005524{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005525 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005526 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005527 struct eb32_node *node;
5528 int fctl_cnt = 0;
5529 int send_cnt = 0;
5530 int tree_cnt = 0;
5531 int orph_cnt = 0;
5532
5533 if (!h2c)
5534 return;
5535
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005536 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005537 fctl_cnt++;
5538
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005539 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005540 send_cnt++;
5541
Willy Tarreau3af37712018-12-18 14:34:41 +01005542 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005543 node = eb32_first(&h2c->streams_by_id);
5544 while (node) {
5545 h2s = container_of(node, struct h2s, by_id);
5546 tree_cnt++;
5547 if (!h2s->cs)
5548 orph_cnt++;
5549 node = eb32_next(node);
5550 }
5551
Willy Tarreau987c0632018-12-18 10:32:05 +01005552 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5553 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5554 " .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 +02005555 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5556 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005557 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005558 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5559 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5560 h2c->msi,
5561 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5562 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5563
5564 if (h2s) {
5565 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5566 h2s, h2s->id, h2s->flags,
5567 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5568 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5569 h2s->cs);
5570 if (h2s->cs)
5571 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5572 h2s->cs->flags, h2s->cs->data);
5573 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005574}
Willy Tarreau62f52692017-10-08 23:01:42 +02005575
5576/*******************************************************/
5577/* functions below are dedicated to the config parsers */
5578/*******************************************************/
5579
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005580/* config parser for global "tune.h2.header-table-size" */
5581static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5582 struct proxy *defpx, const char *file, int line,
5583 char **err)
5584{
5585 if (too_many_args(1, args, err, NULL))
5586 return -1;
5587
5588 h2_settings_header_table_size = atoi(args[1]);
5589 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5590 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5591 return -1;
5592 }
5593 return 0;
5594}
Willy Tarreau62f52692017-10-08 23:01:42 +02005595
Willy Tarreaue6baec02017-07-27 11:45:11 +02005596/* config parser for global "tune.h2.initial-window-size" */
5597static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5598 struct proxy *defpx, const char *file, int line,
5599 char **err)
5600{
5601 if (too_many_args(1, args, err, NULL))
5602 return -1;
5603
5604 h2_settings_initial_window_size = atoi(args[1]);
5605 if (h2_settings_initial_window_size < 0) {
5606 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5607 return -1;
5608 }
5609 return 0;
5610}
5611
Willy Tarreau5242ef82017-07-27 11:47:28 +02005612/* config parser for global "tune.h2.max-concurrent-streams" */
5613static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5614 struct proxy *defpx, const char *file, int line,
5615 char **err)
5616{
5617 if (too_many_args(1, args, err, NULL))
5618 return -1;
5619
5620 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005621 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005622 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5623 return -1;
5624 }
5625 return 0;
5626}
5627
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005628/* config parser for global "tune.h2.max-frame-size" */
5629static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5630 struct proxy *defpx, const char *file, int line,
5631 char **err)
5632{
5633 if (too_many_args(1, args, err, NULL))
5634 return -1;
5635
5636 h2_settings_max_frame_size = atoi(args[1]);
5637 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5638 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5639 return -1;
5640 }
5641 return 0;
5642}
5643
Willy Tarreau62f52692017-10-08 23:01:42 +02005644
5645/****************************************/
5646/* MUX initialization and instanciation */
5647/***************************************/
5648
5649/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005650static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005651 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005652 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005653 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005654 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005655 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005656 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005657 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005658 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005659 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005660 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005661 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005662 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005663 .shutr = h2_shutr,
5664 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005665 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005666 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005667 .name = "H2",
5668};
5669
Christopher Faulet32f61c02018-04-10 14:33:41 +02005670/* PROTO selection : this mux registers PROTO token "h2" */
5671static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005672 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005673
Willy Tarreau0108d902018-11-25 19:14:37 +01005674INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5675
Christopher Faulet9b579102019-04-11 22:52:25 +02005676
5677/* The mux operations */
5678static const struct mux_ops h2_htx_ops = {
5679 .init = h2_init,
5680 .wake = h2_wake,
5681 .snd_buf = h2_snd_buf,
5682 .rcv_buf = h2_rcv_buf,
5683 .subscribe = h2_subscribe,
5684 .unsubscribe = h2_unsubscribe,
5685 .attach = h2_attach,
5686 .get_first_cs = h2_get_first_cs,
5687 .detach = h2_detach,
5688 .destroy = h2_destroy,
5689 .avail_streams = h2_avail_streams,
5690 .used_streams = h2_used_streams,
5691 .shutr = h2_shutr,
5692 .shutw = h2_shutw,
5693 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005694 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005695 .name = "H2",
5696};
5697
Willy Tarreauf8957272018-10-03 10:25:20 +02005698static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005699 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005700
5701INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5702
Willy Tarreau62f52692017-10-08 23:01:42 +02005703/* config keyword parsers */
5704static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005705 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005706 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005707 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005708 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005709 { 0, NULL, NULL }
5710}};
5711
Willy Tarreau0108d902018-11-25 19:14:37 +01005712INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);