blob: 32e1fc1ab14ef3583b78231746d18334230f57d4 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
147/* HTTP/2 stream flags (32 bit), in h2s->flags */
148#define H2_SF_NONE 0x00000000
149#define H2_SF_ES_RCVD 0x00000001
150#define H2_SF_ES_SENT 0x00000002
151
152#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
153#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
154
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200155/* stream flags indicating the reason the stream is blocked */
156#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
157#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
158#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
159#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
160#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
161
Willy Tarreau454f9052017-10-26 19:40:35 +0200162/* stream flags indicating how data is supposed to be sent */
163#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
164#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
165
166/* step we're currently in when sending chunks. This is needed because we may
167 * have to transfer chunks as large as a full buffer so there's no room left
168 * for size nor crlf around.
169 */
170#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
171#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
172#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
173
174#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
175
Willy Tarreau67434202017-11-06 20:20:51 +0100176#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100177#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100178
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100179#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
180
Willy Tarreau18312642017-10-11 07:57:07 +0200181/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
182 * it is being processed in the internal HTTP representation (H1 for now).
183 */
184struct h2s {
185 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100186 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200187 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200188 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200190 int32_t id; /* stream ID */
191 uint32_t flags; /* H2_SF_* */
192 int mws; /* mux window size for this stream */
193 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
194 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200195 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100196 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200197 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200198 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100199 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
200 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100202 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200203};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200204
Willy Tarreauc6405142017-09-21 20:23:50 +0200205/* descriptor for an h2 frame header */
206struct h2_fh {
207 uint32_t len; /* length, host order, 24 bits */
208 uint32_t sid; /* stream id, host order, 31 bits */
209 uint8_t ft; /* frame type */
210 uint8_t ff; /* frame flags */
211};
212
Willy Tarreau8ceae722018-11-26 11:58:30 +0100213/* the h2c connection pool */
214DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
215
216/* the h2s stream pool */
217DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
218
Willy Tarreaudc572362018-12-12 08:08:05 +0100219/* The default connection window size is 65535, it may only be enlarged using
220 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
221 * we'll pretend we already received the difference between the two to send
222 * an equivalent window update to enlarge it to 2G-1.
223 */
224#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
225
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226/* a few settings from the global section */
227static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200228static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100229static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100230static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau2a856182017-05-16 15:20:39 +0200232/* a dmumy closed stream */
233static const struct h2s *h2_closed_stream = &(const struct h2s){
234 .cs = NULL,
235 .h2c = NULL,
236 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100237 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100238 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200239 .id = 0,
240};
241
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100242/* a dmumy closed stream returning a PROTOCOL_ERROR error */
243static const struct h2s *h2_error_stream = &(const struct h2s){
244 .cs = NULL,
245 .h2c = NULL,
246 .st = H2_SS_CLOSED,
247 .errcode = H2_ERR_PROTOCOL_ERROR,
248 .flags = 0,
249 .id = 0,
250};
251
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100252/* a dmumy closed stream returning a REFUSED_STREAM error */
253static const struct h2s *h2_refused_stream = &(const struct h2s){
254 .cs = NULL,
255 .h2c = NULL,
256 .st = H2_SS_CLOSED,
257 .errcode = H2_ERR_REFUSED_STREAM,
258 .flags = 0,
259 .id = 0,
260};
261
Willy Tarreau2a856182017-05-16 15:20:39 +0200262/* and a dummy idle stream for use with any unannounced stream */
263static const struct h2s *h2_idle_stream = &(const struct h2s){
264 .cs = NULL,
265 .h2c = NULL,
266 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100267 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200268 .id = 0,
269};
270
Olivier Houchard9f6af332018-05-25 14:04:04 +0200271static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200272static int h2_send(struct h2c *h2c);
273static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200274static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200275static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100276static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100277static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100278static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200279static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100280static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100281static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200282
Olivier Houchard7a977432019-03-21 15:47:13 +0100283static __inline int
284h2c_is_dead(struct h2c *h2c)
285{
286 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
287 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
288 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
289 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
290 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
291 (conn_xprt_read0_pending(h2c->conn) ||
292 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
293 return 1;
294
295 return 0;
296
297}
298
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200299/*****************************************************/
300/* functions below are for dynamic buffer management */
301/*****************************************************/
302
Willy Tarreau315d8072017-12-10 22:17:57 +0100303/* indicates whether or not the we may call the h2_recv() function to attempt
304 * to receive data into the buffer and/or demux pending data. The condition is
305 * a bit complex due to some API limits for now. The rules are the following :
306 * - if an error or a shutdown was detected on the connection and the buffer
307 * is empty, we must not attempt to receive
308 * - if the demux buf failed to be allocated, we must not try to receive and
309 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100310 * - if no flag indicates a blocking condition, we may attempt to receive,
311 * regardless of whether the demux buffer is full or not, so that only
312 * de demux part decides whether or not to block. This is needed because
313 * the connection API indeed prevents us from re-enabling receipt that is
314 * already enabled in a polled state, so we must always immediately stop
315 * as soon as the demux can't proceed so as never to hit an end of read
316 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100317 * - otherwise must may not attempt
318 */
319static inline int h2_recv_allowed(const struct h2c *h2c)
320{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100322 (h2c->st0 >= H2_CS_ERROR ||
323 h2c->conn->flags & CO_FL_ERROR ||
324 conn_xprt_read0_pending(h2c->conn)))
325 return 0;
326
327 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100328 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100329 return 1;
330
331 return 0;
332}
333
Willy Tarreau47b515a2018-12-21 16:09:41 +0100334/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200335static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100336{
337 if (!h2_recv_allowed(h2c))
338 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200339 if ((!consider_buffer || !b_data(&h2c->dbuf))
340 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100341 return;
342 tasklet_wakeup(h2c->wait_event.task);
343}
344
345
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100346/* returns true if the front connection has too many conn_streams attached */
347static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200348{
Willy Tarreaua8754662018-12-23 20:43:58 +0100349 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200350}
351
Willy Tarreau44e973f2018-03-01 17:49:30 +0100352/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
353 * flags are used to figure what buffer was requested. It returns 1 if the
354 * allocation succeeds, in which case the connection is woken up, or 0 if it's
355 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200356 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100357static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200358{
359 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100360 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200361
Willy Tarreau44e973f2018-03-01 17:49:30 +0100362 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200363 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200364 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200365 return 1;
366 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200367
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
369 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200370
371 if (h2c->flags & H2_CF_DEM_MROOM) {
372 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200373 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200374 }
Willy Tarreau14398122017-09-22 14:26:04 +0200375 return 1;
376 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100377
378 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
379 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200380 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100381 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200382 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100383 return 1;
384 }
385
Willy Tarreau14398122017-09-22 14:26:04 +0200386 return 0;
387}
388
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200389static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200390{
391 struct buffer *buf = NULL;
392
Willy Tarreau44e973f2018-03-01 17:49:30 +0100393 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
394 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
395 h2c->buf_wait.target = h2c;
396 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100397 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100398 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100399 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200400 __conn_xprt_stop_recv(h2c->conn);
401 }
402 return buf;
403}
404
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200405static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200406{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200407 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100408 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200409 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200410 }
411}
412
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100413/* returns the number of allocatable outgoing streams for the connection taking
414 * the last_sid and the reserved ones into account.
415 */
416static inline int h2_streams_left(const struct h2c *h2c)
417{
418 int ret;
419
420 /* consider the number of outgoing streams we're allowed to create before
421 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
422 * nb_reserved is the number of streams which don't yet have an ID.
423 */
424 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
425 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
426 if (ret < 0)
427 ret = 0;
428 return ret;
429}
430
Willy Tarreau00f18a32019-01-26 12:19:01 +0100431/* returns the number of streams in use on a connection to figure if it's
432 * idle or not. We check nb_cs and not nb_streams as the caller will want
433 * to know if it was the last one after a detach().
434 */
435static int h2_used_streams(struct connection *conn)
436{
437 struct h2c *h2c = conn->ctx;
438
439 return h2c->nb_cs;
440}
441
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100442/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100443static int h2_avail_streams(struct connection *conn)
444{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100445 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100446 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100447 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100448
Willy Tarreau6afec462019-01-28 06:40:19 +0100449 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
450 * streams on the connection.
451 */
452 if (h2c->last_sid >= 0)
453 return 0;
454
Willy Tarreau86949782019-01-31 10:42:05 +0100455 /* note: may be negative if a SETTINGS frame changes the limit */
456 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100457
458 /* we must also consider the limit imposed by stream IDs */
459 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100460 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100461 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100462 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
463 ret1 = MIN(ret1, ret2);
464 }
465 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100466}
467
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200468
Willy Tarreau62f52692017-10-08 23:01:42 +0200469/*****************************************************************/
470/* functions below are dedicated to the mux setup and management */
471/*****************************************************************/
472
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200473/* Initialize the mux once it's attached. For outgoing connections, the context
474 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200475 * connections from the fact that the context is still NULL (even during mux
476 * upgrades). <input> is always used as Input buffer and may contain data. It is
477 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200478 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200479static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
480 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481{
482 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100483 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200484
Willy Tarreaubafbe012017-11-24 17:34:44 +0100485 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200486 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200487 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200488
Christopher Faulete9b70722019-04-08 10:46:02 +0200489 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200490 h2c->flags = H2_CF_IS_BACK;
491 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
492 if (tick_isset(prx->timeout.serverfin))
493 h2c->shut_timeout = prx->timeout.serverfin;
494 } else {
495 h2c->flags = H2_CF_NONE;
496 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
497 if (tick_isset(prx->timeout.clientfin))
498 h2c->shut_timeout = prx->timeout.clientfin;
499 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100500
Willy Tarreau0b37d652018-10-03 10:33:02 +0200501 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100502 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100503 if (tick_isset(h2c->timeout)) {
504 t = task_new(tid_bit);
505 if (!t)
506 goto fail;
507
508 h2c->task = t;
509 t->process = h2_timeout_task;
510 t->context = h2c;
511 t->expire = tick_add(now_ms, h2c->timeout);
512 }
Willy Tarreauea392822017-10-31 10:02:25 +0100513
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200514 h2c->wait_event.task = tasklet_new();
515 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200516 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200517 h2c->wait_event.task->process = h2_io_cb;
518 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100519 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200520
Willy Tarreau32218eb2017-09-22 08:07:25 +0200521 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
522 if (!h2c->ddht)
523 goto fail;
524
525 /* Initialise the context. */
526 h2c->st0 = H2_CS_PREFACE;
527 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100528 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 h2c->max_id = -1;
530 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100531 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200532 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100533 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200534 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100535 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100536 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200537
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200538 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539 h2c->dsi = -1;
540 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100541
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 h2c->last_sid = -1;
543
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200544 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200545 h2c->miw = 65535; /* mux initial window size */
546 h2c->mws = 65535; /* mux window size */
547 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200548 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200549 LIST_INIT(&h2c->send_list);
550 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200551 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100552 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553
Willy Tarreau3f133572017-10-31 19:21:06 +0100554 if (t)
555 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100556
Willy Tarreau01b44822018-10-03 14:26:37 +0200557 if (h2c->flags & H2_CF_IS_BACK) {
558 /* FIXME: this is temporary, for outgoing connections we need
559 * to immediately allocate a stream until the code is modified
560 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100561 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200562 */
563 struct h2s *h2s;
564
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100565 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200566 if (!h2s)
567 goto fail_stream;
568 }
569
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100570 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200571
Willy Tarreau0f383582018-10-03 14:22:21 +0200572 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200573 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200574 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200575 fail_stream:
576 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200577 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100578 if (t)
579 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200580 if (h2c->wait_event.task)
581 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100582 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200583 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200584 return -1;
585}
586
Willy Tarreau751f2d02018-10-05 09:35:00 +0200587/* returns the next allocatable outgoing stream ID for the H2 connection, or
588 * -1 if no more is allocatable.
589 */
590static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
591{
592 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100593
594 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200595 id = -1;
596 return id;
597}
598
Willy Tarreau2373acc2017-10-12 17:35:14 +0200599/* returns the stream associated with id <id> or NULL if not found */
600static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
601{
602 struct eb32_node *node;
603
Willy Tarreau751f2d02018-10-05 09:35:00 +0200604 if (id == 0)
605 return (struct h2s *)h2_closed_stream;
606
Willy Tarreau2a856182017-05-16 15:20:39 +0200607 if (id > h2c->max_id)
608 return (struct h2s *)h2_idle_stream;
609
Willy Tarreau2373acc2017-10-12 17:35:14 +0200610 node = eb32_lookup(&h2c->streams_by_id, id);
611 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200612 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200613
614 return container_of(node, struct h2s, by_id);
615}
616
Christopher Faulet73c12072019-04-08 11:23:22 +0200617/* release function. This one should be called to free all resources allocated
618 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200619 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200620static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200621{
Christopher Faulet73c12072019-04-08 11:23:22 +0200622 struct connection *conn = h2c->conn;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200623
Willy Tarreau32218eb2017-09-22 08:07:25 +0200624 if (h2c) {
625 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200626
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100627 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100628 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100629 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200630
Willy Tarreau44e973f2018-03-01 17:49:30 +0100631 h2_release_buf(h2c, &h2c->dbuf);
632 h2_release_buf(h2c, &h2c->mbuf);
633
Willy Tarreauea392822017-10-31 10:02:25 +0100634 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200635 h2c->task->context = NULL;
636 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100637 h2c->task = NULL;
638 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200639 if (h2c->wait_event.task)
640 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100641 if (h2c->wait_event.events != 0)
642 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200643 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100644
Willy Tarreaubafbe012017-11-24 17:34:44 +0100645 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200646 }
647
648 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100649 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200650
651 conn_stop_tracking(conn);
652 conn_full_close(conn);
653 if (conn->destroy_cb)
654 conn->destroy_cb(conn);
655 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200656}
657
658
Willy Tarreau71681172017-10-23 14:39:06 +0200659/******************************************************/
660/* functions below are for the H2 protocol processing */
661/******************************************************/
662
663/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100664static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200665{
666 return h2s ? h2s->id : 0;
667}
668
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200669/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100670static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200671{
672 if (h2c->msi < 0)
673 return 0;
674
675 if (h2c->msi == h2s_id(h2s))
676 return 0;
677
678 return 1;
679}
680
Willy Tarreau741d6df2017-10-17 08:00:59 +0200681/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100682static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200683{
684 h2c->errcode = err;
685 h2c->st0 = H2_CS_ERROR;
686}
687
Willy Tarreau175cebb2019-01-24 10:02:24 +0100688/* marks an error on the stream. It may also update an already closed stream
689 * (e.g. to report an error after an RST was received).
690 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100691static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200692{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100693 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200694 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100695 if (h2s->st < H2_SS_ERROR)
696 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100697 if (h2s->cs)
698 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200699 }
700}
701
Willy Tarreau7e094452018-12-19 18:08:52 +0100702/* attempt to notify the data layer of recv availability */
703static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
704{
705 struct wait_event *sw;
706
707 if (h2s->recv_wait) {
708 sw = h2s->recv_wait;
709 sw->events &= ~SUB_RETRY_RECV;
710 tasklet_wakeup(sw->task);
711 h2s->recv_wait = NULL;
712 }
713}
714
715/* attempt to notify the data layer of send availability */
716static void __maybe_unused h2s_notify_send(struct h2s *h2s)
717{
718 struct wait_event *sw;
719
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100720 if (h2s->send_wait && !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100721 sw = h2s->send_wait;
722 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100723 sw->events |= SUB_CALL_UNSUBSCRIBE;
724 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100725 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100726 }
727}
728
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100729/* alerts the data layer, trying to wake it up by all means, following
730 * this sequence :
731 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
732 * - if its subscribed to send, then it's woken up for send
733 * - if it was subscribed to neither, its ->wake() callback is called
734 * It is safe to call this function with a closed stream which doesn't have a
735 * conn_stream anymore.
736 */
737static void __maybe_unused h2s_alert(struct h2s *h2s)
738{
739 if (h2s->recv_wait || h2s->send_wait) {
740 h2s_notify_recv(h2s);
741 h2s_notify_send(h2s);
742 }
743 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
744 h2s->cs->data_cb->wake(h2s->cs);
745}
746
Willy Tarreaue4820742017-07-27 13:37:23 +0200747/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100748static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200749{
750 uint8_t *out = frame;
751
752 *out = len >> 16;
753 write_n16(out + 1, len);
754}
755
Willy Tarreau54c15062017-10-10 17:10:03 +0200756/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
757 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
758 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200759 * available in the buffer's input prior to calling this function. The buffer
760 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200761 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100762static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200763 const struct buffer *b, int o)
764{
Willy Tarreau591d4452018-06-15 17:21:00 +0200765 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 +0200766}
767
Willy Tarreau1f094672017-11-20 21:27:45 +0100768static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200769{
Willy Tarreau591d4452018-06-15 17:21:00 +0200770 return readv_n16(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 uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200774{
Willy Tarreau591d4452018-06-15 17:21:00 +0200775 return readv_n32(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 uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200779{
Willy Tarreau591d4452018-06-15 17:21:00 +0200780 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200781}
782
783
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100784/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
785 * The algorithm is not obvious. It turns out that H2 headers are neither
786 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
787 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200788 *
789 * b0 b1 b2 b3 b4 b5..b8
790 * +----------+---------+--------+----+----+----------------------+
791 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
792 * +----------+---------+--------+----+----+----------------------+
793 *
794 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
795 * we get the sid properly aligned and ordered, and 16 bits of len properly
796 * ordered as well. The type and flags can be extracted using bit shifts from
797 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200798 * Returns zero if some bytes are missing, otherwise non-zero on success. The
799 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200800 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100801static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200802{
803 uint64_t w;
804
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100805 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200806 return 0;
807
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100808 w = h2_get_n64(b, o + 1);
809 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200810 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
811 h->ff = w >> 32;
812 h->ft = w >> 40;
813 h->len += w >> 48;
814 return 1;
815}
816
817/* skip the next 9 bytes corresponding to the frame header possibly parsed by
818 * h2_peek_frame_hdr() above.
819 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100820static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200821{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200822 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200823}
824
825/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100826static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200827{
828 int ret;
829
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100830 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200831 if (ret > 0)
832 h2_skip_frame_hdr(b);
833 return ret;
834}
835
Willy Tarreau00dd0782018-03-01 16:31:34 +0100836/* marks stream <h2s> as CLOSED and decrement the number of active streams for
837 * its connection if the stream was not yet closed. Please use this exclusively
838 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100839 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100840static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100841{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100842 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100843 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100844 if (!h2s->id)
845 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100846 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100847 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100848 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
849 h2s_notify_recv(h2s);
850 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100851 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100852 h2s->st = H2_SS_CLOSED;
853}
854
Willy Tarreau71049cc2018-03-28 13:56:39 +0200855/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
856static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100857{
858 h2s_close(h2s);
859 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200860 if (b_size(&h2s->rxbuf)) {
861 b_free(&h2s->rxbuf);
862 offer_buffers(NULL, tasks_run_queue);
863 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200864 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100865 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200866 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100867 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800868 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200869 * reference left would be in the h2c send_list/fctl_list, and if
870 * we're in it, we're getting out anyway
871 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100872 LIST_DEL_INIT(&h2s->list);
873 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200874 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100875 pool_free(pool_head_h2s, h2s);
876}
877
Willy Tarreaua8e49542018-10-03 18:53:55 +0200878/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
879 * stream tree. In case of error, nothing is added and NULL is returned. The
880 * causes of errors can be any failed memory allocation. The caller is
881 * responsible for checking if the connection may support an extra stream
882 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200883 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200884static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200885{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200886 struct h2s *h2s;
887
Willy Tarreaubafbe012017-11-24 17:34:44 +0100888 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200889 if (!h2s)
890 goto out;
891
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200892 h2s->wait_event.task = tasklet_new();
893 if (!h2s->wait_event.task) {
894 pool_free(pool_head_h2s, h2s);
895 goto out;
896 }
897 h2s->send_wait = NULL;
898 h2s->recv_wait = NULL;
899 h2s->wait_event.task->process = h2_deferred_shut;
900 h2s->wait_event.task->context = h2s;
901 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100902 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200903 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100904 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200905 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200906 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200907 h2s->mws = h2c->miw;
908 h2s->flags = H2_SF_NONE;
909 h2s->errcode = H2_ERR_NO_ERROR;
910 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200911 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100912 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200913 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200914
915 if (h2c->flags & H2_CF_IS_BACK) {
916 h1m_init_req(&h2s->h1m);
917 h2s->h1m.err_pos = -1; // don't care about errors on the request path
918 h2s->h1m.flags |= H1_MF_TOLOWER;
919 } else {
920 h1m_init_res(&h2s->h1m);
921 h2s->h1m.err_pos = -1; // don't care about errors on the response path
922 h2s->h1m.flags |= H1_MF_TOLOWER;
923 }
924
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200925 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200926 if (id > 0)
927 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100928 else
929 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200930
931 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100932 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100933 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200934
935 return h2s;
936
937 out_free_h2s:
938 pool_free(pool_head_h2s, h2s);
939 out:
940 return NULL;
941}
942
943/* creates a new stream <id> on the h2c connection and returns it, or NULL in
944 * case of memory allocation error.
945 */
946static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
947{
948 struct session *sess = h2c->conn->owner;
949 struct conn_stream *cs;
950 struct h2s *h2s;
951
952 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
953 goto out;
954
955 h2s = h2s_new(h2c, id);
956 if (!h2s)
957 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200958
959 cs = cs_new(h2c->conn);
960 if (!cs)
961 goto out_close;
962
Olivier Houchard746fb772018-12-15 19:42:00 +0100963 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200964 h2s->cs = cs;
965 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200966 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200967
968 if (stream_create_from_cs(cs) < 0)
969 goto out_free_cs;
970
Willy Tarreau590a0512018-09-05 11:56:48 +0200971 /* We want the accept date presented to the next stream to be the one
972 * we have now, the handshake time to be null (since the next stream
973 * is not delayed by a handshake), and the idle time to count since
974 * right now.
975 */
976 sess->accept_date = date;
977 sess->tv_accept = now;
978 sess->t_handshake = 0;
979
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200980 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100981 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200982 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200983 return h2s;
984
985 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200986 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200987 cs_free(cs);
988 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200989 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200990 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200991 sess_log(sess);
992 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200993}
994
Willy Tarreau751f2d02018-10-05 09:35:00 +0200995/* allocates a new stream associated to conn_stream <cs> on the h2c connection
996 * and returns it, or NULL in case of memory allocation error or if the highest
997 * possible stream ID was reached.
998 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100999static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001000{
1001 struct h2s *h2s = NULL;
1002
Willy Tarreau86949782019-01-31 10:42:05 +01001003 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001004 goto out;
1005
Willy Tarreaua80dca82019-01-24 17:08:28 +01001006 if (h2_streams_left(h2c) < 1)
1007 goto out;
1008
Willy Tarreau751f2d02018-10-05 09:35:00 +02001009 /* Defer choosing the ID until we send the first message to create the stream */
1010 h2s = h2s_new(h2c, 0);
1011 if (!h2s)
1012 goto out;
1013
1014 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001015 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001016 cs->ctx = h2s;
1017 h2c->nb_cs++;
1018
Willy Tarreau751f2d02018-10-05 09:35:00 +02001019 out:
1020 return h2s;
1021}
1022
Willy Tarreaube5b7152017-09-25 16:25:39 +02001023/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1024 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1025 * the various settings codes.
1026 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001027static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001028{
1029 struct buffer *res;
1030 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001031 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001032 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001033 int ret;
1034
1035 if (h2c_mux_busy(h2c, NULL)) {
1036 h2c->flags |= H2_CF_DEM_MBUSY;
1037 return 0;
1038 }
1039
Willy Tarreau44e973f2018-03-01 17:49:30 +01001040 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001041 if (!res) {
1042 h2c->flags |= H2_CF_MUX_MALLOC;
1043 h2c->flags |= H2_CF_DEM_MROOM;
1044 return 0;
1045 }
1046
1047 chunk_init(&buf, buf_data, sizeof(buf_data));
1048 chunk_memcpy(&buf,
1049 "\x00\x00\x00" /* length : 0 for now */
1050 "\x04\x00" /* type : 4 (settings), flags : 0 */
1051 "\x00\x00\x00\x00", /* stream ID : 0 */
1052 9);
1053
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001054 if (h2c->flags & H2_CF_IS_BACK) {
1055 /* send settings_enable_push=0 */
1056 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1057 }
1058
Willy Tarreaube5b7152017-09-25 16:25:39 +02001059 if (h2_settings_header_table_size != 4096) {
1060 char str[6] = "\x00\x01"; /* header_table_size */
1061
1062 write_n32(str + 2, h2_settings_header_table_size);
1063 chunk_memcat(&buf, str, 6);
1064 }
1065
1066 if (h2_settings_initial_window_size != 65535) {
1067 char str[6] = "\x00\x04"; /* initial_window_size */
1068
1069 write_n32(str + 2, h2_settings_initial_window_size);
1070 chunk_memcat(&buf, str, 6);
1071 }
1072
1073 if (h2_settings_max_concurrent_streams != 0) {
1074 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1075
1076 /* Note: 0 means "unlimited" for haproxy's config but not for
1077 * the protocol, so never send this value!
1078 */
1079 write_n32(str + 2, h2_settings_max_concurrent_streams);
1080 chunk_memcat(&buf, str, 6);
1081 }
1082
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001083 mfs = h2_settings_max_frame_size;
1084 if (mfs > global.tune.bufsize)
1085 mfs = global.tune.bufsize;
1086
1087 if (!mfs)
1088 mfs = global.tune.bufsize;
1089
1090 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001091 char str[6] = "\x00\x05"; /* max_frame_size */
1092
1093 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1094 * match bufsize - rewrite size, but at the moment it seems
1095 * that clients don't take care of it.
1096 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001097 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001098 chunk_memcat(&buf, str, 6);
1099 }
1100
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001101 h2_set_frame_size(buf.area, buf.data - 9);
1102 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001103 if (unlikely(ret <= 0)) {
1104 if (!ret) {
1105 h2c->flags |= H2_CF_MUX_MFULL;
1106 h2c->flags |= H2_CF_DEM_MROOM;
1107 return 0;
1108 }
1109 else {
1110 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1111 return 0;
1112 }
1113 }
1114 return ret;
1115}
1116
Willy Tarreau52eed752017-09-22 15:05:09 +02001117/* Try to receive a connection preface, then upon success try to send our
1118 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1119 * missing data. It may return an error in h2c.
1120 */
1121static int h2c_frt_recv_preface(struct h2c *h2c)
1122{
1123 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001124 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001125
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001126 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001127
1128 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001129 if (ret1 < 0)
1130 sess_log(h2c->conn->owner);
1131
Willy Tarreau52eed752017-09-22 15:05:09 +02001132 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1133 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1134 return 0;
1135 }
1136
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001137 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001138 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001139 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001140
Willy Tarreaube5b7152017-09-25 16:25:39 +02001141 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001142}
1143
Willy Tarreau01b44822018-10-03 14:26:37 +02001144/* Try to send a connection preface, then upon success try to send our
1145 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1146 * missing data. It may return an error in h2c.
1147 */
1148static int h2c_bck_send_preface(struct h2c *h2c)
1149{
1150 struct buffer *res;
1151
1152 if (h2c_mux_busy(h2c, NULL)) {
1153 h2c->flags |= H2_CF_DEM_MBUSY;
1154 return 0;
1155 }
1156
1157 res = h2_get_buf(h2c, &h2c->mbuf);
1158 if (!res) {
1159 h2c->flags |= H2_CF_MUX_MALLOC;
1160 h2c->flags |= H2_CF_DEM_MROOM;
1161 return 0;
1162 }
1163
1164 if (!b_data(res)) {
1165 /* preface not yet sent */
1166 b_istput(res, ist(H2_CONN_PREFACE));
1167 }
1168
1169 return h2c_send_settings(h2c);
1170}
1171
Willy Tarreau081d4722017-05-16 21:51:05 +02001172/* try to send a GOAWAY frame on the connection to report an error or a graceful
1173 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1174 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1175 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1176 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1177 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1178 * on unrecoverable failure. It will not attempt to send one again in this last
1179 * case so that it is safe to use h2c_error() to report such errors.
1180 */
1181static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1182{
1183 struct buffer *res;
1184 char str[17];
1185 int ret;
1186
1187 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1188 return 1; // claim that it worked
1189
1190 if (h2c_mux_busy(h2c, h2s)) {
1191 if (h2s)
1192 h2s->flags |= H2_SF_BLK_MBUSY;
1193 else
1194 h2c->flags |= H2_CF_DEM_MBUSY;
1195 return 0;
1196 }
1197
Willy Tarreau44e973f2018-03-01 17:49:30 +01001198 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001199 if (!res) {
1200 h2c->flags |= H2_CF_MUX_MALLOC;
1201 if (h2s)
1202 h2s->flags |= H2_SF_BLK_MROOM;
1203 else
1204 h2c->flags |= H2_CF_DEM_MROOM;
1205 return 0;
1206 }
1207
1208 /* len: 8, type: 7, flags: none, sid: 0 */
1209 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1210
1211 if (h2c->last_sid < 0)
1212 h2c->last_sid = h2c->max_id;
1213
1214 write_n32(str + 9, h2c->last_sid);
1215 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001216 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001217 if (unlikely(ret <= 0)) {
1218 if (!ret) {
1219 h2c->flags |= H2_CF_MUX_MFULL;
1220 if (h2s)
1221 h2s->flags |= H2_SF_BLK_MROOM;
1222 else
1223 h2c->flags |= H2_CF_DEM_MROOM;
1224 return 0;
1225 }
1226 else {
1227 /* we cannot report this error using GOAWAY, so we mark
1228 * it and claim a success.
1229 */
1230 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1231 h2c->flags |= H2_CF_GOAWAY_FAILED;
1232 return 1;
1233 }
1234 }
1235 h2c->flags |= H2_CF_GOAWAY_SENT;
1236 return ret;
1237}
1238
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001239/* Try to send an RST_STREAM frame on the connection for the indicated stream
1240 * during mux operations. This stream must be valid and cannot be closed
1241 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1242 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1243 * not yet.
1244 *
1245 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1246 * to write the message, it subscribes the stream to future notifications.
1247 */
1248static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1249{
1250 struct buffer *res;
1251 char str[13];
1252 int ret;
1253
1254 if (!h2s || h2s->st == H2_SS_CLOSED)
1255 return 1;
1256
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001257 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1258 * RST_STREAM in response to a RST_STREAM frame.
1259 */
1260 if (h2c->dft == H2_FT_RST_STREAM) {
1261 ret = 1;
1262 goto ignore;
1263 }
1264
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001265 if (h2c_mux_busy(h2c, h2s)) {
1266 h2s->flags |= H2_SF_BLK_MBUSY;
1267 return 0;
1268 }
1269
Willy Tarreau44e973f2018-03-01 17:49:30 +01001270 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001271 if (!res) {
1272 h2c->flags |= H2_CF_MUX_MALLOC;
1273 h2s->flags |= H2_SF_BLK_MROOM;
1274 return 0;
1275 }
1276
1277 /* len: 4, type: 3, flags: none */
1278 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1279 write_n32(str + 5, h2s->id);
1280 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001281 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001282
1283 if (unlikely(ret <= 0)) {
1284 if (!ret) {
1285 h2c->flags |= H2_CF_MUX_MFULL;
1286 h2s->flags |= H2_SF_BLK_MROOM;
1287 return 0;
1288 }
1289 else {
1290 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1291 return 0;
1292 }
1293 }
1294
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001295 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001296 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001297 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001298 return ret;
1299}
1300
1301/* Try to send an RST_STREAM frame on the connection for the stream being
1302 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001303 * error code, even if the stream is one of the dummy ones, and will update
1304 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001305 *
1306 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1307 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001308 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001309 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001310 */
1311static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1312{
1313 struct buffer *res;
1314 char str[13];
1315 int ret;
1316
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001317 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1318 * RST_STREAM in response to a RST_STREAM frame.
1319 */
1320 if (h2c->dft == H2_FT_RST_STREAM) {
1321 ret = 1;
1322 goto ignore;
1323 }
1324
Willy Tarreau27a84c92017-10-17 08:10:17 +02001325 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001326 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001327 return 0;
1328 }
1329
Willy Tarreau44e973f2018-03-01 17:49:30 +01001330 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001331 if (!res) {
1332 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001333 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001334 return 0;
1335 }
1336
1337 /* len: 4, type: 3, flags: none */
1338 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001339
Willy Tarreau27a84c92017-10-17 08:10:17 +02001340 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001341 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001342 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001343
Willy Tarreau27a84c92017-10-17 08:10:17 +02001344 if (unlikely(ret <= 0)) {
1345 if (!ret) {
1346 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001347 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001348 return 0;
1349 }
1350 else {
1351 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1352 return 0;
1353 }
1354 }
1355
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001356 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001357 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001358 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001359 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001360 }
1361
Willy Tarreau27a84c92017-10-17 08:10:17 +02001362 return ret;
1363}
1364
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001365/* try to send an empty DATA frame with the ES flag set to notify about the
1366 * end of stream and match a shutdown(write). If an ES was already sent as
1367 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1368 * on success or zero if nothing was done. In case of lack of room to write the
1369 * message, it subscribes the requesting stream to future notifications.
1370 */
1371static int h2_send_empty_data_es(struct h2s *h2s)
1372{
1373 struct h2c *h2c = h2s->h2c;
1374 struct buffer *res;
1375 char str[9];
1376 int ret;
1377
Willy Tarreau721c9742017-11-07 11:05:42 +01001378 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001379 return 1;
1380
1381 if (h2c_mux_busy(h2c, h2s)) {
1382 h2s->flags |= H2_SF_BLK_MBUSY;
1383 return 0;
1384 }
1385
Willy Tarreau44e973f2018-03-01 17:49:30 +01001386 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001387 if (!res) {
1388 h2c->flags |= H2_CF_MUX_MALLOC;
1389 h2s->flags |= H2_SF_BLK_MROOM;
1390 return 0;
1391 }
1392
1393 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1394 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1395 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001396 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001397 if (likely(ret > 0)) {
1398 h2s->flags |= H2_SF_ES_SENT;
1399 }
1400 else if (!ret) {
1401 h2c->flags |= H2_CF_MUX_MFULL;
1402 h2s->flags |= H2_SF_BLK_MROOM;
1403 return 0;
1404 }
1405 else {
1406 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1407 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001408 }
1409 return ret;
1410}
1411
Christopher Fauletf02ca002019-03-07 16:21:34 +01001412/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1413 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1414 * connection. The stream's state is automatically updated accordingly. If the
1415 * stream is orphaned, it is destroyed.
1416 */
1417static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1418{
1419 if (!h2s->cs) {
1420 /* this stream was already orphaned */
1421 h2s_destroy(h2s);
1422 return;
1423 }
1424
1425 h2s->cs->flags |= flags;
1426 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1427 h2s->cs->flags |= CS_FL_ERROR;
1428
1429 h2s_alert(h2s);
1430
1431 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1432 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001433 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001434 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001435 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001436 h2s_close(h2s);
1437}
1438
1439/* wake the streams attached to the connection, whose id is greater than <last>
1440 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001441 */
1442static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1443{
1444 struct eb32_node *node;
1445 struct h2s *h2s;
1446
1447 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001448 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001449
1450 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001451 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001452
Christopher Fauletf02ca002019-03-07 16:21:34 +01001453 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001454 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1455 while (node) {
1456 h2s = container_of(node, struct h2s, by_id);
1457 if (h2s->id <= last)
1458 break;
1459 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001460 h2s_wake_one_stream(h2s, flags);
1461 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001462
Christopher Fauletf02ca002019-03-07 16:21:34 +01001463 /* Wake all streams with unassigned ID (ID == 0) */
1464 node = eb32_lookup(&h2c->streams_by_id, 0);
1465 while (node) {
1466 h2s = container_of(node, struct h2s, by_id);
1467 if (h2s->id > 0)
1468 break;
1469 node = eb32_next(node);
1470 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001471 }
1472}
1473
Willy Tarreau3421aba2017-07-27 15:41:03 +02001474/* Increase all streams' outgoing window size by the difference passed in
1475 * argument. This is needed upon receipt of the settings frame if the initial
1476 * window size is different. The difference may be negative and the resulting
1477 * window size as well, for the time it takes to receive some window updates.
1478 */
1479static void h2c_update_all_ws(struct h2c *h2c, int diff)
1480{
1481 struct h2s *h2s;
1482 struct eb32_node *node;
1483
1484 if (!diff)
1485 return;
1486
1487 node = eb32_first(&h2c->streams_by_id);
1488 while (node) {
1489 h2s = container_of(node, struct h2s, by_id);
1490 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001491
1492 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1493 h2s->flags &= ~H2_SF_BLK_SFCTL;
1494 if (h2s->send_wait)
1495 LIST_ADDQ(&h2c->send_list, &h2s->list);
1496
1497 }
1498
Willy Tarreau3421aba2017-07-27 15:41:03 +02001499 node = eb32_next(node);
1500 }
1501}
1502
1503/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1504 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001505 * return an error in h2c. The caller must have already verified frame length
1506 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001507 */
1508static int h2c_handle_settings(struct h2c *h2c)
1509{
1510 unsigned int offset;
1511 int error;
1512
1513 if (h2c->dff & H2_F_SETTINGS_ACK) {
1514 if (h2c->dfl) {
1515 error = H2_ERR_FRAME_SIZE_ERROR;
1516 goto fail;
1517 }
1518 return 1;
1519 }
1520
Willy Tarreau3421aba2017-07-27 15:41:03 +02001521 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001522 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001523 return 0;
1524
1525 /* parse the frame */
1526 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001527 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1528 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001529
1530 switch (type) {
1531 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1532 /* we need to update all existing streams with the
1533 * difference from the previous iws.
1534 */
1535 if (arg < 0) { // RFC7540#6.5.2
1536 error = H2_ERR_FLOW_CONTROL_ERROR;
1537 goto fail;
1538 }
1539 h2c_update_all_ws(h2c, arg - h2c->miw);
1540 h2c->miw = arg;
1541 break;
1542 case H2_SETTINGS_MAX_FRAME_SIZE:
1543 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1544 error = H2_ERR_PROTOCOL_ERROR;
1545 goto fail;
1546 }
1547 h2c->mfs = arg;
1548 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001549 case H2_SETTINGS_ENABLE_PUSH:
1550 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1551 error = H2_ERR_PROTOCOL_ERROR;
1552 goto fail;
1553 }
1554 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001555 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1556 if (h2c->flags & H2_CF_IS_BACK) {
1557 /* the limit is only for the backend; for the frontend it is our limit */
1558 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1559 arg = h2_settings_max_concurrent_streams;
1560 h2c->streams_limit = arg;
1561 }
1562 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001563 }
1564 }
1565
1566 /* need to ACK this frame now */
1567 h2c->st0 = H2_CS_FRAME_A;
1568 return 1;
1569 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001570 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001571 h2c_error(h2c, error);
1572 return 0;
1573}
1574
1575/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1576 * success or one of the h2_status values.
1577 */
1578static int h2c_ack_settings(struct h2c *h2c)
1579{
1580 struct buffer *res;
1581 char str[9];
1582 int ret = -1;
1583
1584 if (h2c_mux_busy(h2c, NULL)) {
1585 h2c->flags |= H2_CF_DEM_MBUSY;
1586 return 0;
1587 }
1588
Willy Tarreau44e973f2018-03-01 17:49:30 +01001589 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001590 if (!res) {
1591 h2c->flags |= H2_CF_MUX_MALLOC;
1592 h2c->flags |= H2_CF_DEM_MROOM;
1593 return 0;
1594 }
1595
1596 memcpy(str,
1597 "\x00\x00\x00" /* length : 0 (no data) */
1598 "\x04" "\x01" /* type : 4, flags : ACK */
1599 "\x00\x00\x00\x00" /* stream ID */, 9);
1600
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001601 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001602 if (unlikely(ret <= 0)) {
1603 if (!ret) {
1604 h2c->flags |= H2_CF_MUX_MFULL;
1605 h2c->flags |= H2_CF_DEM_MROOM;
1606 return 0;
1607 }
1608 else {
1609 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1610 return 0;
1611 }
1612 }
1613 return ret;
1614}
1615
Willy Tarreaucf68c782017-10-10 17:11:41 +02001616/* processes a PING frame and schedules an ACK if needed. The caller must pass
1617 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001618 * missing data. The caller must have already verified frame length
1619 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001620 */
1621static int h2c_handle_ping(struct h2c *h2c)
1622{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001623 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001624 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001625 h2c->st0 = H2_CS_FRAME_A;
1626 return 1;
1627}
1628
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001629/* Try to send a window update for stream id <sid> and value <increment>.
1630 * Returns > 0 on success or zero on missing room or failure. It may return an
1631 * error in h2c.
1632 */
1633static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1634{
1635 struct buffer *res;
1636 char str[13];
1637 int ret = -1;
1638
1639 if (h2c_mux_busy(h2c, NULL)) {
1640 h2c->flags |= H2_CF_DEM_MBUSY;
1641 return 0;
1642 }
1643
Willy Tarreau44e973f2018-03-01 17:49:30 +01001644 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001645 if (!res) {
1646 h2c->flags |= H2_CF_MUX_MALLOC;
1647 h2c->flags |= H2_CF_DEM_MROOM;
1648 return 0;
1649 }
1650
1651 /* length: 4, type: 8, flags: none */
1652 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1653 write_n32(str + 5, sid);
1654 write_n32(str + 9, increment);
1655
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001656 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001657
1658 if (unlikely(ret <= 0)) {
1659 if (!ret) {
1660 h2c->flags |= H2_CF_MUX_MFULL;
1661 h2c->flags |= H2_CF_DEM_MROOM;
1662 return 0;
1663 }
1664 else {
1665 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1666 return 0;
1667 }
1668 }
1669 return ret;
1670}
1671
1672/* try to send pending window update for the connection. It's safe to call it
1673 * with no pending updates. Returns > 0 on success or zero on missing room or
1674 * failure. It may return an error in h2c.
1675 */
1676static int h2c_send_conn_wu(struct h2c *h2c)
1677{
1678 int ret = 1;
1679
1680 if (h2c->rcvd_c <= 0)
1681 return 1;
1682
Willy Tarreau97aaa672018-12-23 09:49:04 +01001683 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1684 /* increase the advertised connection window to 2G on
1685 * first update.
1686 */
1687 h2c->flags |= H2_CF_WINDOW_OPENED;
1688 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1689 }
1690
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001691 /* send WU for the connection */
1692 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1693 if (ret > 0)
1694 h2c->rcvd_c = 0;
1695
1696 return ret;
1697}
1698
1699/* try to send pending window update for the current dmux stream. It's safe to
1700 * call it with no pending updates. Returns > 0 on success or zero on missing
1701 * room or failure. It may return an error in h2c.
1702 */
1703static int h2c_send_strm_wu(struct h2c *h2c)
1704{
1705 int ret = 1;
1706
1707 if (h2c->rcvd_s <= 0)
1708 return 1;
1709
1710 /* send WU for the stream */
1711 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1712 if (ret > 0)
1713 h2c->rcvd_s = 0;
1714
1715 return ret;
1716}
1717
Willy Tarreaucf68c782017-10-10 17:11:41 +02001718/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1719 * success, 0 on missing data or one of the h2_status values.
1720 */
1721static int h2c_ack_ping(struct h2c *h2c)
1722{
1723 struct buffer *res;
1724 char str[17];
1725 int ret = -1;
1726
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001727 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001728 return 0;
1729
1730 if (h2c_mux_busy(h2c, NULL)) {
1731 h2c->flags |= H2_CF_DEM_MBUSY;
1732 return 0;
1733 }
1734
Willy Tarreau44e973f2018-03-01 17:49:30 +01001735 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001736 if (!res) {
1737 h2c->flags |= H2_CF_MUX_MALLOC;
1738 h2c->flags |= H2_CF_DEM_MROOM;
1739 return 0;
1740 }
1741
1742 memcpy(str,
1743 "\x00\x00\x08" /* length : 8 (same payload) */
1744 "\x06" "\x01" /* type : 6, flags : ACK */
1745 "\x00\x00\x00\x00" /* stream ID */, 9);
1746
1747 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001748 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001749
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001750 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001751 if (unlikely(ret <= 0)) {
1752 if (!ret) {
1753 h2c->flags |= H2_CF_MUX_MFULL;
1754 h2c->flags |= H2_CF_DEM_MROOM;
1755 return 0;
1756 }
1757 else {
1758 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1759 return 0;
1760 }
1761 }
1762 return ret;
1763}
1764
Willy Tarreau26f95952017-07-27 17:18:30 +02001765/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1766 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001767 * h2c or h2s. The caller must have already verified frame length and stream ID
1768 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001769 */
1770static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1771{
1772 int32_t inc;
1773 int error;
1774
Willy Tarreau26f95952017-07-27 17:18:30 +02001775 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001776 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001777 return 0;
1778
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001779 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001780
1781 if (h2c->dsi != 0) {
1782 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001783
1784 /* it's not an error to receive WU on a closed stream */
1785 if (h2s->st == H2_SS_CLOSED)
1786 return 1;
1787
1788 if (!inc) {
1789 error = H2_ERR_PROTOCOL_ERROR;
1790 goto strm_err;
1791 }
1792
1793 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1794 error = H2_ERR_FLOW_CONTROL_ERROR;
1795 goto strm_err;
1796 }
1797
1798 h2s->mws += inc;
1799 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1800 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001801 if (h2s->send_wait)
1802 LIST_ADDQ(&h2c->send_list, &h2s->list);
1803
Willy Tarreau26f95952017-07-27 17:18:30 +02001804 }
1805 }
1806 else {
1807 /* connection window update */
1808 if (!inc) {
1809 error = H2_ERR_PROTOCOL_ERROR;
1810 goto conn_err;
1811 }
1812
1813 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1814 error = H2_ERR_FLOW_CONTROL_ERROR;
1815 goto conn_err;
1816 }
1817
1818 h2c->mws += inc;
1819 }
1820
1821 return 1;
1822
1823 conn_err:
1824 h2c_error(h2c, error);
1825 return 0;
1826
1827 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001828 h2s_error(h2s, error);
1829 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001830 return 0;
1831}
1832
Willy Tarreaue96b0922017-10-30 00:28:29 +01001833/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001834 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1835 * have already verified frame length and stream ID validity. Described in
1836 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001837 */
1838static int h2c_handle_goaway(struct h2c *h2c)
1839{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001840 int last;
1841
Willy Tarreaue96b0922017-10-30 00:28:29 +01001842 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001843 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001844 return 0;
1845
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001846 last = h2_get_n32(&h2c->dbuf, 0);
1847 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001848 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001849 if (h2c->last_sid < 0)
1850 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001851 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001852}
1853
Willy Tarreau92153fc2017-12-03 19:46:19 +01001854/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001855 * invalid. Returns > 0 on success or zero on missing data. It may return an
1856 * error in h2c. The caller must have already verified frame length and stream
1857 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001858 */
1859static int h2c_handle_priority(struct h2c *h2c)
1860{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001861 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001862 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001863 return 0;
1864
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001865 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001866 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001867 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1868 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001869 }
1870 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001871}
1872
Willy Tarreaucd234e92017-08-18 10:59:39 +02001873/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001874 * Returns > 0 on success or zero on missing data. The caller must have already
1875 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001876 */
1877static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1878{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001879 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001880 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001881 return 0;
1882
1883 /* late RST, already handled */
1884 if (h2s->st == H2_SS_CLOSED)
1885 return 1;
1886
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001887 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001888 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001889
1890 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001891 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001892 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001893 }
1894
1895 h2s->flags |= H2_SF_RST_RCVD;
1896 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001897}
1898
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001899/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1900 * It may return an error in h2c or h2s. The caller must consider that the
1901 * return value is the new h2s in case one was allocated (most common case).
1902 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001903 * errors here are reported as connection errors since it's impossible to
1904 * recover from such errors after the compression context has been altered.
1905 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001906static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001907{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001908 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001909 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001910 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001911 int error;
1912
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001913 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001914 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001915
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001916 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001917 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001918
1919 /* now either the frame is complete or the buffer is complete */
1920 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001921 /* The stream exists/existed, this must be a trailers frame */
1922 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001923 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001924 goto out;
1925 goto done;
1926 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001927 /* the connection was already killed by an RST, let's consume
1928 * the data and send another RST.
1929 */
1930 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1931 h2s = (struct h2s*)h2_error_stream;
1932 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001933 }
1934 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1935 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1936 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001937 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001938 goto conn_err;
1939 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001940 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1941 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001942
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001943 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001944
Willy Tarreau25919232019-01-03 14:48:18 +01001945 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001946 if (h2c->st0 >= H2_CS_ERROR)
1947 goto out;
1948
Willy Tarreau25919232019-01-03 14:48:18 +01001949 if (error <= 0) {
1950 if (error == 0)
1951 goto out; // missing data
1952
1953 /* Failed to decode this stream (e.g. too large request)
1954 * but the HPACK decompressor is still synchronized.
1955 */
1956 h2s = (struct h2s*)h2_error_stream;
1957 goto send_rst;
1958 }
1959
Willy Tarreau22de8d32018-09-05 19:55:58 +02001960 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001961 * positively from h2c_frt_stream_new(), the stream will report the error,
1962 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001963 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001964 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001965 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001966 h2s = (struct h2s*)h2_refused_stream;
1967 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001968 }
1969
1970 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001971 h2s->rxbuf = rxbuf;
1972 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001973 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001974
Willy Tarreau88d138e2019-01-02 19:38:14 +01001975 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001976 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001977 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001978
1979 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001980 if (h2s->cs)
1981 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01001982 if (h2s->st == H2_SS_OPEN)
1983 h2s->st = H2_SS_HREM;
1984 else
1985 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02001986 }
1987
Willy Tarreau3a429f02019-01-03 11:41:50 +01001988 /* update the max stream ID if the request is being processed */
1989 if (h2s->id > h2c->max_id)
1990 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001991
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001992 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001993
1994 conn_err:
1995 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001996 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001997
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001998 out:
1999 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002000 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002001
2002 send_rst:
2003 /* make the demux send an RST for the current stream. We may only
2004 * do this if we're certain that the HEADERS frame was properly
2005 * decompressed so that the HPACK decoder is still kept up to date.
2006 */
2007 h2_release_buf(h2c, &rxbuf);
2008 h2c->st0 = H2_CS_FRAME_E;
2009 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002010}
2011
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002012/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2013 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2014 * errors here are reported as connection errors since it's impossible to
2015 * recover from such errors after the compression context has been altered.
2016 */
2017static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2018{
2019 int error;
2020
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002021 if (!b_size(&h2c->dbuf))
2022 return NULL; // empty buffer
2023
2024 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2025 return NULL; // incomplete frame
2026
Willy Tarreau1915ca22019-01-24 11:49:37 +01002027 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002028
Willy Tarreau25919232019-01-03 14:48:18 +01002029 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002030 if (h2c->st0 >= H2_CS_ERROR)
2031 return NULL;
2032
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002033 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2034 /* RFC7540#5.1 */
2035 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2036 h2c->st0 = H2_CS_FRAME_E;
2037 return NULL;
2038 }
2039
Willy Tarreau25919232019-01-03 14:48:18 +01002040 if (error <= 0) {
2041 if (error == 0)
2042 return NULL; // missing data
2043
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002044 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002045 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002046 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002047 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002048 }
2049
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002050 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2051 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002052 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002053 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002054 }
2055
Willy Tarreau927b88b2019-03-04 08:03:25 +01002056 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002057 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002058 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 +02002059 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01002060 else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002061 h2s_close(h2s);
2062
2063 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002064}
2065
Willy Tarreau454f9052017-10-26 19:40:35 +02002066/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2067 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2068 */
2069static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2070{
2071 int error;
2072
2073 /* note that empty DATA frames are perfectly valid and sometimes used
2074 * to signal an end of stream (with the ES flag).
2075 */
2076
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002077 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002078 return 0; // empty buffer
2079
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002080 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002081 return 0; // incomplete frame
2082
2083 /* now either the frame is complete or the buffer is complete */
2084
Willy Tarreau454f9052017-10-26 19:40:35 +02002085 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2086 /* RFC7540#6.1 */
2087 error = H2_ERR_STREAM_CLOSED;
2088 goto strm_err;
2089 }
2090
Willy Tarreau1915ca22019-01-24 11:49:37 +01002091 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2092 /* RFC7540#8.1.2 */
2093 error = H2_ERR_PROTOCOL_ERROR;
2094 goto strm_err;
2095 }
2096
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002097 if (!h2_frt_transfer_data(h2s))
2098 return 0;
2099
Willy Tarreau454f9052017-10-26 19:40:35 +02002100 /* call the upper layers to process the frame, then let the upper layer
2101 * notify the stream about any change.
2102 */
2103 if (!h2s->cs) {
2104 error = H2_ERR_STREAM_CLOSED;
2105 goto strm_err;
2106 }
2107
Willy Tarreau8f650c32017-11-21 19:36:21 +01002108 if (h2c->st0 >= H2_CS_ERROR)
2109 return 0;
2110
Willy Tarreau721c9742017-11-07 11:05:42 +01002111 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002112 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002113 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002114 }
2115
2116 /* check for completion : the callee will change this to FRAME_A or
2117 * FRAME_H once done.
2118 */
2119 if (h2c->st0 == H2_CS_FRAME_P)
2120 return 0;
2121
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002122 /* last frame */
2123 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002124 if (h2s->cs)
2125 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002126 if (h2s->st == H2_SS_OPEN)
2127 h2s->st = H2_SS_HREM;
2128 else
2129 h2s_close(h2s);
2130
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002131 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002132
2133 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2134 /* RFC7540#8.1.2 */
2135 error = H2_ERR_PROTOCOL_ERROR;
2136 goto strm_err;
2137 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002138 }
2139
Willy Tarreau454f9052017-10-26 19:40:35 +02002140 return 1;
2141
Willy Tarreau454f9052017-10-26 19:40:35 +02002142 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002143 h2s_error(h2s, error);
2144 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002145 return 0;
2146}
2147
Willy Tarreaubc933932017-10-09 16:21:43 +02002148/* process Rx frames to be demultiplexed */
2149static void h2_process_demux(struct h2c *h2c)
2150{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002151 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002152 struct h2_fh hdr;
2153 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002154
Willy Tarreau081d4722017-05-16 21:51:05 +02002155 if (h2c->st0 >= H2_CS_ERROR)
2156 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002157
2158 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2159 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002160 if (h2c->flags & H2_CF_IS_BACK)
2161 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002162 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2163 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002164 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002165 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002166 sess_log(h2c->conn->owner);
2167 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002168 goto fail;
2169 }
2170
2171 h2c->max_id = 0;
2172 h2c->st0 = H2_CS_SETTINGS1;
2173 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002174
2175 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002176 /* ensure that what is pending is a valid SETTINGS frame
2177 * without an ACK.
2178 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002179 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002180 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002181 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002182 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002183 sess_log(h2c->conn->owner);
2184 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002185 goto fail;
2186 }
2187
2188 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2189 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2190 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2191 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002192 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002193 goto fail;
2194 }
2195
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002196 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002197 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2198 h2c_error(h2c, H2_ERR_FRAME_SIZE_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 Tarreau3bf69182018-12-21 15:34:50 +01002204 /* that's OK, switch to FRAME_P to process it. This is
2205 * a SETTINGS frame whose header has already been
2206 * deleted above.
2207 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002208 padlen = 0;
2209 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002210 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002211 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002212
2213 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002214 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002215 int ret = 0;
2216
2217 if (h2c->st0 >= H2_CS_ERROR)
2218 break;
2219
2220 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002221 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002222 break;
2223
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002224 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002225 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002226 if (!h2c->nb_streams) {
2227 /* only log if no other stream can report the error */
2228 sess_log(h2c->conn->owner);
2229 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002230 break;
2231 }
2232
Willy Tarreau3bf69182018-12-21 15:34:50 +01002233 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2234 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2235 * we read the pad length and drop it from the remaining
2236 * payload (one byte + the 9 remaining ones = 10 total
2237 * removed), so we have a frame payload starting after the
2238 * pad len. Flow controlled frames (DATA) also count the
2239 * padlen in the flow control, so it must be adjusted.
2240 */
2241 if (hdr.len < 1) {
2242 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2243 sess_log(h2c->conn->owner);
2244 goto fail;
2245 }
2246 hdr.len--;
2247
2248 if (b_data(&h2c->dbuf) < 10)
2249 break; // missing padlen
2250
2251 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2252
2253 if (padlen > hdr.len) {
2254 /* RFC7540#6.1 : pad length = length of
2255 * frame payload or greater => error.
2256 */
2257 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2258 sess_log(h2c->conn->owner);
2259 goto fail;
2260 }
2261
2262 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2263 h2c->rcvd_c++;
2264 h2c->rcvd_s++;
2265 }
2266 b_del(&h2c->dbuf, 1);
2267 }
2268 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002269
2270 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002271 h2c->dfl = hdr.len;
2272 h2c->dsi = hdr.sid;
2273 h2c->dft = hdr.ft;
2274 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002275 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002276 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002277
2278 /* check for minimum basic frame format validity */
2279 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2280 if (ret != H2_ERR_NO_ERROR) {
2281 h2c_error(h2c, ret);
2282 sess_log(h2c->conn->owner);
2283 goto fail;
2284 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002285 }
2286
2287 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002288 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2289
Willy Tarreau567beb82018-12-18 16:52:44 +01002290 if (tmp_h2s != h2s && h2s && h2s->cs &&
2291 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002292 (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 +01002293 /* we may have to signal the upper layers */
2294 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002295 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002296 }
2297 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002298
Willy Tarreaud7901432017-12-29 11:34:40 +01002299 if (h2c->st0 == H2_CS_FRAME_E)
2300 goto strm_err;
2301
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002302 if (h2s->st == H2_SS_IDLE &&
2303 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2304 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2305 * this state MUST be treated as a connection error
2306 */
2307 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002308 if (!h2c->nb_streams) {
2309 /* only log if no other stream can report the error */
2310 sess_log(h2c->conn->owner);
2311 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002312 break;
2313 }
2314
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002315 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2316 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2317 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002318 * this state MUST be treated as a stream error.
2319 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2320 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002321 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002322 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2323 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2324 else
2325 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002326 goto strm_err;
2327 }
2328
Willy Tarreauab837502017-12-27 15:07:30 +01002329 /* Below the management of frames received in closed state is a
2330 * bit hackish because the spec makes strong differences between
2331 * streams closed by receiving RST, sending RST, and seeing ES
2332 * in both directions. In addition to this, the creation of a
2333 * new stream reusing the identifier of a closed one will be
2334 * detected here. Given that we cannot keep track of all closed
2335 * streams forever, we consider that unknown closed streams were
2336 * closed on RST received, which allows us to respond with an
2337 * RST without breaking the connection (eg: to abort a transfer).
2338 * Some frames have to be silently ignored as well.
2339 */
2340 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002341 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002342 /* #5.1.1: The identifier of a newly
2343 * established stream MUST be numerically
2344 * greater than all streams that the initiating
2345 * endpoint has opened or reserved. This
2346 * governs streams that are opened using a
2347 * HEADERS frame and streams that are reserved
2348 * using PUSH_PROMISE. An endpoint that
2349 * receives an unexpected stream identifier
2350 * MUST respond with a connection error.
2351 */
2352 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2353 goto strm_err;
2354 }
2355
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002356 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002357 /* RFC7540#5.1:closed: an endpoint that
2358 * receives any frame other than PRIORITY after
2359 * receiving a RST_STREAM MUST treat that as a
2360 * stream error of type STREAM_CLOSED.
2361 *
2362 * Note that old streams fall into this category
2363 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002364 *
2365 * However, we cannot generalize this to all frame types. Those
2366 * carrying compression state must still be processed before
2367 * being dropped or we'll desynchronize the decoder. This can
2368 * happen with request trailers received after sending an
2369 * RST_STREAM, or with header/trailers responses received after
2370 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002371 */
2372 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2373 h2c->st0 = H2_CS_FRAME_E;
2374 goto strm_err;
2375 }
2376
2377 /* RFC7540#5.1:closed: if this state is reached as a
2378 * result of sending a RST_STREAM frame, the peer that
2379 * receives the RST_STREAM might have already sent
2380 * frames on the stream that cannot be withdrawn. An
2381 * endpoint MUST ignore frames that it receives on
2382 * closed streams after it has sent a RST_STREAM
2383 * frame. An endpoint MAY choose to limit the period
2384 * over which it ignores frames and treat frames that
2385 * arrive after this time as being in error.
2386 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002387 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002388 /* RFC7540#5.1:closed: any frame other than
2389 * PRIO/WU/RST in this state MUST be treated as
2390 * a connection error
2391 */
2392 if (h2c->dft != H2_FT_RST_STREAM &&
2393 h2c->dft != H2_FT_PRIORITY &&
2394 h2c->dft != H2_FT_WINDOW_UPDATE) {
2395 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2396 goto strm_err;
2397 }
2398 }
2399 }
2400
Willy Tarreauc0da1962017-10-30 18:38:00 +01002401#if 0
2402 // problem below: it is not possible to completely ignore such
2403 // streams as we need to maintain the compression state as well
2404 // and for this we need to completely process these frames (eg:
2405 // HEADERS frames) as well as counting DATA frames to emit
2406 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2407 // This is a typical case of layer violation where the
2408 // transported contents are critical to the connection's
2409 // validity and must be ignored at the same time :-(
2410
2411 /* graceful shutdown, ignore streams whose ID is higher than
2412 * the one advertised in GOAWAY. RFC7540#6.8.
2413 */
2414 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002415 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2416 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002417 h2c->dfl -= ret;
2418 ret = h2c->dfl == 0;
2419 goto strm_err;
2420 }
2421#endif
2422
Willy Tarreau7e98c052017-10-10 15:56:59 +02002423 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002424 case H2_FT_SETTINGS:
2425 if (h2c->st0 == H2_CS_FRAME_P)
2426 ret = h2c_handle_settings(h2c);
2427
2428 if (h2c->st0 == H2_CS_FRAME_A)
2429 ret = h2c_ack_settings(h2c);
2430 break;
2431
Willy Tarreaucf68c782017-10-10 17:11:41 +02002432 case H2_FT_PING:
2433 if (h2c->st0 == H2_CS_FRAME_P)
2434 ret = h2c_handle_ping(h2c);
2435
2436 if (h2c->st0 == H2_CS_FRAME_A)
2437 ret = h2c_ack_ping(h2c);
2438 break;
2439
Willy Tarreau26f95952017-07-27 17:18:30 +02002440 case H2_FT_WINDOW_UPDATE:
2441 if (h2c->st0 == H2_CS_FRAME_P)
2442 ret = h2c_handle_window_update(h2c, h2s);
2443 break;
2444
Willy Tarreau61290ec2017-10-17 08:19:21 +02002445 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002446 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2447 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2448 * frames' parsers consume all following CONTINUATION
2449 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002450 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002451 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2452 sess_log(h2c->conn->owner);
2453 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002454
Willy Tarreau13278b42017-10-13 19:23:14 +02002455 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002456 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002457 if (h2c->flags & H2_CF_IS_BACK)
2458 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2459 else
2460 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002461 if (tmp_h2s) {
2462 h2s = tmp_h2s;
2463 ret = 1;
2464 }
2465 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002466 break;
2467
Willy Tarreau454f9052017-10-26 19:40:35 +02002468 case H2_FT_DATA:
2469 if (h2c->st0 == H2_CS_FRAME_P)
2470 ret = h2c_frt_handle_data(h2c, h2s);
2471
2472 if (h2c->st0 == H2_CS_FRAME_A)
2473 ret = h2c_send_strm_wu(h2c);
2474 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002475
Willy Tarreau92153fc2017-12-03 19:46:19 +01002476 case H2_FT_PRIORITY:
2477 if (h2c->st0 == H2_CS_FRAME_P)
2478 ret = h2c_handle_priority(h2c);
2479 break;
2480
Willy Tarreaucd234e92017-08-18 10:59:39 +02002481 case H2_FT_RST_STREAM:
2482 if (h2c->st0 == H2_CS_FRAME_P)
2483 ret = h2c_handle_rst_stream(h2c, h2s);
2484 break;
2485
Willy Tarreaue96b0922017-10-30 00:28:29 +01002486 case H2_FT_GOAWAY:
2487 if (h2c->st0 == H2_CS_FRAME_P)
2488 ret = h2c_handle_goaway(h2c);
2489 break;
2490
Willy Tarreau1c661982017-10-30 13:52:01 +01002491 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002492 default:
2493 /* drop frames that we ignore. They may be larger than
2494 * the buffer so we drain all of their contents until
2495 * we reach the end.
2496 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002497 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2498 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002499 h2c->dfl -= ret;
2500 ret = h2c->dfl == 0;
2501 }
2502
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002503 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002504 /* We may have to send an RST if not done yet */
2505 if (h2s->st == H2_SS_ERROR)
2506 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002507
Willy Tarreaua20a5192017-12-27 11:02:06 +01002508 if (h2c->st0 == H2_CS_FRAME_E)
2509 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002510
Willy Tarreau7e98c052017-10-10 15:56:59 +02002511 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002512 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002513 break;
2514
2515 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002516 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002517 h2c->st0 = H2_CS_FRAME_H;
2518 }
2519 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002520
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002521 if (h2c->rcvd_c > 0 &&
2522 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2523 h2c_send_conn_wu(h2c);
2524
Willy Tarreau52eed752017-09-22 15:05:09 +02002525 fail:
2526 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002527 if (h2s && h2s->cs &&
2528 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002529 (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 +01002530 /* we may have to signal the upper layers */
2531 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002532 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002533 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002534
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002535 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002536}
2537
2538/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2539 * the end.
2540 */
2541static int h2_process_mux(struct h2c *h2c)
2542{
Olivier Houchardd360ac62019-03-22 17:37:16 +01002543 struct h2s *h2s;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002544
Willy Tarreau01b44822018-10-03 14:26:37 +02002545 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2546 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2547 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2548 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2549 if (h2c->st0 == H2_CS_ERROR) {
2550 h2c->st0 = H2_CS_ERROR2;
2551 sess_log(h2c->conn->owner);
2552 }
2553 goto fail;
2554 }
2555 h2c->st0 = H2_CS_SETTINGS1;
2556 }
2557 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002558 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002559 return 1;
2560 }
2561
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002562 /* start by sending possibly pending window updates */
2563 if (h2c->rcvd_c > 0 &&
2564 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2565 h2c_send_conn_wu(h2c) < 0)
2566 goto fail;
2567
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002568 /* First we always process the flow control list because the streams
2569 * waiting there were already elected for immediate emission but were
2570 * blocked just on this.
2571 */
2572
Olivier Houchardd360ac62019-03-22 17:37:16 +01002573 list_for_each_entry(h2s, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002574 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2575 h2c->st0 >= H2_CS_ERROR)
2576 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002577 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2578 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002579
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002580 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002581 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2582 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002583 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002584 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002585 }
2586
Olivier Houchardd360ac62019-03-22 17:37:16 +01002587 list_for_each_entry(h2s, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002588 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2589 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002590 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2591 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002592
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002593 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002594 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2595 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002596 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002597 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002598 }
2599
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002600 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002601 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002602 if (h2c->st0 == H2_CS_ERROR) {
2603 if (h2c->max_id >= 0) {
2604 h2c_send_goaway_error(h2c, NULL);
2605 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2606 return 0;
2607 }
2608
2609 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2610 }
2611 return 1;
2612 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002613 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002614}
2615
Willy Tarreau62f52692017-10-08 23:01:42 +02002616
Willy Tarreau479998a2018-11-18 06:30:59 +01002617/* Attempt to read data, and subscribe if none available.
2618 * The function returns 1 if data has been received, otherwise zero.
2619 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002620static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002621{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002622 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002623 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002624 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002625 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002626
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002627 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002628 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002629
Willy Tarreau315d8072017-12-10 22:17:57 +01002630 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002631 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002632
Willy Tarreau44e973f2018-03-01 17:49:30 +01002633 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002634 if (!buf) {
2635 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002636 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002637 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002638
Olivier Houchard7505f942018-08-21 18:10:44 +02002639 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002640 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002641 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2642 /* HTX in use : try to pre-align the buffer like the
2643 * rxbufs will be to optimize memory copies. We'll make
2644 * sure that the frame header lands at the end of the
2645 * HTX block to alias it upon recv. We cannot use the
2646 * head because rcv_buf() will realign the buffer if
2647 * it's empty. Thus we cheat and pretend we already
2648 * have a few bytes there.
2649 */
2650 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002651 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002652 }
2653 else
2654 max = b_room(buf);
2655
Olivier Houchard7505f942018-08-21 18:10:44 +02002656 if (max)
2657 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2658 else
2659 ret = 0;
2660 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002661
Olivier Houchard53216e72018-10-10 15:46:36 +02002662 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002663 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002664
Olivier Houcharda1411e62018-08-17 18:42:48 +02002665 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002666 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002667 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002668 }
2669
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002670 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002671 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002672 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002673}
2674
Willy Tarreau479998a2018-11-18 06:30:59 +01002675/* Try to send data if possible.
2676 * The function returns 1 if data have been sent, otherwise zero.
2677 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002678static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002679{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002680 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002681 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002682 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002683
2684 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002685 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002686
Olivier Houchard7505f942018-08-21 18:10:44 +02002687
Willy Tarreaua2af5122017-10-09 11:56:46 +02002688 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2689 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002690 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002691 }
2692
Willy Tarreaubc933932017-10-09 16:21:43 +02002693 /* This loop is quite simple : it tries to fill as much as it can from
2694 * pending streams into the existing buffer until it's reportedly full
2695 * or the end of send requests is reached. Then it tries to send this
2696 * buffer's contents out, marks it not full if at least one byte could
2697 * be sent, and tries again.
2698 *
2699 * The snd_buf() function normally takes a "flags" argument which may
2700 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2701 * data immediately comes and CO_SFL_STREAMER to indicate that the
2702 * connection is streaming lots of data (used to increase TLS record
2703 * size at the expense of latency). The former can be sent any time
2704 * there's a buffer full flag, as it indicates at least one stream
2705 * attempted to send and failed so there are pending data. An
2706 * alternative would be to set it as long as there's an active stream
2707 * but that would be problematic for ACKs until we have an absolute
2708 * guarantee that all waiters have at least one byte to send. The
2709 * latter should possibly not be set for now.
2710 */
2711
2712 done = 0;
2713 while (!done) {
2714 unsigned int flags = 0;
2715
2716 /* fill as much as we can into the current buffer */
2717 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2718 done = h2_process_mux(h2c);
2719
Olivier Houchard2b094432019-01-29 18:28:36 +01002720 if (h2c->flags & H2_CF_MUX_MALLOC)
2721 break;
2722
Willy Tarreaubc933932017-10-09 16:21:43 +02002723 if (conn->flags & CO_FL_ERROR)
2724 break;
2725
2726 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2727 flags |= CO_SFL_MSG_MORE;
2728
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002729 if (b_data(&h2c->mbuf)) {
2730 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002731 if (!ret)
2732 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002733 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002734 b_del(&h2c->mbuf, ret);
2735 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002736 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002737
2738 /* wrote at least one byte, the buffer is not full anymore */
2739 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2740 }
2741
Willy Tarreaua2af5122017-10-09 11:56:46 +02002742 if (conn->flags & CO_FL_SOCK_WR_SH) {
2743 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002744 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002745 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002746 /* We're not full anymore, so we can wake any task that are waiting
2747 * for us.
2748 */
2749 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002750 struct h2s *h2s;
2751
2752 list_for_each_entry(h2s, &h2c->send_list, list) {
2753 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2754 break;
2755 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2756 continue;
2757
2758 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002759 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2760 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002761 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002762 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002763 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002764 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002765 /* We're done, no more to send */
2766 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002767 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002768schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002769 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2770 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002771 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002772}
2773
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002774/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002775static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2776{
2777 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002778 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002779
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002780 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002781 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002782 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002783 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002784 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002785 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002786 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002787}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002788
Willy Tarreau62f52692017-10-08 23:01:42 +02002789/* callback called on any event by the connection handler.
2790 * It applies changes and returns zero, or < 0 if it wants immediate
2791 * destruction of the connection (which normally doesn not happen in h2).
2792 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002793static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002794{
Olivier Houchard7505f942018-08-21 18:10:44 +02002795 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002796
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002797 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002798 h2_process_demux(h2c);
2799
2800 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002801 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002802
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002803 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002804 h2c->flags &= ~H2_CF_DEM_DFULL;
2805 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002806 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002807
Willy Tarreau0b37d652018-10-03 10:33:02 +02002808 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002809 /* frontend is stopping, reload likely in progress, let's try
2810 * to announce a graceful shutdown if not yet done. We don't
2811 * care if it fails, it will be tried again later.
2812 */
2813 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2814 if (h2c->last_sid < 0)
2815 h2c->last_sid = (1U << 31) - 1;
2816 h2c_send_goaway_error(h2c, NULL);
2817 }
2818 }
2819
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002820 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002821 * If we received early data, and the handshake is done, wake
2822 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002823 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002824 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2825 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2826 struct eb32_node *node;
2827 struct h2s *h2s;
2828
2829 h2c->flags |= H2_CF_WAIT_FOR_HS;
2830 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2831
2832 while (node) {
2833 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002834 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002835 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002836 node = eb32_next(node);
2837 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002838 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002839
Willy Tarreau26bd7612017-10-09 16:47:04 +02002840 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002841 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2842 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2843 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002844 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002845
2846 if (eb_is_empty(&h2c->streams_by_id)) {
2847 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002848 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002849 return -1;
2850 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002851 }
2852
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002853 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002854 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002855
Olivier Houchard53216e72018-10-10 15:46:36 +02002856 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2857 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2858 (h2c->st0 != H2_CS_ERROR &&
2859 !b_data(&h2c->mbuf) &&
2860 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2861 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002862 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002863
Willy Tarreau3f133572017-10-31 19:21:06 +01002864 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002865 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002866 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002867 task_queue(h2c->task);
2868 }
2869 else
2870 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002871 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002872
Olivier Houchard7505f942018-08-21 18:10:44 +02002873 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002874 return 0;
2875}
2876
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002877/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002878static int h2_wake(struct connection *conn)
2879{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002880 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002881
2882 return (h2_process(h2c));
2883}
2884
Willy Tarreauea392822017-10-31 10:02:25 +01002885/* Connection timeout management. The principle is that if there's no receipt
2886 * nor sending for a certain amount of time, the connection is closed. If the
2887 * MUX buffer still has lying data or is not allocatable, the connection is
2888 * immediately killed. If it's allocatable and empty, we attempt to send a
2889 * GOAWAY frame.
2890 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002891static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002892{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002893 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002894 int expired = tick_is_expired(t->expire, now_ms);
2895
Willy Tarreau0975f112018-03-29 15:22:59 +02002896 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002897 return t;
2898
Willy Tarreau0975f112018-03-29 15:22:59 +02002899 task_delete(t);
2900 task_free(t);
2901
2902 if (!h2c) {
2903 /* resources were already deleted */
2904 return NULL;
2905 }
2906
2907 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002908 h2c_error(h2c, H2_ERR_NO_ERROR);
2909 h2_wake_some_streams(h2c, 0, 0);
2910
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002911 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002912 /* don't even try to send a GOAWAY, the buffer is stuck */
2913 h2c->flags |= H2_CF_GOAWAY_FAILED;
2914 }
2915
2916 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002917 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002918 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2919 h2c->flags |= H2_CF_GOAWAY_FAILED;
2920
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002921 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2922 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002923 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002924 b_del(&h2c->mbuf, ret);
2925 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002926 }
2927 }
Willy Tarreauea392822017-10-31 10:02:25 +01002928
Willy Tarreau0975f112018-03-29 15:22:59 +02002929 /* either we can release everything now or it will be done later once
2930 * the last stream closes.
2931 */
2932 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002933 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002934
Willy Tarreauea392822017-10-31 10:02:25 +01002935 return NULL;
2936}
2937
2938
Willy Tarreau62f52692017-10-08 23:01:42 +02002939/*******************************************/
2940/* functions below are used by the streams */
2941/*******************************************/
2942
2943/*
2944 * Attach a new stream to a connection
2945 * (Used for outgoing connections)
2946 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002947static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002948{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002949 struct conn_stream *cs;
2950 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002951 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002952
2953 cs = cs_new(conn);
2954 if (!cs)
2955 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002956 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002957 if (!h2s) {
2958 cs_free(cs);
2959 return NULL;
2960 }
2961 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002962}
2963
Willy Tarreaufafd3982018-11-18 21:29:20 +01002964/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2965 * We have to scan because we may have some orphan streams. It might be
2966 * beneficial to scan backwards from the end to reduce the likeliness to find
2967 * orphans.
2968 */
2969static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2970{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002971 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002972 struct h2s *h2s;
2973 struct eb32_node *node;
2974
2975 node = eb32_first(&h2c->streams_by_id);
2976 while (node) {
2977 h2s = container_of(node, struct h2s, by_id);
2978 if (h2s->cs)
2979 return h2s->cs;
2980 node = eb32_next(node);
2981 }
2982 return NULL;
2983}
2984
Willy Tarreau62f52692017-10-08 23:01:42 +02002985/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002986 * Destroy the mux and the associated connection, if it is no longer used
2987 */
Christopher Faulet73c12072019-04-08 11:23:22 +02002988static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01002989{
Christopher Faulet73c12072019-04-08 11:23:22 +02002990 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002991
2992 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002993 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01002994}
2995
2996/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002997 * Detach the stream from the connection and possibly release the connection.
2998 */
2999static void h2_detach(struct conn_stream *cs)
3000{
Willy Tarreau60935142017-10-16 18:11:19 +02003001 struct h2s *h2s = cs->ctx;
3002 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003003 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003004
3005 cs->ctx = NULL;
3006 if (!h2s)
3007 return;
3008
Olivier Houchardf502aca2018-12-14 19:42:40 +01003009 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003010 h2c = h2s->h2c;
3011 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003012 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003013 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3014 !h2_frt_has_too_many_cs(h2c)) {
3015 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003016 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003017 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003018 }
Willy Tarreau60935142017-10-16 18:11:19 +02003019
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003020 /* this stream may be blocked waiting for some data to leave (possibly
3021 * an ES or RST frame), so orphan it in this case.
3022 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003023 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003024 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003025 (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 +01003026 return;
3027
Willy Tarreau45f752e2017-10-30 15:44:59 +01003028 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3029 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3030 /* unblock the connection if it was blocked on this
3031 * stream.
3032 */
3033 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3034 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003035 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003036 }
3037
Willy Tarreau71049cc2018-03-28 13:56:39 +02003038 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003039
Olivier Houchard8a786902018-12-15 16:05:40 +01003040 if (h2c->flags & H2_CF_IS_BACK &&
3041 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003042 if (!(h2c->conn->flags &
3043 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3044 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003045 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003046 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3047 h2c->conn->owner = NULL;
3048 if (eb_is_empty(&h2c->streams_by_id)) {
3049 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3050 /* The server doesn't want it, let's kill the connection right away */
3051 h2c->conn->mux->destroy(h2c->conn);
3052 return;
3053 }
3054 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003055 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003056 if (eb_is_empty(&h2c->streams_by_id)) {
3057 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3058 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3059 return;
3060 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003061 /* Never ever allow to reuse a connection from a non-reuse backend */
3062 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3063 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003064 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003065 struct server *srv = objt_server(h2c->conn->target);
3066
3067 if (srv) {
3068 if (h2c->conn->flags & CO_FL_PRIVATE)
3069 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3070 else
3071 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3072 }
3073
3074 }
3075 }
3076 }
3077
Willy Tarreaue323f342018-03-28 13:51:45 +02003078 /* We don't want to close right now unless we're removing the
3079 * last stream, and either the connection is in error, or it
3080 * reached the ID already specified in a GOAWAY frame received
3081 * or sent (as seen by last_sid >= 0).
3082 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003083 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003084 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003085 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003086 }
3087 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003088 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003089 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3090 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003091 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003092 else
3093 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003094 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003095}
3096
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003097/* Performs a synchronous or asynchronous shutr().
3098 * FIXME: guess what the return code tries to indicate!
3099 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003100static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003101{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003102 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003103 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003104
Willy Tarreau721c9742017-11-07 11:05:42 +01003105 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003106 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003107
Willy Tarreau18059042019-01-31 19:12:48 +01003108 /* a connstream may require us to immediately kill the whole connection
3109 * for example because of a "tcp-request content reject" rule that is
3110 * normally used to limit abuse. In this case we schedule a goaway to
3111 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003112 */
Willy Tarreau18059042019-01-31 19:12:48 +01003113 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3114 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3115 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3116 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3117 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003118 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3119 /* Nothing was never sent for this stream, so reset with
3120 * REFUSED_STREAM error to let the client retry the
3121 * request.
3122 */
3123 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3124 }
Willy Tarreau18059042019-01-31 19:12:48 +01003125
Willy Tarreau90c32322017-11-24 08:00:30 +01003126 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003127 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003128 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003129
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003130 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003131 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003132 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003133
Olivier Houchard7a977432019-03-21 15:47:13 +01003134 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003135add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003136 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003137 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003138 if (h2s->flags & H2_SF_BLK_MFCTL) {
3139 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3140 h2s->send_wait = sw;
3141 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3142 h2s->send_wait = sw;
3143 LIST_ADDQ(&h2c->send_list, &h2s->list);
3144 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003145 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003146 /* Let the handler know we want shutr */
3147 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003148 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003149}
3150
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003151/* Performs a synchronous or asynchronous shutw().
3152 * FIXME: guess what the return code tries to indicate!
3153 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003154static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003155{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003156 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003157 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003158
Willy Tarreau721c9742017-11-07 11:05:42 +01003159 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003160 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003161
Willy Tarreau67434202017-11-06 20:20:51 +01003162 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003163 /* we can cleanly close using an empty data frame only after headers */
3164
3165 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3166 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003167 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003168
3169 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003170 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003171 else
3172 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003173 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003174 /* a connstream may require us to immediately kill the whole connection
3175 * for example because of a "tcp-request content reject" rule that is
3176 * normally used to limit abuse. In this case we schedule a goaway to
3177 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003178 */
Willy Tarreau18059042019-01-31 19:12:48 +01003179 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3180 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3181 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3182 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3183 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003184 else {
3185 /* Nothing was never sent for this stream, so reset with
3186 * REFUSED_STREAM error to let the client retry the
3187 * request.
3188 */
3189 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3190 }
Willy Tarreau18059042019-01-31 19:12:48 +01003191
Willy Tarreau90c32322017-11-24 08:00:30 +01003192 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003193 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003194 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003195
Willy Tarreau00dd0782018-03-01 16:31:34 +01003196 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003197 }
3198
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003199 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003200 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003201 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003202
3203 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003204 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003205 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003206 if (h2s->flags & H2_SF_BLK_MFCTL) {
3207 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3208 h2s->send_wait = sw;
3209 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3210 h2s->send_wait = sw;
3211 LIST_ADDQ(&h2c->send_list, &h2s->list);
3212 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003213 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003214 /* let the handler know we want to shutw */
3215 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003216 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003217}
3218
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003219/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3220 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3221 * and prevented the last frame from being emitted.
3222 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003223static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3224{
3225 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003226 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003227 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003228
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003229 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003230 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003231 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003232 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003233 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003234 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003235 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003236 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003237 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003238
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003239 if (!ret) {
3240 /* We're done trying to send, remove ourself from the send_list */
3241 h2s->send_wait->events &= ~SUB_RETRY_SEND;
3242 h2s->send_wait = NULL;
3243 LIST_DEL_INIT(&h2s->list);
3244 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003245 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003246 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003247 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003248 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003249
3250 if (h2c_is_dead(h2c))
Christopher Faulet73c12072019-04-08 11:23:22 +02003251 h2_release(h2c);
Olivier Houchard7a977432019-03-21 15:47:13 +01003252 }
3253
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003254 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003255}
3256
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003257/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003258static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3259{
3260 struct h2s *h2s = cs->ctx;
3261
3262 if (!mode)
3263 return;
3264
3265 h2_do_shutr(h2s);
3266}
3267
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003268/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003269static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3270{
3271 struct h2s *h2s = cs->ctx;
3272
3273 h2_do_shutw(h2s);
3274}
3275
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003276/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003277 * HTX request or response depending on the connection's side. Returns a
3278 * positive value on success, a negative value on failure, or 0 if it couldn't
3279 * proceed. May report connection errors in h2c->errcode if the frame is
3280 * non-decodable and the connection unrecoverable. In absence of connection
3281 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003282 *
3283 * The function may fold CONTINUATION frames into the initial HEADERS frame
3284 * by removing padding and next frame header, then moving the CONTINUATION
3285 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3286 * leaving a hole between the main frame and the beginning of the next one.
3287 * The possibly remaining incomplete or next frame at the end may be moved
3288 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3289 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3290 *
3291 * A buffer at the beginning of processing may look like this :
3292 *
3293 * ,---.---------.-----.--------------.--------------.------.---.
3294 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3295 * `---^---------^-----^--------------^--------------^------^---'
3296 * | | <-----> | |
3297 * area | dpl | wrap
3298 * |<--------------> |
3299 * | dfl |
3300 * |<-------------------------------------------------->|
3301 * head data
3302 *
3303 * Padding is automatically overwritten when folding, participating to the
3304 * hole size after dfl :
3305 *
3306 * ,---.------------------------.-----.--------------.------.---.
3307 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3308 * `---^------------------------^-----^--------------^------^---'
3309 * | | <-----> | |
3310 * area | hole | wrap
3311 * |<-----------------------> |
3312 * | dfl |
3313 * |<-------------------------------------------------->|
3314 * head data
3315 *
3316 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3317 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3318 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003319 *
3320 * The <flags> field must point to either the stream's flags or to a copy of it
3321 * so that the function can update the following flags :
3322 * - H2_SF_DATA_CLEN when content-length is seen
3323 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3324 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003325 *
3326 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3327 * decoding, in order to detect if we're dealing with a headers or a trailers
3328 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003329 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003330static 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 +02003331{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003332 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003333 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003334 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003335 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003336 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003337 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003338 int flen; // header frame len
3339 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003340 int ret = 0;
3341 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003342 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003343 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003344
Willy Tarreauea18f862018-12-22 20:19:26 +01003345next_frame:
3346 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3347 goto leave; // incomplete input frame
3348
3349 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3350 * this case, we'll try to paste it immediately after the initial
3351 * HEADERS frame payload and kill any possible padding. The initial
3352 * frame's length will be increased to represent the concatenation
3353 * of the two frames. The next frame is read from position <tlen>
3354 * and written at position <flen> (minus padding if some is present).
3355 */
3356 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3357 struct h2_fh hdr;
3358 int clen; // CONTINUATION frame's payload length
3359
3360 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3361 /* no more data, the buffer may be full, either due to
3362 * too large a frame or because of too large a hole that
3363 * we're going to compact at the end.
3364 */
3365 goto leave;
3366 }
3367
3368 if (hdr.ft != H2_FT_CONTINUATION) {
3369 /* RFC7540#6.10: frame of unexpected type */
3370 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3371 goto fail;
3372 }
3373
3374 if (hdr.sid != h2c->dsi) {
3375 /* RFC7540#6.10: frame of different stream */
3376 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3377 goto fail;
3378 }
3379
3380 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3381 /* RFC7540#4.2: invalid frame length */
3382 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3383 goto fail;
3384 }
3385
3386 /* detect when we must stop aggragating frames */
3387 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3388
3389 /* Take as much as we can of the CONTINUATION frame's payload */
3390 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3391 if (clen > hdr.len)
3392 clen = hdr.len;
3393
3394 /* Move the frame's payload over the padding, hole and frame
3395 * header. At least one of hole or dpl is null (see diagrams
3396 * above). The hole moves after the new aggragated frame.
3397 */
3398 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3399 h2c->dfl += clen - h2c->dpl;
3400 hole += h2c->dpl + 9;
3401 h2c->dpl = 0;
3402 goto next_frame;
3403 }
3404
3405 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003406
Willy Tarreau13278b42017-10-13 19:23:14 +02003407 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003408 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003409 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003410 copy = alloc_trash_chunk();
3411 if (!copy) {
3412 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3413 goto fail;
3414 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003415 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3416 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3417 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003418 }
3419
Willy Tarreau13278b42017-10-13 19:23:14 +02003420 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3421 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003422 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003423 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3424 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003425 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003426 }
3427
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003428 if (flen < 5) {
3429 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3430 goto fail;
3431 }
3432
Willy Tarreau13278b42017-10-13 19:23:14 +02003433 hdrs += 5; // stream dep = 4, weight = 1
3434 flen -= 5;
3435 }
3436
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003437 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003438 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003439 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003440 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003441
Willy Tarreau937f7602018-02-26 15:22:17 +01003442 /* we can't retry a failed decompression operation so we must be very
3443 * careful not to take any risks. In practice the output buffer is
3444 * always empty except maybe for trailers, in which case we simply have
3445 * to wait for the upper layer to finish consuming what is available.
3446 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003447
3448 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003449 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003450 if (!htx_is_empty(htx)) {
3451 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003452 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003453 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003454 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003455 if (b_data(rxbuf)) {
3456 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003457 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003458 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003459
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003460 rxbuf->head = 0;
3461 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003462 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003463
Willy Tarreau25919232019-01-03 14:48:18 +01003464 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003465 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3466 sizeof(list)/sizeof(list[0]), tmp);
3467 if (outlen < 0) {
3468 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3469 goto fail;
3470 }
3471
Willy Tarreau25919232019-01-03 14:48:18 +01003472 /* The PACK decompressor was updated, let's update the input buffer and
3473 * the parser's state to commit these changes and allow us to later
3474 * fail solely on the stream if needed.
3475 */
3476 b_del(&h2c->dbuf, h2c->dfl + hole);
3477 h2c->dfl = hole = 0;
3478 h2c->st0 = H2_CS_FRAME_H;
3479
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003480 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003481 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003482
Willy Tarreau88d138e2019-01-02 19:38:14 +01003483 if (*flags & H2_SF_HEADERS_RCVD)
3484 goto trailers;
3485
3486 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003487 if (htx) {
3488 /* HTX mode */
3489 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003490 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003491 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003492 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003493 } else {
3494 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003495 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003496 if (outlen > 0)
3497 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003498 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003499
3500 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003501 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003502 goto fail;
3503 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003504
Willy Tarreau174b06a2018-04-25 18:13:58 +02003505 if (msgf & H2_MSGF_BODY) {
3506 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003507 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003508 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003509 if (htx)
3510 htx->extra = *body_len;
3511 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003512 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003513 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003514 }
3515
Willy Tarreau88d138e2019-01-02 19:38:14 +01003516 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003517 /* indicate that a HEADERS frame was received for this stream, except
3518 * for 1xx responses. For 1xx responses, another HEADERS frame is
3519 * expected.
3520 */
3521 if (!(msgf & H2_MSGF_RSP_1XX))
3522 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003523
Christopher Faulet0b465482019-02-19 15:14:23 +01003524 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003525 /* Mark the end of message, either using EOM in HTX or with the
3526 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3527 * is not set during headers with END_STREAM.
3528 */
3529 if (htx) {
3530 if (!htx_add_endof(htx, HTX_BLK_EOM))
3531 goto fail;
3532 }
3533 else if (*flags & H2_SF_DATA_CHNK) {
3534 if (!b_putblk(rxbuf, "\r\n", 2))
3535 goto fail;
3536 }
3537 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003538
Willy Tarreau86277d42019-01-02 15:36:11 +01003539 /* success */
3540 ret = 1;
3541
Willy Tarreau68dd9852017-07-03 14:44:26 +02003542 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003543 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003544 * move the remaining data over it.
3545 */
3546 if (hole) {
3547 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3548 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3549 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3550 b_sub(&h2c->dbuf, hole);
3551 }
3552
3553 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3554 /* too large frames */
3555 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003556 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003557 }
3558
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003559 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003560 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003561 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003562 return ret;
3563
Willy Tarreau68dd9852017-07-03 14:44:26 +02003564 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003565 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003566 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003567
3568 trailers:
3569 /* This is the last HEADERS frame hence a trailer */
3570
3571 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3572 /* It's a trailer but it's missing ES flag */
3573 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3574 goto fail;
3575 }
3576
3577 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3578 * block, and when using chunks we must send the 0 CRLF marker. For
3579 * other modes, the trailers are silently dropped.
3580 */
3581 if (htx) {
3582 if (!htx_add_endof(htx, HTX_BLK_EOD))
3583 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003584 if (h2_make_htx_trailers(list, htx) <= 0)
3585 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003586 }
3587 else if (*flags & H2_SF_DATA_CHNK) {
3588 /* Legacy mode with chunked encoding : we must finalize the
3589 * data block message emit the trailing CRLF */
3590 if (!b_putblk(rxbuf, "0\r\n", 3))
3591 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003592
3593 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3594 if (outlen > 0)
3595 b_add(rxbuf, outlen);
3596 else
3597 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003598 }
3599
3600 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003601}
3602
Willy Tarreau454f9052017-10-26 19:40:35 +02003603/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3604 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3605 * in use, a new chunk is emitted for each frame. This is supposed to fit
3606 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3607 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3608 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003609 * parser state is automatically updated. Returns > 0 if it could completely
3610 * send the current frame, 0 if it couldn't complete, in which case
3611 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3612 * DATA frame can return 0 as a valid result). Stream errors are reported in
3613 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3614 * have checked the frame header and ensured that the frame was complete or the
3615 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003616 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003617static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003618{
3619 struct h2c *h2c = h2s->h2c;
3620 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003621 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003622 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003623 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003624 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003625
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003626 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003627
Olivier Houchard638b7992018-08-16 15:41:52 +02003628 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003629 if (!csbuf) {
3630 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003631 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003632 }
3633
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003634try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003635 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003636 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3637 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003638 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003639 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003640
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003641 if (flen > b_data(&h2c->dbuf)) {
3642 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003643 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003644 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003645 }
3646
Willy Tarreaua9b77962019-01-31 07:23:00 +01003647 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003648 block1 = htx_free_data_space(htx);
3649 if (!block1) {
3650 h2c->flags |= H2_CF_DEM_SFULL;
3651 goto fail;
3652 }
3653 if (flen > block1)
3654 flen = block1;
3655
3656 /* here, flen is the max we can copy into the output buffer */
3657 block1 = b_contig_data(&h2c->dbuf, 0);
3658 if (flen > block1)
3659 flen = block1;
3660
3661 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3662 h2c->flags |= H2_CF_DEM_SFULL;
3663 goto fail;
3664 }
3665
3666 b_del(&h2c->dbuf, flen);
3667 h2c->dfl -= flen;
3668 h2c->rcvd_c += flen;
3669 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003670
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003671 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003672 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003673 htx->extra = h2s->body_len;
3674 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003675 goto try_again;
3676 }
3677 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003678 /* it doesn't fit and the buffer is fragmented,
3679 * so let's defragment it and try again.
3680 */
3681 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003682 }
3683
Willy Tarreaueba10f22018-04-25 20:44:22 +02003684 /* chunked-encoding requires more room */
3685 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003686 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003687 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3688 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3689 (chklen < 1048576) ? 4 : 8;
3690 chklen += 4; // CRLF, CRLF
3691 }
3692
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003693 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003694 if (flen + chklen > b_room(csbuf)) {
3695 if (chklen >= b_room(csbuf)) {
3696 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003697 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003698 }
3699 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003700 }
3701
3702 if (h2s->flags & H2_SF_DATA_CHNK) {
3703 /* emit the chunk size */
3704 unsigned int chksz = flen;
3705 char str[10];
3706 char *beg;
3707
3708 beg = str + sizeof(str);
3709 *--beg = '\n';
3710 *--beg = '\r';
3711 do {
3712 *--beg = hextab[chksz & 0xF];
3713 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003714 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003715 }
3716
Willy Tarreau454f9052017-10-26 19:40:35 +02003717 /* Block1 is the length of the first block before the buffer wraps,
3718 * block2 is the optional second block to reach the end of the frame.
3719 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003720 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003721 if (block1 > flen)
3722 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003723 block2 = flen - block1;
3724
3725 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003726 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003727
3728 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003729 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003730
Willy Tarreaueba10f22018-04-25 20:44:22 +02003731 if (h2s->flags & H2_SF_DATA_CHNK) {
3732 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003733 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003734 }
3735
Willy Tarreau454f9052017-10-26 19:40:35 +02003736 /* now mark the input data as consumed (will be deleted from the buffer
3737 * by the caller when seeing FRAME_A after sending the window update).
3738 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003739 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003740 h2c->dfl -= flen;
3741 h2c->rcvd_c += flen;
3742 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3743
Willy Tarreau1915ca22019-01-24 11:49:37 +01003744 if (h2s->flags & H2_SF_DATA_CLEN)
3745 h2s->body_len -= flen;
3746
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003747 if (h2c->dfl > h2c->dpl) {
3748 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003749 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003750 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003751 }
3752
Willy Tarreau4a28da12018-01-04 14:41:00 +01003753 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003754 /* here we're done with the frame, all the payload (except padding) was
3755 * transferred.
3756 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003757
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003758 if (h2c->dff & H2_F_DATA_END_STREAM) {
3759 if (htx) {
3760 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3761 h2c->flags |= H2_CF_DEM_SFULL;
3762 goto fail;
3763 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003764 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003765 else if (h2s->flags & H2_SF_DATA_CHNK) {
3766 /* emit the trailing 0 CRLF CRLF */
3767 if (b_room(csbuf) < 5) {
3768 h2c->flags |= H2_CF_DEM_SFULL;
3769 goto fail;
3770 }
3771 chklen += 5;
3772 b_putblk(csbuf, "0\r\n\r\n", 5);
3773 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003774 }
3775
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003776 h2c->rcvd_c += h2c->dpl;
3777 h2c->rcvd_s += h2c->dpl;
3778 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003779 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003780 if (htx)
3781 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003782 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003783 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003784 if (htx)
3785 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003786 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003787}
3788
Willy Tarreau5dd17352018-06-14 13:33:30 +02003789/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3790 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3791 * number of bytes sent. The caller must check the stream's status to detect
3792 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003793 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003794static 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 +02003795{
3796 struct http_hdr list[MAX_HTTP_HDR];
3797 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003798 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003799 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003800 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003801 int es_now = 0;
3802 int ret = 0;
3803 int hdr;
3804
3805 if (h2c_mux_busy(h2c, h2s)) {
3806 h2s->flags |= H2_SF_BLK_MBUSY;
3807 return 0;
3808 }
3809
Willy Tarreau44e973f2018-03-01 17:49:30 +01003810 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003811 h2c->flags |= H2_CF_MUX_MALLOC;
3812 h2s->flags |= H2_SF_BLK_MROOM;
3813 return 0;
3814 }
3815
3816 /* First, try to parse the H1 response and index it into <list>.
3817 * NOTE! Since it comes from haproxy, we *know* that a response header
3818 * block does not wrap and we can safely read it this way without
3819 * having to realign the buffer.
3820 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003821 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003822 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003823 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003824 /* incomplete or invalid response, this is abnormal coming from
3825 * haproxy and may only result in a bad errorfile or bad Lua code
3826 * so that won't be fixed, raise an error now.
3827 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003828 * FIXME: we should instead add the ability to only return a
3829 * 502 bad gateway. But in theory this is not supposed to
3830 * happen.
3831 */
3832 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3833 ret = 0;
3834 goto end;
3835 }
3836
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003837 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003838
3839 /* certain statuses have no body or an empty one, regardless of
3840 * what the headers say.
3841 */
3842 if (sl.st.status >= 100 && sl.st.status < 200) {
3843 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3844 h1m->curr_len = h1m->body_len = 0;
3845 }
3846 else if (sl.st.status == 204 || sl.st.status == 304) {
3847 /* no contents, claim c-len is present and set to zero */
3848 h1m->flags &= ~H1_MF_CHNK;
3849 h1m->flags |= H1_MF_CLEN;
3850 h1m->curr_len = h1m->body_len = 0;
3851 }
3852
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003853 chunk_reset(&outbuf);
3854
3855 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003856 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003857 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003858 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003859
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003860 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003861 break;
3862 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003863 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003864 }
3865
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003866 if (outbuf.size < 9)
3867 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003868
3869 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003870 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3871 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3872 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003873
3874 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003875 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003876 /* this is an unparsable response */
3877 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3878 ret = 0;
3879 goto end;
3880 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003881
3882 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003883 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003884 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003885 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003886 }
3887
3888 /* encode all headers, stop at empty name */
3889 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003890 /* these ones do not exist in H2 and must be dropped. */
3891 if (isteq(list[hdr].n, ist("connection")) ||
3892 isteq(list[hdr].n, ist("proxy-connection")) ||
3893 isteq(list[hdr].n, ist("keep-alive")) ||
3894 isteq(list[hdr].n, ist("upgrade")) ||
3895 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003896 continue;
3897
3898 if (isteq(list[hdr].n, ist("")))
3899 break; // end
3900
3901 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3902 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003903 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003904 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003905 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003906 }
3907 }
3908
3909 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003910 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003911 es_now = 1;
3912
3913 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003914 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003915
3916 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003917 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003918
3919 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003920 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003921
3922 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003923 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003924 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003925
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003926 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003927 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003928 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003929
Willy Tarreau801250e2018-09-11 11:45:04 +02003930 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003931 h2s->flags |= H2_SF_ES_SENT;
3932 if (h2s->st == H2_SS_OPEN)
3933 h2s->st = H2_SS_HLOC;
3934 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003935 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003936 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003937 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003938 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003939 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003940 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003941 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003942 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003943 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003944
3945 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003946
3947 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003948 //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 +02003949 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003950 full:
3951 h1m_init_res(h1m);
3952 h1m->err_pos = -1; // don't care about errors on the response path
3953 h2c->flags |= H2_CF_MUX_MFULL;
3954 h2s->flags |= H2_SF_BLK_MROOM;
3955 ret = 0;
3956 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003957}
3958
Willy Tarreau5dd17352018-06-14 13:33:30 +02003959/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3960 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3961 * the number of bytes sent. The caller must check the stream's status to
3962 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003963 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003964static 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 +02003965{
3966 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003967 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003968 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003969 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003970 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003971 int es_now = 0;
3972 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003973 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003974 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003975
3976 if (h2c_mux_busy(h2c, h2s)) {
3977 h2s->flags |= H2_SF_BLK_MBUSY;
3978 goto end;
3979 }
3980
Willy Tarreau44e973f2018-03-01 17:49:30 +01003981 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003982 h2c->flags |= H2_CF_MUX_MALLOC;
3983 h2s->flags |= H2_SF_BLK_MROOM;
3984 goto end;
3985 }
3986
3987 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003988 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003989 goto end;
3990
3991 chunk_reset(&outbuf);
3992
3993 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003994 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003995 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003996 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003997
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003998 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003999 break;
4000 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004001 /* If there are pending data in the output buffer, and we have
4002 * less than 1/4 of the mbuf's size and everything fits, we'll
4003 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4004 * is full and wait, to save some slow realign calls.
4005 */
4006 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4007 h2c->flags |= H2_CF_MUX_MFULL;
4008 h2s->flags |= H2_SF_BLK_MROOM;
4009 goto end;
4010 }
4011
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004012 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004013 }
4014
4015 if (outbuf.size < 9) {
4016 h2c->flags |= H2_CF_MUX_MFULL;
4017 h2s->flags |= H2_SF_BLK_MROOM;
4018 goto end;
4019 }
4020
4021 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004022 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4023 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4024 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004025
4026 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4027 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004028 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004029 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004030 break;
4031 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004032 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004033 if ((long long)size > h1m->curr_len)
4034 size = h1m->curr_len;
4035 break;
4036 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004037 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004038 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004039 if (!ret)
4040 goto end;
4041
4042 if (ret < 0) {
4043 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004044 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004045 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4046 goto end;
4047 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004048 max -= ret;
4049 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004050 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004051 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004052 }
4053
Willy Tarreau801250e2018-09-11 11:45:04 +02004054 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004055 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004056 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004057 if (!ret)
4058 goto end;
4059
4060 if (ret < 0) {
4061 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004062 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004063 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4064 goto end;
4065 }
4066
4067 size = chunk;
4068 h1m->curr_len = chunk;
4069 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004070 max -= ret;
4071 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004072 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004073 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004074 if (!size)
4075 goto send_empty;
4076 }
4077
4078 /* in MSG_DATA state, continue below */
4079 size = h1m->curr_len;
4080 break;
4081 }
4082
4083 /* we have in <size> the exact number of bytes we need to copy from
4084 * the H1 buffer. We need to check this against the connection's and
4085 * the stream's send windows, and to ensure that this fits in the max
4086 * frame size and in the buffer's available space minus 9 bytes (for
4087 * the frame header). The connection's flow control is applied last so
4088 * that we can use a separate list of streams which are immediately
4089 * unblocked on window opening. Note: we don't implement padding.
4090 */
4091
Willy Tarreau5dd17352018-06-14 13:33:30 +02004092 if (size > max)
4093 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004094
4095 if (size > h2s->mws)
4096 size = h2s->mws;
4097
4098 if (size <= 0) {
4099 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004100 if (h2s->send_wait) {
4101 LIST_DEL(&h2s->list);
4102 LIST_INIT(&h2s->list);
4103 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004104 goto end;
4105 }
4106
4107 if (h2c->mfs && size > h2c->mfs)
4108 size = h2c->mfs;
4109
4110 if (size + 9 > outbuf.size) {
4111 /* we have an opportunity for enlarging the too small
4112 * available space, let's try.
4113 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004114 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004115 goto realign_again;
4116 size = outbuf.size - 9;
4117 }
4118
4119 if (size <= 0) {
4120 h2c->flags |= H2_CF_MUX_MFULL;
4121 h2s->flags |= H2_SF_BLK_MROOM;
4122 goto end;
4123 }
4124
4125 if (size > h2c->mws)
4126 size = h2c->mws;
4127
4128 if (size <= 0) {
4129 h2s->flags |= H2_SF_BLK_MFCTL;
4130 goto end;
4131 }
4132
4133 /* copy whatever we can */
4134 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004135 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004136 if (ret == 1)
4137 len2 = 0;
4138
4139 if (!ret || len1 + len2 < size) {
4140 /* FIXME: must normally never happen */
4141 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4142 goto end;
4143 }
4144
4145 /* limit len1/len2 to size */
4146 if (len1 + len2 > size) {
4147 int sub = len1 + len2 - size;
4148
4149 if (len2 > sub)
4150 len2 -= sub;
4151 else {
4152 sub -= len2;
4153 len2 = 0;
4154 len1 -= sub;
4155 }
4156 }
4157
4158 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004159 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004160 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004161 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004162
4163 send_empty:
4164 /* we may need to add END_STREAM */
4165 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4166 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004167 *
4168 * FIXME: what we do here is not correct because we send end_stream
4169 * before knowing if we'll have to send a HEADERS frame for the
4170 * trailers. More importantly we're not consuming the trailing CRLF
4171 * after the end of trailers, so it will be left to the caller to
4172 * eat it. The right way to do it would be to measure trailers here
4173 * and to send ES only if there are no trailers.
4174 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004175 */
4176 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004177 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004178 es_now = 1;
4179
4180 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004181 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004182
4183 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004184 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004185
4186 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004187 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004188
4189 /* consume incoming H1 response */
4190 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004191 max -= size;
4192 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004193 total += size;
4194 h1m->curr_len -= size;
4195 h2s->mws -= size;
4196 h2c->mws -= size;
4197
4198 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004199 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004200 goto new_frame;
4201 }
4202 }
4203
4204 if (es_now) {
4205 if (h2s->st == H2_SS_OPEN)
4206 h2s->st = H2_SS_HLOC;
4207 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004208 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004209
Willy Tarreau35a62702018-02-27 15:37:25 +01004210 if (!(h1m->flags & H1_MF_CHNK)) {
4211 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004212 total += max;
4213 ofs += max;
4214 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004215
Willy Tarreau801250e2018-09-11 11:45:04 +02004216 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004217 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004218
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004219 h2s->flags |= H2_SF_ES_SENT;
4220 }
4221
4222 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004223 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 +02004224 return total;
4225}
4226
Willy Tarreau115e83b2018-12-01 19:17:53 +01004227/* Try to send a HEADERS frame matching HTX response present in HTX message
4228 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4229 * must check the stream's status to detect any error which might have happened
4230 * subsequently to a successful send. The htx blocks are automatically removed
4231 * from the message. The htx message is assumed to be valid since produced from
4232 * the internal code, hence it contains a start line, an optional series of
4233 * header blocks and an end of header, otherwise an invalid frame could be
4234 * emitted and the resulting htx message could be left in an inconsistent state.
4235 */
4236static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4237{
4238 struct http_hdr list[MAX_HTTP_HDR];
4239 struct h2c *h2c = h2s->h2c;
4240 struct htx_blk *blk;
4241 struct htx_blk *blk_end;
4242 struct buffer outbuf;
4243 struct htx_sl *sl;
4244 enum htx_blk_type type;
4245 int es_now = 0;
4246 int ret = 0;
4247 int hdr;
4248 int idx;
4249
4250 if (h2c_mux_busy(h2c, h2s)) {
4251 h2s->flags |= H2_SF_BLK_MBUSY;
4252 return 0;
4253 }
4254
4255 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4256 h2c->flags |= H2_CF_MUX_MALLOC;
4257 h2s->flags |= H2_SF_BLK_MROOM;
4258 return 0;
4259 }
4260
4261 /* determine the first block which must not be deleted, blk_end may
4262 * be NULL if all blocks have to be deleted.
4263 */
4264 idx = htx_get_head(htx);
4265 blk_end = NULL;
4266 while (idx != -1) {
4267 type = htx_get_blk_type(htx_get_blk(htx, idx));
4268 idx = htx_get_next(htx, idx);
4269 if (type == HTX_BLK_EOH) {
4270 if (idx != -1)
4271 blk_end = htx_get_blk(htx, idx);
4272 break;
4273 }
4274 }
4275
4276 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004277 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004278 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004279 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004280 if (h2s->status < 100 || h2s->status > 999)
4281 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004282
4283 /* and the rest of the headers, that we dump starting at header 0 */
4284 hdr = 0;
4285
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004286 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004287 while ((idx = htx_get_next(htx, idx)) != -1) {
4288 blk = htx_get_blk(htx, idx);
4289 type = htx_get_blk_type(blk);
4290
4291 if (type == HTX_BLK_UNUSED)
4292 continue;
4293
4294 if (type != HTX_BLK_HDR)
4295 break;
4296
4297 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4298 goto fail;
4299
4300 list[hdr].n = htx_get_blk_name(htx, blk);
4301 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004302 hdr++;
4303 }
4304
4305 /* marker for end of headers */
4306 list[hdr].n = ist("");
4307
4308 if (h2s->status == 204 || h2s->status == 304) {
4309 /* no contents, claim c-len is present and set to zero */
4310 es_now = 1;
4311 }
4312
4313 chunk_reset(&outbuf);
4314
4315 while (1) {
4316 outbuf.area = b_tail(&h2c->mbuf);
4317 outbuf.size = b_contig_space(&h2c->mbuf);
4318 outbuf.data = 0;
4319
4320 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4321 break;
4322 realign_again:
4323 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4324 }
4325
4326 if (outbuf.size < 9)
4327 goto full;
4328
4329 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4330 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4331 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4332 outbuf.data = 9;
4333
4334 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004335 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004336 if (b_space_wraps(&h2c->mbuf))
4337 goto realign_again;
4338 goto full;
4339 }
4340
4341 /* encode all headers, stop at empty name */
4342 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4343 /* these ones do not exist in H2 and must be dropped. */
4344 if (isteq(list[hdr].n, ist("connection")) ||
4345 isteq(list[hdr].n, ist("proxy-connection")) ||
4346 isteq(list[hdr].n, ist("keep-alive")) ||
4347 isteq(list[hdr].n, ist("upgrade")) ||
4348 isteq(list[hdr].n, ist("transfer-encoding")))
4349 continue;
4350
4351 if (isteq(list[hdr].n, ist("")))
4352 break; // end
4353
4354 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4355 /* output full */
4356 if (b_space_wraps(&h2c->mbuf))
4357 goto realign_again;
4358 goto full;
4359 }
4360 }
4361
Christopher Faulet0b465482019-02-19 15:14:23 +01004362 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004363 * FIXME: we should also set it when we know for sure that the
4364 * content-length is zero as well as on 204/304
4365 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004366 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4367 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004368 es_now = 1;
4369
Willy Tarreau927b88b2019-03-04 08:03:25 +01004370 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004371 es_now = 1;
4372
4373 /* update the frame's size */
4374 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4375
4376 if (es_now)
4377 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4378
4379 /* commit the H2 response */
4380 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004381
4382 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4383 * 1xx responses, another HEADERS frame is expected.
4384 */
4385 if (h2s->status >= 200 || h2s->status == 101)
4386 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004387
Willy Tarreau115e83b2018-12-01 19:17:53 +01004388 if (es_now) {
4389 h2s->flags |= H2_SF_ES_SENT;
4390 if (h2s->st == H2_SS_OPEN)
4391 h2s->st = H2_SS_HLOC;
4392 else
4393 h2s_close(h2s);
4394 }
4395
4396 /* OK we could properly deliver the response */
4397
4398 /* remove all header blocks including the EOH and compute the
4399 * corresponding size.
4400 *
4401 * FIXME: We should remove everything when es_now is set.
4402 */
4403 ret = 0;
4404 idx = htx_get_head(htx);
4405 blk = htx_get_blk(htx, idx);
4406 while (blk != blk_end) {
4407 ret += htx_get_blksz(blk);
4408 blk = htx_remove_blk(htx, blk);
4409 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004410
4411 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4412 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004413 end:
4414 return ret;
4415 full:
4416 h2c->flags |= H2_CF_MUX_MFULL;
4417 h2s->flags |= H2_SF_BLK_MROOM;
4418 ret = 0;
4419 goto end;
4420 fail:
4421 /* unparsable HTX messages, too large ones to be produced in the local
4422 * list etc go here (unrecoverable errors).
4423 */
4424 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4425 ret = 0;
4426 goto end;
4427}
4428
Willy Tarreau80739692018-10-05 11:35:57 +02004429/* Try to send a HEADERS frame matching HTX request present in HTX message
4430 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4431 * must check the stream's status to detect any error which might have happened
4432 * subsequently to a successful send. The htx blocks are automatically removed
4433 * from the message. The htx message is assumed to be valid since produced from
4434 * the internal code, hence it contains a start line, an optional series of
4435 * header blocks and an end of header, otherwise an invalid frame could be
4436 * emitted and the resulting htx message could be left in an inconsistent state.
4437 */
4438static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4439{
4440 struct http_hdr list[MAX_HTTP_HDR];
4441 struct h2c *h2c = h2s->h2c;
4442 struct htx_blk *blk;
4443 struct htx_blk *blk_end;
4444 struct buffer outbuf;
4445 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004446 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004447 enum htx_blk_type type;
4448 int es_now = 0;
4449 int ret = 0;
4450 int hdr;
4451 int idx;
4452
4453 if (h2c_mux_busy(h2c, h2s)) {
4454 h2s->flags |= H2_SF_BLK_MBUSY;
4455 return 0;
4456 }
4457
4458 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4459 h2c->flags |= H2_CF_MUX_MALLOC;
4460 h2s->flags |= H2_SF_BLK_MROOM;
4461 return 0;
4462 }
4463
4464 /* determine the first block which must not be deleted, blk_end may
4465 * be NULL if all blocks have to be deleted.
4466 */
4467 idx = htx_get_head(htx);
4468 blk_end = NULL;
4469 while (idx != -1) {
4470 type = htx_get_blk_type(htx_get_blk(htx, idx));
4471 idx = htx_get_next(htx, idx);
4472 if (type == HTX_BLK_EOH) {
4473 if (idx != -1)
4474 blk_end = htx_get_blk(htx, idx);
4475 break;
4476 }
4477 }
4478
4479 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004480 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004481 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004482 meth = htx_sl_req_meth(sl);
4483 path = htx_sl_req_uri(sl);
4484
4485 /* and the rest of the headers, that we dump starting at header 0 */
4486 hdr = 0;
4487
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004488 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004489 while ((idx = htx_get_next(htx, idx)) != -1) {
4490 blk = htx_get_blk(htx, idx);
4491 type = htx_get_blk_type(blk);
4492
4493 if (type == HTX_BLK_UNUSED)
4494 continue;
4495
4496 if (type != HTX_BLK_HDR)
4497 break;
4498
4499 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4500 goto fail;
4501
4502 list[hdr].n = htx_get_blk_name(htx, blk);
4503 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004504 hdr++;
4505 }
4506
4507 /* marker for end of headers */
4508 list[hdr].n = ist("");
4509
4510 chunk_reset(&outbuf);
4511
4512 while (1) {
4513 outbuf.area = b_tail(&h2c->mbuf);
4514 outbuf.size = b_contig_space(&h2c->mbuf);
4515 outbuf.data = 0;
4516
4517 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4518 break;
4519 realign_again:
4520 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4521 }
4522
4523 if (outbuf.size < 9)
4524 goto full;
4525
4526 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4527 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4528 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4529 outbuf.data = 9;
4530
4531 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004532 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004533 if (b_space_wraps(&h2c->mbuf))
4534 goto realign_again;
4535 goto full;
4536 }
4537
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004538 /* RFC7540 #8.3: the CONNECT method must have :
4539 * - :authority set to the URI part (host:port)
4540 * - :method set to CONNECT
4541 * - :scheme and :path omitted
4542 */
4543 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4544 /* encode the scheme which is always "https" (or 0x86 for "http") */
4545 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4546 /* output full */
4547 if (b_space_wraps(&h2c->mbuf))
4548 goto realign_again;
4549 goto full;
4550 }
Willy Tarreau80739692018-10-05 11:35:57 +02004551
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004552 /* encode the path, which necessarily is the second one */
4553 if (!hpack_encode_path(&outbuf, path)) {
4554 /* output full */
4555 if (b_space_wraps(&h2c->mbuf))
4556 goto realign_again;
4557 goto full;
4558 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004559
4560 /* look for the Host header and place it in :authority */
4561 auth = ist2(NULL, 0);
4562 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4563 if (isteq(list[hdr].n, ist("")))
4564 break; // end
4565
4566 if (isteq(list[hdr].n, ist("host"))) {
4567 auth = list[hdr].v;
4568 break;
4569 }
4570 }
4571 }
4572 else {
4573 /* for CONNECT, :authority is taken from the path */
4574 auth = path;
4575 }
4576
4577 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4578 /* output full */
4579 if (b_space_wraps(&h2c->mbuf))
4580 goto realign_again;
4581 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004582 }
4583
4584 /* encode all headers, stop at empty name */
4585 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4586 /* these ones do not exist in H2 and must be dropped. */
4587 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004588 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004589 isteq(list[hdr].n, ist("proxy-connection")) ||
4590 isteq(list[hdr].n, ist("keep-alive")) ||
4591 isteq(list[hdr].n, ist("upgrade")) ||
4592 isteq(list[hdr].n, ist("transfer-encoding")))
4593 continue;
4594
4595 if (isteq(list[hdr].n, ist("")))
4596 break; // end
4597
4598 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4599 /* output full */
4600 if (b_space_wraps(&h2c->mbuf))
4601 goto realign_again;
4602 goto full;
4603 }
4604 }
4605
4606 /* we may need to add END_STREAM if we have no body :
4607 * - request already closed, or :
4608 * - no transfer-encoding, and :
4609 * - no content-length or content-length:0
4610 * Fixme: this doesn't take into account CONNECT requests.
4611 */
4612 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4613 es_now = 1;
4614
4615 if (sl->flags & HTX_SL_F_BODYLESS)
4616 es_now = 1;
4617
Willy Tarreau927b88b2019-03-04 08:03:25 +01004618 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004619 es_now = 1;
4620
4621 /* update the frame's size */
4622 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4623
4624 if (es_now)
4625 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4626
4627 /* commit the H2 response */
4628 b_add(&h2c->mbuf, outbuf.data);
4629 h2s->flags |= H2_SF_HEADERS_SENT;
4630 h2s->st = H2_SS_OPEN;
4631
Willy Tarreau80739692018-10-05 11:35:57 +02004632 if (es_now) {
4633 // trim any possibly pending data (eg: inconsistent content-length)
4634 h2s->flags |= H2_SF_ES_SENT;
4635 h2s->st = H2_SS_HLOC;
4636 }
4637
4638 /* remove all header blocks including the EOH and compute the
4639 * corresponding size.
4640 *
4641 * FIXME: We should remove everything when es_now is set.
4642 */
4643 ret = 0;
4644 idx = htx_get_head(htx);
4645 blk = htx_get_blk(htx, idx);
4646 while (blk != blk_end) {
4647 ret += htx_get_blksz(blk);
4648 blk = htx_remove_blk(htx, blk);
4649 }
4650
4651 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4652 htx_remove_blk(htx, blk_end);
4653
4654 end:
4655 return ret;
4656 full:
4657 h2c->flags |= H2_CF_MUX_MFULL;
4658 h2s->flags |= H2_SF_BLK_MROOM;
4659 ret = 0;
4660 goto end;
4661 fail:
4662 /* unparsable HTX messages, too large ones to be produced in the local
4663 * list etc go here (unrecoverable errors).
4664 */
4665 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4666 ret = 0;
4667 goto end;
4668}
4669
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004670/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004671 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4672 * caller must check the stream's status to detect any error which might have
4673 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004674 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4675 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004676static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004677{
4678 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004679 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004680 struct buffer outbuf;
4681 size_t total = 0;
4682 int es_now = 0;
4683 int bsize; /* htx block size */
4684 int fsize; /* h2 frame size */
4685 struct htx_blk *blk;
4686 enum htx_blk_type type;
4687 int idx;
4688
4689 if (h2c_mux_busy(h2c, h2s)) {
4690 h2s->flags |= H2_SF_BLK_MBUSY;
4691 goto end;
4692 }
4693
4694 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4695 h2c->flags |= H2_CF_MUX_MALLOC;
4696 h2s->flags |= H2_SF_BLK_MROOM;
4697 goto end;
4698 }
4699
Willy Tarreau98de12a2018-12-12 07:03:00 +01004700 htx = htx_from_buf(buf);
4701
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004702 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4703 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4704 * the caller to handle.
4705 */
4706
4707 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004708 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004709 goto end;
4710
4711 idx = htx_get_head(htx);
4712 blk = htx_get_blk(htx, idx);
4713 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4714 bsize = htx_get_blksz(blk);
4715 fsize = bsize;
4716
4717 if (type == HTX_BLK_EOD) {
4718 /* if we have an EOD, we're dealing with chunked data. We may
4719 * have a set of trailers after us that the caller will want to
4720 * deal with. Let's simply remove the EOD and return.
4721 */
4722 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004723 total++; // EOD counts as one byte
4724 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004725 goto end;
4726 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004727 else if (type == HTX_BLK_EOM) {
4728 if (h2s->flags & H2_SF_ES_SENT) {
4729 /* ES already sent */
4730 htx_remove_blk(htx, blk);
4731 total++; // EOM counts as one byte
4732 count--;
4733 goto end;
4734 }
4735 }
4736 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004737 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004738
4739 /* Perform some optimizations to reduce the number of buffer copies.
4740 * First, if the mux's buffer is empty and the htx area contains
4741 * exactly one data block of the same size as the requested count, and
4742 * this count fits within the frame size, the stream's window size, and
4743 * the connection's window size, then it's possible to simply swap the
4744 * caller's buffer with the mux's output buffer and adjust offsets and
4745 * length to match the entire DATA HTX block in the middle. In this
4746 * case we perform a true zero-copy operation from end-to-end. This is
4747 * the situation that happens all the time with large files. Second, if
4748 * this is not possible, but the mux's output buffer is empty, we still
4749 * have an opportunity to avoid the copy to the intermediary buffer, by
4750 * making the intermediary buffer's area point to the output buffer's
4751 * area. In this case we want to skip the HTX header to make sure that
4752 * copies remain aligned and that this operation remains possible all
4753 * the time. This goes for headers, data blocks and any data extracted
4754 * from the HTX blocks.
4755 */
4756 if (unlikely(fsize == count &&
4757 htx->used == 1 && type == HTX_BLK_DATA &&
4758 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4759 void *old_area = h2c->mbuf.area;
4760
4761 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004762 /* Too bad there are data left there. We're willing to memcpy/memmove
4763 * up to 1/4 of the buffer, which means that it's OK to copy a large
4764 * frame into a buffer containing few data if it needs to be realigned,
4765 * and that it's also OK to copy few data without realigning. Otherwise
4766 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004767 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004768 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4769 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4770 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004771 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004772
Willy Tarreau98de12a2018-12-12 07:03:00 +01004773 h2c->flags |= H2_CF_MUX_MFULL;
4774 h2s->flags |= H2_SF_BLK_MROOM;
4775 goto end;
4776 }
4777
4778 /* map an H2 frame to the HTX block so that we can put the
4779 * frame header there.
4780 */
4781 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004782 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004783 h2c->mbuf.data = fsize + 9;
4784 outbuf.area = b_head(&h2c->mbuf);
4785
4786 /* prepend an H2 DATA frame header just before the DATA block */
4787 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4788 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4789 h2_set_frame_size(outbuf.area, fsize);
4790
4791 /* update windows */
4792 h2s->mws -= fsize;
4793 h2c->mws -= fsize;
4794
4795 /* and exchange with our old area */
4796 buf->area = old_area;
4797 buf->data = buf->head = 0;
4798 total += fsize;
4799 goto end;
4800 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004801
Willy Tarreau98de12a2018-12-12 07:03:00 +01004802 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004803 /* for DATA and EOM we'll have to emit a frame, even if empty */
4804
4805 while (1) {
4806 outbuf.area = b_tail(&h2c->mbuf);
4807 outbuf.size = b_contig_space(&h2c->mbuf);
4808 outbuf.data = 0;
4809
4810 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4811 break;
4812 realign_again:
4813 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4814 }
4815
4816 if (outbuf.size < 9) {
4817 h2c->flags |= H2_CF_MUX_MFULL;
4818 h2s->flags |= H2_SF_BLK_MROOM;
4819 goto end;
4820 }
4821
4822 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4823 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4824 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4825 outbuf.data = 9;
4826
4827 /* we have in <fsize> the exact number of bytes we need to copy from
4828 * the HTX buffer. We need to check this against the connection's and
4829 * the stream's send windows, and to ensure that this fits in the max
4830 * frame size and in the buffer's available space minus 9 bytes (for
4831 * the frame header). The connection's flow control is applied last so
4832 * that we can use a separate list of streams which are immediately
4833 * unblocked on window opening. Note: we don't implement padding.
4834 */
4835
4836 /* EOM is presented with bsize==1 but would lead to the emission of an
4837 * empty frame, thus we force it to zero here.
4838 */
4839 if (type == HTX_BLK_EOM)
4840 bsize = fsize = 0;
4841
4842 if (!fsize)
4843 goto send_empty;
4844
4845 if (h2s->mws <= 0) {
4846 h2s->flags |= H2_SF_BLK_SFCTL;
4847 if (h2s->send_wait) {
4848 LIST_DEL(&h2s->list);
4849 LIST_INIT(&h2s->list);
4850 }
4851 goto end;
4852 }
4853
Willy Tarreauee573762018-12-04 15:25:57 +01004854 if (fsize > count)
4855 fsize = count;
4856
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004857 if (fsize > h2s->mws)
4858 fsize = h2s->mws; // >0
4859
4860 if (h2c->mfs && fsize > h2c->mfs)
4861 fsize = h2c->mfs; // >0
4862
4863 if (fsize + 9 > outbuf.size) {
4864 /* we have an opportunity for enlarging the too small
4865 * available space, let's try.
4866 * FIXME: is this really interesting to do? Maybe we'll
4867 * spend lots of time realigning instead of using two
4868 * frames.
4869 */
4870 if (b_space_wraps(&h2c->mbuf))
4871 goto realign_again;
4872 fsize = outbuf.size - 9;
4873
4874 if (fsize <= 0) {
4875 /* no need to send an empty frame here */
4876 h2c->flags |= H2_CF_MUX_MFULL;
4877 h2s->flags |= H2_SF_BLK_MROOM;
4878 goto end;
4879 }
4880 }
4881
4882 if (h2c->mws <= 0) {
4883 h2s->flags |= H2_SF_BLK_MFCTL;
4884 goto end;
4885 }
4886
4887 if (fsize > h2c->mws)
4888 fsize = h2c->mws;
4889
4890 /* now let's copy this this into the output buffer */
4891 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004892 h2s->mws -= fsize;
4893 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004894 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004895
4896 send_empty:
4897 /* update the frame's size */
4898 h2_set_frame_size(outbuf.area, fsize);
4899
4900 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4901 * meeting EOM. We should optimize this later.
4902 */
4903 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004904 total++; // EOM counts as one byte
4905 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004906 es_now = 1;
4907 }
4908
4909 if (es_now)
4910 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4911
4912 /* commit the H2 response */
4913 b_add(&h2c->mbuf, fsize + 9);
4914
4915 /* consume incoming HTX block, including EOM */
4916 total += fsize;
4917 if (fsize == bsize) {
4918 htx_remove_blk(htx, blk);
4919 if (fsize)
4920 goto new_frame;
4921 } else {
4922 /* we've truncated this block */
4923 htx_cut_data_blk(htx, blk, fsize);
4924 }
4925
4926 if (es_now) {
4927 if (h2s->st == H2_SS_OPEN)
4928 h2s->st = H2_SS_HLOC;
4929 else
4930 h2s_close(h2s);
4931
4932 h2s->flags |= H2_SF_ES_SENT;
4933 }
4934
4935 end:
4936 return total;
4937}
4938
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004939/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4940 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4941 * processed. The caller must check the stream's status to detect any error
4942 * which might have happened subsequently to a successful send. The htx blocks
4943 * are automatically removed from the message. The htx message is assumed to be
4944 * valid since produced from the internal code. Processing stops when meeting
4945 * the EOM, which is also removed. All trailers are processed at once and sent
4946 * as a single frame. The ES flag is always set.
4947 */
4948static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4949{
4950 struct http_hdr list[MAX_HTTP_HDR];
4951 struct h2c *h2c = h2s->h2c;
4952 struct htx_blk *blk;
4953 struct htx_blk *blk_end;
4954 struct buffer outbuf;
4955 struct h1m h1m;
4956 enum htx_blk_type type;
4957 uint32_t size;
4958 int ret = 0;
4959 int hdr;
4960 int idx;
4961 void *start;
4962
4963 if (h2c_mux_busy(h2c, h2s)) {
4964 h2s->flags |= H2_SF_BLK_MBUSY;
4965 goto end;
4966 }
4967
4968 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4969 h2c->flags |= H2_CF_MUX_MALLOC;
4970 h2s->flags |= H2_SF_BLK_MROOM;
4971 goto end;
4972 }
4973
4974 /* The principle is that we parse each and every trailers block using
4975 * the H1 headers parser, and append it to the list. We don't proceed
4976 * until EOM is met. blk_end will point to the EOM block.
4977 */
4978 hdr = 0;
4979 memset(list, 0, sizeof(list));
4980 blk_end = NULL;
4981
4982 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4983 blk = htx_get_blk(htx, idx);
4984 type = htx_get_blk_type(blk);
4985
4986 if (type == HTX_BLK_UNUSED)
4987 continue;
4988
4989 if (type != HTX_BLK_TLR) {
4990 if (type == HTX_BLK_EOM)
4991 blk_end = blk;
4992 break;
4993 }
4994
4995 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4996 goto fail;
4997
4998 size = htx_get_blksz(blk);
4999 start = htx_get_blk_ptr(htx, blk);
5000
5001 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5002 h1m.err_pos = 0;
5003 ret = h1_headers_to_hdr_list(start, start + size,
5004 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5005 &h1m, NULL);
5006 if (ret < 0)
5007 goto fail;
5008
5009 /* ret == 0 if an incomplete trailers block was found (missing
5010 * empty line), or > 0 if it was found. We have to continue on
5011 * incomplete messages because the trailers block might be
5012 * incomplete.
5013 */
5014
5015 /* search the new end */
5016 while (hdr <= sizeof(list)/sizeof(list[0])) {
5017 if (!list[hdr].n.len)
5018 break;
5019 hdr++;
5020 }
5021 }
5022
5023 if (!blk_end)
5024 goto end; // end not found yet
5025
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005026 chunk_reset(&outbuf);
5027
5028 while (1) {
5029 outbuf.area = b_tail(&h2c->mbuf);
5030 outbuf.size = b_contig_space(&h2c->mbuf);
5031 outbuf.data = 0;
5032
5033 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5034 break;
5035 realign_again:
5036 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5037 }
5038
5039 if (outbuf.size < 9)
5040 goto full;
5041
5042 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5043 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5044 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5045 outbuf.data = 9;
5046
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005047 /* encode all headers */
5048 for (idx = 0; idx < hdr; idx++) {
5049 /* these ones do not exist in H2 or must not appear in
5050 * trailers and must be dropped.
5051 */
5052 if (isteq(list[idx].n, ist("host")) ||
5053 isteq(list[idx].n, ist("content-length")) ||
5054 isteq(list[idx].n, ist("connection")) ||
5055 isteq(list[idx].n, ist("proxy-connection")) ||
5056 isteq(list[idx].n, ist("keep-alive")) ||
5057 isteq(list[idx].n, ist("upgrade")) ||
5058 isteq(list[idx].n, ist("te")) ||
5059 isteq(list[idx].n, ist("transfer-encoding")))
5060 continue;
5061
5062 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5063 /* output full */
5064 if (b_space_wraps(&h2c->mbuf))
5065 goto realign_again;
5066 goto full;
5067 }
5068 }
5069
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005070 if (!hdr) {
5071 /* here we have a problem, we've received an empty trailers
5072 * block followed by an EOM. Because of this we can't send a
5073 * HEADERS frame, so we have to cheat and instead send an empty
5074 * DATA frame conveying the ES flag.
5075 */
5076 outbuf.area[3] = H2_FT_DATA;
5077 outbuf.area[4] = H2_F_DATA_END_STREAM;
5078 }
5079
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005080 /* update the frame's size */
5081 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5082
5083 /* commit the H2 response */
5084 b_add(&h2c->mbuf, outbuf.data);
5085 h2s->flags |= H2_SF_ES_SENT;
5086
5087 if (h2s->st == H2_SS_OPEN)
5088 h2s->st = H2_SS_HLOC;
5089 else
5090 h2s_close(h2s);
5091
5092 /* OK we could properly deliver the response */
5093 done:
5094 /* remove all header blocks including EOM and compute the corresponding size. */
5095 ret = 0;
5096 idx = htx_get_head(htx);
5097 blk = htx_get_blk(htx, idx);
5098 while (blk != blk_end) {
5099 ret += htx_get_blksz(blk);
5100 blk = htx_remove_blk(htx, blk);
5101 }
5102 blk = htx_remove_blk(htx, blk);
5103 end:
5104 return ret;
5105 full:
5106 h2c->flags |= H2_CF_MUX_MFULL;
5107 h2s->flags |= H2_SF_BLK_MROOM;
5108 ret = 0;
5109 goto end;
5110 fail:
5111 /* unparsable HTX messages, too large ones to be produced in the local
5112 * list etc go here (unrecoverable errors).
5113 */
5114 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5115 ret = 0;
5116 goto end;
5117}
5118
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005119/* Called from the upper layer, to subscribe to events, such as being able to send.
5120 * The <param> argument here is supposed to be a pointer to a wait_event struct
5121 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5122 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5123 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5124 * returns 0 except for the error above.
5125 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005126static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5127{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005128 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005129 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005130 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005131
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005132 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005133 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005134 if (!(sw->events & SUB_RETRY_RECV)) {
5135 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005136 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005137 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005138 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005139 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005140 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005141 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005142 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005143 if (!(sw->events & SUB_RETRY_SEND)) {
5144 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005145 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005146 h2s->send_wait = sw;
5147 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5148 if (h2s->flags & H2_SF_BLK_MFCTL)
5149 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5150 else
5151 LIST_ADDQ(&h2c->send_list, &h2s->list);
5152 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005153 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005154 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005155 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005156 if (event_type != 0)
5157 return -1;
5158 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005159}
5160
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005161/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5162 * The <param> argument here is supposed to be a pointer to the same wait_event
5163 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5164 * It always returns zero.
5165 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005166static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5167{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005168 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005169 struct h2s *h2s = cs->ctx;
5170
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005171 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005172 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005173 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005174 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005175 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005176 }
5177 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005178 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005179 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005180 if (h2s->send_wait == sw) {
5181 LIST_DEL(&h2s->list);
5182 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005183 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005184 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005185 }
5186 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005187 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5188 sw = param;
5189 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005190 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Willy Tarreaue73256f2019-03-25 18:02:54 +01005191 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005192 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005193 LIST_DEL(&h2s->list);
5194 LIST_INIT(&h2s->list);
Olivier Houchard3ea35132019-03-25 14:10:42 +01005195 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005196 }
5197 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005198 return 0;
5199}
5200
5201
Olivier Houchard511efea2018-08-16 15:30:32 +02005202/* Called from the upper layer, to receive data */
5203static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5204{
Olivier Houchard638b7992018-08-16 15:41:52 +02005205 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005206 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005207 struct htx *h2s_htx = NULL;
5208 struct htx *buf_htx = NULL;
5209 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005210 size_t ret = 0;
5211
5212 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005213 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5214 /* in HTX mode we ignore the count argument */
5215 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005216 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005217 /* Here htx_to_buf() will set buffer data to 0 because
5218 * the HTX is empty.
5219 */
5220 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005221 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005222 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005223
5224 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005225 count = htx_free_data_space(buf_htx);
5226 if (flags & CO_RFL_KEEP_RSV) {
5227 if (count <= global.tune.maxrewrite)
5228 goto end;
5229 count -= global.tune.maxrewrite;
5230 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005231
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005232 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005233
5234 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5235 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5236
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005237 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005238 htx_to_buf(buf_htx, buf);
5239 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005240 ret = htx_ret.ret;
5241 }
5242 else {
5243 ret = b_xfer(buf, &h2s->rxbuf, count);
5244 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005245
Christopher Faulet37070b22019-02-14 15:12:14 +01005246 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005247 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005248 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005249 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005250 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005251 if (cs->flags & CS_FL_REOS)
5252 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005253 if (cs->flags & CS_FL_ERR_PENDING)
5254 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005255 if (b_size(&h2s->rxbuf)) {
5256 b_free(&h2s->rxbuf);
5257 offer_buffers(NULL, tasks_run_queue);
5258 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005259 }
5260
Willy Tarreau082f5592018-11-25 08:03:32 +01005261 if (ret && h2c->dsi == h2s->id) {
5262 /* demux is blocking on this stream's buffer */
5263 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005264 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005265 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005266
Olivier Houchard511efea2018-08-16 15:30:32 +02005267 return ret;
5268}
5269
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005270/* stops all senders of this connection for example when the mux buffer is full.
5271 * They are moved from the sending_list to either fctl_list or send_list.
5272 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005273static void h2_stop_senders(struct h2c *h2c)
5274{
5275 struct h2s *h2s, *h2s_back;
5276
Olivier Houchardd360ac62019-03-22 17:37:16 +01005277 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5278 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005279 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005280 h2s->send_wait->events |= SUB_RETRY_SEND;
5281 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005282 }
5283}
5284
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005285/* Called from the upper layer, to send data from buffer <buf> for no more than
5286 * <count> bytes. Returns the number of bytes effectively sent. Some status
5287 * flags may be updated on the conn_stream.
5288 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005289static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005290{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005291 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005292 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005293 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005294 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005295 struct htx *htx;
5296 struct htx_blk *blk;
5297 enum htx_blk_type btype;
5298 uint32_t bsize;
5299 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005300
Olivier Houchardd360ac62019-03-22 17:37:16 +01005301 /* If we were not just woken because we wanted to send but couldn't,
5302 * and there's somebody else that is waiting to send, do nothing,
5303 * we will subscribe later and be put at the end of the list
5304 */
5305 LIST_DEL_INIT(&h2s->sending_list);
5306 if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) &&
5307 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5308 return 0;
5309
Olivier Houchardd846c262018-10-19 17:24:29 +02005310 if (h2s->send_wait) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005311 /* We want to stay in the send_list, so prepare ourself to be
5312 * eventually recalled if needed, and only remove ourself from
5313 * the list if we managed to send anything.
5314 */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005315 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01005316 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005317 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005318 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5319 return 0;
5320
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005321 /* htx will be enough to decide if we're using HTX or legacy */
5322 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5323
Willy Tarreau0bad0432018-06-14 16:54:01 +02005324 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005325 h2s->flags |= H2_SF_OUTGOING_DATA;
5326
Willy Tarreau751f2d02018-10-05 09:35:00 +02005327 if (h2s->id == 0) {
5328 int32_t id = h2c_get_next_sid(h2s->h2c);
5329
5330 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005331 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005332 return 0;
5333 }
5334
5335 eb32_delete(&h2s->by_id);
5336 h2s->by_id.key = h2s->id = id;
5337 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005338 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005339 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5340 }
5341
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005342 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005343 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5344 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005345 idx = htx_get_head(htx);
5346 blk = htx_get_blk(htx, idx);
5347 btype = htx_get_blk_type(blk);
5348 bsize = htx_get_blksz(blk);
5349
5350 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005351 case HTX_BLK_REQ_SL:
5352 /* start-line before headers */
5353 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5354 if (ret > 0) {
5355 total += ret;
5356 count -= ret;
5357 if (ret < bsize)
5358 goto done;
5359 }
5360 break;
5361
Willy Tarreau115e83b2018-12-01 19:17:53 +01005362 case HTX_BLK_RES_SL:
5363 /* start-line before headers */
5364 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5365 if (ret > 0) {
5366 total += ret;
5367 count -= ret;
5368 if (ret < bsize)
5369 goto done;
5370 }
5371 break;
5372
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005373 case HTX_BLK_DATA:
5374 case HTX_BLK_EOD:
5375 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005376 /* all these cause the emission of a DATA frame (possibly empty).
5377 * This EOM necessarily is one before trailers, as the EOM following
5378 * trailers would have been consumed by the trailers parser.
5379 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005380 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005381 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005382 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005383 total += ret;
5384 count -= ret;
5385 if (ret < bsize)
5386 goto done;
5387 }
5388 break;
5389
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005390 case HTX_BLK_TLR:
5391 /* This is the first trailers block, all the subsequent ones AND
5392 * the EOM will be swallowed by the parser.
5393 */
5394 ret = h2s_htx_make_trailers(h2s, htx);
5395 if (ret > 0) {
5396 total += ret;
5397 count -= ret;
5398 if (ret < bsize)
5399 goto done;
5400 }
5401 break;
5402
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005403 default:
5404 htx_remove_blk(htx, blk);
5405 total += bsize;
5406 count -= bsize;
5407 break;
5408 }
5409 }
5410 goto done;
5411 }
5412
5413 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005414 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005415 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005416 if (h2s->h2c->flags & H2_CF_IS_BACK)
5417 ret = -1;
5418 else
5419 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005420 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005421 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005422 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005423 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005424 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005425 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005426 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005427
Willy Tarreau5dd17352018-06-14 13:33:30 +02005428 if (unlikely((int)ret <= 0)) {
5429 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005430 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5431 break;
5432 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005433 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005434 total += count;
5435 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005436 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005437 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005438 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005439 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005440 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005441 break;
5442 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005443
5444 total += ret;
5445 count -= ret;
5446
5447 if (h2s->st >= H2_SS_ERROR)
5448 break;
5449
5450 if (h2s->flags & H2_SF_BLK_ANY)
5451 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005452 }
5453
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005454 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005455 if (h2s->st >= H2_SS_ERROR) {
5456 /* trim any possibly pending data after we close (extra CR-LF,
5457 * unprocessed trailers, abnormal extra data, ...)
5458 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005459 total += count;
5460 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005461 }
5462
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005463 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005464 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005465 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005466 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005467 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005468 }
5469
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005470 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005471 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005472 } else {
5473 b_del(buf, total);
5474 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005475
5476 /* The mux is full, cancel the pending tasks */
5477 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5478 (h2s->flags & H2_SF_BLK_MBUSY))
5479 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005480
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005481 /* If we're running HTX, and we read the whole buffer, then pretend
5482 * we read exactly what the caller specified, as with HTX the caller
5483 * will always give the buffer size, instead of the amount of data
5484 * available.
5485 */
5486 if (htx && !b_data(buf))
5487 total = orig_count;
5488
Olivier Houchard7505f942018-08-21 18:10:44 +02005489 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005490 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005491 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005492
Olivier Houchard7505f942018-08-21 18:10:44 +02005493 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005494 /* If we're waiting for flow control, and we got a shutr on the
5495 * connection, we will never be unlocked, so add an error on
5496 * the conn_stream.
5497 */
5498 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5499 !b_data(&h2s->h2c->dbuf) &&
5500 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5501 if (cs->flags & CS_FL_EOS)
5502 cs->flags |= CS_FL_ERROR;
5503 else
5504 cs->flags |= CS_FL_ERR_PENDING;
5505 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01005506 if (total > 0 && h2s->send_wait) {
5507 /* Ok we managed to send something, leave the send_list */
5508 h2s->send_wait->events &= ~SUB_RETRY_SEND;
5509 h2s->send_wait = NULL;
5510 LIST_DEL_INIT(&h2s->list);
5511 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005512 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005513}
5514
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005515/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005516static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005517{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005518 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005519 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005520 struct eb32_node *node;
5521 int fctl_cnt = 0;
5522 int send_cnt = 0;
5523 int tree_cnt = 0;
5524 int orph_cnt = 0;
5525
5526 if (!h2c)
5527 return;
5528
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005529 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005530 fctl_cnt++;
5531
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005532 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005533 send_cnt++;
5534
Willy Tarreau3af37712018-12-18 14:34:41 +01005535 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005536 node = eb32_first(&h2c->streams_by_id);
5537 while (node) {
5538 h2s = container_of(node, struct h2s, by_id);
5539 tree_cnt++;
5540 if (!h2s->cs)
5541 orph_cnt++;
5542 node = eb32_next(node);
5543 }
5544
Willy Tarreau987c0632018-12-18 10:32:05 +01005545 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5546 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5547 " .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 +02005548 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5549 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005550 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005551 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5552 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5553 h2c->msi,
5554 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5555 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5556
5557 if (h2s) {
5558 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5559 h2s, h2s->id, h2s->flags,
5560 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5561 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5562 h2s->cs);
5563 if (h2s->cs)
5564 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5565 h2s->cs->flags, h2s->cs->data);
5566 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005567}
Willy Tarreau62f52692017-10-08 23:01:42 +02005568
5569/*******************************************************/
5570/* functions below are dedicated to the config parsers */
5571/*******************************************************/
5572
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005573/* config parser for global "tune.h2.header-table-size" */
5574static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5575 struct proxy *defpx, const char *file, int line,
5576 char **err)
5577{
5578 if (too_many_args(1, args, err, NULL))
5579 return -1;
5580
5581 h2_settings_header_table_size = atoi(args[1]);
5582 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5583 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5584 return -1;
5585 }
5586 return 0;
5587}
Willy Tarreau62f52692017-10-08 23:01:42 +02005588
Willy Tarreaue6baec02017-07-27 11:45:11 +02005589/* config parser for global "tune.h2.initial-window-size" */
5590static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5591 struct proxy *defpx, const char *file, int line,
5592 char **err)
5593{
5594 if (too_many_args(1, args, err, NULL))
5595 return -1;
5596
5597 h2_settings_initial_window_size = atoi(args[1]);
5598 if (h2_settings_initial_window_size < 0) {
5599 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5600 return -1;
5601 }
5602 return 0;
5603}
5604
Willy Tarreau5242ef82017-07-27 11:47:28 +02005605/* config parser for global "tune.h2.max-concurrent-streams" */
5606static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5607 struct proxy *defpx, const char *file, int line,
5608 char **err)
5609{
5610 if (too_many_args(1, args, err, NULL))
5611 return -1;
5612
5613 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005614 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005615 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5616 return -1;
5617 }
5618 return 0;
5619}
5620
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005621/* config parser for global "tune.h2.max-frame-size" */
5622static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5623 struct proxy *defpx, const char *file, int line,
5624 char **err)
5625{
5626 if (too_many_args(1, args, err, NULL))
5627 return -1;
5628
5629 h2_settings_max_frame_size = atoi(args[1]);
5630 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5631 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5632 return -1;
5633 }
5634 return 0;
5635}
5636
Willy Tarreau62f52692017-10-08 23:01:42 +02005637
5638/****************************************/
5639/* MUX initialization and instanciation */
5640/***************************************/
5641
5642/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005643static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005644 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005645 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005646 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005647 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005648 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005649 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005650 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005651 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005652 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005653 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005654 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005655 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005656 .shutr = h2_shutr,
5657 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005658 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005659 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005660 .name = "H2",
5661};
5662
Christopher Faulet32f61c02018-04-10 14:33:41 +02005663/* PROTO selection : this mux registers PROTO token "h2" */
5664static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005665 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005666
Willy Tarreau0108d902018-11-25 19:14:37 +01005667INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5668
Christopher Faulet9b579102019-04-11 22:52:25 +02005669
5670/* The mux operations */
5671static const struct mux_ops h2_htx_ops = {
5672 .init = h2_init,
5673 .wake = h2_wake,
5674 .snd_buf = h2_snd_buf,
5675 .rcv_buf = h2_rcv_buf,
5676 .subscribe = h2_subscribe,
5677 .unsubscribe = h2_unsubscribe,
5678 .attach = h2_attach,
5679 .get_first_cs = h2_get_first_cs,
5680 .detach = h2_detach,
5681 .destroy = h2_destroy,
5682 .avail_streams = h2_avail_streams,
5683 .used_streams = h2_used_streams,
5684 .shutr = h2_shutr,
5685 .shutw = h2_shutw,
5686 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005687 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005688 .name = "H2",
5689};
5690
Willy Tarreauf8957272018-10-03 10:25:20 +02005691static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005692 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005693
5694INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5695
Willy Tarreau62f52692017-10-08 23:01:42 +02005696/* config keyword parsers */
5697static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005698 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005699 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005700 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005701 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005702 { 0, NULL, NULL }
5703}};
5704
Willy Tarreau0108d902018-11-25 19:14:37 +01005705INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);