blob: 894c4bdbbf30cc7dd679b8b48e613430ac035fd3 [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);
Willy Tarreauea392822017-10-31 10:02:25 +0100645
Willy Tarreaubafbe012017-11-24 17:34:44 +0100646 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200647 }
648
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200649 if (conn) {
650 conn->mux = NULL;
651 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200652
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200653 conn_force_unsubscribe(conn);
654 conn_stop_tracking(conn);
655 conn_full_close(conn);
656 if (conn->destroy_cb)
657 conn->destroy_cb(conn);
658 conn_free(conn);
659 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200660}
661
662
Willy Tarreau71681172017-10-23 14:39:06 +0200663/******************************************************/
664/* functions below are for the H2 protocol processing */
665/******************************************************/
666
667/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100668static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200669{
670 return h2s ? h2s->id : 0;
671}
672
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200673/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100674static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200675{
676 if (h2c->msi < 0)
677 return 0;
678
679 if (h2c->msi == h2s_id(h2s))
680 return 0;
681
682 return 1;
683}
684
Willy Tarreau741d6df2017-10-17 08:00:59 +0200685/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100686static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200687{
688 h2c->errcode = err;
689 h2c->st0 = H2_CS_ERROR;
690}
691
Willy Tarreau175cebb2019-01-24 10:02:24 +0100692/* marks an error on the stream. It may also update an already closed stream
693 * (e.g. to report an error after an RST was received).
694 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100695static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200696{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100697 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200698 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100699 if (h2s->st < H2_SS_ERROR)
700 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100701 if (h2s->cs)
702 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200703 }
704}
705
Willy Tarreau7e094452018-12-19 18:08:52 +0100706/* attempt to notify the data layer of recv availability */
707static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
708{
709 struct wait_event *sw;
710
711 if (h2s->recv_wait) {
712 sw = h2s->recv_wait;
713 sw->events &= ~SUB_RETRY_RECV;
714 tasklet_wakeup(sw->task);
715 h2s->recv_wait = NULL;
716 }
717}
718
719/* attempt to notify the data layer of send availability */
720static void __maybe_unused h2s_notify_send(struct h2s *h2s)
721{
722 struct wait_event *sw;
723
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100724 if (h2s->send_wait && !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100725 sw = h2s->send_wait;
726 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100727 sw->events |= SUB_CALL_UNSUBSCRIBE;
728 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100729 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100730 }
731}
732
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100733/* alerts the data layer, trying to wake it up by all means, following
734 * this sequence :
735 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
736 * - if its subscribed to send, then it's woken up for send
737 * - if it was subscribed to neither, its ->wake() callback is called
738 * It is safe to call this function with a closed stream which doesn't have a
739 * conn_stream anymore.
740 */
741static void __maybe_unused h2s_alert(struct h2s *h2s)
742{
743 if (h2s->recv_wait || h2s->send_wait) {
744 h2s_notify_recv(h2s);
745 h2s_notify_send(h2s);
746 }
747 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
748 h2s->cs->data_cb->wake(h2s->cs);
749}
750
Willy Tarreaue4820742017-07-27 13:37:23 +0200751/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100752static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200753{
754 uint8_t *out = frame;
755
756 *out = len >> 16;
757 write_n16(out + 1, len);
758}
759
Willy Tarreau54c15062017-10-10 17:10:03 +0200760/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
761 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
762 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200763 * available in the buffer's input prior to calling this function. The buffer
764 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200765 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100766static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200767 const struct buffer *b, int o)
768{
Willy Tarreau591d4452018-06-15 17:21:00 +0200769 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 +0200770}
771
Willy Tarreau1f094672017-11-20 21:27:45 +0100772static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200773{
Willy Tarreau591d4452018-06-15 17:21:00 +0200774 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200775}
776
Willy Tarreau1f094672017-11-20 21:27:45 +0100777static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200778{
Willy Tarreau591d4452018-06-15 17:21:00 +0200779 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200780}
781
Willy Tarreau1f094672017-11-20 21:27:45 +0100782static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200783{
Willy Tarreau591d4452018-06-15 17:21:00 +0200784 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200785}
786
787
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100788/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
789 * The algorithm is not obvious. It turns out that H2 headers are neither
790 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
791 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200792 *
793 * b0 b1 b2 b3 b4 b5..b8
794 * +----------+---------+--------+----+----+----------------------+
795 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
796 * +----------+---------+--------+----+----+----------------------+
797 *
798 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
799 * we get the sid properly aligned and ordered, and 16 bits of len properly
800 * ordered as well. The type and flags can be extracted using bit shifts from
801 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200802 * Returns zero if some bytes are missing, otherwise non-zero on success. The
803 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200804 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100805static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200806{
807 uint64_t w;
808
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100809 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200810 return 0;
811
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100812 w = h2_get_n64(b, o + 1);
813 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200814 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
815 h->ff = w >> 32;
816 h->ft = w >> 40;
817 h->len += w >> 48;
818 return 1;
819}
820
821/* skip the next 9 bytes corresponding to the frame header possibly parsed by
822 * h2_peek_frame_hdr() above.
823 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100824static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200825{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200826 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200827}
828
829/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100830static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200831{
832 int ret;
833
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100834 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200835 if (ret > 0)
836 h2_skip_frame_hdr(b);
837 return ret;
838}
839
Willy Tarreau00dd0782018-03-01 16:31:34 +0100840/* marks stream <h2s> as CLOSED and decrement the number of active streams for
841 * its connection if the stream was not yet closed. Please use this exclusively
842 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100843 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100844static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100845{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100846 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100847 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100848 if (!h2s->id)
849 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100850 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100851 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100852 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
853 h2s_notify_recv(h2s);
854 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100855 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100856 h2s->st = H2_SS_CLOSED;
857}
858
Willy Tarreau71049cc2018-03-28 13:56:39 +0200859/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
860static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100861{
862 h2s_close(h2s);
863 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200864 if (b_size(&h2s->rxbuf)) {
865 b_free(&h2s->rxbuf);
866 offer_buffers(NULL, tasks_run_queue);
867 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200868 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100869 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200870 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100871 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800872 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200873 * reference left would be in the h2c send_list/fctl_list, and if
874 * we're in it, we're getting out anyway
875 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100876 LIST_DEL_INIT(&h2s->list);
877 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200878 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100879 pool_free(pool_head_h2s, h2s);
880}
881
Willy Tarreaua8e49542018-10-03 18:53:55 +0200882/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
883 * stream tree. In case of error, nothing is added and NULL is returned. The
884 * causes of errors can be any failed memory allocation. The caller is
885 * responsible for checking if the connection may support an extra stream
886 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200887 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200888static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200889{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200890 struct h2s *h2s;
891
Willy Tarreaubafbe012017-11-24 17:34:44 +0100892 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200893 if (!h2s)
894 goto out;
895
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200896 h2s->wait_event.task = tasklet_new();
897 if (!h2s->wait_event.task) {
898 pool_free(pool_head_h2s, h2s);
899 goto out;
900 }
901 h2s->send_wait = NULL;
902 h2s->recv_wait = NULL;
903 h2s->wait_event.task->process = h2_deferred_shut;
904 h2s->wait_event.task->context = h2s;
905 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100906 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200907 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100908 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200909 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200910 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200911 h2s->mws = h2c->miw;
912 h2s->flags = H2_SF_NONE;
913 h2s->errcode = H2_ERR_NO_ERROR;
914 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200915 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100916 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200917 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200918
919 if (h2c->flags & H2_CF_IS_BACK) {
920 h1m_init_req(&h2s->h1m);
921 h2s->h1m.err_pos = -1; // don't care about errors on the request path
922 h2s->h1m.flags |= H1_MF_TOLOWER;
923 } else {
924 h1m_init_res(&h2s->h1m);
925 h2s->h1m.err_pos = -1; // don't care about errors on the response path
926 h2s->h1m.flags |= H1_MF_TOLOWER;
927 }
928
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200929 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200930 if (id > 0)
931 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100932 else
933 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200934
935 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100936 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100937 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200938
939 return h2s;
940
941 out_free_h2s:
942 pool_free(pool_head_h2s, h2s);
943 out:
944 return NULL;
945}
946
947/* creates a new stream <id> on the h2c connection and returns it, or NULL in
948 * case of memory allocation error.
949 */
950static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
951{
952 struct session *sess = h2c->conn->owner;
953 struct conn_stream *cs;
954 struct h2s *h2s;
955
956 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
957 goto out;
958
959 h2s = h2s_new(h2c, id);
960 if (!h2s)
961 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200962
963 cs = cs_new(h2c->conn);
964 if (!cs)
965 goto out_close;
966
Olivier Houchard746fb772018-12-15 19:42:00 +0100967 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200968 h2s->cs = cs;
969 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200970 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200971
972 if (stream_create_from_cs(cs) < 0)
973 goto out_free_cs;
974
Willy Tarreau590a0512018-09-05 11:56:48 +0200975 /* We want the accept date presented to the next stream to be the one
976 * we have now, the handshake time to be null (since the next stream
977 * is not delayed by a handshake), and the idle time to count since
978 * right now.
979 */
980 sess->accept_date = date;
981 sess->tv_accept = now;
982 sess->t_handshake = 0;
983
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200984 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100985 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200986 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200987 return h2s;
988
989 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200990 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200991 cs_free(cs);
992 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200993 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200994 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200995 sess_log(sess);
996 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200997}
998
Willy Tarreau751f2d02018-10-05 09:35:00 +0200999/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1000 * and returns it, or NULL in case of memory allocation error or if the highest
1001 * possible stream ID was reached.
1002 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001003static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001004{
1005 struct h2s *h2s = NULL;
1006
Willy Tarreau86949782019-01-31 10:42:05 +01001007 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001008 goto out;
1009
Willy Tarreaua80dca82019-01-24 17:08:28 +01001010 if (h2_streams_left(h2c) < 1)
1011 goto out;
1012
Willy Tarreau751f2d02018-10-05 09:35:00 +02001013 /* Defer choosing the ID until we send the first message to create the stream */
1014 h2s = h2s_new(h2c, 0);
1015 if (!h2s)
1016 goto out;
1017
1018 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001019 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001020 cs->ctx = h2s;
1021 h2c->nb_cs++;
1022
Willy Tarreau751f2d02018-10-05 09:35:00 +02001023 out:
1024 return h2s;
1025}
1026
Willy Tarreaube5b7152017-09-25 16:25:39 +02001027/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1028 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1029 * the various settings codes.
1030 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001031static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001032{
1033 struct buffer *res;
1034 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001035 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001036 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001037 int ret;
1038
1039 if (h2c_mux_busy(h2c, NULL)) {
1040 h2c->flags |= H2_CF_DEM_MBUSY;
1041 return 0;
1042 }
1043
Willy Tarreau44e973f2018-03-01 17:49:30 +01001044 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001045 if (!res) {
1046 h2c->flags |= H2_CF_MUX_MALLOC;
1047 h2c->flags |= H2_CF_DEM_MROOM;
1048 return 0;
1049 }
1050
1051 chunk_init(&buf, buf_data, sizeof(buf_data));
1052 chunk_memcpy(&buf,
1053 "\x00\x00\x00" /* length : 0 for now */
1054 "\x04\x00" /* type : 4 (settings), flags : 0 */
1055 "\x00\x00\x00\x00", /* stream ID : 0 */
1056 9);
1057
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001058 if (h2c->flags & H2_CF_IS_BACK) {
1059 /* send settings_enable_push=0 */
1060 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1061 }
1062
Willy Tarreaube5b7152017-09-25 16:25:39 +02001063 if (h2_settings_header_table_size != 4096) {
1064 char str[6] = "\x00\x01"; /* header_table_size */
1065
1066 write_n32(str + 2, h2_settings_header_table_size);
1067 chunk_memcat(&buf, str, 6);
1068 }
1069
1070 if (h2_settings_initial_window_size != 65535) {
1071 char str[6] = "\x00\x04"; /* initial_window_size */
1072
1073 write_n32(str + 2, h2_settings_initial_window_size);
1074 chunk_memcat(&buf, str, 6);
1075 }
1076
1077 if (h2_settings_max_concurrent_streams != 0) {
1078 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1079
1080 /* Note: 0 means "unlimited" for haproxy's config but not for
1081 * the protocol, so never send this value!
1082 */
1083 write_n32(str + 2, h2_settings_max_concurrent_streams);
1084 chunk_memcat(&buf, str, 6);
1085 }
1086
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001087 mfs = h2_settings_max_frame_size;
1088 if (mfs > global.tune.bufsize)
1089 mfs = global.tune.bufsize;
1090
1091 if (!mfs)
1092 mfs = global.tune.bufsize;
1093
1094 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001095 char str[6] = "\x00\x05"; /* max_frame_size */
1096
1097 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1098 * match bufsize - rewrite size, but at the moment it seems
1099 * that clients don't take care of it.
1100 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001101 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001102 chunk_memcat(&buf, str, 6);
1103 }
1104
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001105 h2_set_frame_size(buf.area, buf.data - 9);
1106 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001107 if (unlikely(ret <= 0)) {
1108 if (!ret) {
1109 h2c->flags |= H2_CF_MUX_MFULL;
1110 h2c->flags |= H2_CF_DEM_MROOM;
1111 return 0;
1112 }
1113 else {
1114 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1115 return 0;
1116 }
1117 }
1118 return ret;
1119}
1120
Willy Tarreau52eed752017-09-22 15:05:09 +02001121/* Try to receive a connection preface, then upon success try to send our
1122 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1123 * missing data. It may return an error in h2c.
1124 */
1125static int h2c_frt_recv_preface(struct h2c *h2c)
1126{
1127 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001128 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001129
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001130 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001131
1132 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001133 if (ret1 < 0)
1134 sess_log(h2c->conn->owner);
1135
Willy Tarreau52eed752017-09-22 15:05:09 +02001136 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1137 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1138 return 0;
1139 }
1140
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001141 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001142 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001143 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001144
Willy Tarreaube5b7152017-09-25 16:25:39 +02001145 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001146}
1147
Willy Tarreau01b44822018-10-03 14:26:37 +02001148/* Try to send a connection preface, then upon success try to send our
1149 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1150 * missing data. It may return an error in h2c.
1151 */
1152static int h2c_bck_send_preface(struct h2c *h2c)
1153{
1154 struct buffer *res;
1155
1156 if (h2c_mux_busy(h2c, NULL)) {
1157 h2c->flags |= H2_CF_DEM_MBUSY;
1158 return 0;
1159 }
1160
1161 res = h2_get_buf(h2c, &h2c->mbuf);
1162 if (!res) {
1163 h2c->flags |= H2_CF_MUX_MALLOC;
1164 h2c->flags |= H2_CF_DEM_MROOM;
1165 return 0;
1166 }
1167
1168 if (!b_data(res)) {
1169 /* preface not yet sent */
1170 b_istput(res, ist(H2_CONN_PREFACE));
1171 }
1172
1173 return h2c_send_settings(h2c);
1174}
1175
Willy Tarreau081d4722017-05-16 21:51:05 +02001176/* try to send a GOAWAY frame on the connection to report an error or a graceful
1177 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1178 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1179 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1180 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1181 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1182 * on unrecoverable failure. It will not attempt to send one again in this last
1183 * case so that it is safe to use h2c_error() to report such errors.
1184 */
1185static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1186{
1187 struct buffer *res;
1188 char str[17];
1189 int ret;
1190
1191 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1192 return 1; // claim that it worked
1193
1194 if (h2c_mux_busy(h2c, h2s)) {
1195 if (h2s)
1196 h2s->flags |= H2_SF_BLK_MBUSY;
1197 else
1198 h2c->flags |= H2_CF_DEM_MBUSY;
1199 return 0;
1200 }
1201
Willy Tarreau44e973f2018-03-01 17:49:30 +01001202 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001203 if (!res) {
1204 h2c->flags |= H2_CF_MUX_MALLOC;
1205 if (h2s)
1206 h2s->flags |= H2_SF_BLK_MROOM;
1207 else
1208 h2c->flags |= H2_CF_DEM_MROOM;
1209 return 0;
1210 }
1211
1212 /* len: 8, type: 7, flags: none, sid: 0 */
1213 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1214
1215 if (h2c->last_sid < 0)
1216 h2c->last_sid = h2c->max_id;
1217
1218 write_n32(str + 9, h2c->last_sid);
1219 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001220 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001221 if (unlikely(ret <= 0)) {
1222 if (!ret) {
1223 h2c->flags |= H2_CF_MUX_MFULL;
1224 if (h2s)
1225 h2s->flags |= H2_SF_BLK_MROOM;
1226 else
1227 h2c->flags |= H2_CF_DEM_MROOM;
1228 return 0;
1229 }
1230 else {
1231 /* we cannot report this error using GOAWAY, so we mark
1232 * it and claim a success.
1233 */
1234 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1235 h2c->flags |= H2_CF_GOAWAY_FAILED;
1236 return 1;
1237 }
1238 }
1239 h2c->flags |= H2_CF_GOAWAY_SENT;
1240 return ret;
1241}
1242
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001243/* Try to send an RST_STREAM frame on the connection for the indicated stream
1244 * during mux operations. This stream must be valid and cannot be closed
1245 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1246 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1247 * not yet.
1248 *
1249 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1250 * to write the message, it subscribes the stream to future notifications.
1251 */
1252static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1253{
1254 struct buffer *res;
1255 char str[13];
1256 int ret;
1257
1258 if (!h2s || h2s->st == H2_SS_CLOSED)
1259 return 1;
1260
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001261 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1262 * RST_STREAM in response to a RST_STREAM frame.
1263 */
1264 if (h2c->dft == H2_FT_RST_STREAM) {
1265 ret = 1;
1266 goto ignore;
1267 }
1268
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001269 if (h2c_mux_busy(h2c, h2s)) {
1270 h2s->flags |= H2_SF_BLK_MBUSY;
1271 return 0;
1272 }
1273
Willy Tarreau44e973f2018-03-01 17:49:30 +01001274 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001275 if (!res) {
1276 h2c->flags |= H2_CF_MUX_MALLOC;
1277 h2s->flags |= H2_SF_BLK_MROOM;
1278 return 0;
1279 }
1280
1281 /* len: 4, type: 3, flags: none */
1282 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1283 write_n32(str + 5, h2s->id);
1284 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001285 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001286
1287 if (unlikely(ret <= 0)) {
1288 if (!ret) {
1289 h2c->flags |= H2_CF_MUX_MFULL;
1290 h2s->flags |= H2_SF_BLK_MROOM;
1291 return 0;
1292 }
1293 else {
1294 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1295 return 0;
1296 }
1297 }
1298
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001299 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001300 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001301 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001302 return ret;
1303}
1304
1305/* Try to send an RST_STREAM frame on the connection for the stream being
1306 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001307 * error code, even if the stream is one of the dummy ones, and will update
1308 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001309 *
1310 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1311 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001312 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001313 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001314 */
1315static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1316{
1317 struct buffer *res;
1318 char str[13];
1319 int ret;
1320
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001321 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1322 * RST_STREAM in response to a RST_STREAM frame.
1323 */
1324 if (h2c->dft == H2_FT_RST_STREAM) {
1325 ret = 1;
1326 goto ignore;
1327 }
1328
Willy Tarreau27a84c92017-10-17 08:10:17 +02001329 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001330 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001331 return 0;
1332 }
1333
Willy Tarreau44e973f2018-03-01 17:49:30 +01001334 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001335 if (!res) {
1336 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001337 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001338 return 0;
1339 }
1340
1341 /* len: 4, type: 3, flags: none */
1342 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001343
Willy Tarreau27a84c92017-10-17 08:10:17 +02001344 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001345 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001346 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001347
Willy Tarreau27a84c92017-10-17 08:10:17 +02001348 if (unlikely(ret <= 0)) {
1349 if (!ret) {
1350 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001351 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001352 return 0;
1353 }
1354 else {
1355 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1356 return 0;
1357 }
1358 }
1359
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001360 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001361 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001362 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001363 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001364 }
1365
Willy Tarreau27a84c92017-10-17 08:10:17 +02001366 return ret;
1367}
1368
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001369/* try to send an empty DATA frame with the ES flag set to notify about the
1370 * end of stream and match a shutdown(write). If an ES was already sent as
1371 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1372 * on success or zero if nothing was done. In case of lack of room to write the
1373 * message, it subscribes the requesting stream to future notifications.
1374 */
1375static int h2_send_empty_data_es(struct h2s *h2s)
1376{
1377 struct h2c *h2c = h2s->h2c;
1378 struct buffer *res;
1379 char str[9];
1380 int ret;
1381
Willy Tarreau721c9742017-11-07 11:05:42 +01001382 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001383 return 1;
1384
1385 if (h2c_mux_busy(h2c, h2s)) {
1386 h2s->flags |= H2_SF_BLK_MBUSY;
1387 return 0;
1388 }
1389
Willy Tarreau44e973f2018-03-01 17:49:30 +01001390 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001391 if (!res) {
1392 h2c->flags |= H2_CF_MUX_MALLOC;
1393 h2s->flags |= H2_SF_BLK_MROOM;
1394 return 0;
1395 }
1396
1397 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1398 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1399 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001400 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001401 if (likely(ret > 0)) {
1402 h2s->flags |= H2_SF_ES_SENT;
1403 }
1404 else if (!ret) {
1405 h2c->flags |= H2_CF_MUX_MFULL;
1406 h2s->flags |= H2_SF_BLK_MROOM;
1407 return 0;
1408 }
1409 else {
1410 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1411 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001412 }
1413 return ret;
1414}
1415
Christopher Fauletf02ca002019-03-07 16:21:34 +01001416/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1417 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1418 * connection. The stream's state is automatically updated accordingly. If the
1419 * stream is orphaned, it is destroyed.
1420 */
1421static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1422{
1423 if (!h2s->cs) {
1424 /* this stream was already orphaned */
1425 h2s_destroy(h2s);
1426 return;
1427 }
1428
1429 h2s->cs->flags |= flags;
1430 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1431 h2s->cs->flags |= CS_FL_ERROR;
1432
1433 h2s_alert(h2s);
1434
1435 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1436 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001437 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001438 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001439 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001440 h2s_close(h2s);
1441}
1442
1443/* wake the streams attached to the connection, whose id is greater than <last>
1444 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001445 */
1446static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1447{
1448 struct eb32_node *node;
1449 struct h2s *h2s;
1450
1451 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001452 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001453
1454 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001455 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001456
Christopher Fauletf02ca002019-03-07 16:21:34 +01001457 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001458 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1459 while (node) {
1460 h2s = container_of(node, struct h2s, by_id);
1461 if (h2s->id <= last)
1462 break;
1463 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001464 h2s_wake_one_stream(h2s, flags);
1465 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001466
Christopher Fauletf02ca002019-03-07 16:21:34 +01001467 /* Wake all streams with unassigned ID (ID == 0) */
1468 node = eb32_lookup(&h2c->streams_by_id, 0);
1469 while (node) {
1470 h2s = container_of(node, struct h2s, by_id);
1471 if (h2s->id > 0)
1472 break;
1473 node = eb32_next(node);
1474 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001475 }
1476}
1477
Willy Tarreau3421aba2017-07-27 15:41:03 +02001478/* Increase all streams' outgoing window size by the difference passed in
1479 * argument. This is needed upon receipt of the settings frame if the initial
1480 * window size is different. The difference may be negative and the resulting
1481 * window size as well, for the time it takes to receive some window updates.
1482 */
1483static void h2c_update_all_ws(struct h2c *h2c, int diff)
1484{
1485 struct h2s *h2s;
1486 struct eb32_node *node;
1487
1488 if (!diff)
1489 return;
1490
1491 node = eb32_first(&h2c->streams_by_id);
1492 while (node) {
1493 h2s = container_of(node, struct h2s, by_id);
1494 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001495
1496 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1497 h2s->flags &= ~H2_SF_BLK_SFCTL;
1498 if (h2s->send_wait)
1499 LIST_ADDQ(&h2c->send_list, &h2s->list);
1500
1501 }
1502
Willy Tarreau3421aba2017-07-27 15:41:03 +02001503 node = eb32_next(node);
1504 }
1505}
1506
1507/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1508 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001509 * return an error in h2c. The caller must have already verified frame length
1510 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001511 */
1512static int h2c_handle_settings(struct h2c *h2c)
1513{
1514 unsigned int offset;
1515 int error;
1516
1517 if (h2c->dff & H2_F_SETTINGS_ACK) {
1518 if (h2c->dfl) {
1519 error = H2_ERR_FRAME_SIZE_ERROR;
1520 goto fail;
1521 }
1522 return 1;
1523 }
1524
Willy Tarreau3421aba2017-07-27 15:41:03 +02001525 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001526 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001527 return 0;
1528
1529 /* parse the frame */
1530 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001531 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1532 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001533
1534 switch (type) {
1535 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1536 /* we need to update all existing streams with the
1537 * difference from the previous iws.
1538 */
1539 if (arg < 0) { // RFC7540#6.5.2
1540 error = H2_ERR_FLOW_CONTROL_ERROR;
1541 goto fail;
1542 }
1543 h2c_update_all_ws(h2c, arg - h2c->miw);
1544 h2c->miw = arg;
1545 break;
1546 case H2_SETTINGS_MAX_FRAME_SIZE:
1547 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1548 error = H2_ERR_PROTOCOL_ERROR;
1549 goto fail;
1550 }
1551 h2c->mfs = arg;
1552 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001553 case H2_SETTINGS_ENABLE_PUSH:
1554 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1555 error = H2_ERR_PROTOCOL_ERROR;
1556 goto fail;
1557 }
1558 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001559 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1560 if (h2c->flags & H2_CF_IS_BACK) {
1561 /* the limit is only for the backend; for the frontend it is our limit */
1562 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1563 arg = h2_settings_max_concurrent_streams;
1564 h2c->streams_limit = arg;
1565 }
1566 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001567 }
1568 }
1569
1570 /* need to ACK this frame now */
1571 h2c->st0 = H2_CS_FRAME_A;
1572 return 1;
1573 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001574 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001575 h2c_error(h2c, error);
1576 return 0;
1577}
1578
1579/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1580 * success or one of the h2_status values.
1581 */
1582static int h2c_ack_settings(struct h2c *h2c)
1583{
1584 struct buffer *res;
1585 char str[9];
1586 int ret = -1;
1587
1588 if (h2c_mux_busy(h2c, NULL)) {
1589 h2c->flags |= H2_CF_DEM_MBUSY;
1590 return 0;
1591 }
1592
Willy Tarreau44e973f2018-03-01 17:49:30 +01001593 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001594 if (!res) {
1595 h2c->flags |= H2_CF_MUX_MALLOC;
1596 h2c->flags |= H2_CF_DEM_MROOM;
1597 return 0;
1598 }
1599
1600 memcpy(str,
1601 "\x00\x00\x00" /* length : 0 (no data) */
1602 "\x04" "\x01" /* type : 4, flags : ACK */
1603 "\x00\x00\x00\x00" /* stream ID */, 9);
1604
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001605 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001606 if (unlikely(ret <= 0)) {
1607 if (!ret) {
1608 h2c->flags |= H2_CF_MUX_MFULL;
1609 h2c->flags |= H2_CF_DEM_MROOM;
1610 return 0;
1611 }
1612 else {
1613 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1614 return 0;
1615 }
1616 }
1617 return ret;
1618}
1619
Willy Tarreaucf68c782017-10-10 17:11:41 +02001620/* processes a PING frame and schedules an ACK if needed. The caller must pass
1621 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001622 * missing data. The caller must have already verified frame length
1623 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001624 */
1625static int h2c_handle_ping(struct h2c *h2c)
1626{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001627 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001628 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001629 h2c->st0 = H2_CS_FRAME_A;
1630 return 1;
1631}
1632
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001633/* Try to send a window update for stream id <sid> and value <increment>.
1634 * Returns > 0 on success or zero on missing room or failure. It may return an
1635 * error in h2c.
1636 */
1637static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1638{
1639 struct buffer *res;
1640 char str[13];
1641 int ret = -1;
1642
1643 if (h2c_mux_busy(h2c, NULL)) {
1644 h2c->flags |= H2_CF_DEM_MBUSY;
1645 return 0;
1646 }
1647
Willy Tarreau44e973f2018-03-01 17:49:30 +01001648 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001649 if (!res) {
1650 h2c->flags |= H2_CF_MUX_MALLOC;
1651 h2c->flags |= H2_CF_DEM_MROOM;
1652 return 0;
1653 }
1654
1655 /* length: 4, type: 8, flags: none */
1656 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1657 write_n32(str + 5, sid);
1658 write_n32(str + 9, increment);
1659
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001660 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001661
1662 if (unlikely(ret <= 0)) {
1663 if (!ret) {
1664 h2c->flags |= H2_CF_MUX_MFULL;
1665 h2c->flags |= H2_CF_DEM_MROOM;
1666 return 0;
1667 }
1668 else {
1669 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1670 return 0;
1671 }
1672 }
1673 return ret;
1674}
1675
1676/* try to send pending window update for the connection. It's safe to call it
1677 * with no pending updates. Returns > 0 on success or zero on missing room or
1678 * failure. It may return an error in h2c.
1679 */
1680static int h2c_send_conn_wu(struct h2c *h2c)
1681{
1682 int ret = 1;
1683
1684 if (h2c->rcvd_c <= 0)
1685 return 1;
1686
Willy Tarreau97aaa672018-12-23 09:49:04 +01001687 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1688 /* increase the advertised connection window to 2G on
1689 * first update.
1690 */
1691 h2c->flags |= H2_CF_WINDOW_OPENED;
1692 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1693 }
1694
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001695 /* send WU for the connection */
1696 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1697 if (ret > 0)
1698 h2c->rcvd_c = 0;
1699
1700 return ret;
1701}
1702
1703/* try to send pending window update for the current dmux stream. It's safe to
1704 * call it with no pending updates. Returns > 0 on success or zero on missing
1705 * room or failure. It may return an error in h2c.
1706 */
1707static int h2c_send_strm_wu(struct h2c *h2c)
1708{
1709 int ret = 1;
1710
1711 if (h2c->rcvd_s <= 0)
1712 return 1;
1713
1714 /* send WU for the stream */
1715 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1716 if (ret > 0)
1717 h2c->rcvd_s = 0;
1718
1719 return ret;
1720}
1721
Willy Tarreaucf68c782017-10-10 17:11:41 +02001722/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1723 * success, 0 on missing data or one of the h2_status values.
1724 */
1725static int h2c_ack_ping(struct h2c *h2c)
1726{
1727 struct buffer *res;
1728 char str[17];
1729 int ret = -1;
1730
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001731 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001732 return 0;
1733
1734 if (h2c_mux_busy(h2c, NULL)) {
1735 h2c->flags |= H2_CF_DEM_MBUSY;
1736 return 0;
1737 }
1738
Willy Tarreau44e973f2018-03-01 17:49:30 +01001739 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001740 if (!res) {
1741 h2c->flags |= H2_CF_MUX_MALLOC;
1742 h2c->flags |= H2_CF_DEM_MROOM;
1743 return 0;
1744 }
1745
1746 memcpy(str,
1747 "\x00\x00\x08" /* length : 8 (same payload) */
1748 "\x06" "\x01" /* type : 6, flags : ACK */
1749 "\x00\x00\x00\x00" /* stream ID */, 9);
1750
1751 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001752 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001753
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001754 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001755 if (unlikely(ret <= 0)) {
1756 if (!ret) {
1757 h2c->flags |= H2_CF_MUX_MFULL;
1758 h2c->flags |= H2_CF_DEM_MROOM;
1759 return 0;
1760 }
1761 else {
1762 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1763 return 0;
1764 }
1765 }
1766 return ret;
1767}
1768
Willy Tarreau26f95952017-07-27 17:18:30 +02001769/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1770 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001771 * h2c or h2s. The caller must have already verified frame length and stream ID
1772 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001773 */
1774static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1775{
1776 int32_t inc;
1777 int error;
1778
Willy Tarreau26f95952017-07-27 17:18:30 +02001779 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001780 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001781 return 0;
1782
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001783 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001784
1785 if (h2c->dsi != 0) {
1786 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001787
1788 /* it's not an error to receive WU on a closed stream */
1789 if (h2s->st == H2_SS_CLOSED)
1790 return 1;
1791
1792 if (!inc) {
1793 error = H2_ERR_PROTOCOL_ERROR;
1794 goto strm_err;
1795 }
1796
1797 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1798 error = H2_ERR_FLOW_CONTROL_ERROR;
1799 goto strm_err;
1800 }
1801
1802 h2s->mws += inc;
1803 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1804 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001805 if (h2s->send_wait)
1806 LIST_ADDQ(&h2c->send_list, &h2s->list);
1807
Willy Tarreau26f95952017-07-27 17:18:30 +02001808 }
1809 }
1810 else {
1811 /* connection window update */
1812 if (!inc) {
1813 error = H2_ERR_PROTOCOL_ERROR;
1814 goto conn_err;
1815 }
1816
1817 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1818 error = H2_ERR_FLOW_CONTROL_ERROR;
1819 goto conn_err;
1820 }
1821
1822 h2c->mws += inc;
1823 }
1824
1825 return 1;
1826
1827 conn_err:
1828 h2c_error(h2c, error);
1829 return 0;
1830
1831 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001832 h2s_error(h2s, error);
1833 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001834 return 0;
1835}
1836
Willy Tarreaue96b0922017-10-30 00:28:29 +01001837/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001838 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1839 * have already verified frame length and stream ID validity. Described in
1840 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001841 */
1842static int h2c_handle_goaway(struct h2c *h2c)
1843{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001844 int last;
1845
Willy Tarreaue96b0922017-10-30 00:28:29 +01001846 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001847 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001848 return 0;
1849
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001850 last = h2_get_n32(&h2c->dbuf, 0);
1851 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001852 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001853 if (h2c->last_sid < 0)
1854 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001855 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001856}
1857
Willy Tarreau92153fc2017-12-03 19:46:19 +01001858/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001859 * invalid. Returns > 0 on success or zero on missing data. It may return an
1860 * error in h2c. The caller must have already verified frame length and stream
1861 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001862 */
1863static int h2c_handle_priority(struct h2c *h2c)
1864{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001865 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001866 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001867 return 0;
1868
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001869 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001870 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001871 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1872 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001873 }
1874 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001875}
1876
Willy Tarreaucd234e92017-08-18 10:59:39 +02001877/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001878 * Returns > 0 on success or zero on missing data. The caller must have already
1879 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001880 */
1881static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1882{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001883 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001884 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001885 return 0;
1886
1887 /* late RST, already handled */
1888 if (h2s->st == H2_SS_CLOSED)
1889 return 1;
1890
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001891 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001892 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001893
1894 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001895 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001896 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001897 }
1898
1899 h2s->flags |= H2_SF_RST_RCVD;
1900 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001901}
1902
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001903/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1904 * It may return an error in h2c or h2s. The caller must consider that the
1905 * return value is the new h2s in case one was allocated (most common case).
1906 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001907 * errors here are reported as connection errors since it's impossible to
1908 * recover from such errors after the compression context has been altered.
1909 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001910static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001911{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001912 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001913 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001914 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001915 int error;
1916
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001917 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001918 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001919
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001920 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001921 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001922
1923 /* now either the frame is complete or the buffer is complete */
1924 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001925 /* The stream exists/existed, this must be a trailers frame */
1926 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001927 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001928 goto out;
1929 goto done;
1930 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001931 /* the connection was already killed by an RST, let's consume
1932 * the data and send another RST.
1933 */
1934 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1935 h2s = (struct h2s*)h2_error_stream;
1936 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001937 }
1938 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1939 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1940 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001941 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001942 goto conn_err;
1943 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001944 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1945 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001946
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001947 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001948
Willy Tarreau25919232019-01-03 14:48:18 +01001949 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001950 if (h2c->st0 >= H2_CS_ERROR)
1951 goto out;
1952
Willy Tarreau25919232019-01-03 14:48:18 +01001953 if (error <= 0) {
1954 if (error == 0)
1955 goto out; // missing data
1956
1957 /* Failed to decode this stream (e.g. too large request)
1958 * but the HPACK decompressor is still synchronized.
1959 */
1960 h2s = (struct h2s*)h2_error_stream;
1961 goto send_rst;
1962 }
1963
Willy Tarreau22de8d32018-09-05 19:55:58 +02001964 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001965 * positively from h2c_frt_stream_new(), the stream will report the error,
1966 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001967 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001968 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001969 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001970 h2s = (struct h2s*)h2_refused_stream;
1971 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001972 }
1973
1974 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001975 h2s->rxbuf = rxbuf;
1976 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001977 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001978
Willy Tarreau88d138e2019-01-02 19:38:14 +01001979 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001980 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001981 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001982
1983 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001984 if (h2s->cs)
1985 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01001986 if (h2s->st == H2_SS_OPEN)
1987 h2s->st = H2_SS_HREM;
1988 else
1989 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02001990 }
1991
Willy Tarreau3a429f02019-01-03 11:41:50 +01001992 /* update the max stream ID if the request is being processed */
1993 if (h2s->id > h2c->max_id)
1994 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001995
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001996 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001997
1998 conn_err:
1999 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002000 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002001
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002002 out:
2003 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002004 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002005
2006 send_rst:
2007 /* make the demux send an RST for the current stream. We may only
2008 * do this if we're certain that the HEADERS frame was properly
2009 * decompressed so that the HPACK decoder is still kept up to date.
2010 */
2011 h2_release_buf(h2c, &rxbuf);
2012 h2c->st0 = H2_CS_FRAME_E;
2013 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002014}
2015
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002016/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2017 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2018 * errors here are reported as connection errors since it's impossible to
2019 * recover from such errors after the compression context has been altered.
2020 */
2021static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2022{
2023 int error;
2024
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002025 if (!b_size(&h2c->dbuf))
2026 return NULL; // empty buffer
2027
2028 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2029 return NULL; // incomplete frame
2030
Willy Tarreau1915ca22019-01-24 11:49:37 +01002031 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002032
Willy Tarreau25919232019-01-03 14:48:18 +01002033 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002034 if (h2c->st0 >= H2_CS_ERROR)
2035 return NULL;
2036
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002037 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2038 /* RFC7540#5.1 */
2039 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2040 h2c->st0 = H2_CS_FRAME_E;
2041 return NULL;
2042 }
2043
Willy Tarreau25919232019-01-03 14:48:18 +01002044 if (error <= 0) {
2045 if (error == 0)
2046 return NULL; // missing data
2047
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002048 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002049 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002050 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002051 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002052 }
2053
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002054 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2055 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002056 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002057 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002058 }
2059
Willy Tarreau927b88b2019-03-04 08:03:25 +01002060 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002061 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002062 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 +02002063 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002064 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002065 h2s_close(h2s);
2066
2067 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002068}
2069
Willy Tarreau454f9052017-10-26 19:40:35 +02002070/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2071 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2072 */
2073static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2074{
2075 int error;
2076
2077 /* note that empty DATA frames are perfectly valid and sometimes used
2078 * to signal an end of stream (with the ES flag).
2079 */
2080
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002081 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002082 return 0; // empty buffer
2083
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002084 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002085 return 0; // incomplete frame
2086
2087 /* now either the frame is complete or the buffer is complete */
2088
Willy Tarreau454f9052017-10-26 19:40:35 +02002089 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2090 /* RFC7540#6.1 */
2091 error = H2_ERR_STREAM_CLOSED;
2092 goto strm_err;
2093 }
2094
Willy Tarreau1915ca22019-01-24 11:49:37 +01002095 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2096 /* RFC7540#8.1.2 */
2097 error = H2_ERR_PROTOCOL_ERROR;
2098 goto strm_err;
2099 }
2100
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002101 if (!h2_frt_transfer_data(h2s))
2102 return 0;
2103
Willy Tarreau454f9052017-10-26 19:40:35 +02002104 /* call the upper layers to process the frame, then let the upper layer
2105 * notify the stream about any change.
2106 */
2107 if (!h2s->cs) {
2108 error = H2_ERR_STREAM_CLOSED;
2109 goto strm_err;
2110 }
2111
Willy Tarreau8f650c32017-11-21 19:36:21 +01002112 if (h2c->st0 >= H2_CS_ERROR)
2113 return 0;
2114
Willy Tarreau721c9742017-11-07 11:05:42 +01002115 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002116 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002117 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002118 }
2119
2120 /* check for completion : the callee will change this to FRAME_A or
2121 * FRAME_H once done.
2122 */
2123 if (h2c->st0 == H2_CS_FRAME_P)
2124 return 0;
2125
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002126 /* last frame */
2127 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002128 if (h2s->cs)
2129 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002130 if (h2s->st == H2_SS_OPEN)
2131 h2s->st = H2_SS_HREM;
2132 else
2133 h2s_close(h2s);
2134
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002135 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002136
2137 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2138 /* RFC7540#8.1.2 */
2139 error = H2_ERR_PROTOCOL_ERROR;
2140 goto strm_err;
2141 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002142 }
2143
Willy Tarreau454f9052017-10-26 19:40:35 +02002144 return 1;
2145
Willy Tarreau454f9052017-10-26 19:40:35 +02002146 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002147 h2s_error(h2s, error);
2148 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002149 return 0;
2150}
2151
Willy Tarreaubc933932017-10-09 16:21:43 +02002152/* process Rx frames to be demultiplexed */
2153static void h2_process_demux(struct h2c *h2c)
2154{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002155 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002156 struct h2_fh hdr;
2157 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002158
Willy Tarreau081d4722017-05-16 21:51:05 +02002159 if (h2c->st0 >= H2_CS_ERROR)
2160 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002161
2162 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2163 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002164 if (h2c->flags & H2_CF_IS_BACK)
2165 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002166 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2167 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002168 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002169 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002170 sess_log(h2c->conn->owner);
2171 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002172 goto fail;
2173 }
2174
2175 h2c->max_id = 0;
2176 h2c->st0 = H2_CS_SETTINGS1;
2177 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002178
2179 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002180 /* ensure that what is pending is a valid SETTINGS frame
2181 * without an ACK.
2182 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002183 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002184 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002185 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002186 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002187 sess_log(h2c->conn->owner);
2188 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002189 goto fail;
2190 }
2191
2192 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2193 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2194 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2195 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002196 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002197 goto fail;
2198 }
2199
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002200 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002201 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2202 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2203 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002204 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002205 goto fail;
2206 }
2207
Willy Tarreau3bf69182018-12-21 15:34:50 +01002208 /* that's OK, switch to FRAME_P to process it. This is
2209 * a SETTINGS frame whose header has already been
2210 * deleted above.
2211 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002212 padlen = 0;
2213 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002214 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002215 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002216
2217 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002218 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002219 int ret = 0;
2220
2221 if (h2c->st0 >= H2_CS_ERROR)
2222 break;
2223
2224 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002225 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002226 break;
2227
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002228 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002229 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002230 if (!h2c->nb_streams) {
2231 /* only log if no other stream can report the error */
2232 sess_log(h2c->conn->owner);
2233 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002234 break;
2235 }
2236
Willy Tarreau3bf69182018-12-21 15:34:50 +01002237 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2238 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2239 * we read the pad length and drop it from the remaining
2240 * payload (one byte + the 9 remaining ones = 10 total
2241 * removed), so we have a frame payload starting after the
2242 * pad len. Flow controlled frames (DATA) also count the
2243 * padlen in the flow control, so it must be adjusted.
2244 */
2245 if (hdr.len < 1) {
2246 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2247 sess_log(h2c->conn->owner);
2248 goto fail;
2249 }
2250 hdr.len--;
2251
2252 if (b_data(&h2c->dbuf) < 10)
2253 break; // missing padlen
2254
2255 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2256
2257 if (padlen > hdr.len) {
2258 /* RFC7540#6.1 : pad length = length of
2259 * frame payload or greater => error.
2260 */
2261 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2262 sess_log(h2c->conn->owner);
2263 goto fail;
2264 }
2265
2266 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2267 h2c->rcvd_c++;
2268 h2c->rcvd_s++;
2269 }
2270 b_del(&h2c->dbuf, 1);
2271 }
2272 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002273
2274 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002275 h2c->dfl = hdr.len;
2276 h2c->dsi = hdr.sid;
2277 h2c->dft = hdr.ft;
2278 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002279 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002280 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002281
2282 /* check for minimum basic frame format validity */
2283 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2284 if (ret != H2_ERR_NO_ERROR) {
2285 h2c_error(h2c, ret);
2286 sess_log(h2c->conn->owner);
2287 goto fail;
2288 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002289 }
2290
2291 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002292 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2293
Willy Tarreau567beb82018-12-18 16:52:44 +01002294 if (tmp_h2s != h2s && h2s && h2s->cs &&
2295 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002296 (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 +01002297 /* we may have to signal the upper layers */
2298 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002299 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002300 }
2301 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002302
Willy Tarreaud7901432017-12-29 11:34:40 +01002303 if (h2c->st0 == H2_CS_FRAME_E)
2304 goto strm_err;
2305
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002306 if (h2s->st == H2_SS_IDLE &&
2307 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2308 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2309 * this state MUST be treated as a connection error
2310 */
2311 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002312 if (!h2c->nb_streams) {
2313 /* only log if no other stream can report the error */
2314 sess_log(h2c->conn->owner);
2315 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002316 break;
2317 }
2318
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002319 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2320 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2321 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002322 * this state MUST be treated as a stream error.
2323 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2324 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002325 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002326 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2327 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2328 else
2329 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002330 goto strm_err;
2331 }
2332
Willy Tarreauab837502017-12-27 15:07:30 +01002333 /* Below the management of frames received in closed state is a
2334 * bit hackish because the spec makes strong differences between
2335 * streams closed by receiving RST, sending RST, and seeing ES
2336 * in both directions. In addition to this, the creation of a
2337 * new stream reusing the identifier of a closed one will be
2338 * detected here. Given that we cannot keep track of all closed
2339 * streams forever, we consider that unknown closed streams were
2340 * closed on RST received, which allows us to respond with an
2341 * RST without breaking the connection (eg: to abort a transfer).
2342 * Some frames have to be silently ignored as well.
2343 */
2344 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002345 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002346 /* #5.1.1: The identifier of a newly
2347 * established stream MUST be numerically
2348 * greater than all streams that the initiating
2349 * endpoint has opened or reserved. This
2350 * governs streams that are opened using a
2351 * HEADERS frame and streams that are reserved
2352 * using PUSH_PROMISE. An endpoint that
2353 * receives an unexpected stream identifier
2354 * MUST respond with a connection error.
2355 */
2356 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2357 goto strm_err;
2358 }
2359
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002360 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002361 /* RFC7540#5.1:closed: an endpoint that
2362 * receives any frame other than PRIORITY after
2363 * receiving a RST_STREAM MUST treat that as a
2364 * stream error of type STREAM_CLOSED.
2365 *
2366 * Note that old streams fall into this category
2367 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002368 *
2369 * However, we cannot generalize this to all frame types. Those
2370 * carrying compression state must still be processed before
2371 * being dropped or we'll desynchronize the decoder. This can
2372 * happen with request trailers received after sending an
2373 * RST_STREAM, or with header/trailers responses received after
2374 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002375 */
2376 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2377 h2c->st0 = H2_CS_FRAME_E;
2378 goto strm_err;
2379 }
2380
2381 /* RFC7540#5.1:closed: if this state is reached as a
2382 * result of sending a RST_STREAM frame, the peer that
2383 * receives the RST_STREAM might have already sent
2384 * frames on the stream that cannot be withdrawn. An
2385 * endpoint MUST ignore frames that it receives on
2386 * closed streams after it has sent a RST_STREAM
2387 * frame. An endpoint MAY choose to limit the period
2388 * over which it ignores frames and treat frames that
2389 * arrive after this time as being in error.
2390 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002391 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002392 /* RFC7540#5.1:closed: any frame other than
2393 * PRIO/WU/RST in this state MUST be treated as
2394 * a connection error
2395 */
2396 if (h2c->dft != H2_FT_RST_STREAM &&
2397 h2c->dft != H2_FT_PRIORITY &&
2398 h2c->dft != H2_FT_WINDOW_UPDATE) {
2399 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2400 goto strm_err;
2401 }
2402 }
2403 }
2404
Willy Tarreauc0da1962017-10-30 18:38:00 +01002405#if 0
2406 // problem below: it is not possible to completely ignore such
2407 // streams as we need to maintain the compression state as well
2408 // and for this we need to completely process these frames (eg:
2409 // HEADERS frames) as well as counting DATA frames to emit
2410 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2411 // This is a typical case of layer violation where the
2412 // transported contents are critical to the connection's
2413 // validity and must be ignored at the same time :-(
2414
2415 /* graceful shutdown, ignore streams whose ID is higher than
2416 * the one advertised in GOAWAY. RFC7540#6.8.
2417 */
2418 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002419 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2420 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002421 h2c->dfl -= ret;
2422 ret = h2c->dfl == 0;
2423 goto strm_err;
2424 }
2425#endif
2426
Willy Tarreau7e98c052017-10-10 15:56:59 +02002427 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002428 case H2_FT_SETTINGS:
2429 if (h2c->st0 == H2_CS_FRAME_P)
2430 ret = h2c_handle_settings(h2c);
2431
2432 if (h2c->st0 == H2_CS_FRAME_A)
2433 ret = h2c_ack_settings(h2c);
2434 break;
2435
Willy Tarreaucf68c782017-10-10 17:11:41 +02002436 case H2_FT_PING:
2437 if (h2c->st0 == H2_CS_FRAME_P)
2438 ret = h2c_handle_ping(h2c);
2439
2440 if (h2c->st0 == H2_CS_FRAME_A)
2441 ret = h2c_ack_ping(h2c);
2442 break;
2443
Willy Tarreau26f95952017-07-27 17:18:30 +02002444 case H2_FT_WINDOW_UPDATE:
2445 if (h2c->st0 == H2_CS_FRAME_P)
2446 ret = h2c_handle_window_update(h2c, h2s);
2447 break;
2448
Willy Tarreau61290ec2017-10-17 08:19:21 +02002449 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002450 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2451 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2452 * frames' parsers consume all following CONTINUATION
2453 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002454 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002455 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2456 sess_log(h2c->conn->owner);
2457 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002458
Willy Tarreau13278b42017-10-13 19:23:14 +02002459 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002460 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002461 if (h2c->flags & H2_CF_IS_BACK)
2462 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2463 else
2464 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002465 if (tmp_h2s) {
2466 h2s = tmp_h2s;
2467 ret = 1;
2468 }
2469 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002470 break;
2471
Willy Tarreau454f9052017-10-26 19:40:35 +02002472 case H2_FT_DATA:
2473 if (h2c->st0 == H2_CS_FRAME_P)
2474 ret = h2c_frt_handle_data(h2c, h2s);
2475
2476 if (h2c->st0 == H2_CS_FRAME_A)
2477 ret = h2c_send_strm_wu(h2c);
2478 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002479
Willy Tarreau92153fc2017-12-03 19:46:19 +01002480 case H2_FT_PRIORITY:
2481 if (h2c->st0 == H2_CS_FRAME_P)
2482 ret = h2c_handle_priority(h2c);
2483 break;
2484
Willy Tarreaucd234e92017-08-18 10:59:39 +02002485 case H2_FT_RST_STREAM:
2486 if (h2c->st0 == H2_CS_FRAME_P)
2487 ret = h2c_handle_rst_stream(h2c, h2s);
2488 break;
2489
Willy Tarreaue96b0922017-10-30 00:28:29 +01002490 case H2_FT_GOAWAY:
2491 if (h2c->st0 == H2_CS_FRAME_P)
2492 ret = h2c_handle_goaway(h2c);
2493 break;
2494
Willy Tarreau1c661982017-10-30 13:52:01 +01002495 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002496 default:
2497 /* drop frames that we ignore. They may be larger than
2498 * the buffer so we drain all of their contents until
2499 * we reach the end.
2500 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002501 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2502 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002503 h2c->dfl -= ret;
2504 ret = h2c->dfl == 0;
2505 }
2506
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002507 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002508 /* We may have to send an RST if not done yet */
2509 if (h2s->st == H2_SS_ERROR)
2510 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002511
Willy Tarreaua20a5192017-12-27 11:02:06 +01002512 if (h2c->st0 == H2_CS_FRAME_E)
2513 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002514
Willy Tarreau7e98c052017-10-10 15:56:59 +02002515 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002516 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002517 break;
2518
2519 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002520 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002521 h2c->st0 = H2_CS_FRAME_H;
2522 }
2523 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002524
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002525 if (h2c->rcvd_c > 0 &&
2526 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2527 h2c_send_conn_wu(h2c);
2528
Willy Tarreau52eed752017-09-22 15:05:09 +02002529 fail:
2530 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002531 if (h2s && h2s->cs &&
2532 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002533 (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 +01002534 /* we may have to signal the upper layers */
2535 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002536 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002537 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002538
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002539 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002540}
2541
2542/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2543 * the end.
2544 */
2545static int h2_process_mux(struct h2c *h2c)
2546{
Olivier Houchardd360ac62019-03-22 17:37:16 +01002547 struct h2s *h2s;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002548
Willy Tarreau01b44822018-10-03 14:26:37 +02002549 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2550 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2551 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2552 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2553 if (h2c->st0 == H2_CS_ERROR) {
2554 h2c->st0 = H2_CS_ERROR2;
2555 sess_log(h2c->conn->owner);
2556 }
2557 goto fail;
2558 }
2559 h2c->st0 = H2_CS_SETTINGS1;
2560 }
2561 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002562 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002563 return 1;
2564 }
2565
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002566 /* start by sending possibly pending window updates */
2567 if (h2c->rcvd_c > 0 &&
2568 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2569 h2c_send_conn_wu(h2c) < 0)
2570 goto fail;
2571
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002572 /* First we always process the flow control list because the streams
2573 * waiting there were already elected for immediate emission but were
2574 * blocked just on this.
2575 */
2576
Olivier Houchardd360ac62019-03-22 17:37:16 +01002577 list_for_each_entry(h2s, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002578 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2579 h2c->st0 >= H2_CS_ERROR)
2580 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002581 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2582 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002583
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002584 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002585 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2586 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002587 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002588 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002589 }
2590
Olivier Houchardd360ac62019-03-22 17:37:16 +01002591 list_for_each_entry(h2s, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002592 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2593 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002594 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2595 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002596
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002597 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002598 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2599 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002600 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002601 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002602 }
2603
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002604 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002605 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002606 if (h2c->st0 == H2_CS_ERROR) {
2607 if (h2c->max_id >= 0) {
2608 h2c_send_goaway_error(h2c, NULL);
2609 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2610 return 0;
2611 }
2612
2613 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2614 }
2615 return 1;
2616 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002617 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002618}
2619
Willy Tarreau62f52692017-10-08 23:01:42 +02002620
Willy Tarreau479998a2018-11-18 06:30:59 +01002621/* Attempt to read data, and subscribe if none available.
2622 * The function returns 1 if data has been received, otherwise zero.
2623 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002624static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002625{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002626 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002627 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002628 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002629 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002630
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002631 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002632 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002633
Willy Tarreau315d8072017-12-10 22:17:57 +01002634 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002635 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002636
Willy Tarreau44e973f2018-03-01 17:49:30 +01002637 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002638 if (!buf) {
2639 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002640 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002641 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002642
Olivier Houchard7505f942018-08-21 18:10:44 +02002643 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002644 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002645 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2646 /* HTX in use : try to pre-align the buffer like the
2647 * rxbufs will be to optimize memory copies. We'll make
2648 * sure that the frame header lands at the end of the
2649 * HTX block to alias it upon recv. We cannot use the
2650 * head because rcv_buf() will realign the buffer if
2651 * it's empty. Thus we cheat and pretend we already
2652 * have a few bytes there.
2653 */
2654 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002655 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002656 }
2657 else
2658 max = b_room(buf);
2659
Olivier Houchard7505f942018-08-21 18:10:44 +02002660 if (max)
2661 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2662 else
2663 ret = 0;
2664 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002665
Olivier Houchard53216e72018-10-10 15:46:36 +02002666 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002667 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002668
Olivier Houcharda1411e62018-08-17 18:42:48 +02002669 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002670 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002671 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002672 }
2673
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002674 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002675 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002676 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002677}
2678
Willy Tarreau479998a2018-11-18 06:30:59 +01002679/* Try to send data if possible.
2680 * The function returns 1 if data have been sent, otherwise zero.
2681 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002682static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002683{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002684 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002685 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002686 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002687
2688 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002689 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002690
Olivier Houchard7505f942018-08-21 18:10:44 +02002691
Willy Tarreaua2af5122017-10-09 11:56:46 +02002692 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2693 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002694 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002695 }
2696
Willy Tarreaubc933932017-10-09 16:21:43 +02002697 /* This loop is quite simple : it tries to fill as much as it can from
2698 * pending streams into the existing buffer until it's reportedly full
2699 * or the end of send requests is reached. Then it tries to send this
2700 * buffer's contents out, marks it not full if at least one byte could
2701 * be sent, and tries again.
2702 *
2703 * The snd_buf() function normally takes a "flags" argument which may
2704 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2705 * data immediately comes and CO_SFL_STREAMER to indicate that the
2706 * connection is streaming lots of data (used to increase TLS record
2707 * size at the expense of latency). The former can be sent any time
2708 * there's a buffer full flag, as it indicates at least one stream
2709 * attempted to send and failed so there are pending data. An
2710 * alternative would be to set it as long as there's an active stream
2711 * but that would be problematic for ACKs until we have an absolute
2712 * guarantee that all waiters have at least one byte to send. The
2713 * latter should possibly not be set for now.
2714 */
2715
2716 done = 0;
2717 while (!done) {
2718 unsigned int flags = 0;
2719
2720 /* fill as much as we can into the current buffer */
2721 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2722 done = h2_process_mux(h2c);
2723
Olivier Houchard2b094432019-01-29 18:28:36 +01002724 if (h2c->flags & H2_CF_MUX_MALLOC)
2725 break;
2726
Willy Tarreaubc933932017-10-09 16:21:43 +02002727 if (conn->flags & CO_FL_ERROR)
2728 break;
2729
2730 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2731 flags |= CO_SFL_MSG_MORE;
2732
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002733 if (b_data(&h2c->mbuf)) {
2734 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002735 if (!ret)
2736 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002737 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002738 b_del(&h2c->mbuf, ret);
2739 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002740 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002741
2742 /* wrote at least one byte, the buffer is not full anymore */
2743 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2744 }
2745
Willy Tarreaua2af5122017-10-09 11:56:46 +02002746 if (conn->flags & CO_FL_SOCK_WR_SH) {
2747 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002748 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002749 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002750 /* We're not full anymore, so we can wake any task that are waiting
2751 * for us.
2752 */
2753 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002754 struct h2s *h2s;
2755
2756 list_for_each_entry(h2s, &h2c->send_list, list) {
2757 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2758 break;
2759 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2760 continue;
2761
2762 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002763 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2764 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002765 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002766 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002767 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002768 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002769 /* We're done, no more to send */
2770 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002771 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002772schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002773 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2774 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002775 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002776}
2777
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002778/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002779static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2780{
2781 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002782 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002783
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002784 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002785 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002786 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002787 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002788 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002789 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002790 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002791}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002792
Willy Tarreau62f52692017-10-08 23:01:42 +02002793/* callback called on any event by the connection handler.
2794 * It applies changes and returns zero, or < 0 if it wants immediate
2795 * destruction of the connection (which normally doesn not happen in h2).
2796 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002797static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002798{
Olivier Houchard7505f942018-08-21 18:10:44 +02002799 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002800
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002801 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002802 h2_process_demux(h2c);
2803
2804 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002805 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002806
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002807 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002808 h2c->flags &= ~H2_CF_DEM_DFULL;
2809 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002810 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002811
Willy Tarreau0b37d652018-10-03 10:33:02 +02002812 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002813 /* frontend is stopping, reload likely in progress, let's try
2814 * to announce a graceful shutdown if not yet done. We don't
2815 * care if it fails, it will be tried again later.
2816 */
2817 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2818 if (h2c->last_sid < 0)
2819 h2c->last_sid = (1U << 31) - 1;
2820 h2c_send_goaway_error(h2c, NULL);
2821 }
2822 }
2823
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002824 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002825 * If we received early data, and the handshake is done, wake
2826 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002827 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002828 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2829 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2830 struct eb32_node *node;
2831 struct h2s *h2s;
2832
2833 h2c->flags |= H2_CF_WAIT_FOR_HS;
2834 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2835
2836 while (node) {
2837 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002838 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002839 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002840 node = eb32_next(node);
2841 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002842 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002843
Willy Tarreau26bd7612017-10-09 16:47:04 +02002844 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002845 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2846 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2847 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002848 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002849
2850 if (eb_is_empty(&h2c->streams_by_id)) {
2851 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002852 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002853 return -1;
2854 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002855 }
2856
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002857 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002858 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002859
Olivier Houchard53216e72018-10-10 15:46:36 +02002860 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2861 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2862 (h2c->st0 != H2_CS_ERROR &&
2863 !b_data(&h2c->mbuf) &&
2864 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2865 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002866 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002867
Willy Tarreau3f133572017-10-31 19:21:06 +01002868 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002869 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002870 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002871 task_queue(h2c->task);
2872 }
2873 else
2874 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002875 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002876
Olivier Houchard7505f942018-08-21 18:10:44 +02002877 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002878 return 0;
2879}
2880
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002881/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002882static int h2_wake(struct connection *conn)
2883{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002884 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002885
2886 return (h2_process(h2c));
2887}
2888
Willy Tarreauea392822017-10-31 10:02:25 +01002889/* Connection timeout management. The principle is that if there's no receipt
2890 * nor sending for a certain amount of time, the connection is closed. If the
2891 * MUX buffer still has lying data or is not allocatable, the connection is
2892 * immediately killed. If it's allocatable and empty, we attempt to send a
2893 * GOAWAY frame.
2894 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002895static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002896{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002897 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002898 int expired = tick_is_expired(t->expire, now_ms);
2899
Willy Tarreau0975f112018-03-29 15:22:59 +02002900 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002901 return t;
2902
Willy Tarreau0975f112018-03-29 15:22:59 +02002903 task_delete(t);
2904 task_free(t);
2905
2906 if (!h2c) {
2907 /* resources were already deleted */
2908 return NULL;
2909 }
2910
2911 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002912 h2c_error(h2c, H2_ERR_NO_ERROR);
2913 h2_wake_some_streams(h2c, 0, 0);
2914
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002915 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002916 /* don't even try to send a GOAWAY, the buffer is stuck */
2917 h2c->flags |= H2_CF_GOAWAY_FAILED;
2918 }
2919
2920 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002921 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002922 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2923 h2c->flags |= H2_CF_GOAWAY_FAILED;
2924
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002925 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2926 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002927 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002928 b_del(&h2c->mbuf, ret);
2929 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002930 }
2931 }
Willy Tarreauea392822017-10-31 10:02:25 +01002932
Willy Tarreau0975f112018-03-29 15:22:59 +02002933 /* either we can release everything now or it will be done later once
2934 * the last stream closes.
2935 */
2936 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002937 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002938
Willy Tarreauea392822017-10-31 10:02:25 +01002939 return NULL;
2940}
2941
2942
Willy Tarreau62f52692017-10-08 23:01:42 +02002943/*******************************************/
2944/* functions below are used by the streams */
2945/*******************************************/
2946
2947/*
2948 * Attach a new stream to a connection
2949 * (Used for outgoing connections)
2950 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002951static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002952{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002953 struct conn_stream *cs;
2954 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002955 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002956
2957 cs = cs_new(conn);
2958 if (!cs)
2959 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002960 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002961 if (!h2s) {
2962 cs_free(cs);
2963 return NULL;
2964 }
2965 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002966}
2967
Willy Tarreaufafd3982018-11-18 21:29:20 +01002968/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2969 * We have to scan because we may have some orphan streams. It might be
2970 * beneficial to scan backwards from the end to reduce the likeliness to find
2971 * orphans.
2972 */
2973static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2974{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002975 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002976 struct h2s *h2s;
2977 struct eb32_node *node;
2978
2979 node = eb32_first(&h2c->streams_by_id);
2980 while (node) {
2981 h2s = container_of(node, struct h2s, by_id);
2982 if (h2s->cs)
2983 return h2s->cs;
2984 node = eb32_next(node);
2985 }
2986 return NULL;
2987}
2988
Willy Tarreau62f52692017-10-08 23:01:42 +02002989/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002990 * Destroy the mux and the associated connection, if it is no longer used
2991 */
Christopher Faulet73c12072019-04-08 11:23:22 +02002992static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01002993{
Christopher Faulet73c12072019-04-08 11:23:22 +02002994 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002995
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002996 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002997 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01002998}
2999
3000/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003001 * Detach the stream from the connection and possibly release the connection.
3002 */
3003static void h2_detach(struct conn_stream *cs)
3004{
Willy Tarreau60935142017-10-16 18:11:19 +02003005 struct h2s *h2s = cs->ctx;
3006 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003007 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003008
3009 cs->ctx = NULL;
3010 if (!h2s)
3011 return;
3012
Olivier Houchardf502aca2018-12-14 19:42:40 +01003013 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003014 h2c = h2s->h2c;
3015 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003016 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003017 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3018 !h2_frt_has_too_many_cs(h2c)) {
3019 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003020 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003021 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003022 }
Willy Tarreau60935142017-10-16 18:11:19 +02003023
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003024 /* this stream may be blocked waiting for some data to leave (possibly
3025 * an ES or RST frame), so orphan it in this case.
3026 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003027 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003028 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003029 (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 +01003030 return;
3031
Willy Tarreau45f752e2017-10-30 15:44:59 +01003032 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3033 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3034 /* unblock the connection if it was blocked on this
3035 * stream.
3036 */
3037 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3038 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003039 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003040 }
3041
Willy Tarreau71049cc2018-03-28 13:56:39 +02003042 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003043
Olivier Houchard8a786902018-12-15 16:05:40 +01003044 if (h2c->flags & H2_CF_IS_BACK &&
3045 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003046 if (!(h2c->conn->flags &
3047 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3048 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003049 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003050 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3051 h2c->conn->owner = NULL;
3052 if (eb_is_empty(&h2c->streams_by_id)) {
3053 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3054 /* The server doesn't want it, let's kill the connection right away */
3055 h2c->conn->mux->destroy(h2c->conn);
3056 return;
3057 }
3058 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003059 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003060 if (eb_is_empty(&h2c->streams_by_id)) {
3061 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3062 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3063 return;
3064 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003065 /* Never ever allow to reuse a connection from a non-reuse backend */
3066 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3067 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003068 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003069 struct server *srv = objt_server(h2c->conn->target);
3070
3071 if (srv) {
3072 if (h2c->conn->flags & CO_FL_PRIVATE)
3073 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3074 else
3075 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3076 }
3077
3078 }
3079 }
3080 }
3081
Willy Tarreaue323f342018-03-28 13:51:45 +02003082 /* We don't want to close right now unless we're removing the
3083 * last stream, and either the connection is in error, or it
3084 * reached the ID already specified in a GOAWAY frame received
3085 * or sent (as seen by last_sid >= 0).
3086 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003087 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003088 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003089 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003090 }
3091 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003092 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003093 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3094 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003095 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003096 else
3097 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003098 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003099}
3100
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003101/* Performs a synchronous or asynchronous shutr().
3102 * FIXME: guess what the return code tries to indicate!
3103 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003104static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003105{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003106 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003107 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003108
Willy Tarreau721c9742017-11-07 11:05:42 +01003109 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003110 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003111
Willy Tarreau18059042019-01-31 19:12:48 +01003112 /* a connstream may require us to immediately kill the whole connection
3113 * for example because of a "tcp-request content reject" rule that is
3114 * normally used to limit abuse. In this case we schedule a goaway to
3115 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003116 */
Willy Tarreau18059042019-01-31 19:12:48 +01003117 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3118 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3119 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3120 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3121 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003122 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3123 /* Nothing was never sent for this stream, so reset with
3124 * REFUSED_STREAM error to let the client retry the
3125 * request.
3126 */
3127 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3128 }
Willy Tarreau18059042019-01-31 19:12:48 +01003129
Willy Tarreau90c32322017-11-24 08:00:30 +01003130 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003131 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003132 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003133
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003134 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003135 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003136 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003137
Olivier Houchard7a977432019-03-21 15:47:13 +01003138 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003139add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003140 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003141 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003142 if (h2s->flags & H2_SF_BLK_MFCTL) {
3143 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3144 h2s->send_wait = sw;
3145 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3146 h2s->send_wait = sw;
3147 LIST_ADDQ(&h2c->send_list, &h2s->list);
3148 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003149 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003150 /* Let the handler know we want shutr */
3151 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003152 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003153}
3154
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003155/* Performs a synchronous or asynchronous shutw().
3156 * FIXME: guess what the return code tries to indicate!
3157 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003158static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003159{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003160 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003161 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003162
Willy Tarreau721c9742017-11-07 11:05:42 +01003163 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003164 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003165
Willy Tarreau67434202017-11-06 20:20:51 +01003166 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003167 /* we can cleanly close using an empty data frame only after headers */
3168
3169 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3170 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003171 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003172
3173 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003174 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003175 else
3176 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003177 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003178 /* a connstream may require us to immediately kill the whole connection
3179 * for example because of a "tcp-request content reject" rule that is
3180 * normally used to limit abuse. In this case we schedule a goaway to
3181 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003182 */
Willy Tarreau18059042019-01-31 19:12:48 +01003183 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3184 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3185 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3186 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3187 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003188 else {
3189 /* Nothing was never sent for this stream, so reset with
3190 * REFUSED_STREAM error to let the client retry the
3191 * request.
3192 */
3193 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3194 }
Willy Tarreau18059042019-01-31 19:12:48 +01003195
Willy Tarreau90c32322017-11-24 08:00:30 +01003196 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003197 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003198 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003199
Willy Tarreau00dd0782018-03-01 16:31:34 +01003200 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003201 }
3202
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003203 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003204 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003205 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003206
3207 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003208 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003209 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003210 if (h2s->flags & H2_SF_BLK_MFCTL) {
3211 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3212 h2s->send_wait = sw;
3213 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3214 h2s->send_wait = sw;
3215 LIST_ADDQ(&h2c->send_list, &h2s->list);
3216 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003217 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003218 /* let the handler know we want to shutw */
3219 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003220 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003221}
3222
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003223/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3224 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3225 * and prevented the last frame from being emitted.
3226 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003227static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3228{
3229 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003230 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003231 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003232
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003233 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003234 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003235 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003236 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003237 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003238 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003239 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003240 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003241 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003242
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003243 if (!ret) {
3244 /* We're done trying to send, remove ourself from the send_list */
3245 h2s->send_wait->events &= ~SUB_RETRY_SEND;
3246 h2s->send_wait = NULL;
3247 LIST_DEL_INIT(&h2s->list);
3248 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003249 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003250 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003251 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003252 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003253
3254 if (h2c_is_dead(h2c))
Christopher Faulet73c12072019-04-08 11:23:22 +02003255 h2_release(h2c);
Olivier Houchard7a977432019-03-21 15:47:13 +01003256 }
3257
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003258 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003259}
3260
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003261/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003262static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3263{
3264 struct h2s *h2s = cs->ctx;
3265
3266 if (!mode)
3267 return;
3268
3269 h2_do_shutr(h2s);
3270}
3271
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003272/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003273static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3274{
3275 struct h2s *h2s = cs->ctx;
3276
3277 h2_do_shutw(h2s);
3278}
3279
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003280/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003281 * HTX request or response depending on the connection's side. Returns a
3282 * positive value on success, a negative value on failure, or 0 if it couldn't
3283 * proceed. May report connection errors in h2c->errcode if the frame is
3284 * non-decodable and the connection unrecoverable. In absence of connection
3285 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003286 *
3287 * The function may fold CONTINUATION frames into the initial HEADERS frame
3288 * by removing padding and next frame header, then moving the CONTINUATION
3289 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3290 * leaving a hole between the main frame and the beginning of the next one.
3291 * The possibly remaining incomplete or next frame at the end may be moved
3292 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3293 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3294 *
3295 * A buffer at the beginning of processing may look like this :
3296 *
3297 * ,---.---------.-----.--------------.--------------.------.---.
3298 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3299 * `---^---------^-----^--------------^--------------^------^---'
3300 * | | <-----> | |
3301 * area | dpl | wrap
3302 * |<--------------> |
3303 * | dfl |
3304 * |<-------------------------------------------------->|
3305 * head data
3306 *
3307 * Padding is automatically overwritten when folding, participating to the
3308 * hole size after dfl :
3309 *
3310 * ,---.------------------------.-----.--------------.------.---.
3311 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3312 * `---^------------------------^-----^--------------^------^---'
3313 * | | <-----> | |
3314 * area | hole | wrap
3315 * |<-----------------------> |
3316 * | dfl |
3317 * |<-------------------------------------------------->|
3318 * head data
3319 *
3320 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3321 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3322 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003323 *
3324 * The <flags> field must point to either the stream's flags or to a copy of it
3325 * so that the function can update the following flags :
3326 * - H2_SF_DATA_CLEN when content-length is seen
3327 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3328 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003329 *
3330 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3331 * decoding, in order to detect if we're dealing with a headers or a trailers
3332 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003333 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003334static 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 +02003335{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003336 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003337 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003338 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003339 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003340 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003341 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003342 int flen; // header frame len
3343 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003344 int ret = 0;
3345 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003346 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003347 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003348
Willy Tarreauea18f862018-12-22 20:19:26 +01003349next_frame:
3350 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3351 goto leave; // incomplete input frame
3352
3353 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3354 * this case, we'll try to paste it immediately after the initial
3355 * HEADERS frame payload and kill any possible padding. The initial
3356 * frame's length will be increased to represent the concatenation
3357 * of the two frames. The next frame is read from position <tlen>
3358 * and written at position <flen> (minus padding if some is present).
3359 */
3360 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3361 struct h2_fh hdr;
3362 int clen; // CONTINUATION frame's payload length
3363
3364 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3365 /* no more data, the buffer may be full, either due to
3366 * too large a frame or because of too large a hole that
3367 * we're going to compact at the end.
3368 */
3369 goto leave;
3370 }
3371
3372 if (hdr.ft != H2_FT_CONTINUATION) {
3373 /* RFC7540#6.10: frame of unexpected type */
3374 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3375 goto fail;
3376 }
3377
3378 if (hdr.sid != h2c->dsi) {
3379 /* RFC7540#6.10: frame of different stream */
3380 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3381 goto fail;
3382 }
3383
3384 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3385 /* RFC7540#4.2: invalid frame length */
3386 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3387 goto fail;
3388 }
3389
3390 /* detect when we must stop aggragating frames */
3391 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3392
3393 /* Take as much as we can of the CONTINUATION frame's payload */
3394 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3395 if (clen > hdr.len)
3396 clen = hdr.len;
3397
3398 /* Move the frame's payload over the padding, hole and frame
3399 * header. At least one of hole or dpl is null (see diagrams
3400 * above). The hole moves after the new aggragated frame.
3401 */
3402 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3403 h2c->dfl += clen - h2c->dpl;
3404 hole += h2c->dpl + 9;
3405 h2c->dpl = 0;
3406 goto next_frame;
3407 }
3408
3409 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003410
Willy Tarreau13278b42017-10-13 19:23:14 +02003411 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003412 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003413 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003414 copy = alloc_trash_chunk();
3415 if (!copy) {
3416 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3417 goto fail;
3418 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003419 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3420 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3421 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003422 }
3423
Willy Tarreau13278b42017-10-13 19:23:14 +02003424 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3425 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003426 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003427 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3428 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003429 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003430 }
3431
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003432 if (flen < 5) {
3433 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3434 goto fail;
3435 }
3436
Willy Tarreau13278b42017-10-13 19:23:14 +02003437 hdrs += 5; // stream dep = 4, weight = 1
3438 flen -= 5;
3439 }
3440
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003441 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003442 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003443 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003444 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003445
Willy Tarreau937f7602018-02-26 15:22:17 +01003446 /* we can't retry a failed decompression operation so we must be very
3447 * careful not to take any risks. In practice the output buffer is
3448 * always empty except maybe for trailers, in which case we simply have
3449 * to wait for the upper layer to finish consuming what is available.
3450 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003451
3452 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003453 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003454 if (!htx_is_empty(htx)) {
3455 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003456 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003457 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003458 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003459 if (b_data(rxbuf)) {
3460 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003461 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003462 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003463
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003464 rxbuf->head = 0;
3465 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003466 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003467
Willy Tarreau25919232019-01-03 14:48:18 +01003468 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003469 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3470 sizeof(list)/sizeof(list[0]), tmp);
3471 if (outlen < 0) {
3472 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3473 goto fail;
3474 }
3475
Willy Tarreau25919232019-01-03 14:48:18 +01003476 /* The PACK decompressor was updated, let's update the input buffer and
3477 * the parser's state to commit these changes and allow us to later
3478 * fail solely on the stream if needed.
3479 */
3480 b_del(&h2c->dbuf, h2c->dfl + hole);
3481 h2c->dfl = hole = 0;
3482 h2c->st0 = H2_CS_FRAME_H;
3483
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003484 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003485 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003486
Willy Tarreau88d138e2019-01-02 19:38:14 +01003487 if (*flags & H2_SF_HEADERS_RCVD)
3488 goto trailers;
3489
3490 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003491 if (htx) {
3492 /* HTX mode */
3493 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003494 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003495 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003496 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003497 } else {
3498 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003499 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003500 if (outlen > 0)
3501 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003502 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003503
3504 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003505 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003506 goto fail;
3507 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003508
Willy Tarreau174b06a2018-04-25 18:13:58 +02003509 if (msgf & H2_MSGF_BODY) {
3510 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003511 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003512 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003513 if (htx)
3514 htx->extra = *body_len;
3515 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003516 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003517 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003518 }
3519
Willy Tarreau88d138e2019-01-02 19:38:14 +01003520 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003521 /* indicate that a HEADERS frame was received for this stream, except
3522 * for 1xx responses. For 1xx responses, another HEADERS frame is
3523 * expected.
3524 */
3525 if (!(msgf & H2_MSGF_RSP_1XX))
3526 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003527
Christopher Faulet0b465482019-02-19 15:14:23 +01003528 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003529 /* Mark the end of message, either using EOM in HTX or with the
3530 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3531 * is not set during headers with END_STREAM.
3532 */
3533 if (htx) {
3534 if (!htx_add_endof(htx, HTX_BLK_EOM))
3535 goto fail;
3536 }
3537 else if (*flags & H2_SF_DATA_CHNK) {
3538 if (!b_putblk(rxbuf, "\r\n", 2))
3539 goto fail;
3540 }
3541 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003542
Willy Tarreau86277d42019-01-02 15:36:11 +01003543 /* success */
3544 ret = 1;
3545
Willy Tarreau68dd9852017-07-03 14:44:26 +02003546 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003547 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003548 * move the remaining data over it.
3549 */
3550 if (hole) {
3551 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3552 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3553 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3554 b_sub(&h2c->dbuf, hole);
3555 }
3556
3557 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3558 /* too large frames */
3559 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003560 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003561 }
3562
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003563 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003564 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003565 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003566 return ret;
3567
Willy Tarreau68dd9852017-07-03 14:44:26 +02003568 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003569 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003570 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003571
3572 trailers:
3573 /* This is the last HEADERS frame hence a trailer */
3574
3575 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3576 /* It's a trailer but it's missing ES flag */
3577 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3578 goto fail;
3579 }
3580
3581 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3582 * block, and when using chunks we must send the 0 CRLF marker. For
3583 * other modes, the trailers are silently dropped.
3584 */
3585 if (htx) {
3586 if (!htx_add_endof(htx, HTX_BLK_EOD))
3587 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003588 if (h2_make_htx_trailers(list, htx) <= 0)
3589 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003590 }
3591 else if (*flags & H2_SF_DATA_CHNK) {
3592 /* Legacy mode with chunked encoding : we must finalize the
3593 * data block message emit the trailing CRLF */
3594 if (!b_putblk(rxbuf, "0\r\n", 3))
3595 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003596
3597 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3598 if (outlen > 0)
3599 b_add(rxbuf, outlen);
3600 else
3601 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003602 }
3603
3604 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003605}
3606
Willy Tarreau454f9052017-10-26 19:40:35 +02003607/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3608 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3609 * in use, a new chunk is emitted for each frame. This is supposed to fit
3610 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3611 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3612 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003613 * parser state is automatically updated. Returns > 0 if it could completely
3614 * send the current frame, 0 if it couldn't complete, in which case
3615 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3616 * DATA frame can return 0 as a valid result). Stream errors are reported in
3617 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3618 * have checked the frame header and ensured that the frame was complete or the
3619 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003620 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003621static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003622{
3623 struct h2c *h2c = h2s->h2c;
3624 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003625 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003626 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003627 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003628 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003629
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003630 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003631
Olivier Houchard638b7992018-08-16 15:41:52 +02003632 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003633 if (!csbuf) {
3634 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003635 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003636 }
3637
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003638try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003639 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003640 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3641 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003642 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003643 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003644
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003645 if (flen > b_data(&h2c->dbuf)) {
3646 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003647 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003648 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003649 }
3650
Willy Tarreaua9b77962019-01-31 07:23:00 +01003651 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003652 block1 = htx_free_data_space(htx);
3653 if (!block1) {
3654 h2c->flags |= H2_CF_DEM_SFULL;
3655 goto fail;
3656 }
3657 if (flen > block1)
3658 flen = block1;
3659
3660 /* here, flen is the max we can copy into the output buffer */
3661 block1 = b_contig_data(&h2c->dbuf, 0);
3662 if (flen > block1)
3663 flen = block1;
3664
3665 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3666 h2c->flags |= H2_CF_DEM_SFULL;
3667 goto fail;
3668 }
3669
3670 b_del(&h2c->dbuf, flen);
3671 h2c->dfl -= flen;
3672 h2c->rcvd_c += flen;
3673 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003674
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003675 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003676 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003677 htx->extra = h2s->body_len;
3678 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003679 goto try_again;
3680 }
3681 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003682 /* it doesn't fit and the buffer is fragmented,
3683 * so let's defragment it and try again.
3684 */
3685 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003686 }
3687
Willy Tarreaueba10f22018-04-25 20:44:22 +02003688 /* chunked-encoding requires more room */
3689 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003690 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003691 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3692 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3693 (chklen < 1048576) ? 4 : 8;
3694 chklen += 4; // CRLF, CRLF
3695 }
3696
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003697 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003698 if (flen + chklen > b_room(csbuf)) {
3699 if (chklen >= b_room(csbuf)) {
3700 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003701 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003702 }
3703 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003704 }
3705
3706 if (h2s->flags & H2_SF_DATA_CHNK) {
3707 /* emit the chunk size */
3708 unsigned int chksz = flen;
3709 char str[10];
3710 char *beg;
3711
3712 beg = str + sizeof(str);
3713 *--beg = '\n';
3714 *--beg = '\r';
3715 do {
3716 *--beg = hextab[chksz & 0xF];
3717 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003718 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003719 }
3720
Willy Tarreau454f9052017-10-26 19:40:35 +02003721 /* Block1 is the length of the first block before the buffer wraps,
3722 * block2 is the optional second block to reach the end of the frame.
3723 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003724 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003725 if (block1 > flen)
3726 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003727 block2 = flen - block1;
3728
3729 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003730 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003731
3732 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003733 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003734
Willy Tarreaueba10f22018-04-25 20:44:22 +02003735 if (h2s->flags & H2_SF_DATA_CHNK) {
3736 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003737 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003738 }
3739
Willy Tarreau454f9052017-10-26 19:40:35 +02003740 /* now mark the input data as consumed (will be deleted from the buffer
3741 * by the caller when seeing FRAME_A after sending the window update).
3742 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003743 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003744 h2c->dfl -= flen;
3745 h2c->rcvd_c += flen;
3746 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3747
Willy Tarreau1915ca22019-01-24 11:49:37 +01003748 if (h2s->flags & H2_SF_DATA_CLEN)
3749 h2s->body_len -= flen;
3750
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003751 if (h2c->dfl > h2c->dpl) {
3752 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003753 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003754 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003755 }
3756
Willy Tarreau4a28da12018-01-04 14:41:00 +01003757 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003758 /* here we're done with the frame, all the payload (except padding) was
3759 * transferred.
3760 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003761
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003762 if (h2c->dff & H2_F_DATA_END_STREAM) {
3763 if (htx) {
3764 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3765 h2c->flags |= H2_CF_DEM_SFULL;
3766 goto fail;
3767 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003768 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003769 else if (h2s->flags & H2_SF_DATA_CHNK) {
3770 /* emit the trailing 0 CRLF CRLF */
3771 if (b_room(csbuf) < 5) {
3772 h2c->flags |= H2_CF_DEM_SFULL;
3773 goto fail;
3774 }
3775 chklen += 5;
3776 b_putblk(csbuf, "0\r\n\r\n", 5);
3777 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003778 }
3779
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003780 h2c->rcvd_c += h2c->dpl;
3781 h2c->rcvd_s += h2c->dpl;
3782 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003783 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003784 if (htx)
3785 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003786 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003787 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003788 if (htx)
3789 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003790 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003791}
3792
Willy Tarreau5dd17352018-06-14 13:33:30 +02003793/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3794 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3795 * number of bytes sent. The caller must check the stream's status to detect
3796 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003797 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003798static 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 +02003799{
3800 struct http_hdr list[MAX_HTTP_HDR];
3801 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003802 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003803 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003804 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003805 int es_now = 0;
3806 int ret = 0;
3807 int hdr;
3808
3809 if (h2c_mux_busy(h2c, h2s)) {
3810 h2s->flags |= H2_SF_BLK_MBUSY;
3811 return 0;
3812 }
3813
Willy Tarreau44e973f2018-03-01 17:49:30 +01003814 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003815 h2c->flags |= H2_CF_MUX_MALLOC;
3816 h2s->flags |= H2_SF_BLK_MROOM;
3817 return 0;
3818 }
3819
3820 /* First, try to parse the H1 response and index it into <list>.
3821 * NOTE! Since it comes from haproxy, we *know* that a response header
3822 * block does not wrap and we can safely read it this way without
3823 * having to realign the buffer.
3824 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003825 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003826 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003827 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003828 /* incomplete or invalid response, this is abnormal coming from
3829 * haproxy and may only result in a bad errorfile or bad Lua code
3830 * so that won't be fixed, raise an error now.
3831 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003832 * FIXME: we should instead add the ability to only return a
3833 * 502 bad gateway. But in theory this is not supposed to
3834 * happen.
3835 */
3836 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3837 ret = 0;
3838 goto end;
3839 }
3840
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003841 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003842
3843 /* certain statuses have no body or an empty one, regardless of
3844 * what the headers say.
3845 */
3846 if (sl.st.status >= 100 && sl.st.status < 200) {
3847 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3848 h1m->curr_len = h1m->body_len = 0;
3849 }
3850 else if (sl.st.status == 204 || sl.st.status == 304) {
3851 /* no contents, claim c-len is present and set to zero */
3852 h1m->flags &= ~H1_MF_CHNK;
3853 h1m->flags |= H1_MF_CLEN;
3854 h1m->curr_len = h1m->body_len = 0;
3855 }
3856
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003857 chunk_reset(&outbuf);
3858
3859 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003860 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003861 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003862 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003863
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003864 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003865 break;
3866 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003867 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003868 }
3869
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003870 if (outbuf.size < 9)
3871 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003872
3873 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003874 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3875 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3876 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003877
3878 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003879 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003880 /* this is an unparsable response */
3881 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3882 ret = 0;
3883 goto end;
3884 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003885
3886 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003887 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003888 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003889 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003890 }
3891
3892 /* encode all headers, stop at empty name */
3893 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003894 /* these ones do not exist in H2 and must be dropped. */
3895 if (isteq(list[hdr].n, ist("connection")) ||
3896 isteq(list[hdr].n, ist("proxy-connection")) ||
3897 isteq(list[hdr].n, ist("keep-alive")) ||
3898 isteq(list[hdr].n, ist("upgrade")) ||
3899 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003900 continue;
3901
3902 if (isteq(list[hdr].n, ist("")))
3903 break; // end
3904
3905 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3906 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003907 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003908 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003909 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003910 }
3911 }
3912
3913 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003914 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003915 es_now = 1;
3916
3917 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003918 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003919
3920 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003921 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003922
3923 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003924 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003925
3926 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003927 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003928 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003929
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003930 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003931 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003932 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003933
Willy Tarreau801250e2018-09-11 11:45:04 +02003934 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003935 h2s->flags |= H2_SF_ES_SENT;
3936 if (h2s->st == H2_SS_OPEN)
3937 h2s->st = H2_SS_HLOC;
3938 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003939 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003940 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003941 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003942 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003943 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003944 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003945 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003946 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003947 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003948
3949 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003950
3951 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003952 //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 +02003953 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003954 full:
3955 h1m_init_res(h1m);
3956 h1m->err_pos = -1; // don't care about errors on the response path
3957 h2c->flags |= H2_CF_MUX_MFULL;
3958 h2s->flags |= H2_SF_BLK_MROOM;
3959 ret = 0;
3960 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003961}
3962
Willy Tarreau5dd17352018-06-14 13:33:30 +02003963/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3964 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3965 * the number of bytes sent. The caller must check the stream's status to
3966 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003967 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003968static 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 +02003969{
3970 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003971 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003972 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003973 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003974 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003975 int es_now = 0;
3976 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003977 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003978 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003979
3980 if (h2c_mux_busy(h2c, h2s)) {
3981 h2s->flags |= H2_SF_BLK_MBUSY;
3982 goto end;
3983 }
3984
Willy Tarreau44e973f2018-03-01 17:49:30 +01003985 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003986 h2c->flags |= H2_CF_MUX_MALLOC;
3987 h2s->flags |= H2_SF_BLK_MROOM;
3988 goto end;
3989 }
3990
3991 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003992 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003993 goto end;
3994
3995 chunk_reset(&outbuf);
3996
3997 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003998 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003999 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004000 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004001
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004002 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004003 break;
4004 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004005 /* If there are pending data in the output buffer, and we have
4006 * less than 1/4 of the mbuf's size and everything fits, we'll
4007 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4008 * is full and wait, to save some slow realign calls.
4009 */
4010 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4011 h2c->flags |= H2_CF_MUX_MFULL;
4012 h2s->flags |= H2_SF_BLK_MROOM;
4013 goto end;
4014 }
4015
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004016 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004017 }
4018
4019 if (outbuf.size < 9) {
4020 h2c->flags |= H2_CF_MUX_MFULL;
4021 h2s->flags |= H2_SF_BLK_MROOM;
4022 goto end;
4023 }
4024
4025 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004026 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4027 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4028 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004029
4030 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4031 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004032 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004033 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004034 break;
4035 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004036 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004037 if ((long long)size > h1m->curr_len)
4038 size = h1m->curr_len;
4039 break;
4040 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004041 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004042 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004043 if (!ret)
4044 goto end;
4045
4046 if (ret < 0) {
4047 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004048 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004049 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4050 goto end;
4051 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004052 max -= ret;
4053 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004054 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004055 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004056 }
4057
Willy Tarreau801250e2018-09-11 11:45:04 +02004058 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004059 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004060 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004061 if (!ret)
4062 goto end;
4063
4064 if (ret < 0) {
4065 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004066 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004067 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4068 goto end;
4069 }
4070
4071 size = chunk;
4072 h1m->curr_len = chunk;
4073 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004074 max -= ret;
4075 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004076 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004077 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004078 if (!size)
4079 goto send_empty;
4080 }
4081
4082 /* in MSG_DATA state, continue below */
4083 size = h1m->curr_len;
4084 break;
4085 }
4086
4087 /* we have in <size> the exact number of bytes we need to copy from
4088 * the H1 buffer. We need to check this against the connection's and
4089 * the stream's send windows, and to ensure that this fits in the max
4090 * frame size and in the buffer's available space minus 9 bytes (for
4091 * the frame header). The connection's flow control is applied last so
4092 * that we can use a separate list of streams which are immediately
4093 * unblocked on window opening. Note: we don't implement padding.
4094 */
4095
Willy Tarreau5dd17352018-06-14 13:33:30 +02004096 if (size > max)
4097 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004098
4099 if (size > h2s->mws)
4100 size = h2s->mws;
4101
4102 if (size <= 0) {
4103 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004104 if (h2s->send_wait) {
4105 LIST_DEL(&h2s->list);
4106 LIST_INIT(&h2s->list);
4107 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004108 goto end;
4109 }
4110
4111 if (h2c->mfs && size > h2c->mfs)
4112 size = h2c->mfs;
4113
4114 if (size + 9 > outbuf.size) {
4115 /* we have an opportunity for enlarging the too small
4116 * available space, let's try.
4117 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004118 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004119 goto realign_again;
4120 size = outbuf.size - 9;
4121 }
4122
4123 if (size <= 0) {
4124 h2c->flags |= H2_CF_MUX_MFULL;
4125 h2s->flags |= H2_SF_BLK_MROOM;
4126 goto end;
4127 }
4128
4129 if (size > h2c->mws)
4130 size = h2c->mws;
4131
4132 if (size <= 0) {
4133 h2s->flags |= H2_SF_BLK_MFCTL;
4134 goto end;
4135 }
4136
4137 /* copy whatever we can */
4138 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004139 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004140 if (ret == 1)
4141 len2 = 0;
4142
4143 if (!ret || len1 + len2 < size) {
4144 /* FIXME: must normally never happen */
4145 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4146 goto end;
4147 }
4148
4149 /* limit len1/len2 to size */
4150 if (len1 + len2 > size) {
4151 int sub = len1 + len2 - size;
4152
4153 if (len2 > sub)
4154 len2 -= sub;
4155 else {
4156 sub -= len2;
4157 len2 = 0;
4158 len1 -= sub;
4159 }
4160 }
4161
4162 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004163 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004164 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004165 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004166
4167 send_empty:
4168 /* we may need to add END_STREAM */
4169 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4170 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004171 *
4172 * FIXME: what we do here is not correct because we send end_stream
4173 * before knowing if we'll have to send a HEADERS frame for the
4174 * trailers. More importantly we're not consuming the trailing CRLF
4175 * after the end of trailers, so it will be left to the caller to
4176 * eat it. The right way to do it would be to measure trailers here
4177 * and to send ES only if there are no trailers.
4178 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004179 */
4180 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004181 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004182 es_now = 1;
4183
4184 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004185 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004186
4187 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004188 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004189
4190 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004191 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004192
4193 /* consume incoming H1 response */
4194 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004195 max -= size;
4196 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004197 total += size;
4198 h1m->curr_len -= size;
4199 h2s->mws -= size;
4200 h2c->mws -= size;
4201
4202 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004203 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004204 goto new_frame;
4205 }
4206 }
4207
4208 if (es_now) {
4209 if (h2s->st == H2_SS_OPEN)
4210 h2s->st = H2_SS_HLOC;
4211 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004212 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004213
Willy Tarreau35a62702018-02-27 15:37:25 +01004214 if (!(h1m->flags & H1_MF_CHNK)) {
4215 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004216 total += max;
4217 ofs += max;
4218 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004219
Willy Tarreau801250e2018-09-11 11:45:04 +02004220 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004221 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004222
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004223 h2s->flags |= H2_SF_ES_SENT;
4224 }
4225
4226 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004227 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 +02004228 return total;
4229}
4230
Willy Tarreau115e83b2018-12-01 19:17:53 +01004231/* Try to send a HEADERS frame matching HTX response present in HTX message
4232 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4233 * must check the stream's status to detect any error which might have happened
4234 * subsequently to a successful send. The htx blocks are automatically removed
4235 * from the message. The htx message is assumed to be valid since produced from
4236 * the internal code, hence it contains a start line, an optional series of
4237 * header blocks and an end of header, otherwise an invalid frame could be
4238 * emitted and the resulting htx message could be left in an inconsistent state.
4239 */
4240static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4241{
4242 struct http_hdr list[MAX_HTTP_HDR];
4243 struct h2c *h2c = h2s->h2c;
4244 struct htx_blk *blk;
4245 struct htx_blk *blk_end;
4246 struct buffer outbuf;
4247 struct htx_sl *sl;
4248 enum htx_blk_type type;
4249 int es_now = 0;
4250 int ret = 0;
4251 int hdr;
4252 int idx;
4253
4254 if (h2c_mux_busy(h2c, h2s)) {
4255 h2s->flags |= H2_SF_BLK_MBUSY;
4256 return 0;
4257 }
4258
4259 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4260 h2c->flags |= H2_CF_MUX_MALLOC;
4261 h2s->flags |= H2_SF_BLK_MROOM;
4262 return 0;
4263 }
4264
4265 /* determine the first block which must not be deleted, blk_end may
4266 * be NULL if all blocks have to be deleted.
4267 */
4268 idx = htx_get_head(htx);
4269 blk_end = NULL;
4270 while (idx != -1) {
4271 type = htx_get_blk_type(htx_get_blk(htx, idx));
4272 idx = htx_get_next(htx, idx);
4273 if (type == HTX_BLK_EOH) {
4274 if (idx != -1)
4275 blk_end = htx_get_blk(htx, idx);
4276 break;
4277 }
4278 }
4279
4280 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004281 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004282 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004283 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004284 if (h2s->status < 100 || h2s->status > 999)
4285 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004286
4287 /* and the rest of the headers, that we dump starting at header 0 */
4288 hdr = 0;
4289
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004290 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004291 while ((idx = htx_get_next(htx, idx)) != -1) {
4292 blk = htx_get_blk(htx, idx);
4293 type = htx_get_blk_type(blk);
4294
4295 if (type == HTX_BLK_UNUSED)
4296 continue;
4297
4298 if (type != HTX_BLK_HDR)
4299 break;
4300
4301 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4302 goto fail;
4303
4304 list[hdr].n = htx_get_blk_name(htx, blk);
4305 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004306 hdr++;
4307 }
4308
4309 /* marker for end of headers */
4310 list[hdr].n = ist("");
4311
4312 if (h2s->status == 204 || h2s->status == 304) {
4313 /* no contents, claim c-len is present and set to zero */
4314 es_now = 1;
4315 }
4316
4317 chunk_reset(&outbuf);
4318
4319 while (1) {
4320 outbuf.area = b_tail(&h2c->mbuf);
4321 outbuf.size = b_contig_space(&h2c->mbuf);
4322 outbuf.data = 0;
4323
4324 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4325 break;
4326 realign_again:
4327 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4328 }
4329
4330 if (outbuf.size < 9)
4331 goto full;
4332
4333 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4334 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4335 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4336 outbuf.data = 9;
4337
4338 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004339 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004340 if (b_space_wraps(&h2c->mbuf))
4341 goto realign_again;
4342 goto full;
4343 }
4344
4345 /* encode all headers, stop at empty name */
4346 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4347 /* these ones do not exist in H2 and must be dropped. */
4348 if (isteq(list[hdr].n, ist("connection")) ||
4349 isteq(list[hdr].n, ist("proxy-connection")) ||
4350 isteq(list[hdr].n, ist("keep-alive")) ||
4351 isteq(list[hdr].n, ist("upgrade")) ||
4352 isteq(list[hdr].n, ist("transfer-encoding")))
4353 continue;
4354
4355 if (isteq(list[hdr].n, ist("")))
4356 break; // end
4357
4358 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4359 /* output full */
4360 if (b_space_wraps(&h2c->mbuf))
4361 goto realign_again;
4362 goto full;
4363 }
4364 }
4365
Christopher Faulet0b465482019-02-19 15:14:23 +01004366 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004367 * FIXME: we should also set it when we know for sure that the
4368 * content-length is zero as well as on 204/304
4369 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004370 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4371 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004372 es_now = 1;
4373
Willy Tarreau927b88b2019-03-04 08:03:25 +01004374 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004375 es_now = 1;
4376
4377 /* update the frame's size */
4378 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4379
4380 if (es_now)
4381 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4382
4383 /* commit the H2 response */
4384 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004385
4386 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4387 * 1xx responses, another HEADERS frame is expected.
4388 */
4389 if (h2s->status >= 200 || h2s->status == 101)
4390 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004391
Willy Tarreau115e83b2018-12-01 19:17:53 +01004392 if (es_now) {
4393 h2s->flags |= H2_SF_ES_SENT;
4394 if (h2s->st == H2_SS_OPEN)
4395 h2s->st = H2_SS_HLOC;
4396 else
4397 h2s_close(h2s);
4398 }
4399
4400 /* OK we could properly deliver the response */
4401
4402 /* remove all header blocks including the EOH and compute the
4403 * corresponding size.
4404 *
4405 * FIXME: We should remove everything when es_now is set.
4406 */
4407 ret = 0;
4408 idx = htx_get_head(htx);
4409 blk = htx_get_blk(htx, idx);
4410 while (blk != blk_end) {
4411 ret += htx_get_blksz(blk);
4412 blk = htx_remove_blk(htx, blk);
4413 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004414
4415 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4416 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004417 end:
4418 return ret;
4419 full:
4420 h2c->flags |= H2_CF_MUX_MFULL;
4421 h2s->flags |= H2_SF_BLK_MROOM;
4422 ret = 0;
4423 goto end;
4424 fail:
4425 /* unparsable HTX messages, too large ones to be produced in the local
4426 * list etc go here (unrecoverable errors).
4427 */
4428 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4429 ret = 0;
4430 goto end;
4431}
4432
Willy Tarreau80739692018-10-05 11:35:57 +02004433/* Try to send a HEADERS frame matching HTX request present in HTX message
4434 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4435 * must check the stream's status to detect any error which might have happened
4436 * subsequently to a successful send. The htx blocks are automatically removed
4437 * from the message. The htx message is assumed to be valid since produced from
4438 * the internal code, hence it contains a start line, an optional series of
4439 * header blocks and an end of header, otherwise an invalid frame could be
4440 * emitted and the resulting htx message could be left in an inconsistent state.
4441 */
4442static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4443{
4444 struct http_hdr list[MAX_HTTP_HDR];
4445 struct h2c *h2c = h2s->h2c;
4446 struct htx_blk *blk;
4447 struct htx_blk *blk_end;
4448 struct buffer outbuf;
4449 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004450 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004451 enum htx_blk_type type;
4452 int es_now = 0;
4453 int ret = 0;
4454 int hdr;
4455 int idx;
4456
4457 if (h2c_mux_busy(h2c, h2s)) {
4458 h2s->flags |= H2_SF_BLK_MBUSY;
4459 return 0;
4460 }
4461
4462 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4463 h2c->flags |= H2_CF_MUX_MALLOC;
4464 h2s->flags |= H2_SF_BLK_MROOM;
4465 return 0;
4466 }
4467
4468 /* determine the first block which must not be deleted, blk_end may
4469 * be NULL if all blocks have to be deleted.
4470 */
4471 idx = htx_get_head(htx);
4472 blk_end = NULL;
4473 while (idx != -1) {
4474 type = htx_get_blk_type(htx_get_blk(htx, idx));
4475 idx = htx_get_next(htx, idx);
4476 if (type == HTX_BLK_EOH) {
4477 if (idx != -1)
4478 blk_end = htx_get_blk(htx, idx);
4479 break;
4480 }
4481 }
4482
4483 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004484 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004485 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004486 meth = htx_sl_req_meth(sl);
4487 path = htx_sl_req_uri(sl);
4488
4489 /* and the rest of the headers, that we dump starting at header 0 */
4490 hdr = 0;
4491
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004492 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004493 while ((idx = htx_get_next(htx, idx)) != -1) {
4494 blk = htx_get_blk(htx, idx);
4495 type = htx_get_blk_type(blk);
4496
4497 if (type == HTX_BLK_UNUSED)
4498 continue;
4499
4500 if (type != HTX_BLK_HDR)
4501 break;
4502
4503 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4504 goto fail;
4505
4506 list[hdr].n = htx_get_blk_name(htx, blk);
4507 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004508 hdr++;
4509 }
4510
4511 /* marker for end of headers */
4512 list[hdr].n = ist("");
4513
4514 chunk_reset(&outbuf);
4515
4516 while (1) {
4517 outbuf.area = b_tail(&h2c->mbuf);
4518 outbuf.size = b_contig_space(&h2c->mbuf);
4519 outbuf.data = 0;
4520
4521 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4522 break;
4523 realign_again:
4524 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4525 }
4526
4527 if (outbuf.size < 9)
4528 goto full;
4529
4530 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4531 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4532 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4533 outbuf.data = 9;
4534
4535 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004536 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004537 if (b_space_wraps(&h2c->mbuf))
4538 goto realign_again;
4539 goto full;
4540 }
4541
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004542 /* RFC7540 #8.3: the CONNECT method must have :
4543 * - :authority set to the URI part (host:port)
4544 * - :method set to CONNECT
4545 * - :scheme and :path omitted
4546 */
4547 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4548 /* encode the scheme which is always "https" (or 0x86 for "http") */
4549 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4550 /* output full */
4551 if (b_space_wraps(&h2c->mbuf))
4552 goto realign_again;
4553 goto full;
4554 }
Willy Tarreau80739692018-10-05 11:35:57 +02004555
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004556 /* encode the path, which necessarily is the second one */
4557 if (!hpack_encode_path(&outbuf, path)) {
4558 /* output full */
4559 if (b_space_wraps(&h2c->mbuf))
4560 goto realign_again;
4561 goto full;
4562 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004563
4564 /* look for the Host header and place it in :authority */
4565 auth = ist2(NULL, 0);
4566 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4567 if (isteq(list[hdr].n, ist("")))
4568 break; // end
4569
4570 if (isteq(list[hdr].n, ist("host"))) {
4571 auth = list[hdr].v;
4572 break;
4573 }
4574 }
4575 }
4576 else {
4577 /* for CONNECT, :authority is taken from the path */
4578 auth = path;
4579 }
4580
4581 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4582 /* output full */
4583 if (b_space_wraps(&h2c->mbuf))
4584 goto realign_again;
4585 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004586 }
4587
4588 /* encode all headers, stop at empty name */
4589 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4590 /* these ones do not exist in H2 and must be dropped. */
4591 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004592 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004593 isteq(list[hdr].n, ist("proxy-connection")) ||
4594 isteq(list[hdr].n, ist("keep-alive")) ||
4595 isteq(list[hdr].n, ist("upgrade")) ||
4596 isteq(list[hdr].n, ist("transfer-encoding")))
4597 continue;
4598
4599 if (isteq(list[hdr].n, ist("")))
4600 break; // end
4601
4602 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4603 /* output full */
4604 if (b_space_wraps(&h2c->mbuf))
4605 goto realign_again;
4606 goto full;
4607 }
4608 }
4609
4610 /* we may need to add END_STREAM if we have no body :
4611 * - request already closed, or :
4612 * - no transfer-encoding, and :
4613 * - no content-length or content-length:0
4614 * Fixme: this doesn't take into account CONNECT requests.
4615 */
4616 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4617 es_now = 1;
4618
4619 if (sl->flags & HTX_SL_F_BODYLESS)
4620 es_now = 1;
4621
Willy Tarreau927b88b2019-03-04 08:03:25 +01004622 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004623 es_now = 1;
4624
4625 /* update the frame's size */
4626 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4627
4628 if (es_now)
4629 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4630
4631 /* commit the H2 response */
4632 b_add(&h2c->mbuf, outbuf.data);
4633 h2s->flags |= H2_SF_HEADERS_SENT;
4634 h2s->st = H2_SS_OPEN;
4635
Willy Tarreau80739692018-10-05 11:35:57 +02004636 if (es_now) {
4637 // trim any possibly pending data (eg: inconsistent content-length)
4638 h2s->flags |= H2_SF_ES_SENT;
4639 h2s->st = H2_SS_HLOC;
4640 }
4641
4642 /* remove all header blocks including the EOH and compute the
4643 * corresponding size.
4644 *
4645 * FIXME: We should remove everything when es_now is set.
4646 */
4647 ret = 0;
4648 idx = htx_get_head(htx);
4649 blk = htx_get_blk(htx, idx);
4650 while (blk != blk_end) {
4651 ret += htx_get_blksz(blk);
4652 blk = htx_remove_blk(htx, blk);
4653 }
4654
4655 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4656 htx_remove_blk(htx, blk_end);
4657
4658 end:
4659 return ret;
4660 full:
4661 h2c->flags |= H2_CF_MUX_MFULL;
4662 h2s->flags |= H2_SF_BLK_MROOM;
4663 ret = 0;
4664 goto end;
4665 fail:
4666 /* unparsable HTX messages, too large ones to be produced in the local
4667 * list etc go here (unrecoverable errors).
4668 */
4669 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4670 ret = 0;
4671 goto end;
4672}
4673
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004674/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004675 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4676 * caller must check the stream's status to detect any error which might have
4677 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004678 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4679 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004680static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004681{
4682 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004683 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004684 struct buffer outbuf;
4685 size_t total = 0;
4686 int es_now = 0;
4687 int bsize; /* htx block size */
4688 int fsize; /* h2 frame size */
4689 struct htx_blk *blk;
4690 enum htx_blk_type type;
4691 int idx;
4692
4693 if (h2c_mux_busy(h2c, h2s)) {
4694 h2s->flags |= H2_SF_BLK_MBUSY;
4695 goto end;
4696 }
4697
4698 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4699 h2c->flags |= H2_CF_MUX_MALLOC;
4700 h2s->flags |= H2_SF_BLK_MROOM;
4701 goto end;
4702 }
4703
Willy Tarreau98de12a2018-12-12 07:03:00 +01004704 htx = htx_from_buf(buf);
4705
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004706 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4707 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4708 * the caller to handle.
4709 */
4710
4711 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004712 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004713 goto end;
4714
4715 idx = htx_get_head(htx);
4716 blk = htx_get_blk(htx, idx);
4717 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4718 bsize = htx_get_blksz(blk);
4719 fsize = bsize;
4720
4721 if (type == HTX_BLK_EOD) {
4722 /* if we have an EOD, we're dealing with chunked data. We may
4723 * have a set of trailers after us that the caller will want to
4724 * deal with. Let's simply remove the EOD and return.
4725 */
4726 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004727 total++; // EOD counts as one byte
4728 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004729 goto end;
4730 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004731 else if (type == HTX_BLK_EOM) {
4732 if (h2s->flags & H2_SF_ES_SENT) {
4733 /* ES already sent */
4734 htx_remove_blk(htx, blk);
4735 total++; // EOM counts as one byte
4736 count--;
4737 goto end;
4738 }
4739 }
4740 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004741 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004742
4743 /* Perform some optimizations to reduce the number of buffer copies.
4744 * First, if the mux's buffer is empty and the htx area contains
4745 * exactly one data block of the same size as the requested count, and
4746 * this count fits within the frame size, the stream's window size, and
4747 * the connection's window size, then it's possible to simply swap the
4748 * caller's buffer with the mux's output buffer and adjust offsets and
4749 * length to match the entire DATA HTX block in the middle. In this
4750 * case we perform a true zero-copy operation from end-to-end. This is
4751 * the situation that happens all the time with large files. Second, if
4752 * this is not possible, but the mux's output buffer is empty, we still
4753 * have an opportunity to avoid the copy to the intermediary buffer, by
4754 * making the intermediary buffer's area point to the output buffer's
4755 * area. In this case we want to skip the HTX header to make sure that
4756 * copies remain aligned and that this operation remains possible all
4757 * the time. This goes for headers, data blocks and any data extracted
4758 * from the HTX blocks.
4759 */
4760 if (unlikely(fsize == count &&
4761 htx->used == 1 && type == HTX_BLK_DATA &&
4762 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4763 void *old_area = h2c->mbuf.area;
4764
4765 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004766 /* Too bad there are data left there. We're willing to memcpy/memmove
4767 * up to 1/4 of the buffer, which means that it's OK to copy a large
4768 * frame into a buffer containing few data if it needs to be realigned,
4769 * and that it's also OK to copy few data without realigning. Otherwise
4770 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004771 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004772 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4773 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4774 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004775 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004776
Willy Tarreau98de12a2018-12-12 07:03:00 +01004777 h2c->flags |= H2_CF_MUX_MFULL;
4778 h2s->flags |= H2_SF_BLK_MROOM;
4779 goto end;
4780 }
4781
4782 /* map an H2 frame to the HTX block so that we can put the
4783 * frame header there.
4784 */
4785 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004786 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004787 h2c->mbuf.data = fsize + 9;
4788 outbuf.area = b_head(&h2c->mbuf);
4789
4790 /* prepend an H2 DATA frame header just before the DATA block */
4791 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4792 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4793 h2_set_frame_size(outbuf.area, fsize);
4794
4795 /* update windows */
4796 h2s->mws -= fsize;
4797 h2c->mws -= fsize;
4798
4799 /* and exchange with our old area */
4800 buf->area = old_area;
4801 buf->data = buf->head = 0;
4802 total += fsize;
4803 goto end;
4804 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004805
Willy Tarreau98de12a2018-12-12 07:03:00 +01004806 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004807 /* for DATA and EOM we'll have to emit a frame, even if empty */
4808
4809 while (1) {
4810 outbuf.area = b_tail(&h2c->mbuf);
4811 outbuf.size = b_contig_space(&h2c->mbuf);
4812 outbuf.data = 0;
4813
4814 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4815 break;
4816 realign_again:
4817 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4818 }
4819
4820 if (outbuf.size < 9) {
4821 h2c->flags |= H2_CF_MUX_MFULL;
4822 h2s->flags |= H2_SF_BLK_MROOM;
4823 goto end;
4824 }
4825
4826 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4827 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4828 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4829 outbuf.data = 9;
4830
4831 /* we have in <fsize> the exact number of bytes we need to copy from
4832 * the HTX buffer. We need to check this against the connection's and
4833 * the stream's send windows, and to ensure that this fits in the max
4834 * frame size and in the buffer's available space minus 9 bytes (for
4835 * the frame header). The connection's flow control is applied last so
4836 * that we can use a separate list of streams which are immediately
4837 * unblocked on window opening. Note: we don't implement padding.
4838 */
4839
4840 /* EOM is presented with bsize==1 but would lead to the emission of an
4841 * empty frame, thus we force it to zero here.
4842 */
4843 if (type == HTX_BLK_EOM)
4844 bsize = fsize = 0;
4845
4846 if (!fsize)
4847 goto send_empty;
4848
4849 if (h2s->mws <= 0) {
4850 h2s->flags |= H2_SF_BLK_SFCTL;
4851 if (h2s->send_wait) {
4852 LIST_DEL(&h2s->list);
4853 LIST_INIT(&h2s->list);
4854 }
4855 goto end;
4856 }
4857
Willy Tarreauee573762018-12-04 15:25:57 +01004858 if (fsize > count)
4859 fsize = count;
4860
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004861 if (fsize > h2s->mws)
4862 fsize = h2s->mws; // >0
4863
4864 if (h2c->mfs && fsize > h2c->mfs)
4865 fsize = h2c->mfs; // >0
4866
4867 if (fsize + 9 > outbuf.size) {
4868 /* we have an opportunity for enlarging the too small
4869 * available space, let's try.
4870 * FIXME: is this really interesting to do? Maybe we'll
4871 * spend lots of time realigning instead of using two
4872 * frames.
4873 */
4874 if (b_space_wraps(&h2c->mbuf))
4875 goto realign_again;
4876 fsize = outbuf.size - 9;
4877
4878 if (fsize <= 0) {
4879 /* no need to send an empty frame here */
4880 h2c->flags |= H2_CF_MUX_MFULL;
4881 h2s->flags |= H2_SF_BLK_MROOM;
4882 goto end;
4883 }
4884 }
4885
4886 if (h2c->mws <= 0) {
4887 h2s->flags |= H2_SF_BLK_MFCTL;
4888 goto end;
4889 }
4890
4891 if (fsize > h2c->mws)
4892 fsize = h2c->mws;
4893
4894 /* now let's copy this this into the output buffer */
4895 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004896 h2s->mws -= fsize;
4897 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004898 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004899
4900 send_empty:
4901 /* update the frame's size */
4902 h2_set_frame_size(outbuf.area, fsize);
4903
4904 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4905 * meeting EOM. We should optimize this later.
4906 */
4907 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004908 total++; // EOM counts as one byte
4909 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004910 es_now = 1;
4911 }
4912
4913 if (es_now)
4914 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4915
4916 /* commit the H2 response */
4917 b_add(&h2c->mbuf, fsize + 9);
4918
4919 /* consume incoming HTX block, including EOM */
4920 total += fsize;
4921 if (fsize == bsize) {
4922 htx_remove_blk(htx, blk);
4923 if (fsize)
4924 goto new_frame;
4925 } else {
4926 /* we've truncated this block */
4927 htx_cut_data_blk(htx, blk, fsize);
4928 }
4929
4930 if (es_now) {
4931 if (h2s->st == H2_SS_OPEN)
4932 h2s->st = H2_SS_HLOC;
4933 else
4934 h2s_close(h2s);
4935
4936 h2s->flags |= H2_SF_ES_SENT;
4937 }
4938
4939 end:
4940 return total;
4941}
4942
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004943/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4944 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4945 * processed. The caller must check the stream's status to detect any error
4946 * which might have happened subsequently to a successful send. The htx blocks
4947 * are automatically removed from the message. The htx message is assumed to be
4948 * valid since produced from the internal code. Processing stops when meeting
4949 * the EOM, which is also removed. All trailers are processed at once and sent
4950 * as a single frame. The ES flag is always set.
4951 */
4952static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4953{
4954 struct http_hdr list[MAX_HTTP_HDR];
4955 struct h2c *h2c = h2s->h2c;
4956 struct htx_blk *blk;
4957 struct htx_blk *blk_end;
4958 struct buffer outbuf;
4959 struct h1m h1m;
4960 enum htx_blk_type type;
4961 uint32_t size;
4962 int ret = 0;
4963 int hdr;
4964 int idx;
4965 void *start;
4966
4967 if (h2c_mux_busy(h2c, h2s)) {
4968 h2s->flags |= H2_SF_BLK_MBUSY;
4969 goto end;
4970 }
4971
4972 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4973 h2c->flags |= H2_CF_MUX_MALLOC;
4974 h2s->flags |= H2_SF_BLK_MROOM;
4975 goto end;
4976 }
4977
4978 /* The principle is that we parse each and every trailers block using
4979 * the H1 headers parser, and append it to the list. We don't proceed
4980 * until EOM is met. blk_end will point to the EOM block.
4981 */
4982 hdr = 0;
4983 memset(list, 0, sizeof(list));
4984 blk_end = NULL;
4985
4986 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4987 blk = htx_get_blk(htx, idx);
4988 type = htx_get_blk_type(blk);
4989
4990 if (type == HTX_BLK_UNUSED)
4991 continue;
4992
4993 if (type != HTX_BLK_TLR) {
4994 if (type == HTX_BLK_EOM)
4995 blk_end = blk;
4996 break;
4997 }
4998
4999 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5000 goto fail;
5001
5002 size = htx_get_blksz(blk);
5003 start = htx_get_blk_ptr(htx, blk);
5004
5005 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5006 h1m.err_pos = 0;
5007 ret = h1_headers_to_hdr_list(start, start + size,
5008 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5009 &h1m, NULL);
5010 if (ret < 0)
5011 goto fail;
5012
5013 /* ret == 0 if an incomplete trailers block was found (missing
5014 * empty line), or > 0 if it was found. We have to continue on
5015 * incomplete messages because the trailers block might be
5016 * incomplete.
5017 */
5018
5019 /* search the new end */
5020 while (hdr <= sizeof(list)/sizeof(list[0])) {
5021 if (!list[hdr].n.len)
5022 break;
5023 hdr++;
5024 }
5025 }
5026
5027 if (!blk_end)
5028 goto end; // end not found yet
5029
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005030 chunk_reset(&outbuf);
5031
5032 while (1) {
5033 outbuf.area = b_tail(&h2c->mbuf);
5034 outbuf.size = b_contig_space(&h2c->mbuf);
5035 outbuf.data = 0;
5036
5037 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5038 break;
5039 realign_again:
5040 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5041 }
5042
5043 if (outbuf.size < 9)
5044 goto full;
5045
5046 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5047 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5048 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5049 outbuf.data = 9;
5050
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005051 /* encode all headers */
5052 for (idx = 0; idx < hdr; idx++) {
5053 /* these ones do not exist in H2 or must not appear in
5054 * trailers and must be dropped.
5055 */
5056 if (isteq(list[idx].n, ist("host")) ||
5057 isteq(list[idx].n, ist("content-length")) ||
5058 isteq(list[idx].n, ist("connection")) ||
5059 isteq(list[idx].n, ist("proxy-connection")) ||
5060 isteq(list[idx].n, ist("keep-alive")) ||
5061 isteq(list[idx].n, ist("upgrade")) ||
5062 isteq(list[idx].n, ist("te")) ||
5063 isteq(list[idx].n, ist("transfer-encoding")))
5064 continue;
5065
5066 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5067 /* output full */
5068 if (b_space_wraps(&h2c->mbuf))
5069 goto realign_again;
5070 goto full;
5071 }
5072 }
5073
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005074 if (!hdr) {
5075 /* here we have a problem, we've received an empty trailers
5076 * block followed by an EOM. Because of this we can't send a
5077 * HEADERS frame, so we have to cheat and instead send an empty
5078 * DATA frame conveying the ES flag.
5079 */
5080 outbuf.area[3] = H2_FT_DATA;
5081 outbuf.area[4] = H2_F_DATA_END_STREAM;
5082 }
5083
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005084 /* update the frame's size */
5085 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5086
5087 /* commit the H2 response */
5088 b_add(&h2c->mbuf, outbuf.data);
5089 h2s->flags |= H2_SF_ES_SENT;
5090
5091 if (h2s->st == H2_SS_OPEN)
5092 h2s->st = H2_SS_HLOC;
5093 else
5094 h2s_close(h2s);
5095
5096 /* OK we could properly deliver the response */
5097 done:
5098 /* remove all header blocks including EOM and compute the corresponding size. */
5099 ret = 0;
5100 idx = htx_get_head(htx);
5101 blk = htx_get_blk(htx, idx);
5102 while (blk != blk_end) {
5103 ret += htx_get_blksz(blk);
5104 blk = htx_remove_blk(htx, blk);
5105 }
5106 blk = htx_remove_blk(htx, blk);
5107 end:
5108 return ret;
5109 full:
5110 h2c->flags |= H2_CF_MUX_MFULL;
5111 h2s->flags |= H2_SF_BLK_MROOM;
5112 ret = 0;
5113 goto end;
5114 fail:
5115 /* unparsable HTX messages, too large ones to be produced in the local
5116 * list etc go here (unrecoverable errors).
5117 */
5118 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5119 ret = 0;
5120 goto end;
5121}
5122
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005123/* Called from the upper layer, to subscribe to events, such as being able to send.
5124 * The <param> argument here is supposed to be a pointer to a wait_event struct
5125 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5126 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5127 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5128 * returns 0 except for the error above.
5129 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005130static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5131{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005132 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005133 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005134 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005135
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005136 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005137 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005138 if (!(sw->events & SUB_RETRY_RECV)) {
5139 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005140 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005141 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005142 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005143 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005144 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005145 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005146 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005147 if (!(sw->events & SUB_RETRY_SEND)) {
5148 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005149 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005150 h2s->send_wait = sw;
5151 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5152 if (h2s->flags & H2_SF_BLK_MFCTL)
5153 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5154 else
5155 LIST_ADDQ(&h2c->send_list, &h2s->list);
5156 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005157 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005158 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005159 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005160 if (event_type != 0)
5161 return -1;
5162 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005163}
5164
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005165/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5166 * The <param> argument here is supposed to be a pointer to the same wait_event
5167 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5168 * It always returns zero.
5169 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005170static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5171{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005172 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005173 struct h2s *h2s = cs->ctx;
5174
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005175 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005176 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005177 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005178 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005179 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005180 }
5181 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005182 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005183 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005184 if (h2s->send_wait == sw) {
5185 LIST_DEL(&h2s->list);
5186 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005187 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005188 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005189 }
5190 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005191 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5192 sw = param;
5193 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005194 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Willy Tarreaue73256f2019-03-25 18:02:54 +01005195 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005196 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005197 LIST_DEL(&h2s->list);
5198 LIST_INIT(&h2s->list);
Olivier Houchard3ea35132019-03-25 14:10:42 +01005199 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005200 }
5201 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005202 return 0;
5203}
5204
5205
Olivier Houchard511efea2018-08-16 15:30:32 +02005206/* Called from the upper layer, to receive data */
5207static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5208{
Olivier Houchard638b7992018-08-16 15:41:52 +02005209 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005210 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005211 struct htx *h2s_htx = NULL;
5212 struct htx *buf_htx = NULL;
5213 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005214 size_t ret = 0;
5215
5216 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005217 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5218 /* in HTX mode we ignore the count argument */
5219 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005220 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005221 /* Here htx_to_buf() will set buffer data to 0 because
5222 * the HTX is empty.
5223 */
5224 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005225 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005226 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005227
5228 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005229 count = htx_free_data_space(buf_htx);
5230 if (flags & CO_RFL_KEEP_RSV) {
5231 if (count <= global.tune.maxrewrite)
5232 goto end;
5233 count -= global.tune.maxrewrite;
5234 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005235
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005236 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005237
5238 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5239 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5240
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005241 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005242 htx_to_buf(buf_htx, buf);
5243 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005244 ret = htx_ret.ret;
5245 }
5246 else {
5247 ret = b_xfer(buf, &h2s->rxbuf, count);
5248 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005249
Christopher Faulet37070b22019-02-14 15:12:14 +01005250 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005251 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005252 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005253 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005254 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005255 if (cs->flags & CS_FL_REOS)
5256 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005257 if (cs->flags & CS_FL_ERR_PENDING)
5258 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005259 if (b_size(&h2s->rxbuf)) {
5260 b_free(&h2s->rxbuf);
5261 offer_buffers(NULL, tasks_run_queue);
5262 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005263 }
5264
Willy Tarreau082f5592018-11-25 08:03:32 +01005265 if (ret && h2c->dsi == h2s->id) {
5266 /* demux is blocking on this stream's buffer */
5267 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005268 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005269 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005270
Olivier Houchard511efea2018-08-16 15:30:32 +02005271 return ret;
5272}
5273
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005274/* stops all senders of this connection for example when the mux buffer is full.
5275 * They are moved from the sending_list to either fctl_list or send_list.
5276 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005277static void h2_stop_senders(struct h2c *h2c)
5278{
5279 struct h2s *h2s, *h2s_back;
5280
Olivier Houchardd360ac62019-03-22 17:37:16 +01005281 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5282 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005283 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005284 h2s->send_wait->events |= SUB_RETRY_SEND;
5285 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005286 }
5287}
5288
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005289/* Called from the upper layer, to send data from buffer <buf> for no more than
5290 * <count> bytes. Returns the number of bytes effectively sent. Some status
5291 * flags may be updated on the conn_stream.
5292 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005293static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005294{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005295 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005296 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005297 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005298 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005299 struct htx *htx;
5300 struct htx_blk *blk;
5301 enum htx_blk_type btype;
5302 uint32_t bsize;
5303 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005304
Olivier Houchardd360ac62019-03-22 17:37:16 +01005305 /* If we were not just woken because we wanted to send but couldn't,
5306 * and there's somebody else that is waiting to send, do nothing,
5307 * we will subscribe later and be put at the end of the list
5308 */
5309 LIST_DEL_INIT(&h2s->sending_list);
5310 if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) &&
5311 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5312 return 0;
5313
Olivier Houchardd846c262018-10-19 17:24:29 +02005314 if (h2s->send_wait) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005315 /* We want to stay in the send_list, so prepare ourself to be
5316 * eventually recalled if needed, and only remove ourself from
5317 * the list if we managed to send anything.
5318 */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005319 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01005320 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005321 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005322 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5323 return 0;
5324
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005325 /* htx will be enough to decide if we're using HTX or legacy */
5326 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5327
Willy Tarreau0bad0432018-06-14 16:54:01 +02005328 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005329 h2s->flags |= H2_SF_OUTGOING_DATA;
5330
Willy Tarreau751f2d02018-10-05 09:35:00 +02005331 if (h2s->id == 0) {
5332 int32_t id = h2c_get_next_sid(h2s->h2c);
5333
5334 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005335 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005336 return 0;
5337 }
5338
5339 eb32_delete(&h2s->by_id);
5340 h2s->by_id.key = h2s->id = id;
5341 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005342 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005343 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5344 }
5345
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005346 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005347 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5348 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005349 idx = htx_get_head(htx);
5350 blk = htx_get_blk(htx, idx);
5351 btype = htx_get_blk_type(blk);
5352 bsize = htx_get_blksz(blk);
5353
5354 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005355 case HTX_BLK_REQ_SL:
5356 /* start-line before headers */
5357 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5358 if (ret > 0) {
5359 total += ret;
5360 count -= ret;
5361 if (ret < bsize)
5362 goto done;
5363 }
5364 break;
5365
Willy Tarreau115e83b2018-12-01 19:17:53 +01005366 case HTX_BLK_RES_SL:
5367 /* start-line before headers */
5368 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5369 if (ret > 0) {
5370 total += ret;
5371 count -= ret;
5372 if (ret < bsize)
5373 goto done;
5374 }
5375 break;
5376
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005377 case HTX_BLK_DATA:
5378 case HTX_BLK_EOD:
5379 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005380 /* all these cause the emission of a DATA frame (possibly empty).
5381 * This EOM necessarily is one before trailers, as the EOM following
5382 * trailers would have been consumed by the trailers parser.
5383 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005384 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005385 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005386 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005387 total += ret;
5388 count -= ret;
5389 if (ret < bsize)
5390 goto done;
5391 }
5392 break;
5393
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005394 case HTX_BLK_TLR:
5395 /* This is the first trailers block, all the subsequent ones AND
5396 * the EOM will be swallowed by the parser.
5397 */
5398 ret = h2s_htx_make_trailers(h2s, htx);
5399 if (ret > 0) {
5400 total += ret;
5401 count -= ret;
5402 if (ret < bsize)
5403 goto done;
5404 }
5405 break;
5406
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005407 default:
5408 htx_remove_blk(htx, blk);
5409 total += bsize;
5410 count -= bsize;
5411 break;
5412 }
5413 }
5414 goto done;
5415 }
5416
5417 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005418 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005419 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005420 if (h2s->h2c->flags & H2_CF_IS_BACK)
5421 ret = -1;
5422 else
5423 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005424 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005425 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005426 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005427 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005428 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005429 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005430 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005431
Willy Tarreau5dd17352018-06-14 13:33:30 +02005432 if (unlikely((int)ret <= 0)) {
5433 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005434 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5435 break;
5436 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005437 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005438 total += count;
5439 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005440 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005441 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005442 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005443 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005444 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005445 break;
5446 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005447
5448 total += ret;
5449 count -= ret;
5450
5451 if (h2s->st >= H2_SS_ERROR)
5452 break;
5453
5454 if (h2s->flags & H2_SF_BLK_ANY)
5455 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005456 }
5457
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005458 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005459 if (h2s->st >= H2_SS_ERROR) {
5460 /* trim any possibly pending data after we close (extra CR-LF,
5461 * unprocessed trailers, abnormal extra data, ...)
5462 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005463 total += count;
5464 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005465 }
5466
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005467 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005468 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005469 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005470 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005471 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005472 }
5473
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005474 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005475 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005476 } else {
5477 b_del(buf, total);
5478 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005479
5480 /* The mux is full, cancel the pending tasks */
5481 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5482 (h2s->flags & H2_SF_BLK_MBUSY))
5483 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005484
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005485 /* If we're running HTX, and we read the whole buffer, then pretend
5486 * we read exactly what the caller specified, as with HTX the caller
5487 * will always give the buffer size, instead of the amount of data
5488 * available.
5489 */
5490 if (htx && !b_data(buf))
5491 total = orig_count;
5492
Olivier Houchard7505f942018-08-21 18:10:44 +02005493 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005494 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005495 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005496
Olivier Houchard7505f942018-08-21 18:10:44 +02005497 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005498 /* If we're waiting for flow control, and we got a shutr on the
5499 * connection, we will never be unlocked, so add an error on
5500 * the conn_stream.
5501 */
5502 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5503 !b_data(&h2s->h2c->dbuf) &&
5504 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5505 if (cs->flags & CS_FL_EOS)
5506 cs->flags |= CS_FL_ERROR;
5507 else
5508 cs->flags |= CS_FL_ERR_PENDING;
5509 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01005510 if (total > 0 && h2s->send_wait) {
5511 /* Ok we managed to send something, leave the send_list */
5512 h2s->send_wait->events &= ~SUB_RETRY_SEND;
5513 h2s->send_wait = NULL;
5514 LIST_DEL_INIT(&h2s->list);
5515 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005516 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005517}
5518
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005519/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005520static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005521{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005522 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005523 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005524 struct eb32_node *node;
5525 int fctl_cnt = 0;
5526 int send_cnt = 0;
5527 int tree_cnt = 0;
5528 int orph_cnt = 0;
5529
5530 if (!h2c)
5531 return;
5532
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005533 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005534 fctl_cnt++;
5535
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005536 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005537 send_cnt++;
5538
Willy Tarreau3af37712018-12-18 14:34:41 +01005539 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005540 node = eb32_first(&h2c->streams_by_id);
5541 while (node) {
5542 h2s = container_of(node, struct h2s, by_id);
5543 tree_cnt++;
5544 if (!h2s->cs)
5545 orph_cnt++;
5546 node = eb32_next(node);
5547 }
5548
Willy Tarreau987c0632018-12-18 10:32:05 +01005549 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5550 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5551 " .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 +02005552 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5553 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005554 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005555 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5556 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5557 h2c->msi,
5558 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5559 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5560
5561 if (h2s) {
5562 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5563 h2s, h2s->id, h2s->flags,
5564 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5565 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5566 h2s->cs);
5567 if (h2s->cs)
5568 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5569 h2s->cs->flags, h2s->cs->data);
5570 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005571}
Willy Tarreau62f52692017-10-08 23:01:42 +02005572
5573/*******************************************************/
5574/* functions below are dedicated to the config parsers */
5575/*******************************************************/
5576
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005577/* config parser for global "tune.h2.header-table-size" */
5578static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5579 struct proxy *defpx, const char *file, int line,
5580 char **err)
5581{
5582 if (too_many_args(1, args, err, NULL))
5583 return -1;
5584
5585 h2_settings_header_table_size = atoi(args[1]);
5586 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5587 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5588 return -1;
5589 }
5590 return 0;
5591}
Willy Tarreau62f52692017-10-08 23:01:42 +02005592
Willy Tarreaue6baec02017-07-27 11:45:11 +02005593/* config parser for global "tune.h2.initial-window-size" */
5594static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5595 struct proxy *defpx, const char *file, int line,
5596 char **err)
5597{
5598 if (too_many_args(1, args, err, NULL))
5599 return -1;
5600
5601 h2_settings_initial_window_size = atoi(args[1]);
5602 if (h2_settings_initial_window_size < 0) {
5603 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5604 return -1;
5605 }
5606 return 0;
5607}
5608
Willy Tarreau5242ef82017-07-27 11:47:28 +02005609/* config parser for global "tune.h2.max-concurrent-streams" */
5610static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5611 struct proxy *defpx, const char *file, int line,
5612 char **err)
5613{
5614 if (too_many_args(1, args, err, NULL))
5615 return -1;
5616
5617 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005618 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005619 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5620 return -1;
5621 }
5622 return 0;
5623}
5624
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005625/* config parser for global "tune.h2.max-frame-size" */
5626static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5627 struct proxy *defpx, const char *file, int line,
5628 char **err)
5629{
5630 if (too_many_args(1, args, err, NULL))
5631 return -1;
5632
5633 h2_settings_max_frame_size = atoi(args[1]);
5634 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5635 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5636 return -1;
5637 }
5638 return 0;
5639}
5640
Willy Tarreau62f52692017-10-08 23:01:42 +02005641
5642/****************************************/
5643/* MUX initialization and instanciation */
5644/***************************************/
5645
5646/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005647static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005648 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005649 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005650 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005651 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005652 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005653 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005654 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005655 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005656 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005657 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005658 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005659 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005660 .shutr = h2_shutr,
5661 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005662 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005663 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005664 .name = "H2",
5665};
5666
Christopher Faulet32f61c02018-04-10 14:33:41 +02005667/* PROTO selection : this mux registers PROTO token "h2" */
5668static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005669 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005670
Willy Tarreau0108d902018-11-25 19:14:37 +01005671INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5672
Christopher Faulet9b579102019-04-11 22:52:25 +02005673
5674/* The mux operations */
5675static const struct mux_ops h2_htx_ops = {
5676 .init = h2_init,
5677 .wake = h2_wake,
5678 .snd_buf = h2_snd_buf,
5679 .rcv_buf = h2_rcv_buf,
5680 .subscribe = h2_subscribe,
5681 .unsubscribe = h2_unsubscribe,
5682 .attach = h2_attach,
5683 .get_first_cs = h2_get_first_cs,
5684 .detach = h2_detach,
5685 .destroy = h2_destroy,
5686 .avail_streams = h2_avail_streams,
5687 .used_streams = h2_used_streams,
5688 .shutr = h2_shutr,
5689 .shutw = h2_shutw,
5690 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005691 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005692 .name = "H2",
5693};
5694
Willy Tarreauf8957272018-10-03 10:25:20 +02005695static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005696 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005697
5698INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5699
Willy Tarreau62f52692017-10-08 23:01:42 +02005700/* config keyword parsers */
5701static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005702 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005703 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005704 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005705 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005706 { 0, NULL, NULL }
5707}};
5708
Willy Tarreau0108d902018-11-25 19:14:37 +01005709INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);