blob: a6d89b92d48790c2b26036f43a443476465feda1 [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)
Olivier Houchard3f795f72019-04-17 22:51:06 +0200579 task_destroy(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200580 if (h2c->wait_event.task)
581 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100582 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200583 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200584 return -1;
585}
586
Willy Tarreau751f2d02018-10-05 09:35:00 +0200587/* returns the next allocatable outgoing stream ID for the H2 connection, or
588 * -1 if no more is allocatable.
589 */
590static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
591{
592 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100593
594 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200595 id = -1;
596 return id;
597}
598
Willy Tarreau2373acc2017-10-12 17:35:14 +0200599/* returns the stream associated with id <id> or NULL if not found */
600static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
601{
602 struct eb32_node *node;
603
Willy Tarreau751f2d02018-10-05 09:35:00 +0200604 if (id == 0)
605 return (struct h2s *)h2_closed_stream;
606
Willy Tarreau2a856182017-05-16 15:20:39 +0200607 if (id > h2c->max_id)
608 return (struct h2s *)h2_idle_stream;
609
Willy Tarreau2373acc2017-10-12 17:35:14 +0200610 node = eb32_lookup(&h2c->streams_by_id, id);
611 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200612 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200613
614 return container_of(node, struct h2s, by_id);
615}
616
Christopher Faulet73c12072019-04-08 11:23:22 +0200617/* release function. This one should be called to free all resources allocated
618 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200619 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200620static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200621{
Christopher Faulet61840e72019-04-15 09:33:32 +0200622 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200623
Willy Tarreau32218eb2017-09-22 08:07:25 +0200624 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200625 /* The connection must be aattached to this mux to be released */
626 if (h2c->conn && h2c->conn->ctx == h2c)
627 conn = h2c->conn;
628
Willy Tarreau32218eb2017-09-22 08:07:25 +0200629 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200630
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100631 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100632 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100633 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200634
Willy Tarreau44e973f2018-03-01 17:49:30 +0100635 h2_release_buf(h2c, &h2c->dbuf);
636 h2_release_buf(h2c, &h2c->mbuf);
637
Willy Tarreauea392822017-10-31 10:02:25 +0100638 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200639 h2c->task->context = NULL;
640 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100641 h2c->task = NULL;
642 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200643 if (h2c->wait_event.task)
644 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200645 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100646 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200647 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100648
Willy Tarreaubafbe012017-11-24 17:34:44 +0100649 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200650 }
651
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200652 if (conn) {
653 conn->mux = NULL;
654 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200655
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200656 conn_stop_tracking(conn);
657 conn_full_close(conn);
658 if (conn->destroy_cb)
659 conn->destroy_cb(conn);
660 conn_free(conn);
661 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200662}
663
664
Willy Tarreau71681172017-10-23 14:39:06 +0200665/******************************************************/
666/* functions below are for the H2 protocol processing */
667/******************************************************/
668
669/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100670static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200671{
672 return h2s ? h2s->id : 0;
673}
674
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200675/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100676static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200677{
678 if (h2c->msi < 0)
679 return 0;
680
681 if (h2c->msi == h2s_id(h2s))
682 return 0;
683
684 return 1;
685}
686
Willy Tarreau741d6df2017-10-17 08:00:59 +0200687/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100688static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200689{
690 h2c->errcode = err;
691 h2c->st0 = H2_CS_ERROR;
692}
693
Willy Tarreau175cebb2019-01-24 10:02:24 +0100694/* marks an error on the stream. It may also update an already closed stream
695 * (e.g. to report an error after an RST was received).
696 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100697static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200698{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100699 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200700 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100701 if (h2s->st < H2_SS_ERROR)
702 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100703 if (h2s->cs)
704 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200705 }
706}
707
Willy Tarreau7e094452018-12-19 18:08:52 +0100708/* attempt to notify the data layer of recv availability */
709static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
710{
711 struct wait_event *sw;
712
713 if (h2s->recv_wait) {
714 sw = h2s->recv_wait;
715 sw->events &= ~SUB_RETRY_RECV;
716 tasklet_wakeup(sw->task);
717 h2s->recv_wait = NULL;
718 }
719}
720
721/* attempt to notify the data layer of send availability */
722static void __maybe_unused h2s_notify_send(struct h2s *h2s)
723{
724 struct wait_event *sw;
725
Olivier Houchard998410a2019-04-15 19:23:37 +0200726 if (h2s->send_wait && LIST_ISEMPTY(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100727 sw = h2s->send_wait;
728 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100729 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100730 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100731 }
732}
733
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100734/* alerts the data layer, trying to wake it up by all means, following
735 * this sequence :
736 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
737 * - if its subscribed to send, then it's woken up for send
738 * - if it was subscribed to neither, its ->wake() callback is called
739 * It is safe to call this function with a closed stream which doesn't have a
740 * conn_stream anymore.
741 */
742static void __maybe_unused h2s_alert(struct h2s *h2s)
743{
744 if (h2s->recv_wait || h2s->send_wait) {
745 h2s_notify_recv(h2s);
746 h2s_notify_send(h2s);
747 }
748 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
749 h2s->cs->data_cb->wake(h2s->cs);
750}
751
Willy Tarreaue4820742017-07-27 13:37:23 +0200752/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100753static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200754{
755 uint8_t *out = frame;
756
757 *out = len >> 16;
758 write_n16(out + 1, len);
759}
760
Willy Tarreau54c15062017-10-10 17:10:03 +0200761/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
762 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
763 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200764 * available in the buffer's input prior to calling this function. The buffer
765 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200766 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100767static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200768 const struct buffer *b, int o)
769{
Willy Tarreau591d4452018-06-15 17:21:00 +0200770 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200771}
772
Willy Tarreau1f094672017-11-20 21:27:45 +0100773static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200774{
Willy Tarreau591d4452018-06-15 17:21:00 +0200775 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200776}
777
Willy Tarreau1f094672017-11-20 21:27:45 +0100778static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200779{
Willy Tarreau591d4452018-06-15 17:21:00 +0200780 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200781}
782
Willy Tarreau1f094672017-11-20 21:27:45 +0100783static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200784{
Willy Tarreau591d4452018-06-15 17:21:00 +0200785 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200786}
787
788
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100789/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
790 * The algorithm is not obvious. It turns out that H2 headers are neither
791 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
792 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200793 *
794 * b0 b1 b2 b3 b4 b5..b8
795 * +----------+---------+--------+----+----+----------------------+
796 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
797 * +----------+---------+--------+----+----+----------------------+
798 *
799 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
800 * we get the sid properly aligned and ordered, and 16 bits of len properly
801 * ordered as well. The type and flags can be extracted using bit shifts from
802 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200803 * Returns zero if some bytes are missing, otherwise non-zero on success. The
804 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200805 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100806static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200807{
808 uint64_t w;
809
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100810 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200811 return 0;
812
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100813 w = h2_get_n64(b, o + 1);
814 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200815 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
816 h->ff = w >> 32;
817 h->ft = w >> 40;
818 h->len += w >> 48;
819 return 1;
820}
821
822/* skip the next 9 bytes corresponding to the frame header possibly parsed by
823 * h2_peek_frame_hdr() above.
824 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100825static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200826{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200827 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200828}
829
830/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100831static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200832{
833 int ret;
834
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100835 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200836 if (ret > 0)
837 h2_skip_frame_hdr(b);
838 return ret;
839}
840
Willy Tarreau00dd0782018-03-01 16:31:34 +0100841/* marks stream <h2s> as CLOSED and decrement the number of active streams for
842 * its connection if the stream was not yet closed. Please use this exclusively
843 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100844 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100845static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100846{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100847 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100848 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100849 if (!h2s->id)
850 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100851 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100852 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100853 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
854 h2s_notify_recv(h2s);
855 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100856 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100857 h2s->st = H2_SS_CLOSED;
858}
859
Willy Tarreau71049cc2018-03-28 13:56:39 +0200860/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
861static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100862{
863 h2s_close(h2s);
864 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200865 if (b_size(&h2s->rxbuf)) {
866 b_free(&h2s->rxbuf);
867 offer_buffers(NULL, tasks_run_queue);
868 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200869 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100870 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200871 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100872 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800873 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200874 * reference left would be in the h2c send_list/fctl_list, and if
875 * we're in it, we're getting out anyway
876 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100877 LIST_DEL_INIT(&h2s->list);
Olivier Houchard998410a2019-04-15 19:23:37 +0200878 if (!LIST_ISEMPTY(&h2s->sending_list)) {
879 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
880 LIST_DEL_INIT(&h2s->sending_list);
881 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200882 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100883 pool_free(pool_head_h2s, h2s);
884}
885
Willy Tarreaua8e49542018-10-03 18:53:55 +0200886/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
887 * stream tree. In case of error, nothing is added and NULL is returned. The
888 * causes of errors can be any failed memory allocation. The caller is
889 * responsible for checking if the connection may support an extra stream
890 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200891 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200892static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200893{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200894 struct h2s *h2s;
895
Willy Tarreaubafbe012017-11-24 17:34:44 +0100896 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200897 if (!h2s)
898 goto out;
899
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200900 h2s->wait_event.task = tasklet_new();
901 if (!h2s->wait_event.task) {
902 pool_free(pool_head_h2s, h2s);
903 goto out;
904 }
905 h2s->send_wait = NULL;
906 h2s->recv_wait = NULL;
907 h2s->wait_event.task->process = h2_deferred_shut;
908 h2s->wait_event.task->context = h2s;
909 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100910 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200911 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100912 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200913 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200914 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200915 h2s->mws = h2c->miw;
916 h2s->flags = H2_SF_NONE;
917 h2s->errcode = H2_ERR_NO_ERROR;
918 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200919 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100920 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200921 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200922
923 if (h2c->flags & H2_CF_IS_BACK) {
924 h1m_init_req(&h2s->h1m);
925 h2s->h1m.err_pos = -1; // don't care about errors on the request path
926 h2s->h1m.flags |= H1_MF_TOLOWER;
927 } else {
928 h1m_init_res(&h2s->h1m);
929 h2s->h1m.err_pos = -1; // don't care about errors on the response path
930 h2s->h1m.flags |= H1_MF_TOLOWER;
931 }
932
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200933 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200934 if (id > 0)
935 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100936 else
937 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200938
939 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100940 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100941 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200942
943 return h2s;
944
945 out_free_h2s:
946 pool_free(pool_head_h2s, h2s);
947 out:
948 return NULL;
949}
950
951/* creates a new stream <id> on the h2c connection and returns it, or NULL in
952 * case of memory allocation error.
953 */
954static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
955{
956 struct session *sess = h2c->conn->owner;
957 struct conn_stream *cs;
958 struct h2s *h2s;
959
960 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
961 goto out;
962
963 h2s = h2s_new(h2c, id);
964 if (!h2s)
965 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200966
967 cs = cs_new(h2c->conn);
968 if (!cs)
969 goto out_close;
970
Olivier Houchard746fb772018-12-15 19:42:00 +0100971 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200972 h2s->cs = cs;
973 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200974 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200975
976 if (stream_create_from_cs(cs) < 0)
977 goto out_free_cs;
978
Willy Tarreau590a0512018-09-05 11:56:48 +0200979 /* We want the accept date presented to the next stream to be the one
980 * we have now, the handshake time to be null (since the next stream
981 * is not delayed by a handshake), and the idle time to count since
982 * right now.
983 */
984 sess->accept_date = date;
985 sess->tv_accept = now;
986 sess->t_handshake = 0;
987
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200988 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100989 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200990 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200991 return h2s;
992
993 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200994 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200995 cs_free(cs);
996 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200997 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200998 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200999 sess_log(sess);
1000 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001001}
1002
Willy Tarreau751f2d02018-10-05 09:35:00 +02001003/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1004 * and returns it, or NULL in case of memory allocation error or if the highest
1005 * possible stream ID was reached.
1006 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001007static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001008{
1009 struct h2s *h2s = NULL;
1010
Willy Tarreau86949782019-01-31 10:42:05 +01001011 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001012 goto out;
1013
Willy Tarreaua80dca82019-01-24 17:08:28 +01001014 if (h2_streams_left(h2c) < 1)
1015 goto out;
1016
Willy Tarreau751f2d02018-10-05 09:35:00 +02001017 /* Defer choosing the ID until we send the first message to create the stream */
1018 h2s = h2s_new(h2c, 0);
1019 if (!h2s)
1020 goto out;
1021
1022 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001023 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001024 cs->ctx = h2s;
1025 h2c->nb_cs++;
1026
Willy Tarreau751f2d02018-10-05 09:35:00 +02001027 out:
1028 return h2s;
1029}
1030
Willy Tarreaube5b7152017-09-25 16:25:39 +02001031/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1032 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1033 * the various settings codes.
1034 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001035static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001036{
1037 struct buffer *res;
1038 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001039 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001040 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001041 int ret;
1042
1043 if (h2c_mux_busy(h2c, NULL)) {
1044 h2c->flags |= H2_CF_DEM_MBUSY;
1045 return 0;
1046 }
1047
Willy Tarreau44e973f2018-03-01 17:49:30 +01001048 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001049 if (!res) {
1050 h2c->flags |= H2_CF_MUX_MALLOC;
1051 h2c->flags |= H2_CF_DEM_MROOM;
1052 return 0;
1053 }
1054
1055 chunk_init(&buf, buf_data, sizeof(buf_data));
1056 chunk_memcpy(&buf,
1057 "\x00\x00\x00" /* length : 0 for now */
1058 "\x04\x00" /* type : 4 (settings), flags : 0 */
1059 "\x00\x00\x00\x00", /* stream ID : 0 */
1060 9);
1061
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001062 if (h2c->flags & H2_CF_IS_BACK) {
1063 /* send settings_enable_push=0 */
1064 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1065 }
1066
Willy Tarreaube5b7152017-09-25 16:25:39 +02001067 if (h2_settings_header_table_size != 4096) {
1068 char str[6] = "\x00\x01"; /* header_table_size */
1069
1070 write_n32(str + 2, h2_settings_header_table_size);
1071 chunk_memcat(&buf, str, 6);
1072 }
1073
1074 if (h2_settings_initial_window_size != 65535) {
1075 char str[6] = "\x00\x04"; /* initial_window_size */
1076
1077 write_n32(str + 2, h2_settings_initial_window_size);
1078 chunk_memcat(&buf, str, 6);
1079 }
1080
1081 if (h2_settings_max_concurrent_streams != 0) {
1082 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1083
1084 /* Note: 0 means "unlimited" for haproxy's config but not for
1085 * the protocol, so never send this value!
1086 */
1087 write_n32(str + 2, h2_settings_max_concurrent_streams);
1088 chunk_memcat(&buf, str, 6);
1089 }
1090
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001091 mfs = h2_settings_max_frame_size;
1092 if (mfs > global.tune.bufsize)
1093 mfs = global.tune.bufsize;
1094
1095 if (!mfs)
1096 mfs = global.tune.bufsize;
1097
1098 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001099 char str[6] = "\x00\x05"; /* max_frame_size */
1100
1101 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1102 * match bufsize - rewrite size, but at the moment it seems
1103 * that clients don't take care of it.
1104 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001105 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001106 chunk_memcat(&buf, str, 6);
1107 }
1108
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001109 h2_set_frame_size(buf.area, buf.data - 9);
1110 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001111 if (unlikely(ret <= 0)) {
1112 if (!ret) {
1113 h2c->flags |= H2_CF_MUX_MFULL;
1114 h2c->flags |= H2_CF_DEM_MROOM;
1115 return 0;
1116 }
1117 else {
1118 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1119 return 0;
1120 }
1121 }
1122 return ret;
1123}
1124
Willy Tarreau52eed752017-09-22 15:05:09 +02001125/* Try to receive a connection preface, then upon success try to send our
1126 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1127 * missing data. It may return an error in h2c.
1128 */
1129static int h2c_frt_recv_preface(struct h2c *h2c)
1130{
1131 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001132 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001133
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001134 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001135
1136 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001137 if (ret1 < 0)
1138 sess_log(h2c->conn->owner);
1139
Willy Tarreau52eed752017-09-22 15:05:09 +02001140 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1141 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1142 return 0;
1143 }
1144
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001145 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001146 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001147 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001148
Willy Tarreaube5b7152017-09-25 16:25:39 +02001149 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001150}
1151
Willy Tarreau01b44822018-10-03 14:26:37 +02001152/* Try to send a connection preface, then upon success try to send our
1153 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1154 * missing data. It may return an error in h2c.
1155 */
1156static int h2c_bck_send_preface(struct h2c *h2c)
1157{
1158 struct buffer *res;
1159
1160 if (h2c_mux_busy(h2c, NULL)) {
1161 h2c->flags |= H2_CF_DEM_MBUSY;
1162 return 0;
1163 }
1164
1165 res = h2_get_buf(h2c, &h2c->mbuf);
1166 if (!res) {
1167 h2c->flags |= H2_CF_MUX_MALLOC;
1168 h2c->flags |= H2_CF_DEM_MROOM;
1169 return 0;
1170 }
1171
1172 if (!b_data(res)) {
1173 /* preface not yet sent */
1174 b_istput(res, ist(H2_CONN_PREFACE));
1175 }
1176
1177 return h2c_send_settings(h2c);
1178}
1179
Willy Tarreau081d4722017-05-16 21:51:05 +02001180/* try to send a GOAWAY frame on the connection to report an error or a graceful
1181 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1182 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1183 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1184 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1185 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1186 * on unrecoverable failure. It will not attempt to send one again in this last
1187 * case so that it is safe to use h2c_error() to report such errors.
1188 */
1189static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1190{
1191 struct buffer *res;
1192 char str[17];
1193 int ret;
1194
1195 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1196 return 1; // claim that it worked
1197
1198 if (h2c_mux_busy(h2c, h2s)) {
1199 if (h2s)
1200 h2s->flags |= H2_SF_BLK_MBUSY;
1201 else
1202 h2c->flags |= H2_CF_DEM_MBUSY;
1203 return 0;
1204 }
1205
Willy Tarreau44e973f2018-03-01 17:49:30 +01001206 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001207 if (!res) {
1208 h2c->flags |= H2_CF_MUX_MALLOC;
1209 if (h2s)
1210 h2s->flags |= H2_SF_BLK_MROOM;
1211 else
1212 h2c->flags |= H2_CF_DEM_MROOM;
1213 return 0;
1214 }
1215
1216 /* len: 8, type: 7, flags: none, sid: 0 */
1217 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1218
1219 if (h2c->last_sid < 0)
1220 h2c->last_sid = h2c->max_id;
1221
1222 write_n32(str + 9, h2c->last_sid);
1223 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001224 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001225 if (unlikely(ret <= 0)) {
1226 if (!ret) {
1227 h2c->flags |= H2_CF_MUX_MFULL;
1228 if (h2s)
1229 h2s->flags |= H2_SF_BLK_MROOM;
1230 else
1231 h2c->flags |= H2_CF_DEM_MROOM;
1232 return 0;
1233 }
1234 else {
1235 /* we cannot report this error using GOAWAY, so we mark
1236 * it and claim a success.
1237 */
1238 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1239 h2c->flags |= H2_CF_GOAWAY_FAILED;
1240 return 1;
1241 }
1242 }
1243 h2c->flags |= H2_CF_GOAWAY_SENT;
1244 return ret;
1245}
1246
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001247/* Try to send an RST_STREAM frame on the connection for the indicated stream
1248 * during mux operations. This stream must be valid and cannot be closed
1249 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1250 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1251 * not yet.
1252 *
1253 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1254 * to write the message, it subscribes the stream to future notifications.
1255 */
1256static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1257{
1258 struct buffer *res;
1259 char str[13];
1260 int ret;
1261
1262 if (!h2s || h2s->st == H2_SS_CLOSED)
1263 return 1;
1264
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001265 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1266 * RST_STREAM in response to a RST_STREAM frame.
1267 */
1268 if (h2c->dft == H2_FT_RST_STREAM) {
1269 ret = 1;
1270 goto ignore;
1271 }
1272
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001273 if (h2c_mux_busy(h2c, h2s)) {
1274 h2s->flags |= H2_SF_BLK_MBUSY;
1275 return 0;
1276 }
1277
Willy Tarreau44e973f2018-03-01 17:49:30 +01001278 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001279 if (!res) {
1280 h2c->flags |= H2_CF_MUX_MALLOC;
1281 h2s->flags |= H2_SF_BLK_MROOM;
1282 return 0;
1283 }
1284
1285 /* len: 4, type: 3, flags: none */
1286 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1287 write_n32(str + 5, h2s->id);
1288 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001289 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001290
1291 if (unlikely(ret <= 0)) {
1292 if (!ret) {
1293 h2c->flags |= H2_CF_MUX_MFULL;
1294 h2s->flags |= H2_SF_BLK_MROOM;
1295 return 0;
1296 }
1297 else {
1298 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1299 return 0;
1300 }
1301 }
1302
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001303 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001304 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001305 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001306 return ret;
1307}
1308
1309/* Try to send an RST_STREAM frame on the connection for the stream being
1310 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001311 * error code, even if the stream is one of the dummy ones, and will update
1312 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001313 *
1314 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1315 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001316 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001317 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001318 */
1319static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1320{
1321 struct buffer *res;
1322 char str[13];
1323 int ret;
1324
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001325 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1326 * RST_STREAM in response to a RST_STREAM frame.
1327 */
1328 if (h2c->dft == H2_FT_RST_STREAM) {
1329 ret = 1;
1330 goto ignore;
1331 }
1332
Willy Tarreau27a84c92017-10-17 08:10:17 +02001333 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001334 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001335 return 0;
1336 }
1337
Willy Tarreau44e973f2018-03-01 17:49:30 +01001338 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001339 if (!res) {
1340 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001341 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001342 return 0;
1343 }
1344
1345 /* len: 4, type: 3, flags: none */
1346 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001347
Willy Tarreau27a84c92017-10-17 08:10:17 +02001348 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001349 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001350 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001351
Willy Tarreau27a84c92017-10-17 08:10:17 +02001352 if (unlikely(ret <= 0)) {
1353 if (!ret) {
1354 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001355 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001356 return 0;
1357 }
1358 else {
1359 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1360 return 0;
1361 }
1362 }
1363
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001364 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001365 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001366 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001367 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001368 }
1369
Willy Tarreau27a84c92017-10-17 08:10:17 +02001370 return ret;
1371}
1372
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001373/* try to send an empty DATA frame with the ES flag set to notify about the
1374 * end of stream and match a shutdown(write). If an ES was already sent as
1375 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1376 * on success or zero if nothing was done. In case of lack of room to write the
1377 * message, it subscribes the requesting stream to future notifications.
1378 */
1379static int h2_send_empty_data_es(struct h2s *h2s)
1380{
1381 struct h2c *h2c = h2s->h2c;
1382 struct buffer *res;
1383 char str[9];
1384 int ret;
1385
Willy Tarreau721c9742017-11-07 11:05:42 +01001386 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001387 return 1;
1388
1389 if (h2c_mux_busy(h2c, h2s)) {
1390 h2s->flags |= H2_SF_BLK_MBUSY;
1391 return 0;
1392 }
1393
Willy Tarreau44e973f2018-03-01 17:49:30 +01001394 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001395 if (!res) {
1396 h2c->flags |= H2_CF_MUX_MALLOC;
1397 h2s->flags |= H2_SF_BLK_MROOM;
1398 return 0;
1399 }
1400
1401 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1402 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1403 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001404 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001405 if (likely(ret > 0)) {
1406 h2s->flags |= H2_SF_ES_SENT;
1407 }
1408 else if (!ret) {
1409 h2c->flags |= H2_CF_MUX_MFULL;
1410 h2s->flags |= H2_SF_BLK_MROOM;
1411 return 0;
1412 }
1413 else {
1414 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1415 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001416 }
1417 return ret;
1418}
1419
Christopher Fauletf02ca002019-03-07 16:21:34 +01001420/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1421 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1422 * connection. The stream's state is automatically updated accordingly. If the
1423 * stream is orphaned, it is destroyed.
1424 */
1425static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1426{
1427 if (!h2s->cs) {
1428 /* this stream was already orphaned */
1429 h2s_destroy(h2s);
1430 return;
1431 }
1432
1433 h2s->cs->flags |= flags;
1434 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1435 h2s->cs->flags |= CS_FL_ERROR;
1436
1437 h2s_alert(h2s);
1438
1439 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1440 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001441 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001442 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001443 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001444 h2s_close(h2s);
1445}
1446
1447/* wake the streams attached to the connection, whose id is greater than <last>
1448 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001449 */
1450static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1451{
1452 struct eb32_node *node;
1453 struct h2s *h2s;
1454
1455 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001456 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001457
1458 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001459 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001460
Christopher Fauletf02ca002019-03-07 16:21:34 +01001461 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001462 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1463 while (node) {
1464 h2s = container_of(node, struct h2s, by_id);
1465 if (h2s->id <= last)
1466 break;
1467 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001468 h2s_wake_one_stream(h2s, flags);
1469 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001470
Christopher Fauletf02ca002019-03-07 16:21:34 +01001471 /* Wake all streams with unassigned ID (ID == 0) */
1472 node = eb32_lookup(&h2c->streams_by_id, 0);
1473 while (node) {
1474 h2s = container_of(node, struct h2s, by_id);
1475 if (h2s->id > 0)
1476 break;
1477 node = eb32_next(node);
1478 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001479 }
1480}
1481
Willy Tarreau3421aba2017-07-27 15:41:03 +02001482/* Increase all streams' outgoing window size by the difference passed in
1483 * argument. This is needed upon receipt of the settings frame if the initial
1484 * window size is different. The difference may be negative and the resulting
1485 * window size as well, for the time it takes to receive some window updates.
1486 */
1487static void h2c_update_all_ws(struct h2c *h2c, int diff)
1488{
1489 struct h2s *h2s;
1490 struct eb32_node *node;
1491
1492 if (!diff)
1493 return;
1494
1495 node = eb32_first(&h2c->streams_by_id);
1496 while (node) {
1497 h2s = container_of(node, struct h2s, by_id);
1498 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001499
1500 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1501 h2s->flags &= ~H2_SF_BLK_SFCTL;
1502 if (h2s->send_wait)
1503 LIST_ADDQ(&h2c->send_list, &h2s->list);
1504
1505 }
1506
Willy Tarreau3421aba2017-07-27 15:41:03 +02001507 node = eb32_next(node);
1508 }
1509}
1510
1511/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1512 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001513 * return an error in h2c. The caller must have already verified frame length
1514 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001515 */
1516static int h2c_handle_settings(struct h2c *h2c)
1517{
1518 unsigned int offset;
1519 int error;
1520
1521 if (h2c->dff & H2_F_SETTINGS_ACK) {
1522 if (h2c->dfl) {
1523 error = H2_ERR_FRAME_SIZE_ERROR;
1524 goto fail;
1525 }
1526 return 1;
1527 }
1528
Willy Tarreau3421aba2017-07-27 15:41:03 +02001529 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001530 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001531 return 0;
1532
1533 /* parse the frame */
1534 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001535 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1536 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001537
1538 switch (type) {
1539 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1540 /* we need to update all existing streams with the
1541 * difference from the previous iws.
1542 */
1543 if (arg < 0) { // RFC7540#6.5.2
1544 error = H2_ERR_FLOW_CONTROL_ERROR;
1545 goto fail;
1546 }
1547 h2c_update_all_ws(h2c, arg - h2c->miw);
1548 h2c->miw = arg;
1549 break;
1550 case H2_SETTINGS_MAX_FRAME_SIZE:
1551 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1552 error = H2_ERR_PROTOCOL_ERROR;
1553 goto fail;
1554 }
1555 h2c->mfs = arg;
1556 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001557 case H2_SETTINGS_ENABLE_PUSH:
1558 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1559 error = H2_ERR_PROTOCOL_ERROR;
1560 goto fail;
1561 }
1562 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001563 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1564 if (h2c->flags & H2_CF_IS_BACK) {
1565 /* the limit is only for the backend; for the frontend it is our limit */
1566 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1567 arg = h2_settings_max_concurrent_streams;
1568 h2c->streams_limit = arg;
1569 }
1570 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001571 }
1572 }
1573
1574 /* need to ACK this frame now */
1575 h2c->st0 = H2_CS_FRAME_A;
1576 return 1;
1577 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001578 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001579 h2c_error(h2c, error);
1580 return 0;
1581}
1582
1583/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1584 * success or one of the h2_status values.
1585 */
1586static int h2c_ack_settings(struct h2c *h2c)
1587{
1588 struct buffer *res;
1589 char str[9];
1590 int ret = -1;
1591
1592 if (h2c_mux_busy(h2c, NULL)) {
1593 h2c->flags |= H2_CF_DEM_MBUSY;
1594 return 0;
1595 }
1596
Willy Tarreau44e973f2018-03-01 17:49:30 +01001597 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001598 if (!res) {
1599 h2c->flags |= H2_CF_MUX_MALLOC;
1600 h2c->flags |= H2_CF_DEM_MROOM;
1601 return 0;
1602 }
1603
1604 memcpy(str,
1605 "\x00\x00\x00" /* length : 0 (no data) */
1606 "\x04" "\x01" /* type : 4, flags : ACK */
1607 "\x00\x00\x00\x00" /* stream ID */, 9);
1608
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001609 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001610 if (unlikely(ret <= 0)) {
1611 if (!ret) {
1612 h2c->flags |= H2_CF_MUX_MFULL;
1613 h2c->flags |= H2_CF_DEM_MROOM;
1614 return 0;
1615 }
1616 else {
1617 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1618 return 0;
1619 }
1620 }
1621 return ret;
1622}
1623
Willy Tarreaucf68c782017-10-10 17:11:41 +02001624/* processes a PING frame and schedules an ACK if needed. The caller must pass
1625 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001626 * missing data. The caller must have already verified frame length
1627 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001628 */
1629static int h2c_handle_ping(struct h2c *h2c)
1630{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001631 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001632 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001633 h2c->st0 = H2_CS_FRAME_A;
1634 return 1;
1635}
1636
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001637/* Try to send a window update for stream id <sid> and value <increment>.
1638 * Returns > 0 on success or zero on missing room or failure. It may return an
1639 * error in h2c.
1640 */
1641static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1642{
1643 struct buffer *res;
1644 char str[13];
1645 int ret = -1;
1646
1647 if (h2c_mux_busy(h2c, NULL)) {
1648 h2c->flags |= H2_CF_DEM_MBUSY;
1649 return 0;
1650 }
1651
Willy Tarreau44e973f2018-03-01 17:49:30 +01001652 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001653 if (!res) {
1654 h2c->flags |= H2_CF_MUX_MALLOC;
1655 h2c->flags |= H2_CF_DEM_MROOM;
1656 return 0;
1657 }
1658
1659 /* length: 4, type: 8, flags: none */
1660 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1661 write_n32(str + 5, sid);
1662 write_n32(str + 9, increment);
1663
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001664 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001665
1666 if (unlikely(ret <= 0)) {
1667 if (!ret) {
1668 h2c->flags |= H2_CF_MUX_MFULL;
1669 h2c->flags |= H2_CF_DEM_MROOM;
1670 return 0;
1671 }
1672 else {
1673 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1674 return 0;
1675 }
1676 }
1677 return ret;
1678}
1679
1680/* try to send pending window update for the connection. It's safe to call it
1681 * with no pending updates. Returns > 0 on success or zero on missing room or
1682 * failure. It may return an error in h2c.
1683 */
1684static int h2c_send_conn_wu(struct h2c *h2c)
1685{
1686 int ret = 1;
1687
1688 if (h2c->rcvd_c <= 0)
1689 return 1;
1690
Willy Tarreau97aaa672018-12-23 09:49:04 +01001691 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1692 /* increase the advertised connection window to 2G on
1693 * first update.
1694 */
1695 h2c->flags |= H2_CF_WINDOW_OPENED;
1696 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1697 }
1698
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001699 /* send WU for the connection */
1700 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1701 if (ret > 0)
1702 h2c->rcvd_c = 0;
1703
1704 return ret;
1705}
1706
1707/* try to send pending window update for the current dmux stream. It's safe to
1708 * call it with no pending updates. Returns > 0 on success or zero on missing
1709 * room or failure. It may return an error in h2c.
1710 */
1711static int h2c_send_strm_wu(struct h2c *h2c)
1712{
1713 int ret = 1;
1714
1715 if (h2c->rcvd_s <= 0)
1716 return 1;
1717
1718 /* send WU for the stream */
1719 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1720 if (ret > 0)
1721 h2c->rcvd_s = 0;
1722
1723 return ret;
1724}
1725
Willy Tarreaucf68c782017-10-10 17:11:41 +02001726/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1727 * success, 0 on missing data or one of the h2_status values.
1728 */
1729static int h2c_ack_ping(struct h2c *h2c)
1730{
1731 struct buffer *res;
1732 char str[17];
1733 int ret = -1;
1734
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001735 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001736 return 0;
1737
1738 if (h2c_mux_busy(h2c, NULL)) {
1739 h2c->flags |= H2_CF_DEM_MBUSY;
1740 return 0;
1741 }
1742
Willy Tarreau44e973f2018-03-01 17:49:30 +01001743 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001744 if (!res) {
1745 h2c->flags |= H2_CF_MUX_MALLOC;
1746 h2c->flags |= H2_CF_DEM_MROOM;
1747 return 0;
1748 }
1749
1750 memcpy(str,
1751 "\x00\x00\x08" /* length : 8 (same payload) */
1752 "\x06" "\x01" /* type : 6, flags : ACK */
1753 "\x00\x00\x00\x00" /* stream ID */, 9);
1754
1755 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001756 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001757
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001758 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001759 if (unlikely(ret <= 0)) {
1760 if (!ret) {
1761 h2c->flags |= H2_CF_MUX_MFULL;
1762 h2c->flags |= H2_CF_DEM_MROOM;
1763 return 0;
1764 }
1765 else {
1766 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1767 return 0;
1768 }
1769 }
1770 return ret;
1771}
1772
Willy Tarreau26f95952017-07-27 17:18:30 +02001773/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1774 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001775 * h2c or h2s. The caller must have already verified frame length and stream ID
1776 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001777 */
1778static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1779{
1780 int32_t inc;
1781 int error;
1782
Willy Tarreau26f95952017-07-27 17:18:30 +02001783 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001784 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001785 return 0;
1786
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001787 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001788
1789 if (h2c->dsi != 0) {
1790 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001791
1792 /* it's not an error to receive WU on a closed stream */
1793 if (h2s->st == H2_SS_CLOSED)
1794 return 1;
1795
1796 if (!inc) {
1797 error = H2_ERR_PROTOCOL_ERROR;
1798 goto strm_err;
1799 }
1800
1801 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1802 error = H2_ERR_FLOW_CONTROL_ERROR;
1803 goto strm_err;
1804 }
1805
1806 h2s->mws += inc;
1807 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1808 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001809 if (h2s->send_wait)
1810 LIST_ADDQ(&h2c->send_list, &h2s->list);
1811
Willy Tarreau26f95952017-07-27 17:18:30 +02001812 }
1813 }
1814 else {
1815 /* connection window update */
1816 if (!inc) {
1817 error = H2_ERR_PROTOCOL_ERROR;
1818 goto conn_err;
1819 }
1820
1821 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1822 error = H2_ERR_FLOW_CONTROL_ERROR;
1823 goto conn_err;
1824 }
1825
1826 h2c->mws += inc;
1827 }
1828
1829 return 1;
1830
1831 conn_err:
1832 h2c_error(h2c, error);
1833 return 0;
1834
1835 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001836 h2s_error(h2s, error);
1837 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001838 return 0;
1839}
1840
Willy Tarreaue96b0922017-10-30 00:28:29 +01001841/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001842 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1843 * have already verified frame length and stream ID validity. Described in
1844 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001845 */
1846static int h2c_handle_goaway(struct h2c *h2c)
1847{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001848 int last;
1849
Willy Tarreaue96b0922017-10-30 00:28:29 +01001850 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001851 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001852 return 0;
1853
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001854 last = h2_get_n32(&h2c->dbuf, 0);
1855 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001856 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001857 if (h2c->last_sid < 0)
1858 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001859 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001860}
1861
Willy Tarreau92153fc2017-12-03 19:46:19 +01001862/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001863 * invalid. Returns > 0 on success or zero on missing data. It may return an
1864 * error in h2c. The caller must have already verified frame length and stream
1865 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001866 */
1867static int h2c_handle_priority(struct h2c *h2c)
1868{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001869 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001870 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001871 return 0;
1872
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001873 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001874 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001875 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1876 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001877 }
1878 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001879}
1880
Willy Tarreaucd234e92017-08-18 10:59:39 +02001881/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001882 * Returns > 0 on success or zero on missing data. The caller must have already
1883 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001884 */
1885static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1886{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001887 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001888 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001889 return 0;
1890
1891 /* late RST, already handled */
1892 if (h2s->st == H2_SS_CLOSED)
1893 return 1;
1894
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001895 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001896 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001897
1898 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001899 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001900 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001901 }
1902
1903 h2s->flags |= H2_SF_RST_RCVD;
1904 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001905}
1906
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001907/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1908 * It may return an error in h2c or h2s. The caller must consider that the
1909 * return value is the new h2s in case one was allocated (most common case).
1910 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001911 * errors here are reported as connection errors since it's impossible to
1912 * recover from such errors after the compression context has been altered.
1913 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001914static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001915{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001916 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001917 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001918 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001919 int error;
1920
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001921 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001922 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001923
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001924 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001925 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001926
1927 /* now either the frame is complete or the buffer is complete */
1928 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001929 /* The stream exists/existed, this must be a trailers frame */
1930 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001931 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001932 goto out;
1933 goto done;
1934 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001935 /* the connection was already killed by an RST, let's consume
1936 * the data and send another RST.
1937 */
1938 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1939 h2s = (struct h2s*)h2_error_stream;
1940 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001941 }
1942 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1943 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1944 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001945 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001946 goto conn_err;
1947 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001948 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1949 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001950
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001951 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001952
Willy Tarreau25919232019-01-03 14:48:18 +01001953 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001954 if (h2c->st0 >= H2_CS_ERROR)
1955 goto out;
1956
Willy Tarreau25919232019-01-03 14:48:18 +01001957 if (error <= 0) {
1958 if (error == 0)
1959 goto out; // missing data
1960
1961 /* Failed to decode this stream (e.g. too large request)
1962 * but the HPACK decompressor is still synchronized.
1963 */
1964 h2s = (struct h2s*)h2_error_stream;
1965 goto send_rst;
1966 }
1967
Willy Tarreau22de8d32018-09-05 19:55:58 +02001968 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001969 * positively from h2c_frt_stream_new(), the stream will report the error,
1970 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001971 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001972 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001973 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001974 h2s = (struct h2s*)h2_refused_stream;
1975 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001976 }
1977
1978 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001979 h2s->rxbuf = rxbuf;
1980 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001981 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001982
Willy Tarreau88d138e2019-01-02 19:38:14 +01001983 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001984 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001985 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001986
1987 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001988 if (h2s->cs)
1989 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01001990 if (h2s->st == H2_SS_OPEN)
1991 h2s->st = H2_SS_HREM;
1992 else
1993 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02001994 }
1995
Willy Tarreau3a429f02019-01-03 11:41:50 +01001996 /* update the max stream ID if the request is being processed */
1997 if (h2s->id > h2c->max_id)
1998 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001999
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002000 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002001
2002 conn_err:
2003 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002004 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002005
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002006 out:
2007 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002008 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002009
2010 send_rst:
2011 /* make the demux send an RST for the current stream. We may only
2012 * do this if we're certain that the HEADERS frame was properly
2013 * decompressed so that the HPACK decoder is still kept up to date.
2014 */
2015 h2_release_buf(h2c, &rxbuf);
2016 h2c->st0 = H2_CS_FRAME_E;
2017 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002018}
2019
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002020/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2021 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2022 * errors here are reported as connection errors since it's impossible to
2023 * recover from such errors after the compression context has been altered.
2024 */
2025static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2026{
2027 int error;
2028
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002029 if (!b_size(&h2c->dbuf))
2030 return NULL; // empty buffer
2031
2032 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2033 return NULL; // incomplete frame
2034
Willy Tarreau1915ca22019-01-24 11:49:37 +01002035 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002036
Willy Tarreau25919232019-01-03 14:48:18 +01002037 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002038 if (h2c->st0 >= H2_CS_ERROR)
2039 return NULL;
2040
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002041 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2042 /* RFC7540#5.1 */
2043 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2044 h2c->st0 = H2_CS_FRAME_E;
2045 return NULL;
2046 }
2047
Willy Tarreau25919232019-01-03 14:48:18 +01002048 if (error <= 0) {
2049 if (error == 0)
2050 return NULL; // missing data
2051
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002052 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002053 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002054 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002055 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002056 }
2057
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002058 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2059 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002060 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002061 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002062 }
2063
Willy Tarreau927b88b2019-03-04 08:03:25 +01002064 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002065 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002066 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 +02002067 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002068 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002069 h2s_close(h2s);
2070
2071 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002072}
2073
Willy Tarreau454f9052017-10-26 19:40:35 +02002074/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2075 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2076 */
2077static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2078{
2079 int error;
2080
2081 /* note that empty DATA frames are perfectly valid and sometimes used
2082 * to signal an end of stream (with the ES flag).
2083 */
2084
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002085 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002086 return 0; // empty buffer
2087
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002088 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002089 return 0; // incomplete frame
2090
2091 /* now either the frame is complete or the buffer is complete */
2092
Willy Tarreau454f9052017-10-26 19:40:35 +02002093 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2094 /* RFC7540#6.1 */
2095 error = H2_ERR_STREAM_CLOSED;
2096 goto strm_err;
2097 }
2098
Willy Tarreau1915ca22019-01-24 11:49:37 +01002099 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2100 /* RFC7540#8.1.2 */
2101 error = H2_ERR_PROTOCOL_ERROR;
2102 goto strm_err;
2103 }
2104
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002105 if (!h2_frt_transfer_data(h2s))
2106 return 0;
2107
Willy Tarreau454f9052017-10-26 19:40:35 +02002108 /* call the upper layers to process the frame, then let the upper layer
2109 * notify the stream about any change.
2110 */
2111 if (!h2s->cs) {
2112 error = H2_ERR_STREAM_CLOSED;
2113 goto strm_err;
2114 }
2115
Willy Tarreau8f650c32017-11-21 19:36:21 +01002116 if (h2c->st0 >= H2_CS_ERROR)
2117 return 0;
2118
Willy Tarreau721c9742017-11-07 11:05:42 +01002119 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002120 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002121 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002122 }
2123
2124 /* check for completion : the callee will change this to FRAME_A or
2125 * FRAME_H once done.
2126 */
2127 if (h2c->st0 == H2_CS_FRAME_P)
2128 return 0;
2129
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002130 /* last frame */
2131 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002132 if (h2s->cs)
2133 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002134 if (h2s->st == H2_SS_OPEN)
2135 h2s->st = H2_SS_HREM;
2136 else
2137 h2s_close(h2s);
2138
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002139 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002140
2141 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2142 /* RFC7540#8.1.2 */
2143 error = H2_ERR_PROTOCOL_ERROR;
2144 goto strm_err;
2145 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002146 }
2147
Willy Tarreau454f9052017-10-26 19:40:35 +02002148 return 1;
2149
Willy Tarreau454f9052017-10-26 19:40:35 +02002150 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002151 h2s_error(h2s, error);
2152 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002153 return 0;
2154}
2155
Willy Tarreaubc933932017-10-09 16:21:43 +02002156/* process Rx frames to be demultiplexed */
2157static void h2_process_demux(struct h2c *h2c)
2158{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002159 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002160 struct h2_fh hdr;
2161 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002162
Willy Tarreau081d4722017-05-16 21:51:05 +02002163 if (h2c->st0 >= H2_CS_ERROR)
2164 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002165
2166 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2167 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002168 if (h2c->flags & H2_CF_IS_BACK)
2169 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002170 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2171 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002172 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002173 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002174 sess_log(h2c->conn->owner);
2175 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002176 goto fail;
2177 }
2178
2179 h2c->max_id = 0;
2180 h2c->st0 = H2_CS_SETTINGS1;
2181 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002182
2183 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002184 /* ensure that what is pending is a valid SETTINGS frame
2185 * without an ACK.
2186 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002187 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002188 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002189 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002190 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002191 sess_log(h2c->conn->owner);
2192 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002193 goto fail;
2194 }
2195
2196 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2197 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2198 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2199 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002200 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002201 goto fail;
2202 }
2203
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002204 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002205 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2206 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2207 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002208 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002209 goto fail;
2210 }
2211
Willy Tarreau3bf69182018-12-21 15:34:50 +01002212 /* that's OK, switch to FRAME_P to process it. This is
2213 * a SETTINGS frame whose header has already been
2214 * deleted above.
2215 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002216 padlen = 0;
2217 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002218 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002219 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002220
2221 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002222 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002223 int ret = 0;
2224
2225 if (h2c->st0 >= H2_CS_ERROR)
2226 break;
2227
2228 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002229 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002230 break;
2231
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002232 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002233 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002234 if (!h2c->nb_streams) {
2235 /* only log if no other stream can report the error */
2236 sess_log(h2c->conn->owner);
2237 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002238 break;
2239 }
2240
Willy Tarreau3bf69182018-12-21 15:34:50 +01002241 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2242 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2243 * we read the pad length and drop it from the remaining
2244 * payload (one byte + the 9 remaining ones = 10 total
2245 * removed), so we have a frame payload starting after the
2246 * pad len. Flow controlled frames (DATA) also count the
2247 * padlen in the flow control, so it must be adjusted.
2248 */
2249 if (hdr.len < 1) {
2250 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2251 sess_log(h2c->conn->owner);
2252 goto fail;
2253 }
2254 hdr.len--;
2255
2256 if (b_data(&h2c->dbuf) < 10)
2257 break; // missing padlen
2258
2259 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2260
2261 if (padlen > hdr.len) {
2262 /* RFC7540#6.1 : pad length = length of
2263 * frame payload or greater => error.
2264 */
2265 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2266 sess_log(h2c->conn->owner);
2267 goto fail;
2268 }
2269
2270 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2271 h2c->rcvd_c++;
2272 h2c->rcvd_s++;
2273 }
2274 b_del(&h2c->dbuf, 1);
2275 }
2276 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002277
2278 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002279 h2c->dfl = hdr.len;
2280 h2c->dsi = hdr.sid;
2281 h2c->dft = hdr.ft;
2282 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002283 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002284 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002285
2286 /* check for minimum basic frame format validity */
2287 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2288 if (ret != H2_ERR_NO_ERROR) {
2289 h2c_error(h2c, ret);
2290 sess_log(h2c->conn->owner);
2291 goto fail;
2292 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002293 }
2294
2295 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002296 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2297
Willy Tarreau567beb82018-12-18 16:52:44 +01002298 if (tmp_h2s != h2s && h2s && h2s->cs &&
2299 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002300 (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 +01002301 /* we may have to signal the upper layers */
2302 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002303 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002304 }
2305 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002306
Willy Tarreaud7901432017-12-29 11:34:40 +01002307 if (h2c->st0 == H2_CS_FRAME_E)
2308 goto strm_err;
2309
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002310 if (h2s->st == H2_SS_IDLE &&
2311 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2312 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2313 * this state MUST be treated as a connection error
2314 */
2315 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002316 if (!h2c->nb_streams) {
2317 /* only log if no other stream can report the error */
2318 sess_log(h2c->conn->owner);
2319 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002320 break;
2321 }
2322
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002323 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2324 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2325 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002326 * this state MUST be treated as a stream error.
2327 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2328 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002329 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002330 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2331 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2332 else
2333 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002334 goto strm_err;
2335 }
2336
Willy Tarreauab837502017-12-27 15:07:30 +01002337 /* Below the management of frames received in closed state is a
2338 * bit hackish because the spec makes strong differences between
2339 * streams closed by receiving RST, sending RST, and seeing ES
2340 * in both directions. In addition to this, the creation of a
2341 * new stream reusing the identifier of a closed one will be
2342 * detected here. Given that we cannot keep track of all closed
2343 * streams forever, we consider that unknown closed streams were
2344 * closed on RST received, which allows us to respond with an
2345 * RST without breaking the connection (eg: to abort a transfer).
2346 * Some frames have to be silently ignored as well.
2347 */
2348 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002349 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002350 /* #5.1.1: The identifier of a newly
2351 * established stream MUST be numerically
2352 * greater than all streams that the initiating
2353 * endpoint has opened or reserved. This
2354 * governs streams that are opened using a
2355 * HEADERS frame and streams that are reserved
2356 * using PUSH_PROMISE. An endpoint that
2357 * receives an unexpected stream identifier
2358 * MUST respond with a connection error.
2359 */
2360 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2361 goto strm_err;
2362 }
2363
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002364 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002365 /* RFC7540#5.1:closed: an endpoint that
2366 * receives any frame other than PRIORITY after
2367 * receiving a RST_STREAM MUST treat that as a
2368 * stream error of type STREAM_CLOSED.
2369 *
2370 * Note that old streams fall into this category
2371 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002372 *
2373 * However, we cannot generalize this to all frame types. Those
2374 * carrying compression state must still be processed before
2375 * being dropped or we'll desynchronize the decoder. This can
2376 * happen with request trailers received after sending an
2377 * RST_STREAM, or with header/trailers responses received after
2378 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002379 */
2380 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2381 h2c->st0 = H2_CS_FRAME_E;
2382 goto strm_err;
2383 }
2384
2385 /* RFC7540#5.1:closed: if this state is reached as a
2386 * result of sending a RST_STREAM frame, the peer that
2387 * receives the RST_STREAM might have already sent
2388 * frames on the stream that cannot be withdrawn. An
2389 * endpoint MUST ignore frames that it receives on
2390 * closed streams after it has sent a RST_STREAM
2391 * frame. An endpoint MAY choose to limit the period
2392 * over which it ignores frames and treat frames that
2393 * arrive after this time as being in error.
2394 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002395 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002396 /* RFC7540#5.1:closed: any frame other than
2397 * PRIO/WU/RST in this state MUST be treated as
2398 * a connection error
2399 */
2400 if (h2c->dft != H2_FT_RST_STREAM &&
2401 h2c->dft != H2_FT_PRIORITY &&
2402 h2c->dft != H2_FT_WINDOW_UPDATE) {
2403 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2404 goto strm_err;
2405 }
2406 }
2407 }
2408
Willy Tarreauc0da1962017-10-30 18:38:00 +01002409#if 0
2410 // problem below: it is not possible to completely ignore such
2411 // streams as we need to maintain the compression state as well
2412 // and for this we need to completely process these frames (eg:
2413 // HEADERS frames) as well as counting DATA frames to emit
2414 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2415 // This is a typical case of layer violation where the
2416 // transported contents are critical to the connection's
2417 // validity and must be ignored at the same time :-(
2418
2419 /* graceful shutdown, ignore streams whose ID is higher than
2420 * the one advertised in GOAWAY. RFC7540#6.8.
2421 */
2422 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002423 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2424 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002425 h2c->dfl -= ret;
2426 ret = h2c->dfl == 0;
2427 goto strm_err;
2428 }
2429#endif
2430
Willy Tarreau7e98c052017-10-10 15:56:59 +02002431 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002432 case H2_FT_SETTINGS:
2433 if (h2c->st0 == H2_CS_FRAME_P)
2434 ret = h2c_handle_settings(h2c);
2435
2436 if (h2c->st0 == H2_CS_FRAME_A)
2437 ret = h2c_ack_settings(h2c);
2438 break;
2439
Willy Tarreaucf68c782017-10-10 17:11:41 +02002440 case H2_FT_PING:
2441 if (h2c->st0 == H2_CS_FRAME_P)
2442 ret = h2c_handle_ping(h2c);
2443
2444 if (h2c->st0 == H2_CS_FRAME_A)
2445 ret = h2c_ack_ping(h2c);
2446 break;
2447
Willy Tarreau26f95952017-07-27 17:18:30 +02002448 case H2_FT_WINDOW_UPDATE:
2449 if (h2c->st0 == H2_CS_FRAME_P)
2450 ret = h2c_handle_window_update(h2c, h2s);
2451 break;
2452
Willy Tarreau61290ec2017-10-17 08:19:21 +02002453 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002454 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2455 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2456 * frames' parsers consume all following CONTINUATION
2457 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002458 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002459 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2460 sess_log(h2c->conn->owner);
2461 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002462
Willy Tarreau13278b42017-10-13 19:23:14 +02002463 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002464 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002465 if (h2c->flags & H2_CF_IS_BACK)
2466 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2467 else
2468 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002469 if (tmp_h2s) {
2470 h2s = tmp_h2s;
2471 ret = 1;
2472 }
2473 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002474 break;
2475
Willy Tarreau454f9052017-10-26 19:40:35 +02002476 case H2_FT_DATA:
2477 if (h2c->st0 == H2_CS_FRAME_P)
2478 ret = h2c_frt_handle_data(h2c, h2s);
2479
2480 if (h2c->st0 == H2_CS_FRAME_A)
2481 ret = h2c_send_strm_wu(h2c);
2482 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002483
Willy Tarreau92153fc2017-12-03 19:46:19 +01002484 case H2_FT_PRIORITY:
2485 if (h2c->st0 == H2_CS_FRAME_P)
2486 ret = h2c_handle_priority(h2c);
2487 break;
2488
Willy Tarreaucd234e92017-08-18 10:59:39 +02002489 case H2_FT_RST_STREAM:
2490 if (h2c->st0 == H2_CS_FRAME_P)
2491 ret = h2c_handle_rst_stream(h2c, h2s);
2492 break;
2493
Willy Tarreaue96b0922017-10-30 00:28:29 +01002494 case H2_FT_GOAWAY:
2495 if (h2c->st0 == H2_CS_FRAME_P)
2496 ret = h2c_handle_goaway(h2c);
2497 break;
2498
Willy Tarreau1c661982017-10-30 13:52:01 +01002499 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002500 default:
2501 /* drop frames that we ignore. They may be larger than
2502 * the buffer so we drain all of their contents until
2503 * we reach the end.
2504 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002505 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2506 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002507 h2c->dfl -= ret;
2508 ret = h2c->dfl == 0;
2509 }
2510
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002511 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002512 /* We may have to send an RST if not done yet */
2513 if (h2s->st == H2_SS_ERROR)
2514 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002515
Willy Tarreaua20a5192017-12-27 11:02:06 +01002516 if (h2c->st0 == H2_CS_FRAME_E)
2517 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002518
Willy Tarreau7e98c052017-10-10 15:56:59 +02002519 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002520 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002521 break;
2522
2523 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002524 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002525 h2c->st0 = H2_CS_FRAME_H;
2526 }
2527 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002528
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002529 if (h2c->rcvd_c > 0 &&
2530 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2531 h2c_send_conn_wu(h2c);
2532
Willy Tarreau52eed752017-09-22 15:05:09 +02002533 fail:
2534 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002535 if (h2s && h2s->cs &&
2536 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002537 (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 +01002538 /* we may have to signal the upper layers */
2539 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002540 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002541 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002542
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002543 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002544}
2545
2546/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2547 * the end.
2548 */
2549static int h2_process_mux(struct h2c *h2c)
2550{
Olivier Houchard998410a2019-04-15 19:23:37 +02002551 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002552
Willy Tarreau01b44822018-10-03 14:26:37 +02002553 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2554 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2555 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2556 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2557 if (h2c->st0 == H2_CS_ERROR) {
2558 h2c->st0 = H2_CS_ERROR2;
2559 sess_log(h2c->conn->owner);
2560 }
2561 goto fail;
2562 }
2563 h2c->st0 = H2_CS_SETTINGS1;
2564 }
2565 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002566 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002567 return 1;
2568 }
2569
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002570 /* start by sending possibly pending window updates */
2571 if (h2c->rcvd_c > 0 &&
2572 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2573 h2c_send_conn_wu(h2c) < 0)
2574 goto fail;
2575
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002576 /* First we always process the flow control list because the streams
2577 * waiting there were already elected for immediate emission but were
2578 * blocked just on this.
2579 */
2580
Olivier Houchard998410a2019-04-15 19:23:37 +02002581 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002582 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2583 h2c->st0 >= H2_CS_ERROR)
2584 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002585
2586 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002587 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002588
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002589 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002590 /* For some reason, the upper layer failed to subsribe again,
2591 * so remove it from the send_list
2592 */
2593 if (!h2s->send_wait) {
2594 LIST_DEL_INIT(&h2s->list);
2595 continue;
2596 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002597 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002598 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002599 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002600 }
2601
Olivier Houchard998410a2019-04-15 19:23:37 +02002602 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002603 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2604 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002605
2606 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002607 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002608
Olivier Houchard998410a2019-04-15 19:23:37 +02002609 /* For some reason, the upper layer failed to subsribe again,
2610 * so remove it from the send_list
2611 */
2612 if (!h2s->send_wait) {
2613 LIST_DEL_INIT(&h2s->list);
2614 continue;
2615 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002616 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002617 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002618 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002619 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002620 }
2621
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002622 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002623 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002624 if (h2c->st0 == H2_CS_ERROR) {
2625 if (h2c->max_id >= 0) {
2626 h2c_send_goaway_error(h2c, NULL);
2627 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2628 return 0;
2629 }
2630
2631 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2632 }
2633 return 1;
2634 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002635 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002636}
2637
Willy Tarreau62f52692017-10-08 23:01:42 +02002638
Willy Tarreau479998a2018-11-18 06:30:59 +01002639/* Attempt to read data, and subscribe if none available.
2640 * The function returns 1 if data has been received, otherwise zero.
2641 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002642static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002643{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002644 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002645 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002646 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002647 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002648
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002649 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002650 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002651
Willy Tarreau315d8072017-12-10 22:17:57 +01002652 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002653 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002654
Willy Tarreau44e973f2018-03-01 17:49:30 +01002655 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002656 if (!buf) {
2657 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002658 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002659 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002660
Olivier Houchard7505f942018-08-21 18:10:44 +02002661 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002662 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002663 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2664 /* HTX in use : try to pre-align the buffer like the
2665 * rxbufs will be to optimize memory copies. We'll make
2666 * sure that the frame header lands at the end of the
2667 * HTX block to alias it upon recv. We cannot use the
2668 * head because rcv_buf() will realign the buffer if
2669 * it's empty. Thus we cheat and pretend we already
2670 * have a few bytes there.
2671 */
2672 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002673 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002674 }
2675 else
2676 max = b_room(buf);
2677
Olivier Houchard7505f942018-08-21 18:10:44 +02002678 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002679 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002680 else
2681 ret = 0;
2682 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002683
Olivier Houchard53216e72018-10-10 15:46:36 +02002684 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002685 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002686
Olivier Houcharda1411e62018-08-17 18:42:48 +02002687 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002688 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002689 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002690 }
2691
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002692 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002693 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002694 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002695}
2696
Willy Tarreau479998a2018-11-18 06:30:59 +01002697/* Try to send data if possible.
2698 * The function returns 1 if data have been sent, otherwise zero.
2699 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002700static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002701{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002702 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002703 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002704 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002705
2706 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002707 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002708
Olivier Houchard7505f942018-08-21 18:10:44 +02002709
Willy Tarreaua2af5122017-10-09 11:56:46 +02002710 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2711 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002712 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002713 }
2714
Willy Tarreaubc933932017-10-09 16:21:43 +02002715 /* This loop is quite simple : it tries to fill as much as it can from
2716 * pending streams into the existing buffer until it's reportedly full
2717 * or the end of send requests is reached. Then it tries to send this
2718 * buffer's contents out, marks it not full if at least one byte could
2719 * be sent, and tries again.
2720 *
2721 * The snd_buf() function normally takes a "flags" argument which may
2722 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2723 * data immediately comes and CO_SFL_STREAMER to indicate that the
2724 * connection is streaming lots of data (used to increase TLS record
2725 * size at the expense of latency). The former can be sent any time
2726 * there's a buffer full flag, as it indicates at least one stream
2727 * attempted to send and failed so there are pending data. An
2728 * alternative would be to set it as long as there's an active stream
2729 * but that would be problematic for ACKs until we have an absolute
2730 * guarantee that all waiters have at least one byte to send. The
2731 * latter should possibly not be set for now.
2732 */
2733
2734 done = 0;
2735 while (!done) {
2736 unsigned int flags = 0;
2737
2738 /* fill as much as we can into the current buffer */
2739 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2740 done = h2_process_mux(h2c);
2741
Olivier Houchard2b094432019-01-29 18:28:36 +01002742 if (h2c->flags & H2_CF_MUX_MALLOC)
2743 break;
2744
Willy Tarreaubc933932017-10-09 16:21:43 +02002745 if (conn->flags & CO_FL_ERROR)
2746 break;
2747
2748 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2749 flags |= CO_SFL_MSG_MORE;
2750
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002751 if (b_data(&h2c->mbuf)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002752 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002753 if (!ret)
2754 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002755 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002756 b_del(&h2c->mbuf, ret);
2757 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002758 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002759
2760 /* wrote at least one byte, the buffer is not full anymore */
2761 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2762 }
2763
Willy Tarreaua2af5122017-10-09 11:56:46 +02002764 if (conn->flags & CO_FL_SOCK_WR_SH) {
2765 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002766 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002767 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002768 /* We're not full anymore, so we can wake any task that are waiting
2769 * for us.
2770 */
2771 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002772 struct h2s *h2s;
2773
2774 list_for_each_entry(h2s, &h2c->send_list, list) {
2775 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2776 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002777
2778 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002779 continue;
2780
Olivier Houchard998410a2019-04-15 19:23:37 +02002781 /* For some reason, the upper layer failed to subsribe again,
2782 * so remove it from the send_list
2783 */
2784 if (!h2s->send_wait) {
2785 LIST_DEL_INIT(&h2s->list);
2786 continue;
2787 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002788 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002789 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002790 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002791 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002792 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002793 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002794 /* We're done, no more to send */
2795 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002796 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002797schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002798 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002799 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002800 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002801}
2802
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002803/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002804static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2805{
2806 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002807 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002808
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002809 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002810 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002811 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002812 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002813 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002814 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002815 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002816}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002817
Willy Tarreau62f52692017-10-08 23:01:42 +02002818/* callback called on any event by the connection handler.
2819 * It applies changes and returns zero, or < 0 if it wants immediate
2820 * destruction of the connection (which normally doesn not happen in h2).
2821 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002822static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002823{
Olivier Houchard7505f942018-08-21 18:10:44 +02002824 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002825
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002826 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002827 h2_process_demux(h2c);
2828
2829 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002830 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002831
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002832 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002833 h2c->flags &= ~H2_CF_DEM_DFULL;
2834 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002835 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002836
Willy Tarreau0b37d652018-10-03 10:33:02 +02002837 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002838 /* frontend is stopping, reload likely in progress, let's try
2839 * to announce a graceful shutdown if not yet done. We don't
2840 * care if it fails, it will be tried again later.
2841 */
2842 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2843 if (h2c->last_sid < 0)
2844 h2c->last_sid = (1U << 31) - 1;
2845 h2c_send_goaway_error(h2c, NULL);
2846 }
2847 }
2848
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002849 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002850 * If we received early data, and the handshake is done, wake
2851 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002852 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002853 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2854 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2855 struct eb32_node *node;
2856 struct h2s *h2s;
2857
2858 h2c->flags |= H2_CF_WAIT_FOR_HS;
2859 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2860
2861 while (node) {
2862 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002863 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002864 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002865 node = eb32_next(node);
2866 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002867 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002868
Willy Tarreau26bd7612017-10-09 16:47:04 +02002869 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002870 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2871 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2872 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002873 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002874
2875 if (eb_is_empty(&h2c->streams_by_id)) {
2876 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002877 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002878 return -1;
2879 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002880 }
2881
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002882 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002883 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002884
Olivier Houchard53216e72018-10-10 15:46:36 +02002885 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2886 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2887 (h2c->st0 != H2_CS_ERROR &&
2888 !b_data(&h2c->mbuf) &&
2889 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2890 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002891 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002892
Willy Tarreau3f133572017-10-31 19:21:06 +01002893 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002894 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002895 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002896 task_queue(h2c->task);
2897 }
2898 else
2899 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002900 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002901
Olivier Houchard7505f942018-08-21 18:10:44 +02002902 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002903 return 0;
2904}
2905
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002906/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002907static int h2_wake(struct connection *conn)
2908{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002909 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002910
2911 return (h2_process(h2c));
2912}
2913
Willy Tarreauea392822017-10-31 10:02:25 +01002914/* Connection timeout management. The principle is that if there's no receipt
2915 * nor sending for a certain amount of time, the connection is closed. If the
2916 * MUX buffer still has lying data or is not allocatable, the connection is
2917 * immediately killed. If it's allocatable and empty, we attempt to send a
2918 * GOAWAY frame.
2919 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002920static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002921{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002922 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002923 int expired = tick_is_expired(t->expire, now_ms);
2924
Willy Tarreau0975f112018-03-29 15:22:59 +02002925 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002926 return t;
2927
Olivier Houchard3f795f72019-04-17 22:51:06 +02002928 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02002929
2930 if (!h2c) {
2931 /* resources were already deleted */
2932 return NULL;
2933 }
2934
2935 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002936 h2c_error(h2c, H2_ERR_NO_ERROR);
2937 h2_wake_some_streams(h2c, 0, 0);
2938
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002939 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002940 /* don't even try to send a GOAWAY, the buffer is stuck */
2941 h2c->flags |= H2_CF_GOAWAY_FAILED;
2942 }
2943
2944 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002945 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002946 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2947 h2c->flags |= H2_CF_GOAWAY_FAILED;
2948
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002949 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002950 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002951 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002952 b_del(&h2c->mbuf, ret);
2953 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002954 }
2955 }
Willy Tarreauea392822017-10-31 10:02:25 +01002956
Willy Tarreau0975f112018-03-29 15:22:59 +02002957 /* either we can release everything now or it will be done later once
2958 * the last stream closes.
2959 */
2960 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002961 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002962
Willy Tarreauea392822017-10-31 10:02:25 +01002963 return NULL;
2964}
2965
2966
Willy Tarreau62f52692017-10-08 23:01:42 +02002967/*******************************************/
2968/* functions below are used by the streams */
2969/*******************************************/
2970
2971/*
2972 * Attach a new stream to a connection
2973 * (Used for outgoing connections)
2974 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002975static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002976{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002977 struct conn_stream *cs;
2978 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002979 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002980
2981 cs = cs_new(conn);
2982 if (!cs)
2983 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002984 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002985 if (!h2s) {
2986 cs_free(cs);
2987 return NULL;
2988 }
2989 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002990}
2991
Willy Tarreaufafd3982018-11-18 21:29:20 +01002992/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2993 * We have to scan because we may have some orphan streams. It might be
2994 * beneficial to scan backwards from the end to reduce the likeliness to find
2995 * orphans.
2996 */
2997static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2998{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002999 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003000 struct h2s *h2s;
3001 struct eb32_node *node;
3002
3003 node = eb32_first(&h2c->streams_by_id);
3004 while (node) {
3005 h2s = container_of(node, struct h2s, by_id);
3006 if (h2s->cs)
3007 return h2s->cs;
3008 node = eb32_next(node);
3009 }
3010 return NULL;
3011}
3012
Willy Tarreau62f52692017-10-08 23:01:42 +02003013/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003014 * Destroy the mux and the associated connection, if it is no longer used
3015 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003016static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003017{
Christopher Faulet73c12072019-04-08 11:23:22 +02003018 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003019
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003020 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003021 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003022}
3023
3024/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003025 * Detach the stream from the connection and possibly release the connection.
3026 */
3027static void h2_detach(struct conn_stream *cs)
3028{
Willy Tarreau60935142017-10-16 18:11:19 +02003029 struct h2s *h2s = cs->ctx;
3030 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003031 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003032
3033 cs->ctx = NULL;
3034 if (!h2s)
3035 return;
3036
Olivier Houchard998410a2019-04-15 19:23:37 +02003037 /* The stream is about to die, so no need to attempt to run its task */
3038 if (!LIST_ISEMPTY(&h2s->sending_list) &&
3039 h2s->send_wait != &h2s->wait_event) {
3040 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3041 LIST_DEL_INIT(&h2s->sending_list);
3042 }
3043
Olivier Houchardf502aca2018-12-14 19:42:40 +01003044 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003045 h2c = h2s->h2c;
3046 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003047 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003048 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3049 !h2_frt_has_too_many_cs(h2c)) {
3050 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003051 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003052 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003053 }
Willy Tarreau60935142017-10-16 18:11:19 +02003054
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003055 /* this stream may be blocked waiting for some data to leave (possibly
3056 * an ES or RST frame), so orphan it in this case.
3057 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003058 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003059 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003060 (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 +01003061 return;
3062
Willy Tarreau45f752e2017-10-30 15:44:59 +01003063 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3064 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3065 /* unblock the connection if it was blocked on this
3066 * stream.
3067 */
3068 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3069 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003070 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003071 }
3072
Willy Tarreau71049cc2018-03-28 13:56:39 +02003073 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003074
Olivier Houchard8a786902018-12-15 16:05:40 +01003075 if (h2c->flags & H2_CF_IS_BACK &&
3076 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003077 if (!(h2c->conn->flags &
3078 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3079 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003080 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003081 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3082 h2c->conn->owner = NULL;
3083 if (eb_is_empty(&h2c->streams_by_id)) {
3084 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3085 /* The server doesn't want it, let's kill the connection right away */
3086 h2c->conn->mux->destroy(h2c->conn);
3087 return;
3088 }
3089 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003090 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003091 if (eb_is_empty(&h2c->streams_by_id)) {
3092 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3093 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3094 return;
3095 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003096 /* Never ever allow to reuse a connection from a non-reuse backend */
3097 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3098 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003099 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003100 struct server *srv = objt_server(h2c->conn->target);
3101
3102 if (srv) {
3103 if (h2c->conn->flags & CO_FL_PRIVATE)
3104 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3105 else
3106 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3107 }
3108
3109 }
3110 }
3111 }
3112
Willy Tarreaue323f342018-03-28 13:51:45 +02003113 /* We don't want to close right now unless we're removing the
3114 * last stream, and either the connection is in error, or it
3115 * reached the ID already specified in a GOAWAY frame received
3116 * or sent (as seen by last_sid >= 0).
3117 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003118 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003119 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003120 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003121 }
3122 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003123 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003124 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3125 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003126 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003127 else
3128 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003129 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003130}
3131
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003132/* Performs a synchronous or asynchronous shutr().
3133 * FIXME: guess what the return code tries to indicate!
3134 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003135static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003136{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003137 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003138 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003139
Willy Tarreau721c9742017-11-07 11:05:42 +01003140 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003141 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003142
Willy Tarreau18059042019-01-31 19:12:48 +01003143 /* a connstream may require us to immediately kill the whole connection
3144 * for example because of a "tcp-request content reject" rule that is
3145 * normally used to limit abuse. In this case we schedule a goaway to
3146 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003147 */
Willy Tarreau18059042019-01-31 19:12:48 +01003148 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3149 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3150 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3151 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3152 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003153 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3154 /* Nothing was never sent for this stream, so reset with
3155 * REFUSED_STREAM error to let the client retry the
3156 * request.
3157 */
3158 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3159 }
Willy Tarreau18059042019-01-31 19:12:48 +01003160
Willy Tarreau90c32322017-11-24 08:00:30 +01003161 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003162 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003163 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003164
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003165 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003166 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003167 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003168
Olivier Houchard7a977432019-03-21 15:47:13 +01003169 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003170add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003171 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003172 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003173 if (h2s->flags & H2_SF_BLK_MFCTL) {
3174 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3175 h2s->send_wait = sw;
3176 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3177 h2s->send_wait = sw;
3178 LIST_ADDQ(&h2c->send_list, &h2s->list);
3179 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003180 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003181 /* Let the handler know we want shutr */
3182 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003183 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003184}
3185
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003186/* Performs a synchronous or asynchronous shutw().
3187 * FIXME: guess what the return code tries to indicate!
3188 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003189static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003190{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003191 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003192 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003193
Willy Tarreau721c9742017-11-07 11:05:42 +01003194 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003195 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003196
Willy Tarreau67434202017-11-06 20:20:51 +01003197 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003198 /* we can cleanly close using an empty data frame only after headers */
3199
3200 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3201 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003202 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003203
3204 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003205 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003206 else
3207 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003208 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003209 /* a connstream may require us to immediately kill the whole connection
3210 * for example because of a "tcp-request content reject" rule that is
3211 * normally used to limit abuse. In this case we schedule a goaway to
3212 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003213 */
Willy Tarreau18059042019-01-31 19:12:48 +01003214 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3215 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3216 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3217 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3218 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003219 else {
3220 /* Nothing was never sent for this stream, so reset with
3221 * REFUSED_STREAM error to let the client retry the
3222 * request.
3223 */
3224 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3225 }
Willy Tarreau18059042019-01-31 19:12:48 +01003226
Willy Tarreau90c32322017-11-24 08:00:30 +01003227 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003228 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003229 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003230
Willy Tarreau00dd0782018-03-01 16:31:34 +01003231 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003232 }
3233
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003234 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003235 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003236 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003237
3238 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003239 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003240 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003241 if (h2s->flags & H2_SF_BLK_MFCTL) {
3242 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3243 h2s->send_wait = sw;
3244 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3245 h2s->send_wait = sw;
3246 LIST_ADDQ(&h2c->send_list, &h2s->list);
3247 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003248 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003249 /* let the handler know we want to shutw */
3250 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003251 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003252}
3253
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003254/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3255 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3256 * and prevented the last frame from being emitted.
3257 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003258static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3259{
3260 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003261 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003262 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003263
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003264 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003265 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003266 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003267 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003268 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003269
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003270 if (!ret) {
3271 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003272 LIST_DEL_INIT(&h2s->list);
3273 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003274 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003275 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003276 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003277 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003278
3279 if (h2c_is_dead(h2c))
Christopher Faulet73c12072019-04-08 11:23:22 +02003280 h2_release(h2c);
Olivier Houchard7a977432019-03-21 15:47:13 +01003281 }
3282
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003283 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003284}
3285
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003286/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003287static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3288{
3289 struct h2s *h2s = cs->ctx;
3290
3291 if (!mode)
3292 return;
3293
3294 h2_do_shutr(h2s);
3295}
3296
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003297/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003298static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3299{
3300 struct h2s *h2s = cs->ctx;
3301
3302 h2_do_shutw(h2s);
3303}
3304
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003305/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003306 * HTX request or response depending on the connection's side. Returns a
3307 * positive value on success, a negative value on failure, or 0 if it couldn't
3308 * proceed. May report connection errors in h2c->errcode if the frame is
3309 * non-decodable and the connection unrecoverable. In absence of connection
3310 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003311 *
3312 * The function may fold CONTINUATION frames into the initial HEADERS frame
3313 * by removing padding and next frame header, then moving the CONTINUATION
3314 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3315 * leaving a hole between the main frame and the beginning of the next one.
3316 * The possibly remaining incomplete or next frame at the end may be moved
3317 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3318 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3319 *
3320 * A buffer at the beginning of processing may look like this :
3321 *
3322 * ,---.---------.-----.--------------.--------------.------.---.
3323 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3324 * `---^---------^-----^--------------^--------------^------^---'
3325 * | | <-----> | |
3326 * area | dpl | wrap
3327 * |<--------------> |
3328 * | dfl |
3329 * |<-------------------------------------------------->|
3330 * head data
3331 *
3332 * Padding is automatically overwritten when folding, participating to the
3333 * hole size after dfl :
3334 *
3335 * ,---.------------------------.-----.--------------.------.---.
3336 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3337 * `---^------------------------^-----^--------------^------^---'
3338 * | | <-----> | |
3339 * area | hole | wrap
3340 * |<-----------------------> |
3341 * | dfl |
3342 * |<-------------------------------------------------->|
3343 * head data
3344 *
3345 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3346 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3347 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003348 *
3349 * The <flags> field must point to either the stream's flags or to a copy of it
3350 * so that the function can update the following flags :
3351 * - H2_SF_DATA_CLEN when content-length is seen
3352 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3353 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003354 *
3355 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3356 * decoding, in order to detect if we're dealing with a headers or a trailers
3357 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003358 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003359static 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 +02003360{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003361 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003362 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003363 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003364 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003365 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003366 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003367 int flen; // header frame len
3368 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003369 int ret = 0;
3370 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003371 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003372 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003373
Willy Tarreauea18f862018-12-22 20:19:26 +01003374next_frame:
3375 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3376 goto leave; // incomplete input frame
3377
3378 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3379 * this case, we'll try to paste it immediately after the initial
3380 * HEADERS frame payload and kill any possible padding. The initial
3381 * frame's length will be increased to represent the concatenation
3382 * of the two frames. The next frame is read from position <tlen>
3383 * and written at position <flen> (minus padding if some is present).
3384 */
3385 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3386 struct h2_fh hdr;
3387 int clen; // CONTINUATION frame's payload length
3388
3389 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3390 /* no more data, the buffer may be full, either due to
3391 * too large a frame or because of too large a hole that
3392 * we're going to compact at the end.
3393 */
3394 goto leave;
3395 }
3396
3397 if (hdr.ft != H2_FT_CONTINUATION) {
3398 /* RFC7540#6.10: frame of unexpected type */
3399 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3400 goto fail;
3401 }
3402
3403 if (hdr.sid != h2c->dsi) {
3404 /* RFC7540#6.10: frame of different stream */
3405 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3406 goto fail;
3407 }
3408
3409 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3410 /* RFC7540#4.2: invalid frame length */
3411 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3412 goto fail;
3413 }
3414
3415 /* detect when we must stop aggragating frames */
3416 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3417
3418 /* Take as much as we can of the CONTINUATION frame's payload */
3419 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3420 if (clen > hdr.len)
3421 clen = hdr.len;
3422
3423 /* Move the frame's payload over the padding, hole and frame
3424 * header. At least one of hole or dpl is null (see diagrams
3425 * above). The hole moves after the new aggragated frame.
3426 */
3427 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3428 h2c->dfl += clen - h2c->dpl;
3429 hole += h2c->dpl + 9;
3430 h2c->dpl = 0;
3431 goto next_frame;
3432 }
3433
3434 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003435
Willy Tarreau13278b42017-10-13 19:23:14 +02003436 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003437 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003438 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003439 copy = alloc_trash_chunk();
3440 if (!copy) {
3441 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3442 goto fail;
3443 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003444 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3445 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3446 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003447 }
3448
Willy Tarreau13278b42017-10-13 19:23:14 +02003449 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3450 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003451 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003452 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3453 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003454 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003455 }
3456
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003457 if (flen < 5) {
3458 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3459 goto fail;
3460 }
3461
Willy Tarreau13278b42017-10-13 19:23:14 +02003462 hdrs += 5; // stream dep = 4, weight = 1
3463 flen -= 5;
3464 }
3465
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003466 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003467 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003468 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003469 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003470
Willy Tarreau937f7602018-02-26 15:22:17 +01003471 /* we can't retry a failed decompression operation so we must be very
3472 * careful not to take any risks. In practice the output buffer is
3473 * always empty except maybe for trailers, in which case we simply have
3474 * to wait for the upper layer to finish consuming what is available.
3475 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003476
3477 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003478 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003479 if (!htx_is_empty(htx)) {
3480 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003481 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003482 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003483 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003484 if (b_data(rxbuf)) {
3485 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003486 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003487 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003488
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003489 rxbuf->head = 0;
3490 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003491 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003492
Willy Tarreau25919232019-01-03 14:48:18 +01003493 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003494 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3495 sizeof(list)/sizeof(list[0]), tmp);
3496 if (outlen < 0) {
3497 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3498 goto fail;
3499 }
3500
Willy Tarreau25919232019-01-03 14:48:18 +01003501 /* The PACK decompressor was updated, let's update the input buffer and
3502 * the parser's state to commit these changes and allow us to later
3503 * fail solely on the stream if needed.
3504 */
3505 b_del(&h2c->dbuf, h2c->dfl + hole);
3506 h2c->dfl = hole = 0;
3507 h2c->st0 = H2_CS_FRAME_H;
3508
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003509 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003510 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003511
Willy Tarreau88d138e2019-01-02 19:38:14 +01003512 if (*flags & H2_SF_HEADERS_RCVD)
3513 goto trailers;
3514
3515 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003516 if (htx) {
3517 /* HTX mode */
3518 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003519 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003520 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003521 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003522 } else {
3523 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003524 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003525 if (outlen > 0)
3526 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003527 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003528
3529 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003530 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003531 goto fail;
3532 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003533
Willy Tarreau174b06a2018-04-25 18:13:58 +02003534 if (msgf & H2_MSGF_BODY) {
3535 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003536 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003537 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003538 if (htx)
3539 htx->extra = *body_len;
3540 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003541 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003542 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003543 }
3544
Willy Tarreau88d138e2019-01-02 19:38:14 +01003545 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003546 /* indicate that a HEADERS frame was received for this stream, except
3547 * for 1xx responses. For 1xx responses, another HEADERS frame is
3548 * expected.
3549 */
3550 if (!(msgf & H2_MSGF_RSP_1XX))
3551 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003552
Christopher Faulet0b465482019-02-19 15:14:23 +01003553 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003554 /* Mark the end of message, either using EOM in HTX or with the
3555 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3556 * is not set during headers with END_STREAM.
3557 */
3558 if (htx) {
3559 if (!htx_add_endof(htx, HTX_BLK_EOM))
3560 goto fail;
3561 }
3562 else if (*flags & H2_SF_DATA_CHNK) {
3563 if (!b_putblk(rxbuf, "\r\n", 2))
3564 goto fail;
3565 }
3566 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003567
Willy Tarreau86277d42019-01-02 15:36:11 +01003568 /* success */
3569 ret = 1;
3570
Willy Tarreau68dd9852017-07-03 14:44:26 +02003571 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003572 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003573 * move the remaining data over it.
3574 */
3575 if (hole) {
3576 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3577 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3578 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3579 b_sub(&h2c->dbuf, hole);
3580 }
3581
3582 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3583 /* too large frames */
3584 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003585 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003586 }
3587
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003588 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003589 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003590 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003591 return ret;
3592
Willy Tarreau68dd9852017-07-03 14:44:26 +02003593 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003594 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003595 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003596
3597 trailers:
3598 /* This is the last HEADERS frame hence a trailer */
3599
3600 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3601 /* It's a trailer but it's missing ES flag */
3602 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3603 goto fail;
3604 }
3605
3606 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3607 * block, and when using chunks we must send the 0 CRLF marker. For
3608 * other modes, the trailers are silently dropped.
3609 */
3610 if (htx) {
3611 if (!htx_add_endof(htx, HTX_BLK_EOD))
3612 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003613 if (h2_make_htx_trailers(list, htx) <= 0)
3614 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003615 }
3616 else if (*flags & H2_SF_DATA_CHNK) {
3617 /* Legacy mode with chunked encoding : we must finalize the
3618 * data block message emit the trailing CRLF */
3619 if (!b_putblk(rxbuf, "0\r\n", 3))
3620 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003621
3622 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3623 if (outlen > 0)
3624 b_add(rxbuf, outlen);
3625 else
3626 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003627 }
3628
3629 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003630}
3631
Willy Tarreau454f9052017-10-26 19:40:35 +02003632/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3633 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3634 * in use, a new chunk is emitted for each frame. This is supposed to fit
3635 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3636 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3637 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003638 * parser state is automatically updated. Returns > 0 if it could completely
3639 * send the current frame, 0 if it couldn't complete, in which case
3640 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3641 * DATA frame can return 0 as a valid result). Stream errors are reported in
3642 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3643 * have checked the frame header and ensured that the frame was complete or the
3644 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003645 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003646static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003647{
3648 struct h2c *h2c = h2s->h2c;
3649 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003650 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003651 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003652 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003653 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003654
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003655 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003656
Olivier Houchard638b7992018-08-16 15:41:52 +02003657 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003658 if (!csbuf) {
3659 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003660 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003661 }
3662
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003663try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003664 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003665 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3666 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003667 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003668 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003669
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003670 if (flen > b_data(&h2c->dbuf)) {
3671 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003672 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003673 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003674 }
3675
Willy Tarreaua9b77962019-01-31 07:23:00 +01003676 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003677 block1 = htx_free_data_space(htx);
3678 if (!block1) {
3679 h2c->flags |= H2_CF_DEM_SFULL;
3680 goto fail;
3681 }
3682 if (flen > block1)
3683 flen = block1;
3684
3685 /* here, flen is the max we can copy into the output buffer */
3686 block1 = b_contig_data(&h2c->dbuf, 0);
3687 if (flen > block1)
3688 flen = block1;
3689
3690 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3691 h2c->flags |= H2_CF_DEM_SFULL;
3692 goto fail;
3693 }
3694
3695 b_del(&h2c->dbuf, flen);
3696 h2c->dfl -= flen;
3697 h2c->rcvd_c += flen;
3698 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003699
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003700 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003701 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003702 htx->extra = h2s->body_len;
3703 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003704 goto try_again;
3705 }
3706 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003707 /* it doesn't fit and the buffer is fragmented,
3708 * so let's defragment it and try again.
3709 */
3710 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003711 }
3712
Willy Tarreaueba10f22018-04-25 20:44:22 +02003713 /* chunked-encoding requires more room */
3714 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003715 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003716 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3717 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3718 (chklen < 1048576) ? 4 : 8;
3719 chklen += 4; // CRLF, CRLF
3720 }
3721
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003722 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003723 if (flen + chklen > b_room(csbuf)) {
3724 if (chklen >= b_room(csbuf)) {
3725 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003726 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003727 }
3728 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003729 }
3730
3731 if (h2s->flags & H2_SF_DATA_CHNK) {
3732 /* emit the chunk size */
3733 unsigned int chksz = flen;
3734 char str[10];
3735 char *beg;
3736
3737 beg = str + sizeof(str);
3738 *--beg = '\n';
3739 *--beg = '\r';
3740 do {
3741 *--beg = hextab[chksz & 0xF];
3742 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003743 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003744 }
3745
Willy Tarreau454f9052017-10-26 19:40:35 +02003746 /* Block1 is the length of the first block before the buffer wraps,
3747 * block2 is the optional second block to reach the end of the frame.
3748 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003749 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003750 if (block1 > flen)
3751 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003752 block2 = flen - block1;
3753
3754 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003755 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003756
3757 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003758 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003759
Willy Tarreaueba10f22018-04-25 20:44:22 +02003760 if (h2s->flags & H2_SF_DATA_CHNK) {
3761 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003762 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003763 }
3764
Willy Tarreau454f9052017-10-26 19:40:35 +02003765 /* now mark the input data as consumed (will be deleted from the buffer
3766 * by the caller when seeing FRAME_A after sending the window update).
3767 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003768 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003769 h2c->dfl -= flen;
3770 h2c->rcvd_c += flen;
3771 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3772
Willy Tarreau1915ca22019-01-24 11:49:37 +01003773 if (h2s->flags & H2_SF_DATA_CLEN)
3774 h2s->body_len -= flen;
3775
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003776 if (h2c->dfl > h2c->dpl) {
3777 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003778 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003779 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003780 }
3781
Willy Tarreau4a28da12018-01-04 14:41:00 +01003782 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003783 /* here we're done with the frame, all the payload (except padding) was
3784 * transferred.
3785 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003786
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003787 if (h2c->dff & H2_F_DATA_END_STREAM) {
3788 if (htx) {
3789 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3790 h2c->flags |= H2_CF_DEM_SFULL;
3791 goto fail;
3792 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003793 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003794 else if (h2s->flags & H2_SF_DATA_CHNK) {
3795 /* emit the trailing 0 CRLF CRLF */
3796 if (b_room(csbuf) < 5) {
3797 h2c->flags |= H2_CF_DEM_SFULL;
3798 goto fail;
3799 }
3800 chklen += 5;
3801 b_putblk(csbuf, "0\r\n\r\n", 5);
3802 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003803 }
3804
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003805 h2c->rcvd_c += h2c->dpl;
3806 h2c->rcvd_s += h2c->dpl;
3807 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003808 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003809 if (htx)
3810 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003811 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003812 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003813 if (htx)
3814 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003815 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003816}
3817
Willy Tarreau5dd17352018-06-14 13:33:30 +02003818/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3819 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3820 * number of bytes sent. The caller must check the stream's status to detect
3821 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003822 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003823static 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 +02003824{
3825 struct http_hdr list[MAX_HTTP_HDR];
3826 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003827 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003828 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003829 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003830 int es_now = 0;
3831 int ret = 0;
3832 int hdr;
3833
3834 if (h2c_mux_busy(h2c, h2s)) {
3835 h2s->flags |= H2_SF_BLK_MBUSY;
3836 return 0;
3837 }
3838
Willy Tarreau44e973f2018-03-01 17:49:30 +01003839 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003840 h2c->flags |= H2_CF_MUX_MALLOC;
3841 h2s->flags |= H2_SF_BLK_MROOM;
3842 return 0;
3843 }
3844
3845 /* First, try to parse the H1 response and index it into <list>.
3846 * NOTE! Since it comes from haproxy, we *know* that a response header
3847 * block does not wrap and we can safely read it this way without
3848 * having to realign the buffer.
3849 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003850 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003851 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003852 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003853 /* incomplete or invalid response, this is abnormal coming from
3854 * haproxy and may only result in a bad errorfile or bad Lua code
3855 * so that won't be fixed, raise an error now.
3856 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003857 * FIXME: we should instead add the ability to only return a
3858 * 502 bad gateway. But in theory this is not supposed to
3859 * happen.
3860 */
3861 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3862 ret = 0;
3863 goto end;
3864 }
3865
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003866 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003867
3868 /* certain statuses have no body or an empty one, regardless of
3869 * what the headers say.
3870 */
3871 if (sl.st.status >= 100 && sl.st.status < 200) {
3872 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3873 h1m->curr_len = h1m->body_len = 0;
3874 }
3875 else if (sl.st.status == 204 || sl.st.status == 304) {
3876 /* no contents, claim c-len is present and set to zero */
3877 h1m->flags &= ~H1_MF_CHNK;
3878 h1m->flags |= H1_MF_CLEN;
3879 h1m->curr_len = h1m->body_len = 0;
3880 }
3881
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003882 chunk_reset(&outbuf);
3883
3884 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003885 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003886 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003887 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003888
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003889 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003890 break;
3891 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003892 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003893 }
3894
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003895 if (outbuf.size < 9)
3896 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003897
3898 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003899 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3900 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3901 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003902
3903 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003904 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003905 /* this is an unparsable response */
3906 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3907 ret = 0;
3908 goto end;
3909 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003910
3911 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003912 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003913 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003914 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003915 }
3916
3917 /* encode all headers, stop at empty name */
3918 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003919 /* these ones do not exist in H2 and must be dropped. */
3920 if (isteq(list[hdr].n, ist("connection")) ||
3921 isteq(list[hdr].n, ist("proxy-connection")) ||
3922 isteq(list[hdr].n, ist("keep-alive")) ||
3923 isteq(list[hdr].n, ist("upgrade")) ||
3924 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003925 continue;
3926
3927 if (isteq(list[hdr].n, ist("")))
3928 break; // end
3929
3930 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3931 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003932 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003933 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003934 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003935 }
3936 }
3937
3938 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003939 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003940 es_now = 1;
3941
3942 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003943 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003944
3945 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003946 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003947
3948 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003949 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003950
3951 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003952 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003953 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003954
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003955 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003956 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003957 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003958
Willy Tarreau801250e2018-09-11 11:45:04 +02003959 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003960 h2s->flags |= H2_SF_ES_SENT;
3961 if (h2s->st == H2_SS_OPEN)
3962 h2s->st = H2_SS_HLOC;
3963 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003964 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003965 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003966 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003967 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003968 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003969 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003970 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003971 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003972 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003973
3974 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003975
3976 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003977 //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 +02003978 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003979 full:
3980 h1m_init_res(h1m);
3981 h1m->err_pos = -1; // don't care about errors on the response path
3982 h2c->flags |= H2_CF_MUX_MFULL;
3983 h2s->flags |= H2_SF_BLK_MROOM;
3984 ret = 0;
3985 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003986}
3987
Willy Tarreau5dd17352018-06-14 13:33:30 +02003988/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3989 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3990 * the number of bytes sent. The caller must check the stream's status to
3991 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003992 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003993static 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 +02003994{
3995 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003996 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003997 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003998 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003999 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004000 int es_now = 0;
4001 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004002 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004003 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004004
4005 if (h2c_mux_busy(h2c, h2s)) {
4006 h2s->flags |= H2_SF_BLK_MBUSY;
4007 goto end;
4008 }
4009
Willy Tarreau44e973f2018-03-01 17:49:30 +01004010 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004011 h2c->flags |= H2_CF_MUX_MALLOC;
4012 h2s->flags |= H2_SF_BLK_MROOM;
4013 goto end;
4014 }
4015
4016 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004017 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004018 goto end;
4019
4020 chunk_reset(&outbuf);
4021
4022 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004023 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004024 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004025 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004026
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004027 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004028 break;
4029 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004030 /* If there are pending data in the output buffer, and we have
4031 * less than 1/4 of the mbuf's size and everything fits, we'll
4032 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4033 * is full and wait, to save some slow realign calls.
4034 */
4035 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4036 h2c->flags |= H2_CF_MUX_MFULL;
4037 h2s->flags |= H2_SF_BLK_MROOM;
4038 goto end;
4039 }
4040
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004041 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004042 }
4043
4044 if (outbuf.size < 9) {
4045 h2c->flags |= H2_CF_MUX_MFULL;
4046 h2s->flags |= H2_SF_BLK_MROOM;
4047 goto end;
4048 }
4049
4050 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004051 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4052 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4053 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004054
4055 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4056 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004057 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004058 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004059 break;
4060 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004061 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004062 if ((long long)size > h1m->curr_len)
4063 size = h1m->curr_len;
4064 break;
4065 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004066 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004067 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004068 if (!ret)
4069 goto end;
4070
4071 if (ret < 0) {
4072 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004073 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004074 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4075 goto end;
4076 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004077 max -= ret;
4078 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004079 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004080 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004081 }
4082
Willy Tarreau801250e2018-09-11 11:45:04 +02004083 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004084 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004085 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004086 if (!ret)
4087 goto end;
4088
4089 if (ret < 0) {
4090 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004091 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004092 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4093 goto end;
4094 }
4095
4096 size = chunk;
4097 h1m->curr_len = chunk;
4098 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004099 max -= ret;
4100 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004101 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004102 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004103 if (!size)
4104 goto send_empty;
4105 }
4106
4107 /* in MSG_DATA state, continue below */
4108 size = h1m->curr_len;
4109 break;
4110 }
4111
4112 /* we have in <size> the exact number of bytes we need to copy from
4113 * the H1 buffer. We need to check this against the connection's and
4114 * the stream's send windows, and to ensure that this fits in the max
4115 * frame size and in the buffer's available space minus 9 bytes (for
4116 * the frame header). The connection's flow control is applied last so
4117 * that we can use a separate list of streams which are immediately
4118 * unblocked on window opening. Note: we don't implement padding.
4119 */
4120
Willy Tarreau5dd17352018-06-14 13:33:30 +02004121 if (size > max)
4122 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004123
4124 if (size > h2s->mws)
4125 size = h2s->mws;
4126
4127 if (size <= 0) {
4128 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004129 if (h2s->send_wait) {
4130 LIST_DEL(&h2s->list);
4131 LIST_INIT(&h2s->list);
4132 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004133 goto end;
4134 }
4135
4136 if (h2c->mfs && size > h2c->mfs)
4137 size = h2c->mfs;
4138
4139 if (size + 9 > outbuf.size) {
4140 /* we have an opportunity for enlarging the too small
4141 * available space, let's try.
4142 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004143 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004144 goto realign_again;
4145 size = outbuf.size - 9;
4146 }
4147
4148 if (size <= 0) {
4149 h2c->flags |= H2_CF_MUX_MFULL;
4150 h2s->flags |= H2_SF_BLK_MROOM;
4151 goto end;
4152 }
4153
4154 if (size > h2c->mws)
4155 size = h2c->mws;
4156
4157 if (size <= 0) {
4158 h2s->flags |= H2_SF_BLK_MFCTL;
4159 goto end;
4160 }
4161
4162 /* copy whatever we can */
4163 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004164 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004165 if (ret == 1)
4166 len2 = 0;
4167
4168 if (!ret || len1 + len2 < size) {
4169 /* FIXME: must normally never happen */
4170 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4171 goto end;
4172 }
4173
4174 /* limit len1/len2 to size */
4175 if (len1 + len2 > size) {
4176 int sub = len1 + len2 - size;
4177
4178 if (len2 > sub)
4179 len2 -= sub;
4180 else {
4181 sub -= len2;
4182 len2 = 0;
4183 len1 -= sub;
4184 }
4185 }
4186
4187 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004188 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004189 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004190 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004191
4192 send_empty:
4193 /* we may need to add END_STREAM */
4194 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4195 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004196 *
4197 * FIXME: what we do here is not correct because we send end_stream
4198 * before knowing if we'll have to send a HEADERS frame for the
4199 * trailers. More importantly we're not consuming the trailing CRLF
4200 * after the end of trailers, so it will be left to the caller to
4201 * eat it. The right way to do it would be to measure trailers here
4202 * and to send ES only if there are no trailers.
4203 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004204 */
4205 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004206 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004207 es_now = 1;
4208
4209 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004210 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004211
4212 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004213 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004214
4215 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004216 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004217
4218 /* consume incoming H1 response */
4219 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004220 max -= size;
4221 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004222 total += size;
4223 h1m->curr_len -= size;
4224 h2s->mws -= size;
4225 h2c->mws -= size;
4226
4227 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004228 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004229 goto new_frame;
4230 }
4231 }
4232
4233 if (es_now) {
4234 if (h2s->st == H2_SS_OPEN)
4235 h2s->st = H2_SS_HLOC;
4236 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004237 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004238
Willy Tarreau35a62702018-02-27 15:37:25 +01004239 if (!(h1m->flags & H1_MF_CHNK)) {
4240 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004241 total += max;
4242 ofs += max;
4243 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004244
Willy Tarreau801250e2018-09-11 11:45:04 +02004245 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004246 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004247
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004248 h2s->flags |= H2_SF_ES_SENT;
4249 }
4250
4251 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004252 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 +02004253 return total;
4254}
4255
Willy Tarreau115e83b2018-12-01 19:17:53 +01004256/* Try to send a HEADERS frame matching HTX response present in HTX message
4257 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4258 * must check the stream's status to detect any error which might have happened
4259 * subsequently to a successful send. The htx blocks are automatically removed
4260 * from the message. The htx message is assumed to be valid since produced from
4261 * the internal code, hence it contains a start line, an optional series of
4262 * header blocks and an end of header, otherwise an invalid frame could be
4263 * emitted and the resulting htx message could be left in an inconsistent state.
4264 */
4265static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4266{
4267 struct http_hdr list[MAX_HTTP_HDR];
4268 struct h2c *h2c = h2s->h2c;
4269 struct htx_blk *blk;
4270 struct htx_blk *blk_end;
4271 struct buffer outbuf;
4272 struct htx_sl *sl;
4273 enum htx_blk_type type;
4274 int es_now = 0;
4275 int ret = 0;
4276 int hdr;
4277 int idx;
4278
4279 if (h2c_mux_busy(h2c, h2s)) {
4280 h2s->flags |= H2_SF_BLK_MBUSY;
4281 return 0;
4282 }
4283
4284 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4285 h2c->flags |= H2_CF_MUX_MALLOC;
4286 h2s->flags |= H2_SF_BLK_MROOM;
4287 return 0;
4288 }
4289
4290 /* determine the first block which must not be deleted, blk_end may
4291 * be NULL if all blocks have to be deleted.
4292 */
4293 idx = htx_get_head(htx);
4294 blk_end = NULL;
4295 while (idx != -1) {
4296 type = htx_get_blk_type(htx_get_blk(htx, idx));
4297 idx = htx_get_next(htx, idx);
4298 if (type == HTX_BLK_EOH) {
4299 if (idx != -1)
4300 blk_end = htx_get_blk(htx, idx);
4301 break;
4302 }
4303 }
4304
4305 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004306 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004307 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004308 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004309 if (h2s->status < 100 || h2s->status > 999)
4310 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004311
4312 /* and the rest of the headers, that we dump starting at header 0 */
4313 hdr = 0;
4314
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004315 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004316 while ((idx = htx_get_next(htx, idx)) != -1) {
4317 blk = htx_get_blk(htx, idx);
4318 type = htx_get_blk_type(blk);
4319
4320 if (type == HTX_BLK_UNUSED)
4321 continue;
4322
4323 if (type != HTX_BLK_HDR)
4324 break;
4325
4326 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4327 goto fail;
4328
4329 list[hdr].n = htx_get_blk_name(htx, blk);
4330 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004331 hdr++;
4332 }
4333
4334 /* marker for end of headers */
4335 list[hdr].n = ist("");
4336
4337 if (h2s->status == 204 || h2s->status == 304) {
4338 /* no contents, claim c-len is present and set to zero */
4339 es_now = 1;
4340 }
4341
4342 chunk_reset(&outbuf);
4343
4344 while (1) {
4345 outbuf.area = b_tail(&h2c->mbuf);
4346 outbuf.size = b_contig_space(&h2c->mbuf);
4347 outbuf.data = 0;
4348
4349 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4350 break;
4351 realign_again:
4352 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4353 }
4354
4355 if (outbuf.size < 9)
4356 goto full;
4357
4358 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4359 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4360 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4361 outbuf.data = 9;
4362
4363 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004364 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004365 if (b_space_wraps(&h2c->mbuf))
4366 goto realign_again;
4367 goto full;
4368 }
4369
4370 /* encode all headers, stop at empty name */
4371 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4372 /* these ones do not exist in H2 and must be dropped. */
4373 if (isteq(list[hdr].n, ist("connection")) ||
4374 isteq(list[hdr].n, ist("proxy-connection")) ||
4375 isteq(list[hdr].n, ist("keep-alive")) ||
4376 isteq(list[hdr].n, ist("upgrade")) ||
4377 isteq(list[hdr].n, ist("transfer-encoding")))
4378 continue;
4379
4380 if (isteq(list[hdr].n, ist("")))
4381 break; // end
4382
4383 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4384 /* output full */
4385 if (b_space_wraps(&h2c->mbuf))
4386 goto realign_again;
4387 goto full;
4388 }
4389 }
4390
Christopher Faulet0b465482019-02-19 15:14:23 +01004391 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004392 * FIXME: we should also set it when we know for sure that the
4393 * content-length is zero as well as on 204/304
4394 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004395 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4396 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004397 es_now = 1;
4398
Willy Tarreau927b88b2019-03-04 08:03:25 +01004399 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004400 es_now = 1;
4401
4402 /* update the frame's size */
4403 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4404
4405 if (es_now)
4406 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4407
4408 /* commit the H2 response */
4409 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004410
4411 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4412 * 1xx responses, another HEADERS frame is expected.
4413 */
4414 if (h2s->status >= 200 || h2s->status == 101)
4415 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004416
Willy Tarreau115e83b2018-12-01 19:17:53 +01004417 if (es_now) {
4418 h2s->flags |= H2_SF_ES_SENT;
4419 if (h2s->st == H2_SS_OPEN)
4420 h2s->st = H2_SS_HLOC;
4421 else
4422 h2s_close(h2s);
4423 }
4424
4425 /* OK we could properly deliver the response */
4426
4427 /* remove all header blocks including the EOH and compute the
4428 * corresponding size.
4429 *
4430 * FIXME: We should remove everything when es_now is set.
4431 */
4432 ret = 0;
4433 idx = htx_get_head(htx);
4434 blk = htx_get_blk(htx, idx);
4435 while (blk != blk_end) {
4436 ret += htx_get_blksz(blk);
4437 blk = htx_remove_blk(htx, blk);
4438 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004439
4440 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4441 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004442 end:
4443 return ret;
4444 full:
4445 h2c->flags |= H2_CF_MUX_MFULL;
4446 h2s->flags |= H2_SF_BLK_MROOM;
4447 ret = 0;
4448 goto end;
4449 fail:
4450 /* unparsable HTX messages, too large ones to be produced in the local
4451 * list etc go here (unrecoverable errors).
4452 */
4453 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4454 ret = 0;
4455 goto end;
4456}
4457
Willy Tarreau80739692018-10-05 11:35:57 +02004458/* Try to send a HEADERS frame matching HTX request present in HTX message
4459 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4460 * must check the stream's status to detect any error which might have happened
4461 * subsequently to a successful send. The htx blocks are automatically removed
4462 * from the message. The htx message is assumed to be valid since produced from
4463 * the internal code, hence it contains a start line, an optional series of
4464 * header blocks and an end of header, otherwise an invalid frame could be
4465 * emitted and the resulting htx message could be left in an inconsistent state.
4466 */
4467static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4468{
4469 struct http_hdr list[MAX_HTTP_HDR];
4470 struct h2c *h2c = h2s->h2c;
4471 struct htx_blk *blk;
4472 struct htx_blk *blk_end;
4473 struct buffer outbuf;
4474 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004475 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004476 enum htx_blk_type type;
4477 int es_now = 0;
4478 int ret = 0;
4479 int hdr;
4480 int idx;
4481
4482 if (h2c_mux_busy(h2c, h2s)) {
4483 h2s->flags |= H2_SF_BLK_MBUSY;
4484 return 0;
4485 }
4486
4487 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4488 h2c->flags |= H2_CF_MUX_MALLOC;
4489 h2s->flags |= H2_SF_BLK_MROOM;
4490 return 0;
4491 }
4492
4493 /* determine the first block which must not be deleted, blk_end may
4494 * be NULL if all blocks have to be deleted.
4495 */
4496 idx = htx_get_head(htx);
4497 blk_end = NULL;
4498 while (idx != -1) {
4499 type = htx_get_blk_type(htx_get_blk(htx, idx));
4500 idx = htx_get_next(htx, idx);
4501 if (type == HTX_BLK_EOH) {
4502 if (idx != -1)
4503 blk_end = htx_get_blk(htx, idx);
4504 break;
4505 }
4506 }
4507
4508 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004509 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004510 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004511 meth = htx_sl_req_meth(sl);
4512 path = htx_sl_req_uri(sl);
4513
4514 /* and the rest of the headers, that we dump starting at header 0 */
4515 hdr = 0;
4516
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004517 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004518 while ((idx = htx_get_next(htx, idx)) != -1) {
4519 blk = htx_get_blk(htx, idx);
4520 type = htx_get_blk_type(blk);
4521
4522 if (type == HTX_BLK_UNUSED)
4523 continue;
4524
4525 if (type != HTX_BLK_HDR)
4526 break;
4527
4528 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4529 goto fail;
4530
4531 list[hdr].n = htx_get_blk_name(htx, blk);
4532 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004533 hdr++;
4534 }
4535
4536 /* marker for end of headers */
4537 list[hdr].n = ist("");
4538
4539 chunk_reset(&outbuf);
4540
4541 while (1) {
4542 outbuf.area = b_tail(&h2c->mbuf);
4543 outbuf.size = b_contig_space(&h2c->mbuf);
4544 outbuf.data = 0;
4545
4546 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4547 break;
4548 realign_again:
4549 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4550 }
4551
4552 if (outbuf.size < 9)
4553 goto full;
4554
4555 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4556 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4557 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4558 outbuf.data = 9;
4559
4560 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004561 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004562 if (b_space_wraps(&h2c->mbuf))
4563 goto realign_again;
4564 goto full;
4565 }
4566
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004567 /* RFC7540 #8.3: the CONNECT method must have :
4568 * - :authority set to the URI part (host:port)
4569 * - :method set to CONNECT
4570 * - :scheme and :path omitted
4571 */
4572 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4573 /* encode the scheme which is always "https" (or 0x86 for "http") */
4574 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4575 /* output full */
4576 if (b_space_wraps(&h2c->mbuf))
4577 goto realign_again;
4578 goto full;
4579 }
Willy Tarreau80739692018-10-05 11:35:57 +02004580
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004581 /* encode the path, which necessarily is the second one */
4582 if (!hpack_encode_path(&outbuf, path)) {
4583 /* output full */
4584 if (b_space_wraps(&h2c->mbuf))
4585 goto realign_again;
4586 goto full;
4587 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004588
4589 /* look for the Host header and place it in :authority */
4590 auth = ist2(NULL, 0);
4591 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4592 if (isteq(list[hdr].n, ist("")))
4593 break; // end
4594
4595 if (isteq(list[hdr].n, ist("host"))) {
4596 auth = list[hdr].v;
4597 break;
4598 }
4599 }
4600 }
4601 else {
4602 /* for CONNECT, :authority is taken from the path */
4603 auth = path;
4604 }
4605
4606 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4607 /* output full */
4608 if (b_space_wraps(&h2c->mbuf))
4609 goto realign_again;
4610 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004611 }
4612
4613 /* encode all headers, stop at empty name */
4614 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4615 /* these ones do not exist in H2 and must be dropped. */
4616 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004617 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004618 isteq(list[hdr].n, ist("proxy-connection")) ||
4619 isteq(list[hdr].n, ist("keep-alive")) ||
4620 isteq(list[hdr].n, ist("upgrade")) ||
4621 isteq(list[hdr].n, ist("transfer-encoding")))
4622 continue;
4623
4624 if (isteq(list[hdr].n, ist("")))
4625 break; // end
4626
4627 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4628 /* output full */
4629 if (b_space_wraps(&h2c->mbuf))
4630 goto realign_again;
4631 goto full;
4632 }
4633 }
4634
4635 /* we may need to add END_STREAM if we have no body :
4636 * - request already closed, or :
4637 * - no transfer-encoding, and :
4638 * - no content-length or content-length:0
4639 * Fixme: this doesn't take into account CONNECT requests.
4640 */
4641 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4642 es_now = 1;
4643
4644 if (sl->flags & HTX_SL_F_BODYLESS)
4645 es_now = 1;
4646
Willy Tarreau927b88b2019-03-04 08:03:25 +01004647 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004648 es_now = 1;
4649
4650 /* update the frame's size */
4651 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4652
4653 if (es_now)
4654 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4655
4656 /* commit the H2 response */
4657 b_add(&h2c->mbuf, outbuf.data);
4658 h2s->flags |= H2_SF_HEADERS_SENT;
4659 h2s->st = H2_SS_OPEN;
4660
Willy Tarreau80739692018-10-05 11:35:57 +02004661 if (es_now) {
4662 // trim any possibly pending data (eg: inconsistent content-length)
4663 h2s->flags |= H2_SF_ES_SENT;
4664 h2s->st = H2_SS_HLOC;
4665 }
4666
4667 /* remove all header blocks including the EOH and compute the
4668 * corresponding size.
4669 *
4670 * FIXME: We should remove everything when es_now is set.
4671 */
4672 ret = 0;
4673 idx = htx_get_head(htx);
4674 blk = htx_get_blk(htx, idx);
4675 while (blk != blk_end) {
4676 ret += htx_get_blksz(blk);
4677 blk = htx_remove_blk(htx, blk);
4678 }
4679
4680 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4681 htx_remove_blk(htx, blk_end);
4682
4683 end:
4684 return ret;
4685 full:
4686 h2c->flags |= H2_CF_MUX_MFULL;
4687 h2s->flags |= H2_SF_BLK_MROOM;
4688 ret = 0;
4689 goto end;
4690 fail:
4691 /* unparsable HTX messages, too large ones to be produced in the local
4692 * list etc go here (unrecoverable errors).
4693 */
4694 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4695 ret = 0;
4696 goto end;
4697}
4698
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004699/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004700 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4701 * caller must check the stream's status to detect any error which might have
4702 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004703 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4704 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004705static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004706{
4707 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004708 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004709 struct buffer outbuf;
4710 size_t total = 0;
4711 int es_now = 0;
4712 int bsize; /* htx block size */
4713 int fsize; /* h2 frame size */
4714 struct htx_blk *blk;
4715 enum htx_blk_type type;
4716 int idx;
4717
4718 if (h2c_mux_busy(h2c, h2s)) {
4719 h2s->flags |= H2_SF_BLK_MBUSY;
4720 goto end;
4721 }
4722
4723 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4724 h2c->flags |= H2_CF_MUX_MALLOC;
4725 h2s->flags |= H2_SF_BLK_MROOM;
4726 goto end;
4727 }
4728
Willy Tarreau98de12a2018-12-12 07:03:00 +01004729 htx = htx_from_buf(buf);
4730
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004731 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4732 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4733 * the caller to handle.
4734 */
4735
4736 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004737 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004738 goto end;
4739
4740 idx = htx_get_head(htx);
4741 blk = htx_get_blk(htx, idx);
4742 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4743 bsize = htx_get_blksz(blk);
4744 fsize = bsize;
4745
4746 if (type == HTX_BLK_EOD) {
4747 /* if we have an EOD, we're dealing with chunked data. We may
4748 * have a set of trailers after us that the caller will want to
4749 * deal with. Let's simply remove the EOD and return.
4750 */
4751 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004752 total++; // EOD counts as one byte
4753 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004754 goto end;
4755 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004756 else if (type == HTX_BLK_EOM) {
4757 if (h2s->flags & H2_SF_ES_SENT) {
4758 /* ES already sent */
4759 htx_remove_blk(htx, blk);
4760 total++; // EOM counts as one byte
4761 count--;
4762 goto end;
4763 }
4764 }
4765 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004766 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004767
4768 /* Perform some optimizations to reduce the number of buffer copies.
4769 * First, if the mux's buffer is empty and the htx area contains
4770 * exactly one data block of the same size as the requested count, and
4771 * this count fits within the frame size, the stream's window size, and
4772 * the connection's window size, then it's possible to simply swap the
4773 * caller's buffer with the mux's output buffer and adjust offsets and
4774 * length to match the entire DATA HTX block in the middle. In this
4775 * case we perform a true zero-copy operation from end-to-end. This is
4776 * the situation that happens all the time with large files. Second, if
4777 * this is not possible, but the mux's output buffer is empty, we still
4778 * have an opportunity to avoid the copy to the intermediary buffer, by
4779 * making the intermediary buffer's area point to the output buffer's
4780 * area. In this case we want to skip the HTX header to make sure that
4781 * copies remain aligned and that this operation remains possible all
4782 * the time. This goes for headers, data blocks and any data extracted
4783 * from the HTX blocks.
4784 */
4785 if (unlikely(fsize == count &&
4786 htx->used == 1 && type == HTX_BLK_DATA &&
4787 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4788 void *old_area = h2c->mbuf.area;
4789
4790 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004791 /* Too bad there are data left there. We're willing to memcpy/memmove
4792 * up to 1/4 of the buffer, which means that it's OK to copy a large
4793 * frame into a buffer containing few data if it needs to be realigned,
4794 * and that it's also OK to copy few data without realigning. Otherwise
4795 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004796 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004797 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4798 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4799 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004800 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004801
Willy Tarreau98de12a2018-12-12 07:03:00 +01004802 h2c->flags |= H2_CF_MUX_MFULL;
4803 h2s->flags |= H2_SF_BLK_MROOM;
4804 goto end;
4805 }
4806
4807 /* map an H2 frame to the HTX block so that we can put the
4808 * frame header there.
4809 */
4810 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004811 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004812 h2c->mbuf.data = fsize + 9;
4813 outbuf.area = b_head(&h2c->mbuf);
4814
4815 /* prepend an H2 DATA frame header just before the DATA block */
4816 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4817 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4818 h2_set_frame_size(outbuf.area, fsize);
4819
4820 /* update windows */
4821 h2s->mws -= fsize;
4822 h2c->mws -= fsize;
4823
4824 /* and exchange with our old area */
4825 buf->area = old_area;
4826 buf->data = buf->head = 0;
4827 total += fsize;
4828 goto end;
4829 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004830
Willy Tarreau98de12a2018-12-12 07:03:00 +01004831 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004832 /* for DATA and EOM we'll have to emit a frame, even if empty */
4833
4834 while (1) {
4835 outbuf.area = b_tail(&h2c->mbuf);
4836 outbuf.size = b_contig_space(&h2c->mbuf);
4837 outbuf.data = 0;
4838
4839 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4840 break;
4841 realign_again:
4842 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4843 }
4844
4845 if (outbuf.size < 9) {
4846 h2c->flags |= H2_CF_MUX_MFULL;
4847 h2s->flags |= H2_SF_BLK_MROOM;
4848 goto end;
4849 }
4850
4851 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4852 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4853 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4854 outbuf.data = 9;
4855
4856 /* we have in <fsize> the exact number of bytes we need to copy from
4857 * the HTX buffer. We need to check this against the connection's and
4858 * the stream's send windows, and to ensure that this fits in the max
4859 * frame size and in the buffer's available space minus 9 bytes (for
4860 * the frame header). The connection's flow control is applied last so
4861 * that we can use a separate list of streams which are immediately
4862 * unblocked on window opening. Note: we don't implement padding.
4863 */
4864
4865 /* EOM is presented with bsize==1 but would lead to the emission of an
4866 * empty frame, thus we force it to zero here.
4867 */
4868 if (type == HTX_BLK_EOM)
4869 bsize = fsize = 0;
4870
4871 if (!fsize)
4872 goto send_empty;
4873
4874 if (h2s->mws <= 0) {
4875 h2s->flags |= H2_SF_BLK_SFCTL;
4876 if (h2s->send_wait) {
4877 LIST_DEL(&h2s->list);
4878 LIST_INIT(&h2s->list);
4879 }
4880 goto end;
4881 }
4882
Willy Tarreauee573762018-12-04 15:25:57 +01004883 if (fsize > count)
4884 fsize = count;
4885
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004886 if (fsize > h2s->mws)
4887 fsize = h2s->mws; // >0
4888
4889 if (h2c->mfs && fsize > h2c->mfs)
4890 fsize = h2c->mfs; // >0
4891
4892 if (fsize + 9 > outbuf.size) {
4893 /* we have an opportunity for enlarging the too small
4894 * available space, let's try.
4895 * FIXME: is this really interesting to do? Maybe we'll
4896 * spend lots of time realigning instead of using two
4897 * frames.
4898 */
4899 if (b_space_wraps(&h2c->mbuf))
4900 goto realign_again;
4901 fsize = outbuf.size - 9;
4902
4903 if (fsize <= 0) {
4904 /* no need to send an empty frame here */
4905 h2c->flags |= H2_CF_MUX_MFULL;
4906 h2s->flags |= H2_SF_BLK_MROOM;
4907 goto end;
4908 }
4909 }
4910
4911 if (h2c->mws <= 0) {
4912 h2s->flags |= H2_SF_BLK_MFCTL;
4913 goto end;
4914 }
4915
4916 if (fsize > h2c->mws)
4917 fsize = h2c->mws;
4918
4919 /* now let's copy this this into the output buffer */
4920 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004921 h2s->mws -= fsize;
4922 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004923 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004924
4925 send_empty:
4926 /* update the frame's size */
4927 h2_set_frame_size(outbuf.area, fsize);
4928
4929 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4930 * meeting EOM. We should optimize this later.
4931 */
4932 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004933 total++; // EOM counts as one byte
4934 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004935 es_now = 1;
4936 }
4937
4938 if (es_now)
4939 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4940
4941 /* commit the H2 response */
4942 b_add(&h2c->mbuf, fsize + 9);
4943
4944 /* consume incoming HTX block, including EOM */
4945 total += fsize;
4946 if (fsize == bsize) {
4947 htx_remove_blk(htx, blk);
4948 if (fsize)
4949 goto new_frame;
4950 } else {
4951 /* we've truncated this block */
4952 htx_cut_data_blk(htx, blk, fsize);
4953 }
4954
4955 if (es_now) {
4956 if (h2s->st == H2_SS_OPEN)
4957 h2s->st = H2_SS_HLOC;
4958 else
4959 h2s_close(h2s);
4960
4961 h2s->flags |= H2_SF_ES_SENT;
4962 }
4963
4964 end:
4965 return total;
4966}
4967
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004968/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4969 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4970 * processed. The caller must check the stream's status to detect any error
4971 * which might have happened subsequently to a successful send. The htx blocks
4972 * are automatically removed from the message. The htx message is assumed to be
4973 * valid since produced from the internal code. Processing stops when meeting
4974 * the EOM, which is also removed. All trailers are processed at once and sent
4975 * as a single frame. The ES flag is always set.
4976 */
4977static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4978{
4979 struct http_hdr list[MAX_HTTP_HDR];
4980 struct h2c *h2c = h2s->h2c;
4981 struct htx_blk *blk;
4982 struct htx_blk *blk_end;
4983 struct buffer outbuf;
4984 struct h1m h1m;
4985 enum htx_blk_type type;
4986 uint32_t size;
4987 int ret = 0;
4988 int hdr;
4989 int idx;
4990 void *start;
4991
4992 if (h2c_mux_busy(h2c, h2s)) {
4993 h2s->flags |= H2_SF_BLK_MBUSY;
4994 goto end;
4995 }
4996
4997 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4998 h2c->flags |= H2_CF_MUX_MALLOC;
4999 h2s->flags |= H2_SF_BLK_MROOM;
5000 goto end;
5001 }
5002
5003 /* The principle is that we parse each and every trailers block using
5004 * the H1 headers parser, and append it to the list. We don't proceed
5005 * until EOM is met. blk_end will point to the EOM block.
5006 */
5007 hdr = 0;
5008 memset(list, 0, sizeof(list));
5009 blk_end = NULL;
5010
5011 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5012 blk = htx_get_blk(htx, idx);
5013 type = htx_get_blk_type(blk);
5014
5015 if (type == HTX_BLK_UNUSED)
5016 continue;
5017
5018 if (type != HTX_BLK_TLR) {
5019 if (type == HTX_BLK_EOM)
5020 blk_end = blk;
5021 break;
5022 }
5023
5024 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5025 goto fail;
5026
5027 size = htx_get_blksz(blk);
5028 start = htx_get_blk_ptr(htx, blk);
5029
5030 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5031 h1m.err_pos = 0;
5032 ret = h1_headers_to_hdr_list(start, start + size,
5033 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5034 &h1m, NULL);
5035 if (ret < 0)
5036 goto fail;
5037
5038 /* ret == 0 if an incomplete trailers block was found (missing
5039 * empty line), or > 0 if it was found. We have to continue on
5040 * incomplete messages because the trailers block might be
5041 * incomplete.
5042 */
5043
5044 /* search the new end */
5045 while (hdr <= sizeof(list)/sizeof(list[0])) {
5046 if (!list[hdr].n.len)
5047 break;
5048 hdr++;
5049 }
5050 }
5051
5052 if (!blk_end)
5053 goto end; // end not found yet
5054
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005055 chunk_reset(&outbuf);
5056
5057 while (1) {
5058 outbuf.area = b_tail(&h2c->mbuf);
5059 outbuf.size = b_contig_space(&h2c->mbuf);
5060 outbuf.data = 0;
5061
5062 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5063 break;
5064 realign_again:
5065 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5066 }
5067
5068 if (outbuf.size < 9)
5069 goto full;
5070
5071 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5072 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5073 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5074 outbuf.data = 9;
5075
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005076 /* encode all headers */
5077 for (idx = 0; idx < hdr; idx++) {
5078 /* these ones do not exist in H2 or must not appear in
5079 * trailers and must be dropped.
5080 */
5081 if (isteq(list[idx].n, ist("host")) ||
5082 isteq(list[idx].n, ist("content-length")) ||
5083 isteq(list[idx].n, ist("connection")) ||
5084 isteq(list[idx].n, ist("proxy-connection")) ||
5085 isteq(list[idx].n, ist("keep-alive")) ||
5086 isteq(list[idx].n, ist("upgrade")) ||
5087 isteq(list[idx].n, ist("te")) ||
5088 isteq(list[idx].n, ist("transfer-encoding")))
5089 continue;
5090
5091 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5092 /* output full */
5093 if (b_space_wraps(&h2c->mbuf))
5094 goto realign_again;
5095 goto full;
5096 }
5097 }
5098
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005099 if (!hdr) {
5100 /* here we have a problem, we've received an empty trailers
5101 * block followed by an EOM. Because of this we can't send a
5102 * HEADERS frame, so we have to cheat and instead send an empty
5103 * DATA frame conveying the ES flag.
5104 */
5105 outbuf.area[3] = H2_FT_DATA;
5106 outbuf.area[4] = H2_F_DATA_END_STREAM;
5107 }
5108
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005109 /* update the frame's size */
5110 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5111
5112 /* commit the H2 response */
5113 b_add(&h2c->mbuf, outbuf.data);
5114 h2s->flags |= H2_SF_ES_SENT;
5115
5116 if (h2s->st == H2_SS_OPEN)
5117 h2s->st = H2_SS_HLOC;
5118 else
5119 h2s_close(h2s);
5120
5121 /* OK we could properly deliver the response */
5122 done:
5123 /* remove all header blocks including EOM and compute the corresponding size. */
5124 ret = 0;
5125 idx = htx_get_head(htx);
5126 blk = htx_get_blk(htx, idx);
5127 while (blk != blk_end) {
5128 ret += htx_get_blksz(blk);
5129 blk = htx_remove_blk(htx, blk);
5130 }
5131 blk = htx_remove_blk(htx, blk);
5132 end:
5133 return ret;
5134 full:
5135 h2c->flags |= H2_CF_MUX_MFULL;
5136 h2s->flags |= H2_SF_BLK_MROOM;
5137 ret = 0;
5138 goto end;
5139 fail:
5140 /* unparsable HTX messages, too large ones to be produced in the local
5141 * list etc go here (unrecoverable errors).
5142 */
5143 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5144 ret = 0;
5145 goto end;
5146}
5147
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005148/* Called from the upper layer, to subscribe to events, such as being able to send.
5149 * The <param> argument here is supposed to be a pointer to a wait_event struct
5150 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5151 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5152 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5153 * returns 0 except for the error above.
5154 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005155static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5156{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005157 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005158 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005159 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005160
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005161 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005162 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005163 if (!(sw->events & SUB_RETRY_RECV)) {
5164 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005165 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005166 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005167 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005168 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005169 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005170 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005171 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005172 if (!(sw->events & SUB_RETRY_SEND)) {
5173 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005174 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005175 h2s->send_wait = sw;
Olivier Houchard9a0f5592019-04-15 19:22:24 +02005176 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5177 LIST_ISEMPTY(&h2s->list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005178 if (h2s->flags & H2_SF_BLK_MFCTL)
5179 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5180 else
5181 LIST_ADDQ(&h2c->send_list, &h2s->list);
5182 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005183 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005184 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005185 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005186 if (event_type != 0)
5187 return -1;
5188 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005189}
5190
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005191/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5192 * The <param> argument here is supposed to be a pointer to the same wait_event
5193 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5194 * It always returns zero.
5195 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005196static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5197{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005198 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005199 struct h2s *h2s = cs->ctx;
5200
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005201 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005202 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005203 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005204 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005205 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005206 }
5207 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005208 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005209 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005210 if (h2s->send_wait == sw) {
5211 LIST_DEL(&h2s->list);
5212 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005213 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard998410a2019-04-15 19:23:37 +02005214 /* We were about to send, make sure it does not happen */
5215 if (!LIST_ISEMPTY(&h2s->sending_list) &&
5216 h2s->send_wait != &h2s->wait_event) {
5217 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5218 LIST_DEL_INIT(&h2s->sending_list);
5219 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005220 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02005221
Olivier Houchardd846c262018-10-19 17:24:29 +02005222 }
5223 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005224 return 0;
5225}
5226
5227
Olivier Houchard511efea2018-08-16 15:30:32 +02005228/* Called from the upper layer, to receive data */
5229static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5230{
Olivier Houchard638b7992018-08-16 15:41:52 +02005231 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005232 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005233 struct htx *h2s_htx = NULL;
5234 struct htx *buf_htx = NULL;
5235 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005236 size_t ret = 0;
5237
5238 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005239 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5240 /* in HTX mode we ignore the count argument */
5241 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005242 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005243 /* Here htx_to_buf() will set buffer data to 0 because
5244 * the HTX is empty.
5245 */
5246 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005247 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005248 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005249
5250 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005251 count = htx_free_data_space(buf_htx);
5252 if (flags & CO_RFL_KEEP_RSV) {
5253 if (count <= global.tune.maxrewrite)
5254 goto end;
5255 count -= global.tune.maxrewrite;
5256 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005257
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005258 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005259
5260 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5261 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5262
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005263 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005264 htx_to_buf(buf_htx, buf);
5265 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005266 ret = htx_ret.ret;
5267 }
5268 else {
5269 ret = b_xfer(buf, &h2s->rxbuf, count);
5270 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005271
Christopher Faulet37070b22019-02-14 15:12:14 +01005272 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005273 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005274 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005275 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005276 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005277 if (cs->flags & CS_FL_REOS)
5278 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005279 if (cs->flags & CS_FL_ERR_PENDING)
5280 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005281 if (b_size(&h2s->rxbuf)) {
5282 b_free(&h2s->rxbuf);
5283 offer_buffers(NULL, tasks_run_queue);
5284 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005285 }
5286
Willy Tarreau082f5592018-11-25 08:03:32 +01005287 if (ret && h2c->dsi == h2s->id) {
5288 /* demux is blocking on this stream's buffer */
5289 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005290 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005291 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005292
Olivier Houchard511efea2018-08-16 15:30:32 +02005293 return ret;
5294}
5295
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005296/* stops all senders of this connection for example when the mux buffer is full.
5297 * They are moved from the sending_list to either fctl_list or send_list.
5298 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005299static void h2_stop_senders(struct h2c *h2c)
5300{
5301 struct h2s *h2s, *h2s_back;
5302
Olivier Houchardd360ac62019-03-22 17:37:16 +01005303 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5304 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005305 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005306 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005307 }
5308}
5309
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005310/* Called from the upper layer, to send data from buffer <buf> for no more than
5311 * <count> bytes. Returns the number of bytes effectively sent. Some status
5312 * flags may be updated on the conn_stream.
5313 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005314static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005315{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005316 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005317 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005318 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005319 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005320 struct htx *htx;
5321 struct htx_blk *blk;
5322 enum htx_blk_type btype;
5323 uint32_t bsize;
5324 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005325
Olivier Houchardd360ac62019-03-22 17:37:16 +01005326 /* If we were not just woken because we wanted to send but couldn't,
5327 * and there's somebody else that is waiting to send, do nothing,
5328 * we will subscribe later and be put at the end of the list
5329 */
Olivier Houchard998410a2019-04-15 19:23:37 +02005330 if (LIST_ISEMPTY(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005331 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5332 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005333 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005334
Olivier Houchard998410a2019-04-15 19:23:37 +02005335 /* We couldn't set it to NULL before, because we needed it in case
5336 * we had to cancel the tasklet
5337 */
5338 h2s->send_wait = NULL;
5339
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005340 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5341 return 0;
5342
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005343 /* htx will be enough to decide if we're using HTX or legacy */
5344 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5345
Willy Tarreau0bad0432018-06-14 16:54:01 +02005346 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005347 h2s->flags |= H2_SF_OUTGOING_DATA;
5348
Willy Tarreau751f2d02018-10-05 09:35:00 +02005349 if (h2s->id == 0) {
5350 int32_t id = h2c_get_next_sid(h2s->h2c);
5351
5352 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005353 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005354 return 0;
5355 }
5356
5357 eb32_delete(&h2s->by_id);
5358 h2s->by_id.key = h2s->id = id;
5359 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005360 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005361 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5362 }
5363
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005364 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005365 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5366 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005367 idx = htx_get_head(htx);
5368 blk = htx_get_blk(htx, idx);
5369 btype = htx_get_blk_type(blk);
5370 bsize = htx_get_blksz(blk);
5371
5372 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005373 case HTX_BLK_REQ_SL:
5374 /* start-line before headers */
5375 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5376 if (ret > 0) {
5377 total += ret;
5378 count -= ret;
5379 if (ret < bsize)
5380 goto done;
5381 }
5382 break;
5383
Willy Tarreau115e83b2018-12-01 19:17:53 +01005384 case HTX_BLK_RES_SL:
5385 /* start-line before headers */
5386 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5387 if (ret > 0) {
5388 total += ret;
5389 count -= ret;
5390 if (ret < bsize)
5391 goto done;
5392 }
5393 break;
5394
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005395 case HTX_BLK_DATA:
5396 case HTX_BLK_EOD:
5397 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005398 /* all these cause the emission of a DATA frame (possibly empty).
5399 * This EOM necessarily is one before trailers, as the EOM following
5400 * trailers would have been consumed by the trailers parser.
5401 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005402 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005403 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005404 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005405 total += ret;
5406 count -= ret;
5407 if (ret < bsize)
5408 goto done;
5409 }
5410 break;
5411
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005412 case HTX_BLK_TLR:
5413 /* This is the first trailers block, all the subsequent ones AND
5414 * the EOM will be swallowed by the parser.
5415 */
5416 ret = h2s_htx_make_trailers(h2s, htx);
5417 if (ret > 0) {
5418 total += ret;
5419 count -= ret;
5420 if (ret < bsize)
5421 goto done;
5422 }
5423 break;
5424
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005425 default:
5426 htx_remove_blk(htx, blk);
5427 total += bsize;
5428 count -= bsize;
5429 break;
5430 }
5431 }
5432 goto done;
5433 }
5434
5435 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005436 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005437 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005438 if (h2s->h2c->flags & H2_CF_IS_BACK)
5439 ret = -1;
5440 else
5441 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005442 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005443 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005444 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005445 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005446 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005447 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005448 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005449
Willy Tarreau5dd17352018-06-14 13:33:30 +02005450 if (unlikely((int)ret <= 0)) {
5451 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005452 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5453 break;
5454 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005455 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005456 total += count;
5457 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005458 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005459 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005460 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005461 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005462 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005463 break;
5464 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005465
5466 total += ret;
5467 count -= ret;
5468
5469 if (h2s->st >= H2_SS_ERROR)
5470 break;
5471
5472 if (h2s->flags & H2_SF_BLK_ANY)
5473 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005474 }
5475
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005476 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005477 if (h2s->st >= H2_SS_ERROR) {
5478 /* trim any possibly pending data after we close (extra CR-LF,
5479 * unprocessed trailers, abnormal extra data, ...)
5480 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005481 total += count;
5482 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005483 }
5484
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005485 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005486 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005487 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005488 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005489 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005490 }
5491
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005492 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005493 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005494 } else {
5495 b_del(buf, total);
5496 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005497
5498 /* The mux is full, cancel the pending tasks */
5499 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5500 (h2s->flags & H2_SF_BLK_MBUSY))
5501 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005502
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005503 /* If we're running HTX, and we read the whole buffer, then pretend
5504 * we read exactly what the caller specified, as with HTX the caller
5505 * will always give the buffer size, instead of the amount of data
5506 * available.
5507 */
5508 if (htx && !b_data(buf))
5509 total = orig_count;
5510
Olivier Houchard7505f942018-08-21 18:10:44 +02005511 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005512 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005513 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005514
Olivier Houchard7505f942018-08-21 18:10:44 +02005515 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005516 /* If we're waiting for flow control, and we got a shutr on the
5517 * connection, we will never be unlocked, so add an error on
5518 * the conn_stream.
5519 */
5520 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5521 !b_data(&h2s->h2c->dbuf) &&
5522 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5523 if (cs->flags & CS_FL_EOS)
5524 cs->flags |= CS_FL_ERROR;
5525 else
5526 cs->flags |= CS_FL_ERR_PENDING;
5527 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005528 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005529 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005530 LIST_DEL_INIT(&h2s->list);
5531 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005532 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005533}
5534
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005535/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005536static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005537{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005538 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005539 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005540 struct eb32_node *node;
5541 int fctl_cnt = 0;
5542 int send_cnt = 0;
5543 int tree_cnt = 0;
5544 int orph_cnt = 0;
5545
5546 if (!h2c)
5547 return;
5548
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005549 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005550 fctl_cnt++;
5551
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005552 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005553 send_cnt++;
5554
Willy Tarreau3af37712018-12-18 14:34:41 +01005555 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005556 node = eb32_first(&h2c->streams_by_id);
5557 while (node) {
5558 h2s = container_of(node, struct h2s, by_id);
5559 tree_cnt++;
5560 if (!h2s->cs)
5561 orph_cnt++;
5562 node = eb32_next(node);
5563 }
5564
Willy Tarreau987c0632018-12-18 10:32:05 +01005565 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5566 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5567 " .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 +02005568 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5569 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005570 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005571 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5572 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5573 h2c->msi,
5574 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5575 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5576
5577 if (h2s) {
5578 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5579 h2s, h2s->id, h2s->flags,
5580 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5581 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5582 h2s->cs);
5583 if (h2s->cs)
5584 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5585 h2s->cs->flags, h2s->cs->data);
5586 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005587}
Willy Tarreau62f52692017-10-08 23:01:42 +02005588
5589/*******************************************************/
5590/* functions below are dedicated to the config parsers */
5591/*******************************************************/
5592
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005593/* config parser for global "tune.h2.header-table-size" */
5594static int h2_parse_header_table_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_header_table_size = atoi(args[1]);
5602 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5603 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5604 return -1;
5605 }
5606 return 0;
5607}
Willy Tarreau62f52692017-10-08 23:01:42 +02005608
Willy Tarreaue6baec02017-07-27 11:45:11 +02005609/* config parser for global "tune.h2.initial-window-size" */
5610static int h2_parse_initial_window_size(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_initial_window_size = atoi(args[1]);
5618 if (h2_settings_initial_window_size < 0) {
5619 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5620 return -1;
5621 }
5622 return 0;
5623}
5624
Willy Tarreau5242ef82017-07-27 11:47:28 +02005625/* config parser for global "tune.h2.max-concurrent-streams" */
5626static int h2_parse_max_concurrent_streams(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_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005634 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005635 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5636 return -1;
5637 }
5638 return 0;
5639}
5640
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005641/* config parser for global "tune.h2.max-frame-size" */
5642static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5643 struct proxy *defpx, const char *file, int line,
5644 char **err)
5645{
5646 if (too_many_args(1, args, err, NULL))
5647 return -1;
5648
5649 h2_settings_max_frame_size = atoi(args[1]);
5650 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5651 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5652 return -1;
5653 }
5654 return 0;
5655}
5656
Willy Tarreau62f52692017-10-08 23:01:42 +02005657
5658/****************************************/
5659/* MUX initialization and instanciation */
5660/***************************************/
5661
5662/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005663static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005664 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005665 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005666 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005667 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005668 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005669 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005670 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005671 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005672 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005673 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005674 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005675 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005676 .shutr = h2_shutr,
5677 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005678 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005679 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005680 .name = "H2",
5681};
5682
Christopher Faulet32f61c02018-04-10 14:33:41 +02005683/* PROTO selection : this mux registers PROTO token "h2" */
5684static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005685 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005686
Willy Tarreau0108d902018-11-25 19:14:37 +01005687INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5688
Christopher Faulet9b579102019-04-11 22:52:25 +02005689
5690/* The mux operations */
5691static const struct mux_ops h2_htx_ops = {
5692 .init = h2_init,
5693 .wake = h2_wake,
5694 .snd_buf = h2_snd_buf,
5695 .rcv_buf = h2_rcv_buf,
5696 .subscribe = h2_subscribe,
5697 .unsubscribe = h2_unsubscribe,
5698 .attach = h2_attach,
5699 .get_first_cs = h2_get_first_cs,
5700 .detach = h2_detach,
5701 .destroy = h2_destroy,
5702 .avail_streams = h2_avail_streams,
5703 .used_streams = h2_used_streams,
5704 .shutr = h2_shutr,
5705 .shutw = h2_shutw,
5706 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005707 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005708 .name = "H2",
5709};
5710
Willy Tarreauf8957272018-10-03 10:25:20 +02005711static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005712 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005713
5714INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5715
Willy Tarreau62f52692017-10-08 23:01:42 +02005716/* config keyword parsers */
5717static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005718 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005719 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005720 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005721 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005722 { 0, NULL, NULL }
5723}};
5724
Willy Tarreau0108d902018-11-25 19:14:37 +01005725INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);