blob: adf8f144dbf6a3f22d465fdd94f4ddb73e92c1f4 [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 */
199 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
200 struct wait_event *send_wait; /* The streeam is waiting for flow control */
201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200202};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200203
Willy Tarreauc6405142017-09-21 20:23:50 +0200204/* descriptor for an h2 frame header */
205struct h2_fh {
206 uint32_t len; /* length, host order, 24 bits */
207 uint32_t sid; /* stream id, host order, 31 bits */
208 uint8_t ft; /* frame type */
209 uint8_t ff; /* frame flags */
210};
211
Willy Tarreau8ceae722018-11-26 11:58:30 +0100212/* the h2c connection pool */
213DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
214
215/* the h2s stream pool */
216DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
217
Willy Tarreaudc572362018-12-12 08:08:05 +0100218/* The default connection window size is 65535, it may only be enlarged using
219 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
220 * we'll pretend we already received the difference between the two to send
221 * an equivalent window update to enlarge it to 2G-1.
222 */
223#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
224
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200225/* a few settings from the global section */
226static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200227static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100228static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200229
Willy Tarreau2a856182017-05-16 15:20:39 +0200230/* a dmumy closed stream */
231static const struct h2s *h2_closed_stream = &(const struct h2s){
232 .cs = NULL,
233 .h2c = NULL,
234 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100235 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100236 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200237 .id = 0,
238};
239
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100240/* a dmumy closed stream returning a PROTOCOL_ERROR error */
241static const struct h2s *h2_error_stream = &(const struct h2s){
242 .cs = NULL,
243 .h2c = NULL,
244 .st = H2_SS_CLOSED,
245 .errcode = H2_ERR_PROTOCOL_ERROR,
246 .flags = 0,
247 .id = 0,
248};
249
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100250/* a dmumy closed stream returning a REFUSED_STREAM error */
251static const struct h2s *h2_refused_stream = &(const struct h2s){
252 .cs = NULL,
253 .h2c = NULL,
254 .st = H2_SS_CLOSED,
255 .errcode = H2_ERR_REFUSED_STREAM,
256 .flags = 0,
257 .id = 0,
258};
259
Willy Tarreau2a856182017-05-16 15:20:39 +0200260/* and a dummy idle stream for use with any unannounced stream */
261static const struct h2s *h2_idle_stream = &(const struct h2s){
262 .cs = NULL,
263 .h2c = NULL,
264 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100265 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200266 .id = 0,
267};
268
Olivier Houchard9f6af332018-05-25 14:04:04 +0200269static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200270static int h2_send(struct h2c *h2c);
271static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200272static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200273static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100274static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100275static 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 +0100276static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200277static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100278static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100279static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200280
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200281/*****************************************************/
282/* functions below are for dynamic buffer management */
283/*****************************************************/
284
Willy Tarreau315d8072017-12-10 22:17:57 +0100285/* indicates whether or not the we may call the h2_recv() function to attempt
286 * to receive data into the buffer and/or demux pending data. The condition is
287 * a bit complex due to some API limits for now. The rules are the following :
288 * - if an error or a shutdown was detected on the connection and the buffer
289 * is empty, we must not attempt to receive
290 * - if the demux buf failed to be allocated, we must not try to receive and
291 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100292 * - if no flag indicates a blocking condition, we may attempt to receive,
293 * regardless of whether the demux buffer is full or not, so that only
294 * de demux part decides whether or not to block. This is needed because
295 * the connection API indeed prevents us from re-enabling receipt that is
296 * already enabled in a polled state, so we must always immediately stop
297 * as soon as the demux can't proceed so as never to hit an end of read
298 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100299 * - otherwise must may not attempt
300 */
301static inline int h2_recv_allowed(const struct h2c *h2c)
302{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200303 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100304 (h2c->st0 >= H2_CS_ERROR ||
305 h2c->conn->flags & CO_FL_ERROR ||
306 conn_xprt_read0_pending(h2c->conn)))
307 return 0;
308
309 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100310 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100311 return 1;
312
313 return 0;
314}
315
Willy Tarreau47b515a2018-12-21 16:09:41 +0100316/* restarts reading on the connection if it was not enabled */
317static inline void h2c_restart_reading(const struct h2c *h2c)
318{
319 if (!h2_recv_allowed(h2c))
320 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100321 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100322 return;
323 tasklet_wakeup(h2c->wait_event.task);
324}
325
326
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100327/* returns true if the front connection has too many conn_streams attached */
328static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200329{
Willy Tarreaua8754662018-12-23 20:43:58 +0100330 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200331}
332
Willy Tarreau44e973f2018-03-01 17:49:30 +0100333/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
334 * flags are used to figure what buffer was requested. It returns 1 if the
335 * allocation succeeds, in which case the connection is woken up, or 0 if it's
336 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200337 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100338static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200339{
340 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100341 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200342
Willy Tarreau44e973f2018-03-01 17:49:30 +0100343 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200344 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100345 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200346 return 1;
347 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200348
Willy Tarreau44e973f2018-03-01 17:49:30 +0100349 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
350 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200351
352 if (h2c->flags & H2_CF_DEM_MROOM) {
353 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100354 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200355 }
Willy Tarreau14398122017-09-22 14:26:04 +0200356 return 1;
357 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100358
359 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
360 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200361 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100362 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100363 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100364 return 1;
365 }
366
Willy Tarreau14398122017-09-22 14:26:04 +0200367 return 0;
368}
369
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200370static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200371{
372 struct buffer *buf = NULL;
373
Willy Tarreau44e973f2018-03-01 17:49:30 +0100374 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
375 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
376 h2c->buf_wait.target = h2c;
377 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100378 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100379 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100380 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200381 __conn_xprt_stop_recv(h2c->conn);
382 }
383 return buf;
384}
385
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200386static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200387{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200388 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100389 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200390 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200391 }
392}
393
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100394/* returns the number of allocatable outgoing streams for the connection taking
395 * the last_sid and the reserved ones into account.
396 */
397static inline int h2_streams_left(const struct h2c *h2c)
398{
399 int ret;
400
401 /* consider the number of outgoing streams we're allowed to create before
402 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
403 * nb_reserved is the number of streams which don't yet have an ID.
404 */
405 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
406 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
407 if (ret < 0)
408 ret = 0;
409 return ret;
410}
411
Willy Tarreau00f18a32019-01-26 12:19:01 +0100412/* returns the number of streams in use on a connection to figure if it's
413 * idle or not. We check nb_cs and not nb_streams as the caller will want
414 * to know if it was the last one after a detach().
415 */
416static int h2_used_streams(struct connection *conn)
417{
418 struct h2c *h2c = conn->ctx;
419
420 return h2c->nb_cs;
421}
422
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100423/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100424static int h2_avail_streams(struct connection *conn)
425{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100426 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100427 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100428 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100429
Willy Tarreau6afec462019-01-28 06:40:19 +0100430 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
431 * streams on the connection.
432 */
433 if (h2c->last_sid >= 0)
434 return 0;
435
Willy Tarreau86949782019-01-31 10:42:05 +0100436 /* note: may be negative if a SETTINGS frame changes the limit */
437 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100438
439 /* we must also consider the limit imposed by stream IDs */
440 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100441 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100442 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100443 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
444 ret1 = MIN(ret1, ret2);
445 }
446 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100447}
448
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200449
Willy Tarreau62f52692017-10-08 23:01:42 +0200450/*****************************************************************/
451/* functions below are dedicated to the mux setup and management */
452/*****************************************************************/
453
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200454/* Initialize the mux once it's attached. For outgoing connections, the context
455 * is already initialized before installing the mux, so we detect incoming
456 * connections from the fact that the context is still NULL. Returns < 0 on
457 * error.
458 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100459static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200460{
461 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100462 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200463
Willy Tarreaubafbe012017-11-24 17:34:44 +0100464 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200465 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200466 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200467
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100468 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200469 h2c->flags = H2_CF_IS_BACK;
470 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
471 if (tick_isset(prx->timeout.serverfin))
472 h2c->shut_timeout = prx->timeout.serverfin;
473 } else {
474 h2c->flags = H2_CF_NONE;
475 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
476 if (tick_isset(prx->timeout.clientfin))
477 h2c->shut_timeout = prx->timeout.clientfin;
478 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100479
Willy Tarreau0b37d652018-10-03 10:33:02 +0200480 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100481 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100482 if (tick_isset(h2c->timeout)) {
483 t = task_new(tid_bit);
484 if (!t)
485 goto fail;
486
487 h2c->task = t;
488 t->process = h2_timeout_task;
489 t->context = h2c;
490 t->expire = tick_add(now_ms, h2c->timeout);
491 }
Willy Tarreauea392822017-10-31 10:02:25 +0100492
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200493 h2c->wait_event.task = tasklet_new();
494 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200495 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200496 h2c->wait_event.task->process = h2_io_cb;
497 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100498 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200499
Willy Tarreau32218eb2017-09-22 08:07:25 +0200500 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
501 if (!h2c->ddht)
502 goto fail;
503
504 /* Initialise the context. */
505 h2c->st0 = H2_CS_PREFACE;
506 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100507 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200508 h2c->max_id = -1;
509 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100510 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200511 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100512 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200513 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100514 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100515 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200516
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200517 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200518 h2c->dsi = -1;
519 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100520
Willy Tarreau32218eb2017-09-22 08:07:25 +0200521 h2c->last_sid = -1;
522
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200523 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200524 h2c->miw = 65535; /* mux initial window size */
525 h2c->mws = 65535; /* mux window size */
526 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200527 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200528 LIST_INIT(&h2c->send_list);
529 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200530 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100531 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200532
Willy Tarreau3f133572017-10-31 19:21:06 +0100533 if (t)
534 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100535
Willy Tarreau01b44822018-10-03 14:26:37 +0200536 if (h2c->flags & H2_CF_IS_BACK) {
537 /* FIXME: this is temporary, for outgoing connections we need
538 * to immediately allocate a stream until the code is modified
539 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100540 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200541 */
542 struct h2s *h2s;
543
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100544 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200545 if (!h2s)
546 goto fail_stream;
547 }
548
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100549 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200550
Willy Tarreau0f383582018-10-03 14:22:21 +0200551 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100552 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200554 fail_stream:
555 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200556 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100557 if (t)
558 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200559 if (h2c->wait_event.task)
560 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100561 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200562 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200563 return -1;
564}
565
Willy Tarreau751f2d02018-10-05 09:35:00 +0200566/* returns the next allocatable outgoing stream ID for the H2 connection, or
567 * -1 if no more is allocatable.
568 */
569static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
570{
571 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100572
573 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200574 id = -1;
575 return id;
576}
577
Willy Tarreau2373acc2017-10-12 17:35:14 +0200578/* returns the stream associated with id <id> or NULL if not found */
579static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
580{
581 struct eb32_node *node;
582
Willy Tarreau751f2d02018-10-05 09:35:00 +0200583 if (id == 0)
584 return (struct h2s *)h2_closed_stream;
585
Willy Tarreau2a856182017-05-16 15:20:39 +0200586 if (id > h2c->max_id)
587 return (struct h2s *)h2_idle_stream;
588
Willy Tarreau2373acc2017-10-12 17:35:14 +0200589 node = eb32_lookup(&h2c->streams_by_id, id);
590 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200591 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200592
593 return container_of(node, struct h2s, by_id);
594}
595
Willy Tarreau62f52692017-10-08 23:01:42 +0200596/* release function for a connection. This one should be called to free all
597 * resources allocated to the mux.
598 */
599static void h2_release(struct connection *conn)
600{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100601 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200602
Willy Tarreau32218eb2017-09-22 08:07:25 +0200603 if (h2c) {
604 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200605
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100606 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100607 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100608 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200609
Willy Tarreau44e973f2018-03-01 17:49:30 +0100610 h2_release_buf(h2c, &h2c->dbuf);
611 h2_release_buf(h2c, &h2c->mbuf);
612
Willy Tarreauea392822017-10-31 10:02:25 +0100613 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200614 h2c->task->context = NULL;
615 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100616 h2c->task = NULL;
617 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200618 if (h2c->wait_event.task)
619 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100620 if (h2c->wait_event.events != 0)
621 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200622 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100623
Willy Tarreaubafbe012017-11-24 17:34:44 +0100624 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200625 }
626
627 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100628 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200629
630 conn_stop_tracking(conn);
631 conn_full_close(conn);
632 if (conn->destroy_cb)
633 conn->destroy_cb(conn);
634 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200635}
636
637
Willy Tarreau71681172017-10-23 14:39:06 +0200638/******************************************************/
639/* functions below are for the H2 protocol processing */
640/******************************************************/
641
642/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100643static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200644{
645 return h2s ? h2s->id : 0;
646}
647
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200648/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100649static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200650{
651 if (h2c->msi < 0)
652 return 0;
653
654 if (h2c->msi == h2s_id(h2s))
655 return 0;
656
657 return 1;
658}
659
Willy Tarreau741d6df2017-10-17 08:00:59 +0200660/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100661static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200662{
663 h2c->errcode = err;
664 h2c->st0 = H2_CS_ERROR;
665}
666
Willy Tarreau175cebb2019-01-24 10:02:24 +0100667/* marks an error on the stream. It may also update an already closed stream
668 * (e.g. to report an error after an RST was received).
669 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100670static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200671{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100672 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200673 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100674 if (h2s->st < H2_SS_ERROR)
675 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100676 if (h2s->cs)
677 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200678 }
679}
680
Willy Tarreau7e094452018-12-19 18:08:52 +0100681/* attempt to notify the data layer of recv availability */
682static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
683{
684 struct wait_event *sw;
685
686 if (h2s->recv_wait) {
687 sw = h2s->recv_wait;
688 sw->events &= ~SUB_RETRY_RECV;
689 tasklet_wakeup(sw->task);
690 h2s->recv_wait = NULL;
691 }
692}
693
694/* attempt to notify the data layer of send availability */
695static void __maybe_unused h2s_notify_send(struct h2s *h2s)
696{
697 struct wait_event *sw;
698
699 if (h2s->send_wait) {
700 sw = h2s->send_wait;
701 sw->events &= ~SUB_RETRY_SEND;
702 tasklet_wakeup(sw->task);
703 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100704 LIST_DEL(&h2s->list);
705 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100706 }
707}
708
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100709/* alerts the data layer, trying to wake it up by all means, following
710 * this sequence :
711 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
712 * - if its subscribed to send, then it's woken up for send
713 * - if it was subscribed to neither, its ->wake() callback is called
714 * It is safe to call this function with a closed stream which doesn't have a
715 * conn_stream anymore.
716 */
717static void __maybe_unused h2s_alert(struct h2s *h2s)
718{
719 if (h2s->recv_wait || h2s->send_wait) {
720 h2s_notify_recv(h2s);
721 h2s_notify_send(h2s);
722 }
723 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
724 h2s->cs->data_cb->wake(h2s->cs);
725}
726
Willy Tarreaue4820742017-07-27 13:37:23 +0200727/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100728static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200729{
730 uint8_t *out = frame;
731
732 *out = len >> 16;
733 write_n16(out + 1, len);
734}
735
Willy Tarreau54c15062017-10-10 17:10:03 +0200736/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
737 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
738 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200739 * available in the buffer's input prior to calling this function. The buffer
740 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200741 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100742static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200743 const struct buffer *b, int o)
744{
Willy Tarreau591d4452018-06-15 17:21:00 +0200745 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 +0200746}
747
Willy Tarreau1f094672017-11-20 21:27:45 +0100748static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200749{
Willy Tarreau591d4452018-06-15 17:21:00 +0200750 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200751}
752
Willy Tarreau1f094672017-11-20 21:27:45 +0100753static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200754{
Willy Tarreau591d4452018-06-15 17:21:00 +0200755 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200756}
757
Willy Tarreau1f094672017-11-20 21:27:45 +0100758static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200759{
Willy Tarreau591d4452018-06-15 17:21:00 +0200760 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200761}
762
763
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100764/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
765 * The algorithm is not obvious. It turns out that H2 headers are neither
766 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
767 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200768 *
769 * b0 b1 b2 b3 b4 b5..b8
770 * +----------+---------+--------+----+----+----------------------+
771 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
772 * +----------+---------+--------+----+----+----------------------+
773 *
774 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
775 * we get the sid properly aligned and ordered, and 16 bits of len properly
776 * ordered as well. The type and flags can be extracted using bit shifts from
777 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200778 * Returns zero if some bytes are missing, otherwise non-zero on success. The
779 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200780 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100781static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200782{
783 uint64_t w;
784
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100785 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200786 return 0;
787
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100788 w = h2_get_n64(b, o + 1);
789 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200790 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
791 h->ff = w >> 32;
792 h->ft = w >> 40;
793 h->len += w >> 48;
794 return 1;
795}
796
797/* skip the next 9 bytes corresponding to the frame header possibly parsed by
798 * h2_peek_frame_hdr() above.
799 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100800static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200801{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200802 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200803}
804
805/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100806static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200807{
808 int ret;
809
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100810 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200811 if (ret > 0)
812 h2_skip_frame_hdr(b);
813 return ret;
814}
815
Willy Tarreau00dd0782018-03-01 16:31:34 +0100816/* marks stream <h2s> as CLOSED and decrement the number of active streams for
817 * its connection if the stream was not yet closed. Please use this exclusively
818 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100819 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100820static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100821{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100822 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100823 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100824 if (!h2s->id)
825 h2s->h2c->nb_reserved--;
826 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100827 h2s->st = H2_SS_CLOSED;
828}
829
Willy Tarreau71049cc2018-03-28 13:56:39 +0200830/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
831static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100832{
833 h2s_close(h2s);
834 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200835 if (b_size(&h2s->rxbuf)) {
836 b_free(&h2s->rxbuf);
837 offer_buffers(NULL, tasks_run_queue);
838 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200839 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100840 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200841 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100842 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800843 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200844 * reference left would be in the h2c send_list/fctl_list, and if
845 * we're in it, we're getting out anyway
846 */
847 LIST_DEL(&h2s->list);
848 LIST_INIT(&h2s->list);
849 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100850 pool_free(pool_head_h2s, h2s);
851}
852
Willy Tarreaua8e49542018-10-03 18:53:55 +0200853/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
854 * stream tree. In case of error, nothing is added and NULL is returned. The
855 * causes of errors can be any failed memory allocation. The caller is
856 * responsible for checking if the connection may support an extra stream
857 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200858 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200859static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200860{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200861 struct h2s *h2s;
862
Willy Tarreaubafbe012017-11-24 17:34:44 +0100863 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200864 if (!h2s)
865 goto out;
866
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200867 h2s->wait_event.task = tasklet_new();
868 if (!h2s->wait_event.task) {
869 pool_free(pool_head_h2s, h2s);
870 goto out;
871 }
872 h2s->send_wait = NULL;
873 h2s->recv_wait = NULL;
874 h2s->wait_event.task->process = h2_deferred_shut;
875 h2s->wait_event.task->context = h2s;
876 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100877 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200878 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200879 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200880 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200881 h2s->mws = h2c->miw;
882 h2s->flags = H2_SF_NONE;
883 h2s->errcode = H2_ERR_NO_ERROR;
884 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200885 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100886 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200887 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200888
889 if (h2c->flags & H2_CF_IS_BACK) {
890 h1m_init_req(&h2s->h1m);
891 h2s->h1m.err_pos = -1; // don't care about errors on the request path
892 h2s->h1m.flags |= H1_MF_TOLOWER;
893 } else {
894 h1m_init_res(&h2s->h1m);
895 h2s->h1m.err_pos = -1; // don't care about errors on the response path
896 h2s->h1m.flags |= H1_MF_TOLOWER;
897 }
898
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200899 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200900 if (id > 0)
901 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100902 else
903 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200904
905 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100906 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100907 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200908
909 return h2s;
910
911 out_free_h2s:
912 pool_free(pool_head_h2s, h2s);
913 out:
914 return NULL;
915}
916
917/* creates a new stream <id> on the h2c connection and returns it, or NULL in
918 * case of memory allocation error.
919 */
920static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
921{
922 struct session *sess = h2c->conn->owner;
923 struct conn_stream *cs;
924 struct h2s *h2s;
925
926 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
927 goto out;
928
929 h2s = h2s_new(h2c, id);
930 if (!h2s)
931 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200932
933 cs = cs_new(h2c->conn);
934 if (!cs)
935 goto out_close;
936
Olivier Houchard746fb772018-12-15 19:42:00 +0100937 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200938 h2s->cs = cs;
939 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200940 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200941
942 if (stream_create_from_cs(cs) < 0)
943 goto out_free_cs;
944
Willy Tarreau590a0512018-09-05 11:56:48 +0200945 /* We want the accept date presented to the next stream to be the one
946 * we have now, the handshake time to be null (since the next stream
947 * is not delayed by a handshake), and the idle time to count since
948 * right now.
949 */
950 sess->accept_date = date;
951 sess->tv_accept = now;
952 sess->t_handshake = 0;
953
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200954 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100955 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200956 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200957 return h2s;
958
959 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200960 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200961 cs_free(cs);
962 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200963 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200964 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200965 sess_log(sess);
966 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200967}
968
Willy Tarreau751f2d02018-10-05 09:35:00 +0200969/* allocates a new stream associated to conn_stream <cs> on the h2c connection
970 * and returns it, or NULL in case of memory allocation error or if the highest
971 * possible stream ID was reached.
972 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100973static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200974{
975 struct h2s *h2s = NULL;
976
Willy Tarreau86949782019-01-31 10:42:05 +0100977 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200978 goto out;
979
Willy Tarreaua80dca82019-01-24 17:08:28 +0100980 if (h2_streams_left(h2c) < 1)
981 goto out;
982
Willy Tarreau751f2d02018-10-05 09:35:00 +0200983 /* Defer choosing the ID until we send the first message to create the stream */
984 h2s = h2s_new(h2c, 0);
985 if (!h2s)
986 goto out;
987
988 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100989 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200990 cs->ctx = h2s;
991 h2c->nb_cs++;
992
Willy Tarreau751f2d02018-10-05 09:35:00 +0200993 out:
994 return h2s;
995}
996
Willy Tarreaube5b7152017-09-25 16:25:39 +0200997/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
998 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
999 * the various settings codes.
1000 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001001static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001002{
1003 struct buffer *res;
1004 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001005 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001006 int ret;
1007
1008 if (h2c_mux_busy(h2c, NULL)) {
1009 h2c->flags |= H2_CF_DEM_MBUSY;
1010 return 0;
1011 }
1012
Willy Tarreau44e973f2018-03-01 17:49:30 +01001013 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001014 if (!res) {
1015 h2c->flags |= H2_CF_MUX_MALLOC;
1016 h2c->flags |= H2_CF_DEM_MROOM;
1017 return 0;
1018 }
1019
1020 chunk_init(&buf, buf_data, sizeof(buf_data));
1021 chunk_memcpy(&buf,
1022 "\x00\x00\x00" /* length : 0 for now */
1023 "\x04\x00" /* type : 4 (settings), flags : 0 */
1024 "\x00\x00\x00\x00", /* stream ID : 0 */
1025 9);
1026
1027 if (h2_settings_header_table_size != 4096) {
1028 char str[6] = "\x00\x01"; /* header_table_size */
1029
1030 write_n32(str + 2, h2_settings_header_table_size);
1031 chunk_memcat(&buf, str, 6);
1032 }
1033
1034 if (h2_settings_initial_window_size != 65535) {
1035 char str[6] = "\x00\x04"; /* initial_window_size */
1036
1037 write_n32(str + 2, h2_settings_initial_window_size);
1038 chunk_memcat(&buf, str, 6);
1039 }
1040
1041 if (h2_settings_max_concurrent_streams != 0) {
1042 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1043
1044 /* Note: 0 means "unlimited" for haproxy's config but not for
1045 * the protocol, so never send this value!
1046 */
1047 write_n32(str + 2, h2_settings_max_concurrent_streams);
1048 chunk_memcat(&buf, str, 6);
1049 }
1050
1051 if (global.tune.bufsize != 16384) {
1052 char str[6] = "\x00\x05"; /* max_frame_size */
1053
1054 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1055 * match bufsize - rewrite size, but at the moment it seems
1056 * that clients don't take care of it.
1057 */
1058 write_n32(str + 2, global.tune.bufsize);
1059 chunk_memcat(&buf, str, 6);
1060 }
1061
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001062 h2_set_frame_size(buf.area, buf.data - 9);
1063 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001064 if (unlikely(ret <= 0)) {
1065 if (!ret) {
1066 h2c->flags |= H2_CF_MUX_MFULL;
1067 h2c->flags |= H2_CF_DEM_MROOM;
1068 return 0;
1069 }
1070 else {
1071 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1072 return 0;
1073 }
1074 }
1075 return ret;
1076}
1077
Willy Tarreau52eed752017-09-22 15:05:09 +02001078/* Try to receive a connection preface, then upon success try to send our
1079 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1080 * missing data. It may return an error in h2c.
1081 */
1082static int h2c_frt_recv_preface(struct h2c *h2c)
1083{
1084 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001085 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001086
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001087 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001088
1089 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001090 if (ret1 < 0)
1091 sess_log(h2c->conn->owner);
1092
Willy Tarreau52eed752017-09-22 15:05:09 +02001093 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1094 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1095 return 0;
1096 }
1097
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001098 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001099 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001100 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001101
Willy Tarreaube5b7152017-09-25 16:25:39 +02001102 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001103}
1104
Willy Tarreau01b44822018-10-03 14:26:37 +02001105/* Try to send a connection preface, then upon success try to send our
1106 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1107 * missing data. It may return an error in h2c.
1108 */
1109static int h2c_bck_send_preface(struct h2c *h2c)
1110{
1111 struct buffer *res;
1112
1113 if (h2c_mux_busy(h2c, NULL)) {
1114 h2c->flags |= H2_CF_DEM_MBUSY;
1115 return 0;
1116 }
1117
1118 res = h2_get_buf(h2c, &h2c->mbuf);
1119 if (!res) {
1120 h2c->flags |= H2_CF_MUX_MALLOC;
1121 h2c->flags |= H2_CF_DEM_MROOM;
1122 return 0;
1123 }
1124
1125 if (!b_data(res)) {
1126 /* preface not yet sent */
1127 b_istput(res, ist(H2_CONN_PREFACE));
1128 }
1129
1130 return h2c_send_settings(h2c);
1131}
1132
Willy Tarreau081d4722017-05-16 21:51:05 +02001133/* try to send a GOAWAY frame on the connection to report an error or a graceful
1134 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1135 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1136 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1137 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1138 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1139 * on unrecoverable failure. It will not attempt to send one again in this last
1140 * case so that it is safe to use h2c_error() to report such errors.
1141 */
1142static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1143{
1144 struct buffer *res;
1145 char str[17];
1146 int ret;
1147
1148 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1149 return 1; // claim that it worked
1150
1151 if (h2c_mux_busy(h2c, h2s)) {
1152 if (h2s)
1153 h2s->flags |= H2_SF_BLK_MBUSY;
1154 else
1155 h2c->flags |= H2_CF_DEM_MBUSY;
1156 return 0;
1157 }
1158
Willy Tarreau44e973f2018-03-01 17:49:30 +01001159 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001160 if (!res) {
1161 h2c->flags |= H2_CF_MUX_MALLOC;
1162 if (h2s)
1163 h2s->flags |= H2_SF_BLK_MROOM;
1164 else
1165 h2c->flags |= H2_CF_DEM_MROOM;
1166 return 0;
1167 }
1168
1169 /* len: 8, type: 7, flags: none, sid: 0 */
1170 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1171
1172 if (h2c->last_sid < 0)
1173 h2c->last_sid = h2c->max_id;
1174
1175 write_n32(str + 9, h2c->last_sid);
1176 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001177 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001178 if (unlikely(ret <= 0)) {
1179 if (!ret) {
1180 h2c->flags |= H2_CF_MUX_MFULL;
1181 if (h2s)
1182 h2s->flags |= H2_SF_BLK_MROOM;
1183 else
1184 h2c->flags |= H2_CF_DEM_MROOM;
1185 return 0;
1186 }
1187 else {
1188 /* we cannot report this error using GOAWAY, so we mark
1189 * it and claim a success.
1190 */
1191 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1192 h2c->flags |= H2_CF_GOAWAY_FAILED;
1193 return 1;
1194 }
1195 }
1196 h2c->flags |= H2_CF_GOAWAY_SENT;
1197 return ret;
1198}
1199
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001200/* Try to send an RST_STREAM frame on the connection for the indicated stream
1201 * during mux operations. This stream must be valid and cannot be closed
1202 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1203 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1204 * not yet.
1205 *
1206 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1207 * to write the message, it subscribes the stream to future notifications.
1208 */
1209static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1210{
1211 struct buffer *res;
1212 char str[13];
1213 int ret;
1214
1215 if (!h2s || h2s->st == H2_SS_CLOSED)
1216 return 1;
1217
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001218 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1219 * RST_STREAM in response to a RST_STREAM frame.
1220 */
1221 if (h2c->dft == H2_FT_RST_STREAM) {
1222 ret = 1;
1223 goto ignore;
1224 }
1225
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001226 if (h2c_mux_busy(h2c, h2s)) {
1227 h2s->flags |= H2_SF_BLK_MBUSY;
1228 return 0;
1229 }
1230
Willy Tarreau44e973f2018-03-01 17:49:30 +01001231 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001232 if (!res) {
1233 h2c->flags |= H2_CF_MUX_MALLOC;
1234 h2s->flags |= H2_SF_BLK_MROOM;
1235 return 0;
1236 }
1237
1238 /* len: 4, type: 3, flags: none */
1239 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1240 write_n32(str + 5, h2s->id);
1241 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001242 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001243
1244 if (unlikely(ret <= 0)) {
1245 if (!ret) {
1246 h2c->flags |= H2_CF_MUX_MFULL;
1247 h2s->flags |= H2_SF_BLK_MROOM;
1248 return 0;
1249 }
1250 else {
1251 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1252 return 0;
1253 }
1254 }
1255
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001256 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001257 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001258 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001259 return ret;
1260}
1261
1262/* Try to send an RST_STREAM frame on the connection for the stream being
1263 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001264 * error code, even if the stream is one of the dummy ones, and will update
1265 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001266 *
1267 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1268 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001269 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001270 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001271 */
1272static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1273{
1274 struct buffer *res;
1275 char str[13];
1276 int ret;
1277
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001278 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1279 * RST_STREAM in response to a RST_STREAM frame.
1280 */
1281 if (h2c->dft == H2_FT_RST_STREAM) {
1282 ret = 1;
1283 goto ignore;
1284 }
1285
Willy Tarreau27a84c92017-10-17 08:10:17 +02001286 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001287 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001288 return 0;
1289 }
1290
Willy Tarreau44e973f2018-03-01 17:49:30 +01001291 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001292 if (!res) {
1293 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001294 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001295 return 0;
1296 }
1297
1298 /* len: 4, type: 3, flags: none */
1299 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001300
Willy Tarreau27a84c92017-10-17 08:10:17 +02001301 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001302 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001303 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001304
Willy Tarreau27a84c92017-10-17 08:10:17 +02001305 if (unlikely(ret <= 0)) {
1306 if (!ret) {
1307 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001308 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001309 return 0;
1310 }
1311 else {
1312 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1313 return 0;
1314 }
1315 }
1316
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001317 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001318 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001319 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001320 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001321 }
1322
Willy Tarreau27a84c92017-10-17 08:10:17 +02001323 return ret;
1324}
1325
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001326/* try to send an empty DATA frame with the ES flag set to notify about the
1327 * end of stream and match a shutdown(write). If an ES was already sent as
1328 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1329 * on success or zero if nothing was done. In case of lack of room to write the
1330 * message, it subscribes the requesting stream to future notifications.
1331 */
1332static int h2_send_empty_data_es(struct h2s *h2s)
1333{
1334 struct h2c *h2c = h2s->h2c;
1335 struct buffer *res;
1336 char str[9];
1337 int ret;
1338
Willy Tarreau721c9742017-11-07 11:05:42 +01001339 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001340 return 1;
1341
1342 if (h2c_mux_busy(h2c, h2s)) {
1343 h2s->flags |= H2_SF_BLK_MBUSY;
1344 return 0;
1345 }
1346
Willy Tarreau44e973f2018-03-01 17:49:30 +01001347 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001348 if (!res) {
1349 h2c->flags |= H2_CF_MUX_MALLOC;
1350 h2s->flags |= H2_SF_BLK_MROOM;
1351 return 0;
1352 }
1353
1354 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1355 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1356 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001357 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001358 if (likely(ret > 0)) {
1359 h2s->flags |= H2_SF_ES_SENT;
1360 }
1361 else if (!ret) {
1362 h2c->flags |= H2_CF_MUX_MFULL;
1363 h2s->flags |= H2_SF_BLK_MROOM;
1364 return 0;
1365 }
1366 else {
1367 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1368 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001369 }
1370 return ret;
1371}
1372
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001373/* wake the streams attached to the connection, whose id is greater than <last>,
1374 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001375 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1376 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001377 */
1378static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1379{
1380 struct eb32_node *node;
1381 struct h2s *h2s;
1382
1383 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001384 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001385
1386 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001387 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001388
1389 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1390 while (node) {
1391 h2s = container_of(node, struct h2s, by_id);
1392 if (h2s->id <= last)
1393 break;
1394 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001395
1396 if (!h2s->cs) {
1397 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001398 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001399 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001400 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001401
1402 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001403 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1404 h2s->cs->flags |= CS_FL_ERROR;
1405
Willy Tarreauf830f012018-12-19 17:44:55 +01001406 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001407
Willy Tarreaua8519352018-12-18 16:44:28 +01001408 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001409 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001410 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001411 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001412 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001413 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001414 }
1415}
1416
Willy Tarreau3421aba2017-07-27 15:41:03 +02001417/* Increase all streams' outgoing window size by the difference passed in
1418 * argument. This is needed upon receipt of the settings frame if the initial
1419 * window size is different. The difference may be negative and the resulting
1420 * window size as well, for the time it takes to receive some window updates.
1421 */
1422static void h2c_update_all_ws(struct h2c *h2c, int diff)
1423{
1424 struct h2s *h2s;
1425 struct eb32_node *node;
1426
1427 if (!diff)
1428 return;
1429
1430 node = eb32_first(&h2c->streams_by_id);
1431 while (node) {
1432 h2s = container_of(node, struct h2s, by_id);
1433 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001434
1435 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1436 h2s->flags &= ~H2_SF_BLK_SFCTL;
1437 if (h2s->send_wait)
1438 LIST_ADDQ(&h2c->send_list, &h2s->list);
1439
1440 }
1441
Willy Tarreau3421aba2017-07-27 15:41:03 +02001442 node = eb32_next(node);
1443 }
1444}
1445
1446/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1447 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001448 * return an error in h2c. The caller must have already verified frame length
1449 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001450 */
1451static int h2c_handle_settings(struct h2c *h2c)
1452{
1453 unsigned int offset;
1454 int error;
1455
1456 if (h2c->dff & H2_F_SETTINGS_ACK) {
1457 if (h2c->dfl) {
1458 error = H2_ERR_FRAME_SIZE_ERROR;
1459 goto fail;
1460 }
1461 return 1;
1462 }
1463
Willy Tarreau3421aba2017-07-27 15:41:03 +02001464 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001465 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001466 return 0;
1467
1468 /* parse the frame */
1469 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001470 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1471 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001472
1473 switch (type) {
1474 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1475 /* we need to update all existing streams with the
1476 * difference from the previous iws.
1477 */
1478 if (arg < 0) { // RFC7540#6.5.2
1479 error = H2_ERR_FLOW_CONTROL_ERROR;
1480 goto fail;
1481 }
1482 h2c_update_all_ws(h2c, arg - h2c->miw);
1483 h2c->miw = arg;
1484 break;
1485 case H2_SETTINGS_MAX_FRAME_SIZE:
1486 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1487 error = H2_ERR_PROTOCOL_ERROR;
1488 goto fail;
1489 }
1490 h2c->mfs = arg;
1491 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001492 case H2_SETTINGS_ENABLE_PUSH:
1493 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1494 error = H2_ERR_PROTOCOL_ERROR;
1495 goto fail;
1496 }
1497 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001498 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1499 if (h2c->flags & H2_CF_IS_BACK) {
1500 /* the limit is only for the backend; for the frontend it is our limit */
1501 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1502 arg = h2_settings_max_concurrent_streams;
1503 h2c->streams_limit = arg;
1504 }
1505 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001506 }
1507 }
1508
1509 /* need to ACK this frame now */
1510 h2c->st0 = H2_CS_FRAME_A;
1511 return 1;
1512 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001513 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001514 h2c_error(h2c, error);
1515 return 0;
1516}
1517
1518/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1519 * success or one of the h2_status values.
1520 */
1521static int h2c_ack_settings(struct h2c *h2c)
1522{
1523 struct buffer *res;
1524 char str[9];
1525 int ret = -1;
1526
1527 if (h2c_mux_busy(h2c, NULL)) {
1528 h2c->flags |= H2_CF_DEM_MBUSY;
1529 return 0;
1530 }
1531
Willy Tarreau44e973f2018-03-01 17:49:30 +01001532 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001533 if (!res) {
1534 h2c->flags |= H2_CF_MUX_MALLOC;
1535 h2c->flags |= H2_CF_DEM_MROOM;
1536 return 0;
1537 }
1538
1539 memcpy(str,
1540 "\x00\x00\x00" /* length : 0 (no data) */
1541 "\x04" "\x01" /* type : 4, flags : ACK */
1542 "\x00\x00\x00\x00" /* stream ID */, 9);
1543
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001544 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001545 if (unlikely(ret <= 0)) {
1546 if (!ret) {
1547 h2c->flags |= H2_CF_MUX_MFULL;
1548 h2c->flags |= H2_CF_DEM_MROOM;
1549 return 0;
1550 }
1551 else {
1552 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1553 return 0;
1554 }
1555 }
1556 return ret;
1557}
1558
Willy Tarreaucf68c782017-10-10 17:11:41 +02001559/* processes a PING frame and schedules an ACK if needed. The caller must pass
1560 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001561 * missing data. The caller must have already verified frame length
1562 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001563 */
1564static int h2c_handle_ping(struct h2c *h2c)
1565{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001566 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001567 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001568 h2c->st0 = H2_CS_FRAME_A;
1569 return 1;
1570}
1571
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001572/* Try to send a window update for stream id <sid> and value <increment>.
1573 * Returns > 0 on success or zero on missing room or failure. It may return an
1574 * error in h2c.
1575 */
1576static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1577{
1578 struct buffer *res;
1579 char str[13];
1580 int ret = -1;
1581
1582 if (h2c_mux_busy(h2c, NULL)) {
1583 h2c->flags |= H2_CF_DEM_MBUSY;
1584 return 0;
1585 }
1586
Willy Tarreau44e973f2018-03-01 17:49:30 +01001587 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001588 if (!res) {
1589 h2c->flags |= H2_CF_MUX_MALLOC;
1590 h2c->flags |= H2_CF_DEM_MROOM;
1591 return 0;
1592 }
1593
1594 /* length: 4, type: 8, flags: none */
1595 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1596 write_n32(str + 5, sid);
1597 write_n32(str + 9, increment);
1598
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001599 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001600
1601 if (unlikely(ret <= 0)) {
1602 if (!ret) {
1603 h2c->flags |= H2_CF_MUX_MFULL;
1604 h2c->flags |= H2_CF_DEM_MROOM;
1605 return 0;
1606 }
1607 else {
1608 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1609 return 0;
1610 }
1611 }
1612 return ret;
1613}
1614
1615/* try to send pending window update for the connection. It's safe to call it
1616 * with no pending updates. Returns > 0 on success or zero on missing room or
1617 * failure. It may return an error in h2c.
1618 */
1619static int h2c_send_conn_wu(struct h2c *h2c)
1620{
1621 int ret = 1;
1622
1623 if (h2c->rcvd_c <= 0)
1624 return 1;
1625
Willy Tarreau97aaa672018-12-23 09:49:04 +01001626 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1627 /* increase the advertised connection window to 2G on
1628 * first update.
1629 */
1630 h2c->flags |= H2_CF_WINDOW_OPENED;
1631 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1632 }
1633
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001634 /* send WU for the connection */
1635 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1636 if (ret > 0)
1637 h2c->rcvd_c = 0;
1638
1639 return ret;
1640}
1641
1642/* try to send pending window update for the current dmux stream. It's safe to
1643 * call it with no pending updates. Returns > 0 on success or zero on missing
1644 * room or failure. It may return an error in h2c.
1645 */
1646static int h2c_send_strm_wu(struct h2c *h2c)
1647{
1648 int ret = 1;
1649
1650 if (h2c->rcvd_s <= 0)
1651 return 1;
1652
1653 /* send WU for the stream */
1654 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1655 if (ret > 0)
1656 h2c->rcvd_s = 0;
1657
1658 return ret;
1659}
1660
Willy Tarreaucf68c782017-10-10 17:11:41 +02001661/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1662 * success, 0 on missing data or one of the h2_status values.
1663 */
1664static int h2c_ack_ping(struct h2c *h2c)
1665{
1666 struct buffer *res;
1667 char str[17];
1668 int ret = -1;
1669
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001670 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001671 return 0;
1672
1673 if (h2c_mux_busy(h2c, NULL)) {
1674 h2c->flags |= H2_CF_DEM_MBUSY;
1675 return 0;
1676 }
1677
Willy Tarreau44e973f2018-03-01 17:49:30 +01001678 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001679 if (!res) {
1680 h2c->flags |= H2_CF_MUX_MALLOC;
1681 h2c->flags |= H2_CF_DEM_MROOM;
1682 return 0;
1683 }
1684
1685 memcpy(str,
1686 "\x00\x00\x08" /* length : 8 (same payload) */
1687 "\x06" "\x01" /* type : 6, flags : ACK */
1688 "\x00\x00\x00\x00" /* stream ID */, 9);
1689
1690 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001691 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001692
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001693 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001694 if (unlikely(ret <= 0)) {
1695 if (!ret) {
1696 h2c->flags |= H2_CF_MUX_MFULL;
1697 h2c->flags |= H2_CF_DEM_MROOM;
1698 return 0;
1699 }
1700 else {
1701 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1702 return 0;
1703 }
1704 }
1705 return ret;
1706}
1707
Willy Tarreau26f95952017-07-27 17:18:30 +02001708/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1709 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001710 * h2c or h2s. The caller must have already verified frame length and stream ID
1711 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001712 */
1713static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1714{
1715 int32_t inc;
1716 int error;
1717
Willy Tarreau26f95952017-07-27 17:18:30 +02001718 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001719 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001720 return 0;
1721
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001722 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001723
1724 if (h2c->dsi != 0) {
1725 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001726
1727 /* it's not an error to receive WU on a closed stream */
1728 if (h2s->st == H2_SS_CLOSED)
1729 return 1;
1730
1731 if (!inc) {
1732 error = H2_ERR_PROTOCOL_ERROR;
1733 goto strm_err;
1734 }
1735
1736 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1737 error = H2_ERR_FLOW_CONTROL_ERROR;
1738 goto strm_err;
1739 }
1740
1741 h2s->mws += inc;
1742 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1743 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001744 if (h2s->send_wait)
1745 LIST_ADDQ(&h2c->send_list, &h2s->list);
1746
Willy Tarreau26f95952017-07-27 17:18:30 +02001747 }
1748 }
1749 else {
1750 /* connection window update */
1751 if (!inc) {
1752 error = H2_ERR_PROTOCOL_ERROR;
1753 goto conn_err;
1754 }
1755
1756 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1757 error = H2_ERR_FLOW_CONTROL_ERROR;
1758 goto conn_err;
1759 }
1760
1761 h2c->mws += inc;
1762 }
1763
1764 return 1;
1765
1766 conn_err:
1767 h2c_error(h2c, error);
1768 return 0;
1769
1770 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001771 h2s_error(h2s, error);
1772 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001773 return 0;
1774}
1775
Willy Tarreaue96b0922017-10-30 00:28:29 +01001776/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001777 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1778 * have already verified frame length and stream ID validity. Described in
1779 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001780 */
1781static int h2c_handle_goaway(struct h2c *h2c)
1782{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001783 int last;
1784
Willy Tarreaue96b0922017-10-30 00:28:29 +01001785 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001786 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001787 return 0;
1788
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001789 last = h2_get_n32(&h2c->dbuf, 0);
1790 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001791 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001792 if (h2c->last_sid < 0)
1793 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001794 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001795}
1796
Willy Tarreau92153fc2017-12-03 19:46:19 +01001797/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001798 * invalid. Returns > 0 on success or zero on missing data. It may return an
1799 * error in h2c. The caller must have already verified frame length and stream
1800 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001801 */
1802static int h2c_handle_priority(struct h2c *h2c)
1803{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001804 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001805 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001806 return 0;
1807
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001808 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001809 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001810 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1811 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001812 }
1813 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001814}
1815
Willy Tarreaucd234e92017-08-18 10:59:39 +02001816/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001817 * Returns > 0 on success or zero on missing data. The caller must have already
1818 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001819 */
1820static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1821{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001822 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001823 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001824 return 0;
1825
1826 /* late RST, already handled */
1827 if (h2s->st == H2_SS_CLOSED)
1828 return 1;
1829
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001830 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001831 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001832
1833 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001834 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001835 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001836 }
1837
1838 h2s->flags |= H2_SF_RST_RCVD;
1839 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001840}
1841
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001842/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1843 * It may return an error in h2c or h2s. The caller must consider that the
1844 * return value is the new h2s in case one was allocated (most common case).
1845 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001846 * errors here are reported as connection errors since it's impossible to
1847 * recover from such errors after the compression context has been altered.
1848 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001849static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001850{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001851 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001852 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001853 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001854 int error;
1855
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001856 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001857 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001858
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001859 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001860 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001861
1862 /* now either the frame is complete or the buffer is complete */
1863 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001864 /* The stream exists/existed, this must be a trailers frame */
1865 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001866 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001867 goto out;
1868 goto done;
1869 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001870 /* the connection was already killed by an RST, let's consume
1871 * the data and send another RST.
1872 */
1873 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1874 h2s = (struct h2s*)h2_error_stream;
1875 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001876 }
1877 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1878 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1879 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001880 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001881 goto conn_err;
1882 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001883 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1884 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001885
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001886 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001887
Willy Tarreau25919232019-01-03 14:48:18 +01001888 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001889 if (h2c->st0 >= H2_CS_ERROR)
1890 goto out;
1891
Willy Tarreau25919232019-01-03 14:48:18 +01001892 if (error <= 0) {
1893 if (error == 0)
1894 goto out; // missing data
1895
1896 /* Failed to decode this stream (e.g. too large request)
1897 * but the HPACK decompressor is still synchronized.
1898 */
1899 h2s = (struct h2s*)h2_error_stream;
1900 goto send_rst;
1901 }
1902
Willy Tarreau22de8d32018-09-05 19:55:58 +02001903 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001904 * positively from h2c_frt_stream_new(), the stream will report the error,
1905 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001906 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001907 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001908 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001909 h2s = (struct h2s*)h2_refused_stream;
1910 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001911 }
1912
1913 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001914 h2s->rxbuf = rxbuf;
1915 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001916 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001917
Willy Tarreau88d138e2019-01-02 19:38:14 +01001918 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001919 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001920 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001921
1922 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01001923 if (h2s->st == H2_SS_OPEN)
1924 h2s->st = H2_SS_HREM;
1925 else
1926 h2s_close(h2s);
Willy Tarreau39d68502018-03-02 12:26:37 +01001927 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001928 }
1929
Willy Tarreau3a429f02019-01-03 11:41:50 +01001930 /* update the max stream ID if the request is being processed */
1931 if (h2s->id > h2c->max_id)
1932 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001933
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001934 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001935
1936 conn_err:
1937 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001938 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001939
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001940 out:
1941 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001942 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001943
1944 send_rst:
1945 /* make the demux send an RST for the current stream. We may only
1946 * do this if we're certain that the HEADERS frame was properly
1947 * decompressed so that the HPACK decoder is still kept up to date.
1948 */
1949 h2_release_buf(h2c, &rxbuf);
1950 h2c->st0 = H2_CS_FRAME_E;
1951 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001952}
1953
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001954/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1955 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1956 * errors here are reported as connection errors since it's impossible to
1957 * recover from such errors after the compression context has been altered.
1958 */
1959static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1960{
1961 int error;
1962
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001963 if (!b_size(&h2c->dbuf))
1964 return NULL; // empty buffer
1965
1966 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1967 return NULL; // incomplete frame
1968
Willy Tarreau1915ca22019-01-24 11:49:37 +01001969 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001970
Willy Tarreau25919232019-01-03 14:48:18 +01001971 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001972 if (h2c->st0 >= H2_CS_ERROR)
1973 return NULL;
1974
Willy Tarreau08bb1d62019-01-30 16:55:48 +01001975 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1976 /* RFC7540#5.1 */
1977 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1978 h2c->st0 = H2_CS_FRAME_E;
1979 return NULL;
1980 }
1981
Willy Tarreau25919232019-01-03 14:48:18 +01001982 if (error <= 0) {
1983 if (error == 0)
1984 return NULL; // missing data
1985
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001986 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01001987 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001988 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01001989 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001990 }
1991
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01001992 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1993 h2s->flags |= H2_SF_ES_RCVD;
1994 h2s->cs->flags |= CS_FL_REOS;
1995 }
1996
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001997 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1998 h2s->st = H2_SS_ERROR;
1999 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
2000 h2s->st = H2_SS_HREM;
2001 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
2002 h2s_close(h2s);
2003
2004 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002005}
2006
Willy Tarreau454f9052017-10-26 19:40:35 +02002007/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2008 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2009 */
2010static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2011{
2012 int error;
2013
2014 /* note that empty DATA frames are perfectly valid and sometimes used
2015 * to signal an end of stream (with the ES flag).
2016 */
2017
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002018 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002019 return 0; // empty buffer
2020
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002021 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002022 return 0; // incomplete frame
2023
2024 /* now either the frame is complete or the buffer is complete */
2025
Willy Tarreau454f9052017-10-26 19:40:35 +02002026 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2027 /* RFC7540#6.1 */
2028 error = H2_ERR_STREAM_CLOSED;
2029 goto strm_err;
2030 }
2031
Willy Tarreau1915ca22019-01-24 11:49:37 +01002032 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2033 /* RFC7540#8.1.2 */
2034 error = H2_ERR_PROTOCOL_ERROR;
2035 goto strm_err;
2036 }
2037
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002038 if (!h2_frt_transfer_data(h2s))
2039 return 0;
2040
Willy Tarreau454f9052017-10-26 19:40:35 +02002041 /* call the upper layers to process the frame, then let the upper layer
2042 * notify the stream about any change.
2043 */
2044 if (!h2s->cs) {
2045 error = H2_ERR_STREAM_CLOSED;
2046 goto strm_err;
2047 }
2048
Willy Tarreau8f650c32017-11-21 19:36:21 +01002049 if (h2c->st0 >= H2_CS_ERROR)
2050 return 0;
2051
Willy Tarreau721c9742017-11-07 11:05:42 +01002052 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002053 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002054 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002055 }
2056
2057 /* check for completion : the callee will change this to FRAME_A or
2058 * FRAME_H once done.
2059 */
2060 if (h2c->st0 == H2_CS_FRAME_P)
2061 return 0;
2062
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002063 /* last frame */
2064 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002065 if (h2s->st == H2_SS_OPEN)
2066 h2s->st = H2_SS_HREM;
2067 else
2068 h2s_close(h2s);
2069
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002070 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002071 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002072
2073 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2074 /* RFC7540#8.1.2 */
2075 error = H2_ERR_PROTOCOL_ERROR;
2076 goto strm_err;
2077 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002078 }
2079
Willy Tarreau454f9052017-10-26 19:40:35 +02002080 return 1;
2081
Willy Tarreau454f9052017-10-26 19:40:35 +02002082 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002083 h2s_error(h2s, error);
2084 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002085 return 0;
2086}
2087
Willy Tarreaubc933932017-10-09 16:21:43 +02002088/* process Rx frames to be demultiplexed */
2089static void h2_process_demux(struct h2c *h2c)
2090{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002091 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002092 struct h2_fh hdr;
2093 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002094
Willy Tarreau081d4722017-05-16 21:51:05 +02002095 if (h2c->st0 >= H2_CS_ERROR)
2096 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002097
2098 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2099 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002100 if (h2c->flags & H2_CF_IS_BACK)
2101 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002102 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2103 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002104 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002105 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002106 sess_log(h2c->conn->owner);
2107 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002108 goto fail;
2109 }
2110
2111 h2c->max_id = 0;
2112 h2c->st0 = H2_CS_SETTINGS1;
2113 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002114
2115 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002116 /* ensure that what is pending is a valid SETTINGS frame
2117 * without an ACK.
2118 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002119 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002120 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002121 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002122 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002123 sess_log(h2c->conn->owner);
2124 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002125 goto fail;
2126 }
2127
2128 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2129 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2130 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2131 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002132 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002133 goto fail;
2134 }
2135
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002136 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002137 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2138 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2139 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002140 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002141 goto fail;
2142 }
2143
Willy Tarreau3bf69182018-12-21 15:34:50 +01002144 /* that's OK, switch to FRAME_P to process it. This is
2145 * a SETTINGS frame whose header has already been
2146 * deleted above.
2147 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002148 padlen = 0;
2149 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002150 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002151 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002152
2153 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002154 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002155 int ret = 0;
2156
2157 if (h2c->st0 >= H2_CS_ERROR)
2158 break;
2159
2160 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002161 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002162 break;
2163
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002164 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002165 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002166 if (!h2c->nb_streams) {
2167 /* only log if no other stream can report the error */
2168 sess_log(h2c->conn->owner);
2169 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002170 break;
2171 }
2172
Willy Tarreau3bf69182018-12-21 15:34:50 +01002173 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2174 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2175 * we read the pad length and drop it from the remaining
2176 * payload (one byte + the 9 remaining ones = 10 total
2177 * removed), so we have a frame payload starting after the
2178 * pad len. Flow controlled frames (DATA) also count the
2179 * padlen in the flow control, so it must be adjusted.
2180 */
2181 if (hdr.len < 1) {
2182 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2183 sess_log(h2c->conn->owner);
2184 goto fail;
2185 }
2186 hdr.len--;
2187
2188 if (b_data(&h2c->dbuf) < 10)
2189 break; // missing padlen
2190
2191 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2192
2193 if (padlen > hdr.len) {
2194 /* RFC7540#6.1 : pad length = length of
2195 * frame payload or greater => error.
2196 */
2197 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2198 sess_log(h2c->conn->owner);
2199 goto fail;
2200 }
2201
2202 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2203 h2c->rcvd_c++;
2204 h2c->rcvd_s++;
2205 }
2206 b_del(&h2c->dbuf, 1);
2207 }
2208 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002209
2210 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002211 h2c->dfl = hdr.len;
2212 h2c->dsi = hdr.sid;
2213 h2c->dft = hdr.ft;
2214 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002215 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002216 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002217
2218 /* check for minimum basic frame format validity */
2219 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2220 if (ret != H2_ERR_NO_ERROR) {
2221 h2c_error(h2c, ret);
2222 sess_log(h2c->conn->owner);
2223 goto fail;
2224 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002225 }
2226
2227 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002228 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2229
Willy Tarreau567beb82018-12-18 16:52:44 +01002230 if (tmp_h2s != h2s && h2s && h2s->cs &&
2231 (b_data(&h2s->rxbuf) ||
2232 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002233 /* we may have to signal the upper layers */
2234 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002235 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002236 }
2237 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002238
Willy Tarreaud7901432017-12-29 11:34:40 +01002239 if (h2c->st0 == H2_CS_FRAME_E)
2240 goto strm_err;
2241
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002242 if (h2s->st == H2_SS_IDLE &&
2243 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2244 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2245 * this state MUST be treated as a connection error
2246 */
2247 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002248 if (!h2c->nb_streams) {
2249 /* only log if no other stream can report the error */
2250 sess_log(h2c->conn->owner);
2251 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002252 break;
2253 }
2254
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002255 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2256 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2257 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002258 * this state MUST be treated as a stream error.
2259 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2260 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002261 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002262 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2263 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2264 else
2265 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002266 goto strm_err;
2267 }
2268
Willy Tarreauab837502017-12-27 15:07:30 +01002269 /* Below the management of frames received in closed state is a
2270 * bit hackish because the spec makes strong differences between
2271 * streams closed by receiving RST, sending RST, and seeing ES
2272 * in both directions. In addition to this, the creation of a
2273 * new stream reusing the identifier of a closed one will be
2274 * detected here. Given that we cannot keep track of all closed
2275 * streams forever, we consider that unknown closed streams were
2276 * closed on RST received, which allows us to respond with an
2277 * RST without breaking the connection (eg: to abort a transfer).
2278 * Some frames have to be silently ignored as well.
2279 */
2280 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002281 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002282 /* #5.1.1: The identifier of a newly
2283 * established stream MUST be numerically
2284 * greater than all streams that the initiating
2285 * endpoint has opened or reserved. This
2286 * governs streams that are opened using a
2287 * HEADERS frame and streams that are reserved
2288 * using PUSH_PROMISE. An endpoint that
2289 * receives an unexpected stream identifier
2290 * MUST respond with a connection error.
2291 */
2292 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2293 goto strm_err;
2294 }
2295
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002296 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002297 /* RFC7540#5.1:closed: an endpoint that
2298 * receives any frame other than PRIORITY after
2299 * receiving a RST_STREAM MUST treat that as a
2300 * stream error of type STREAM_CLOSED.
2301 *
2302 * Note that old streams fall into this category
2303 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002304 *
2305 * However, we cannot generalize this to all frame types. Those
2306 * carrying compression state must still be processed before
2307 * being dropped or we'll desynchronize the decoder. This can
2308 * happen with request trailers received after sending an
2309 * RST_STREAM, or with header/trailers responses received after
2310 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002311 */
2312 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2313 h2c->st0 = H2_CS_FRAME_E;
2314 goto strm_err;
2315 }
2316
2317 /* RFC7540#5.1:closed: if this state is reached as a
2318 * result of sending a RST_STREAM frame, the peer that
2319 * receives the RST_STREAM might have already sent
2320 * frames on the stream that cannot be withdrawn. An
2321 * endpoint MUST ignore frames that it receives on
2322 * closed streams after it has sent a RST_STREAM
2323 * frame. An endpoint MAY choose to limit the period
2324 * over which it ignores frames and treat frames that
2325 * arrive after this time as being in error.
2326 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002327 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002328 /* RFC7540#5.1:closed: any frame other than
2329 * PRIO/WU/RST in this state MUST be treated as
2330 * a connection error
2331 */
2332 if (h2c->dft != H2_FT_RST_STREAM &&
2333 h2c->dft != H2_FT_PRIORITY &&
2334 h2c->dft != H2_FT_WINDOW_UPDATE) {
2335 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2336 goto strm_err;
2337 }
2338 }
2339 }
2340
Willy Tarreauc0da1962017-10-30 18:38:00 +01002341#if 0
2342 // problem below: it is not possible to completely ignore such
2343 // streams as we need to maintain the compression state as well
2344 // and for this we need to completely process these frames (eg:
2345 // HEADERS frames) as well as counting DATA frames to emit
2346 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2347 // This is a typical case of layer violation where the
2348 // transported contents are critical to the connection's
2349 // validity and must be ignored at the same time :-(
2350
2351 /* graceful shutdown, ignore streams whose ID is higher than
2352 * the one advertised in GOAWAY. RFC7540#6.8.
2353 */
2354 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002355 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2356 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002357 h2c->dfl -= ret;
2358 ret = h2c->dfl == 0;
2359 goto strm_err;
2360 }
2361#endif
2362
Willy Tarreau7e98c052017-10-10 15:56:59 +02002363 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002364 case H2_FT_SETTINGS:
2365 if (h2c->st0 == H2_CS_FRAME_P)
2366 ret = h2c_handle_settings(h2c);
2367
2368 if (h2c->st0 == H2_CS_FRAME_A)
2369 ret = h2c_ack_settings(h2c);
2370 break;
2371
Willy Tarreaucf68c782017-10-10 17:11:41 +02002372 case H2_FT_PING:
2373 if (h2c->st0 == H2_CS_FRAME_P)
2374 ret = h2c_handle_ping(h2c);
2375
2376 if (h2c->st0 == H2_CS_FRAME_A)
2377 ret = h2c_ack_ping(h2c);
2378 break;
2379
Willy Tarreau26f95952017-07-27 17:18:30 +02002380 case H2_FT_WINDOW_UPDATE:
2381 if (h2c->st0 == H2_CS_FRAME_P)
2382 ret = h2c_handle_window_update(h2c, h2s);
2383 break;
2384
Willy Tarreau61290ec2017-10-17 08:19:21 +02002385 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002386 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2387 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2388 * frames' parsers consume all following CONTINUATION
2389 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002390 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002391 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2392 sess_log(h2c->conn->owner);
2393 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002394
Willy Tarreau13278b42017-10-13 19:23:14 +02002395 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002396 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002397 if (h2c->flags & H2_CF_IS_BACK)
2398 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2399 else
2400 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002401 if (tmp_h2s) {
2402 h2s = tmp_h2s;
2403 ret = 1;
2404 }
2405 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002406 break;
2407
Willy Tarreau454f9052017-10-26 19:40:35 +02002408 case H2_FT_DATA:
2409 if (h2c->st0 == H2_CS_FRAME_P)
2410 ret = h2c_frt_handle_data(h2c, h2s);
2411
2412 if (h2c->st0 == H2_CS_FRAME_A)
2413 ret = h2c_send_strm_wu(h2c);
2414 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002415
Willy Tarreau92153fc2017-12-03 19:46:19 +01002416 case H2_FT_PRIORITY:
2417 if (h2c->st0 == H2_CS_FRAME_P)
2418 ret = h2c_handle_priority(h2c);
2419 break;
2420
Willy Tarreaucd234e92017-08-18 10:59:39 +02002421 case H2_FT_RST_STREAM:
2422 if (h2c->st0 == H2_CS_FRAME_P)
2423 ret = h2c_handle_rst_stream(h2c, h2s);
2424 break;
2425
Willy Tarreaue96b0922017-10-30 00:28:29 +01002426 case H2_FT_GOAWAY:
2427 if (h2c->st0 == H2_CS_FRAME_P)
2428 ret = h2c_handle_goaway(h2c);
2429 break;
2430
Willy Tarreau1c661982017-10-30 13:52:01 +01002431 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002432 default:
2433 /* drop frames that we ignore. They may be larger than
2434 * the buffer so we drain all of their contents until
2435 * we reach the end.
2436 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002437 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2438 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002439 h2c->dfl -= ret;
2440 ret = h2c->dfl == 0;
2441 }
2442
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002443 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002444 /* We may have to send an RST if not done yet */
2445 if (h2s->st == H2_SS_ERROR)
2446 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002447
Willy Tarreaua20a5192017-12-27 11:02:06 +01002448 if (h2c->st0 == H2_CS_FRAME_E)
2449 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002450
Willy Tarreau7e98c052017-10-10 15:56:59 +02002451 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002452 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002453 break;
2454
2455 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002456 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002457 h2c->st0 = H2_CS_FRAME_H;
2458 }
2459 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002460
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002461 if (h2c->rcvd_c > 0 &&
2462 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2463 h2c_send_conn_wu(h2c);
2464
Willy Tarreau52eed752017-09-22 15:05:09 +02002465 fail:
2466 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002467 if (h2s && h2s->cs &&
2468 (b_data(&h2s->rxbuf) ||
2469 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002470 /* we may have to signal the upper layers */
2471 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002472 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002473 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002474
Willy Tarreau47b515a2018-12-21 16:09:41 +01002475 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002476}
2477
2478/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2479 * the end.
2480 */
2481static int h2_process_mux(struct h2c *h2c)
2482{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002483 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002484
Willy Tarreau01b44822018-10-03 14:26:37 +02002485 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2486 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2487 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2488 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2489 if (h2c->st0 == H2_CS_ERROR) {
2490 h2c->st0 = H2_CS_ERROR2;
2491 sess_log(h2c->conn->owner);
2492 }
2493 goto fail;
2494 }
2495 h2c->st0 = H2_CS_SETTINGS1;
2496 }
2497 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002498 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002499 return 1;
2500 }
2501
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002502 /* start by sending possibly pending window updates */
2503 if (h2c->rcvd_c > 0 &&
2504 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2505 h2c_send_conn_wu(h2c) < 0)
2506 goto fail;
2507
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002508 /* First we always process the flow control list because the streams
2509 * waiting there were already elected for immediate emission but were
2510 * blocked just on this.
2511 */
2512
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002513 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002514 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2515 h2c->st0 >= H2_CS_ERROR)
2516 break;
2517
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002518 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002519 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2520 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002521 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002522 LIST_DEL(&h2s->list);
2523 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002524 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002525 }
2526
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002527 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002528 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2529 break;
2530
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002531 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002532 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2533 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002534 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002535 LIST_DEL(&h2s->list);
2536 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002537 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002538 }
2539
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002540 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002541 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002542 if (h2c->st0 == H2_CS_ERROR) {
2543 if (h2c->max_id >= 0) {
2544 h2c_send_goaway_error(h2c, NULL);
2545 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2546 return 0;
2547 }
2548
2549 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2550 }
2551 return 1;
2552 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002553 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002554}
2555
Willy Tarreau62f52692017-10-08 23:01:42 +02002556
Willy Tarreau479998a2018-11-18 06:30:59 +01002557/* Attempt to read data, and subscribe if none available.
2558 * The function returns 1 if data has been received, otherwise zero.
2559 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002560static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002561{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002562 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002563 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002564 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002565 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002566
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002567 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002568 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002569
Willy Tarreau315d8072017-12-10 22:17:57 +01002570 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002571 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002572
Willy Tarreau44e973f2018-03-01 17:49:30 +01002573 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002574 if (!buf) {
2575 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002576 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002577 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002578
Olivier Houchard7505f942018-08-21 18:10:44 +02002579 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002580 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002581 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2582 /* HTX in use : try to pre-align the buffer like the
2583 * rxbufs will be to optimize memory copies. We'll make
2584 * sure that the frame header lands at the end of the
2585 * HTX block to alias it upon recv. We cannot use the
2586 * head because rcv_buf() will realign the buffer if
2587 * it's empty. Thus we cheat and pretend we already
2588 * have a few bytes there.
2589 */
2590 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002591 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002592 }
2593 else
2594 max = b_room(buf);
2595
Olivier Houchard7505f942018-08-21 18:10:44 +02002596 if (max)
2597 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2598 else
2599 ret = 0;
2600 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002601
Olivier Houchard53216e72018-10-10 15:46:36 +02002602 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002603 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002604
Olivier Houcharda1411e62018-08-17 18:42:48 +02002605 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002606 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002607 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002608 }
2609
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002610 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002611 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002612 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002613}
2614
Willy Tarreau479998a2018-11-18 06:30:59 +01002615/* Try to send data if possible.
2616 * The function returns 1 if data have been sent, otherwise zero.
2617 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002618static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002619{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002620 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002621 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002622 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002623
2624 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002625 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002626
Olivier Houchard7505f942018-08-21 18:10:44 +02002627
Willy Tarreaua2af5122017-10-09 11:56:46 +02002628 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2629 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002630 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002631 }
2632
Willy Tarreaubc933932017-10-09 16:21:43 +02002633 /* This loop is quite simple : it tries to fill as much as it can from
2634 * pending streams into the existing buffer until it's reportedly full
2635 * or the end of send requests is reached. Then it tries to send this
2636 * buffer's contents out, marks it not full if at least one byte could
2637 * be sent, and tries again.
2638 *
2639 * The snd_buf() function normally takes a "flags" argument which may
2640 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2641 * data immediately comes and CO_SFL_STREAMER to indicate that the
2642 * connection is streaming lots of data (used to increase TLS record
2643 * size at the expense of latency). The former can be sent any time
2644 * there's a buffer full flag, as it indicates at least one stream
2645 * attempted to send and failed so there are pending data. An
2646 * alternative would be to set it as long as there's an active stream
2647 * but that would be problematic for ACKs until we have an absolute
2648 * guarantee that all waiters have at least one byte to send. The
2649 * latter should possibly not be set for now.
2650 */
2651
2652 done = 0;
2653 while (!done) {
2654 unsigned int flags = 0;
2655
2656 /* fill as much as we can into the current buffer */
2657 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2658 done = h2_process_mux(h2c);
2659
Olivier Houchard2b094432019-01-29 18:28:36 +01002660 if (h2c->flags & H2_CF_MUX_MALLOC)
2661 break;
2662
Willy Tarreaubc933932017-10-09 16:21:43 +02002663 if (conn->flags & CO_FL_ERROR)
2664 break;
2665
2666 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2667 flags |= CO_SFL_MSG_MORE;
2668
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002669 if (b_data(&h2c->mbuf)) {
2670 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002671 if (!ret)
2672 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002673 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002674 b_del(&h2c->mbuf, ret);
2675 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002676 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002677
2678 /* wrote at least one byte, the buffer is not full anymore */
2679 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2680 }
2681
Willy Tarreaua2af5122017-10-09 11:56:46 +02002682 if (conn->flags & CO_FL_SOCK_WR_SH) {
2683 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002684 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002685 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002686 /* We're not full anymore, so we can wake any task that are waiting
2687 * for us.
2688 */
2689 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002690 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002691 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2692 struct h2s *, list);
2693 LIST_DEL(&h2s->list);
2694 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002695 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002696 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2697 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002698 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002699 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002700 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002701 /* We're done, no more to send */
2702 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002703 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002704schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002705 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2706 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002707 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002708}
2709
2710static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2711{
2712 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002713 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002714
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002715 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002716 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002717 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002718 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002719 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002720 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002721 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002722}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002723
Willy Tarreau62f52692017-10-08 23:01:42 +02002724/* callback called on any event by the connection handler.
2725 * It applies changes and returns zero, or < 0 if it wants immediate
2726 * destruction of the connection (which normally doesn not happen in h2).
2727 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002728static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002729{
Olivier Houchard7505f942018-08-21 18:10:44 +02002730 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002731
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002732 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002733 h2_process_demux(h2c);
2734
2735 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002736 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002737
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002738 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002739 h2c->flags &= ~H2_CF_DEM_DFULL;
2740 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002741 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002742
Willy Tarreau0b37d652018-10-03 10:33:02 +02002743 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002744 /* frontend is stopping, reload likely in progress, let's try
2745 * to announce a graceful shutdown if not yet done. We don't
2746 * care if it fails, it will be tried again later.
2747 */
2748 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2749 if (h2c->last_sid < 0)
2750 h2c->last_sid = (1U << 31) - 1;
2751 h2c_send_goaway_error(h2c, NULL);
2752 }
2753 }
2754
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002755 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002756 * If we received early data, and the handshake is done, wake
2757 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002758 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002759 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2760 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2761 struct eb32_node *node;
2762 struct h2s *h2s;
2763
2764 h2c->flags |= H2_CF_WAIT_FOR_HS;
2765 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2766
2767 while (node) {
2768 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002769 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002770 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002771 node = eb32_next(node);
2772 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002773 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002774
Willy Tarreau26bd7612017-10-09 16:47:04 +02002775 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002776 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2777 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2778 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002779 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002780
2781 if (eb_is_empty(&h2c->streams_by_id)) {
2782 /* no more stream, kill the connection now */
2783 h2_release(conn);
2784 return -1;
2785 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002786 }
2787
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002788 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002789 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002790
Olivier Houchard53216e72018-10-10 15:46:36 +02002791 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2792 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2793 (h2c->st0 != H2_CS_ERROR &&
2794 !b_data(&h2c->mbuf) &&
2795 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2796 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002797 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002798
Willy Tarreau3f133572017-10-31 19:21:06 +01002799 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002800 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002801 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002802 task_queue(h2c->task);
2803 }
2804 else
2805 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002806 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002807
Olivier Houchard7505f942018-08-21 18:10:44 +02002808 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002809 return 0;
2810}
2811
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002812static int h2_wake(struct connection *conn)
2813{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002814 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002815
2816 return (h2_process(h2c));
2817}
2818
Willy Tarreauea392822017-10-31 10:02:25 +01002819/* Connection timeout management. The principle is that if there's no receipt
2820 * nor sending for a certain amount of time, the connection is closed. If the
2821 * MUX buffer still has lying data or is not allocatable, the connection is
2822 * immediately killed. If it's allocatable and empty, we attempt to send a
2823 * GOAWAY frame.
2824 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002825static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002826{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002827 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002828 int expired = tick_is_expired(t->expire, now_ms);
2829
Willy Tarreau0975f112018-03-29 15:22:59 +02002830 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002831 return t;
2832
Willy Tarreau0975f112018-03-29 15:22:59 +02002833 task_delete(t);
2834 task_free(t);
2835
2836 if (!h2c) {
2837 /* resources were already deleted */
2838 return NULL;
2839 }
2840
2841 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002842 h2c_error(h2c, H2_ERR_NO_ERROR);
2843 h2_wake_some_streams(h2c, 0, 0);
2844
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002845 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002846 /* don't even try to send a GOAWAY, the buffer is stuck */
2847 h2c->flags |= H2_CF_GOAWAY_FAILED;
2848 }
2849
2850 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002851 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002852 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2853 h2c->flags |= H2_CF_GOAWAY_FAILED;
2854
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002855 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2856 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002857 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002858 b_del(&h2c->mbuf, ret);
2859 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002860 }
2861 }
Willy Tarreauea392822017-10-31 10:02:25 +01002862
Willy Tarreau0975f112018-03-29 15:22:59 +02002863 /* either we can release everything now or it will be done later once
2864 * the last stream closes.
2865 */
2866 if (eb_is_empty(&h2c->streams_by_id))
2867 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002868
Willy Tarreauea392822017-10-31 10:02:25 +01002869 return NULL;
2870}
2871
2872
Willy Tarreau62f52692017-10-08 23:01:42 +02002873/*******************************************/
2874/* functions below are used by the streams */
2875/*******************************************/
2876
2877/*
2878 * Attach a new stream to a connection
2879 * (Used for outgoing connections)
2880 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002881static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002882{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002883 struct conn_stream *cs;
2884 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002885 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002886
2887 cs = cs_new(conn);
2888 if (!cs)
2889 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002890 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002891 if (!h2s) {
2892 cs_free(cs);
2893 return NULL;
2894 }
2895 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002896}
2897
Willy Tarreaufafd3982018-11-18 21:29:20 +01002898/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2899 * We have to scan because we may have some orphan streams. It might be
2900 * beneficial to scan backwards from the end to reduce the likeliness to find
2901 * orphans.
2902 */
2903static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2904{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002905 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002906 struct h2s *h2s;
2907 struct eb32_node *node;
2908
2909 node = eb32_first(&h2c->streams_by_id);
2910 while (node) {
2911 h2s = container_of(node, struct h2s, by_id);
2912 if (h2s->cs)
2913 return h2s->cs;
2914 node = eb32_next(node);
2915 }
2916 return NULL;
2917}
2918
Willy Tarreau62f52692017-10-08 23:01:42 +02002919/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002920 * Destroy the mux and the associated connection, if it is no longer used
2921 */
2922static void h2_destroy(struct connection *conn)
2923{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002924 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002925
2926 if (eb_is_empty(&h2c->streams_by_id))
2927 h2_release(h2c->conn);
2928}
2929
2930/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002931 * Detach the stream from the connection and possibly release the connection.
2932 */
2933static void h2_detach(struct conn_stream *cs)
2934{
Willy Tarreau60935142017-10-16 18:11:19 +02002935 struct h2s *h2s = cs->ctx;
2936 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002937 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002938
2939 cs->ctx = NULL;
2940 if (!h2s)
2941 return;
2942
Olivier Houchardf502aca2018-12-14 19:42:40 +01002943 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002944 h2c = h2s->h2c;
2945 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002946 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01002947 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
2948 !h2_frt_has_too_many_cs(h2c)) {
2949 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02002950 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002951 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002952 }
Willy Tarreau60935142017-10-16 18:11:19 +02002953
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002954 /* this stream may be blocked waiting for some data to leave (possibly
2955 * an ES or RST frame), so orphan it in this case.
2956 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002957 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002958 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002959 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002960 return;
2961
Willy Tarreau45f752e2017-10-30 15:44:59 +01002962 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2963 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2964 /* unblock the connection if it was blocked on this
2965 * stream.
2966 */
2967 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2968 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002969 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002970 }
2971
Willy Tarreau71049cc2018-03-28 13:56:39 +02002972 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002973
Olivier Houchard8a786902018-12-15 16:05:40 +01002974 if (h2c->flags & H2_CF_IS_BACK &&
2975 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002976 if (!(h2c->conn->flags &
2977 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2978 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002979 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002980 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
2981 h2c->conn->owner = NULL;
2982 if (eb_is_empty(&h2c->streams_by_id)) {
2983 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
2984 /* The server doesn't want it, let's kill the connection right away */
2985 h2c->conn->mux->destroy(h2c->conn);
2986 return;
2987 }
2988 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002989 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002990 if (eb_is_empty(&h2c->streams_by_id)) {
2991 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
2992 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
2993 return;
2994 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002995 /* Never ever allow to reuse a connection from a non-reuse backend */
2996 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2997 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01002998 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002999 struct server *srv = objt_server(h2c->conn->target);
3000
3001 if (srv) {
3002 if (h2c->conn->flags & CO_FL_PRIVATE)
3003 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3004 else
3005 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3006 }
3007
3008 }
3009 }
3010 }
3011
Willy Tarreaue323f342018-03-28 13:51:45 +02003012 /* We don't want to close right now unless we're removing the
3013 * last stream, and either the connection is in error, or it
3014 * reached the ID already specified in a GOAWAY frame received
3015 * or sent (as seen by last_sid >= 0).
3016 */
3017 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3018 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003019 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard93c88522018-11-30 15:39:16 +01003020 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003021 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003022 (conn_xprt_read0_pending(h2c->conn) ||
3023 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3024 /* no more stream will come, kill it now */
3025 h2_release(h2c->conn);
3026 }
3027 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003028 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003029 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3030 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003031 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003032 else
3033 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003034 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003035}
3036
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003037static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003038{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003039 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003040 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003041
Willy Tarreau721c9742017-11-07 11:05:42 +01003042 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003043 return;
3044
Willy Tarreau18059042019-01-31 19:12:48 +01003045 /* a connstream may require us to immediately kill the whole connection
3046 * for example because of a "tcp-request content reject" rule that is
3047 * normally used to limit abuse. In this case we schedule a goaway to
3048 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003049 */
Willy Tarreau18059042019-01-31 19:12:48 +01003050 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3051 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3052 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3053 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3054 }
3055
Willy Tarreau90c32322017-11-24 08:00:30 +01003056 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003057 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003058 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003059
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003060 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003061 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003062 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003063
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003064 return;
3065add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003066 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003067 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003068 if (h2s->flags & H2_SF_BLK_MFCTL) {
3069 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3070 h2s->send_wait = sw;
3071 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3072 h2s->send_wait = sw;
3073 LIST_ADDQ(&h2c->send_list, &h2s->list);
3074 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003075 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003076 /* Let the handler know we want shutr */
3077 sw->handle = (void *)((long)sw->handle | 1);
Willy Tarreau62f52692017-10-08 23:01:42 +02003078}
3079
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003080static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003081{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003082 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003083 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003084
Willy Tarreau721c9742017-11-07 11:05:42 +01003085 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003086 return;
3087
Willy Tarreau67434202017-11-06 20:20:51 +01003088 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003089 /* we can cleanly close using an empty data frame only after headers */
3090
3091 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3092 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003093 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003094
3095 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003096 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003097 else
3098 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003099 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003100 /* a connstream may require us to immediately kill the whole connection
3101 * for example because of a "tcp-request content reject" rule that is
3102 * normally used to limit abuse. In this case we schedule a goaway to
3103 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003104 */
Willy Tarreau18059042019-01-31 19:12:48 +01003105 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3106 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3107 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3108 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3109 }
3110
Willy Tarreau90c32322017-11-24 08:00:30 +01003111 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003112 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003113 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003114
Willy Tarreau00dd0782018-03-01 16:31:34 +01003115 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003116 }
3117
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003118 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003119 tasklet_wakeup(h2c->wait_event.task);
3120 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003121
3122 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003123 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003124 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003125 if (h2s->flags & H2_SF_BLK_MFCTL) {
3126 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3127 h2s->send_wait = sw;
3128 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3129 h2s->send_wait = sw;
3130 LIST_ADDQ(&h2c->send_list, &h2s->list);
3131 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003132 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003133 /* let the handler know we want to shutw */
3134 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003135}
3136
3137static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3138{
3139 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003140 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003141
Olivier Houchard2c68a462018-12-15 22:42:20 +01003142 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003143 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003144 h2s->send_wait = NULL;
3145 LIST_DEL(&h2s->list);
3146 LIST_INIT(&h2s->list);
3147 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003148 if (reason & 2)
3149 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003150 if (reason & 1)
3151 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003152
Olivier Houchard2c68a462018-12-15 22:42:20 +01003153 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003154 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003155 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003156 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003157}
3158
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003159static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3160{
3161 struct h2s *h2s = cs->ctx;
3162
3163 if (!mode)
3164 return;
3165
3166 h2_do_shutr(h2s);
3167}
3168
3169static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3170{
3171 struct h2s *h2s = cs->ctx;
3172
3173 h2_do_shutw(h2s);
3174}
3175
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003176/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003177 * HTX request or response depending on the connection's side. Returns a
3178 * positive value on success, a negative value on failure, or 0 if it couldn't
3179 * proceed. May report connection errors in h2c->errcode if the frame is
3180 * non-decodable and the connection unrecoverable. In absence of connection
3181 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003182 *
3183 * The function may fold CONTINUATION frames into the initial HEADERS frame
3184 * by removing padding and next frame header, then moving the CONTINUATION
3185 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3186 * leaving a hole between the main frame and the beginning of the next one.
3187 * The possibly remaining incomplete or next frame at the end may be moved
3188 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3189 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3190 *
3191 * A buffer at the beginning of processing may look like this :
3192 *
3193 * ,---.---------.-----.--------------.--------------.------.---.
3194 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3195 * `---^---------^-----^--------------^--------------^------^---'
3196 * | | <-----> | |
3197 * area | dpl | wrap
3198 * |<--------------> |
3199 * | dfl |
3200 * |<-------------------------------------------------->|
3201 * head data
3202 *
3203 * Padding is automatically overwritten when folding, participating to the
3204 * hole size after dfl :
3205 *
3206 * ,---.------------------------.-----.--------------.------.---.
3207 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3208 * `---^------------------------^-----^--------------^------^---'
3209 * | | <-----> | |
3210 * area | hole | wrap
3211 * |<-----------------------> |
3212 * | dfl |
3213 * |<-------------------------------------------------->|
3214 * head data
3215 *
3216 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3217 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3218 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003219 *
3220 * The <flags> field must point to either the stream's flags or to a copy of it
3221 * so that the function can update the following flags :
3222 * - H2_SF_DATA_CLEN when content-length is seen
3223 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3224 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003225 *
3226 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3227 * decoding, in order to detect if we're dealing with a headers or a trailers
3228 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003229 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003230static 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 +02003231{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003232 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003233 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003234 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003235 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003236 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003237 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003238 int flen; // header frame len
3239 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003240 int ret = 0;
3241 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003242 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003243 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003244
Willy Tarreauea18f862018-12-22 20:19:26 +01003245next_frame:
3246 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3247 goto leave; // incomplete input frame
3248
3249 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3250 * this case, we'll try to paste it immediately after the initial
3251 * HEADERS frame payload and kill any possible padding. The initial
3252 * frame's length will be increased to represent the concatenation
3253 * of the two frames. The next frame is read from position <tlen>
3254 * and written at position <flen> (minus padding if some is present).
3255 */
3256 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3257 struct h2_fh hdr;
3258 int clen; // CONTINUATION frame's payload length
3259
3260 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3261 /* no more data, the buffer may be full, either due to
3262 * too large a frame or because of too large a hole that
3263 * we're going to compact at the end.
3264 */
3265 goto leave;
3266 }
3267
3268 if (hdr.ft != H2_FT_CONTINUATION) {
3269 /* RFC7540#6.10: frame of unexpected type */
3270 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3271 goto fail;
3272 }
3273
3274 if (hdr.sid != h2c->dsi) {
3275 /* RFC7540#6.10: frame of different stream */
3276 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3277 goto fail;
3278 }
3279
3280 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3281 /* RFC7540#4.2: invalid frame length */
3282 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3283 goto fail;
3284 }
3285
3286 /* detect when we must stop aggragating frames */
3287 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3288
3289 /* Take as much as we can of the CONTINUATION frame's payload */
3290 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3291 if (clen > hdr.len)
3292 clen = hdr.len;
3293
3294 /* Move the frame's payload over the padding, hole and frame
3295 * header. At least one of hole or dpl is null (see diagrams
3296 * above). The hole moves after the new aggragated frame.
3297 */
3298 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3299 h2c->dfl += clen - h2c->dpl;
3300 hole += h2c->dpl + 9;
3301 h2c->dpl = 0;
3302 goto next_frame;
3303 }
3304
3305 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003306
Willy Tarreau13278b42017-10-13 19:23:14 +02003307 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003308 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003309 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003310 copy = alloc_trash_chunk();
3311 if (!copy) {
3312 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3313 goto fail;
3314 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003315 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3316 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3317 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003318 }
3319
Willy Tarreau13278b42017-10-13 19:23:14 +02003320 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3321 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003322 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003323 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3324 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003325 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003326 }
3327
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003328 if (flen < 5) {
3329 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3330 goto fail;
3331 }
3332
Willy Tarreau13278b42017-10-13 19:23:14 +02003333 hdrs += 5; // stream dep = 4, weight = 1
3334 flen -= 5;
3335 }
3336
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003337 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003338 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003339 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003340 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003341
Willy Tarreau937f7602018-02-26 15:22:17 +01003342 /* we can't retry a failed decompression operation so we must be very
3343 * careful not to take any risks. In practice the output buffer is
3344 * always empty except maybe for trailers, in which case we simply have
3345 * to wait for the upper layer to finish consuming what is available.
3346 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003347
3348 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003349 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003350 if (!htx_is_empty(htx)) {
3351 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003352 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003353 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003354 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003355 if (b_data(rxbuf)) {
3356 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003357 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003358 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003359
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003360 rxbuf->head = 0;
3361 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003362 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003363
Willy Tarreau25919232019-01-03 14:48:18 +01003364 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003365 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3366 sizeof(list)/sizeof(list[0]), tmp);
3367 if (outlen < 0) {
3368 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3369 goto fail;
3370 }
3371
Willy Tarreau25919232019-01-03 14:48:18 +01003372 /* The PACK decompressor was updated, let's update the input buffer and
3373 * the parser's state to commit these changes and allow us to later
3374 * fail solely on the stream if needed.
3375 */
3376 b_del(&h2c->dbuf, h2c->dfl + hole);
3377 h2c->dfl = hole = 0;
3378 h2c->st0 = H2_CS_FRAME_H;
3379
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003380 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003381 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003382
Willy Tarreau88d138e2019-01-02 19:38:14 +01003383 if (*flags & H2_SF_HEADERS_RCVD)
3384 goto trailers;
3385
3386 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003387 if (htx) {
3388 /* HTX mode */
3389 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003390 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003391 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003392 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003393 } else {
3394 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003395 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003396 if (outlen > 0)
3397 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003398 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003399
3400 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003401 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003402 goto fail;
3403 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003404
Willy Tarreau174b06a2018-04-25 18:13:58 +02003405 if (msgf & H2_MSGF_BODY) {
3406 /* a payload is present */
3407 if (msgf & H2_MSGF_BODY_CL)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003408 *flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003409 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003410 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003411 }
3412
Willy Tarreau88d138e2019-01-02 19:38:14 +01003413 done:
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003414 /* indicate that a HEADERS frame was received for this stream */
3415 *flags |= H2_SF_HEADERS_RCVD;
3416
Willy Tarreau88d138e2019-01-02 19:38:14 +01003417 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
3418 /* Mark the end of message, either using EOM in HTX or with the
3419 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3420 * is not set during headers with END_STREAM.
3421 */
3422 if (htx) {
3423 if (!htx_add_endof(htx, HTX_BLK_EOM))
3424 goto fail;
3425 }
3426 else if (*flags & H2_SF_DATA_CHNK) {
3427 if (!b_putblk(rxbuf, "\r\n", 2))
3428 goto fail;
3429 }
3430 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003431
Willy Tarreau86277d42019-01-02 15:36:11 +01003432 /* success */
3433 ret = 1;
3434
Willy Tarreau68dd9852017-07-03 14:44:26 +02003435 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003436 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003437 * move the remaining data over it.
3438 */
3439 if (hole) {
3440 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3441 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3442 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3443 b_sub(&h2c->dbuf, hole);
3444 }
3445
3446 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3447 /* too large frames */
3448 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003449 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003450 }
3451
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003452 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003453 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003454 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003455 return ret;
3456
Willy Tarreau68dd9852017-07-03 14:44:26 +02003457 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003458 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003459 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003460
3461 trailers:
3462 /* This is the last HEADERS frame hence a trailer */
3463
3464 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3465 /* It's a trailer but it's missing ES flag */
3466 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3467 goto fail;
3468 }
3469
3470 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3471 * block, and when using chunks we must send the 0 CRLF marker. For
3472 * other modes, the trailers are silently dropped.
3473 */
3474 if (htx) {
3475 if (!htx_add_endof(htx, HTX_BLK_EOD))
3476 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003477 if (h2_make_htx_trailers(list, htx) <= 0)
3478 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003479 }
3480 else if (*flags & H2_SF_DATA_CHNK) {
3481 /* Legacy mode with chunked encoding : we must finalize the
3482 * data block message emit the trailing CRLF */
3483 if (!b_putblk(rxbuf, "0\r\n", 3))
3484 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003485
3486 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3487 if (outlen > 0)
3488 b_add(rxbuf, outlen);
3489 else
3490 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003491 }
3492
3493 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003494}
3495
Willy Tarreau454f9052017-10-26 19:40:35 +02003496/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3497 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3498 * in use, a new chunk is emitted for each frame. This is supposed to fit
3499 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3500 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3501 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003502 * parser state is automatically updated. Returns > 0 if it could completely
3503 * send the current frame, 0 if it couldn't complete, in which case
3504 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3505 * DATA frame can return 0 as a valid result). Stream errors are reported in
3506 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3507 * have checked the frame header and ensured that the frame was complete or the
3508 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003509 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003510static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003511{
3512 struct h2c *h2c = h2s->h2c;
3513 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003514 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003515 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003516 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003517 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003518
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003519 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003520
Olivier Houchard638b7992018-08-16 15:41:52 +02003521 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003522 if (!csbuf) {
3523 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003524 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003525 }
3526
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003527try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003528 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003529 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3530 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003531 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003532 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003533
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003534 if (flen > b_data(&h2c->dbuf)) {
3535 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003536 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003537 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003538 }
3539
Willy Tarreaua9b77962019-01-31 07:23:00 +01003540 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003541 block1 = htx_free_data_space(htx);
3542 if (!block1) {
3543 h2c->flags |= H2_CF_DEM_SFULL;
3544 goto fail;
3545 }
3546 if (flen > block1)
3547 flen = block1;
3548
3549 /* here, flen is the max we can copy into the output buffer */
3550 block1 = b_contig_data(&h2c->dbuf, 0);
3551 if (flen > block1)
3552 flen = block1;
3553
3554 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3555 h2c->flags |= H2_CF_DEM_SFULL;
3556 goto fail;
3557 }
3558
3559 b_del(&h2c->dbuf, flen);
3560 h2c->dfl -= flen;
3561 h2c->rcvd_c += flen;
3562 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003563
3564 if (h2s->flags & H2_SF_DATA_CLEN)
3565 h2s->body_len -= flen;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003566 goto try_again;
3567 }
3568 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003569 /* it doesn't fit and the buffer is fragmented,
3570 * so let's defragment it and try again.
3571 */
3572 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003573 }
3574
Willy Tarreaueba10f22018-04-25 20:44:22 +02003575 /* chunked-encoding requires more room */
3576 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003577 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003578 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3579 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3580 (chklen < 1048576) ? 4 : 8;
3581 chklen += 4; // CRLF, CRLF
3582 }
3583
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003584 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003585 if (flen + chklen > b_room(csbuf)) {
3586 if (chklen >= b_room(csbuf)) {
3587 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003588 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003589 }
3590 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003591 }
3592
3593 if (h2s->flags & H2_SF_DATA_CHNK) {
3594 /* emit the chunk size */
3595 unsigned int chksz = flen;
3596 char str[10];
3597 char *beg;
3598
3599 beg = str + sizeof(str);
3600 *--beg = '\n';
3601 *--beg = '\r';
3602 do {
3603 *--beg = hextab[chksz & 0xF];
3604 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003605 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003606 }
3607
Willy Tarreau454f9052017-10-26 19:40:35 +02003608 /* Block1 is the length of the first block before the buffer wraps,
3609 * block2 is the optional second block to reach the end of the frame.
3610 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003611 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003612 if (block1 > flen)
3613 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003614 block2 = flen - block1;
3615
3616 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003617 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003618
3619 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003620 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003621
Willy Tarreaueba10f22018-04-25 20:44:22 +02003622 if (h2s->flags & H2_SF_DATA_CHNK) {
3623 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003624 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003625 }
3626
Willy Tarreau454f9052017-10-26 19:40:35 +02003627 /* now mark the input data as consumed (will be deleted from the buffer
3628 * by the caller when seeing FRAME_A after sending the window update).
3629 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003630 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003631 h2c->dfl -= flen;
3632 h2c->rcvd_c += flen;
3633 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3634
Willy Tarreau1915ca22019-01-24 11:49:37 +01003635 if (h2s->flags & H2_SF_DATA_CLEN)
3636 h2s->body_len -= flen;
3637
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003638 if (h2c->dfl > h2c->dpl) {
3639 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003640 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003641 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003642 }
3643
Willy Tarreau4a28da12018-01-04 14:41:00 +01003644 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003645 /* here we're done with the frame, all the payload (except padding) was
3646 * transferred.
3647 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003648
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003649 if (h2c->dff & H2_F_DATA_END_STREAM) {
3650 if (htx) {
3651 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3652 h2c->flags |= H2_CF_DEM_SFULL;
3653 goto fail;
3654 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003655 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003656 else if (h2s->flags & H2_SF_DATA_CHNK) {
3657 /* emit the trailing 0 CRLF CRLF */
3658 if (b_room(csbuf) < 5) {
3659 h2c->flags |= H2_CF_DEM_SFULL;
3660 goto fail;
3661 }
3662 chklen += 5;
3663 b_putblk(csbuf, "0\r\n\r\n", 5);
3664 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003665 }
3666
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003667 h2c->rcvd_c += h2c->dpl;
3668 h2c->rcvd_s += h2c->dpl;
3669 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003670 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3671
Willy Tarreau39d68502018-03-02 12:26:37 +01003672 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003673 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003674 h2s->cs->flags |= CS_FL_REOS;
3675 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003676 if (htx)
3677 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003678 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003679 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003680 if (htx)
3681 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003682 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003683}
3684
Willy Tarreau5dd17352018-06-14 13:33:30 +02003685/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3686 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3687 * number of bytes sent. The caller must check the stream's status to detect
3688 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003689 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003690static 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 +02003691{
3692 struct http_hdr list[MAX_HTTP_HDR];
3693 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003694 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003695 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003696 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003697 int es_now = 0;
3698 int ret = 0;
3699 int hdr;
3700
3701 if (h2c_mux_busy(h2c, h2s)) {
3702 h2s->flags |= H2_SF_BLK_MBUSY;
3703 return 0;
3704 }
3705
Willy Tarreau44e973f2018-03-01 17:49:30 +01003706 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003707 h2c->flags |= H2_CF_MUX_MALLOC;
3708 h2s->flags |= H2_SF_BLK_MROOM;
3709 return 0;
3710 }
3711
3712 /* First, try to parse the H1 response and index it into <list>.
3713 * NOTE! Since it comes from haproxy, we *know* that a response header
3714 * block does not wrap and we can safely read it this way without
3715 * having to realign the buffer.
3716 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003717 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003718 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003719 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003720 /* incomplete or invalid response, this is abnormal coming from
3721 * haproxy and may only result in a bad errorfile or bad Lua code
3722 * so that won't be fixed, raise an error now.
3723 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003724 * FIXME: we should instead add the ability to only return a
3725 * 502 bad gateway. But in theory this is not supposed to
3726 * happen.
3727 */
3728 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3729 ret = 0;
3730 goto end;
3731 }
3732
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003733 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003734
3735 /* certain statuses have no body or an empty one, regardless of
3736 * what the headers say.
3737 */
3738 if (sl.st.status >= 100 && sl.st.status < 200) {
3739 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3740 h1m->curr_len = h1m->body_len = 0;
3741 }
3742 else if (sl.st.status == 204 || sl.st.status == 304) {
3743 /* no contents, claim c-len is present and set to zero */
3744 h1m->flags &= ~H1_MF_CHNK;
3745 h1m->flags |= H1_MF_CLEN;
3746 h1m->curr_len = h1m->body_len = 0;
3747 }
3748
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003749 chunk_reset(&outbuf);
3750
3751 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003752 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003753 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003754 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003755
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003756 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003757 break;
3758 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003759 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003760 }
3761
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003762 if (outbuf.size < 9)
3763 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003764
3765 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003766 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3767 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3768 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003769
3770 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003771 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003772 /* this is an unparsable response */
3773 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3774 ret = 0;
3775 goto end;
3776 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003777
3778 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003779 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003780 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003781 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003782 }
3783
3784 /* encode all headers, stop at empty name */
3785 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003786 /* these ones do not exist in H2 and must be dropped. */
3787 if (isteq(list[hdr].n, ist("connection")) ||
3788 isteq(list[hdr].n, ist("proxy-connection")) ||
3789 isteq(list[hdr].n, ist("keep-alive")) ||
3790 isteq(list[hdr].n, ist("upgrade")) ||
3791 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003792 continue;
3793
3794 if (isteq(list[hdr].n, ist("")))
3795 break; // end
3796
3797 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3798 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003799 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003800 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003801 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003802 }
3803 }
3804
3805 /* we may need to add END_STREAM */
3806 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3807 es_now = 1;
3808
3809 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003810 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003811
3812 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003813 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003814
3815 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003816 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003817
3818 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003819 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003820 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003821
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003822 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003823 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003824 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003825
Willy Tarreau801250e2018-09-11 11:45:04 +02003826 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003827 h2s->flags |= H2_SF_ES_SENT;
3828 if (h2s->st == H2_SS_OPEN)
3829 h2s->st = H2_SS_HLOC;
3830 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003831 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003832 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003833 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003834 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003835 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003836 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003837 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003838 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003839 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003840
3841 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003842
3843 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003844 //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 +02003845 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003846 full:
3847 h1m_init_res(h1m);
3848 h1m->err_pos = -1; // don't care about errors on the response path
3849 h2c->flags |= H2_CF_MUX_MFULL;
3850 h2s->flags |= H2_SF_BLK_MROOM;
3851 ret = 0;
3852 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003853}
3854
Willy Tarreau5dd17352018-06-14 13:33:30 +02003855/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3856 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3857 * the number of bytes sent. The caller must check the stream's status to
3858 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003859 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003860static 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 +02003861{
3862 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003863 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003864 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003865 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003866 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003867 int es_now = 0;
3868 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003869 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003870 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003871
3872 if (h2c_mux_busy(h2c, h2s)) {
3873 h2s->flags |= H2_SF_BLK_MBUSY;
3874 goto end;
3875 }
3876
Willy Tarreau44e973f2018-03-01 17:49:30 +01003877 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003878 h2c->flags |= H2_CF_MUX_MALLOC;
3879 h2s->flags |= H2_SF_BLK_MROOM;
3880 goto end;
3881 }
3882
3883 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003884 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003885 goto end;
3886
3887 chunk_reset(&outbuf);
3888
3889 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003890 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003891 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003892 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003893
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003894 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003895 break;
3896 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003897 /* If there are pending data in the output buffer, and we have
3898 * less than 1/4 of the mbuf's size and everything fits, we'll
3899 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3900 * is full and wait, to save some slow realign calls.
3901 */
3902 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3903 h2c->flags |= H2_CF_MUX_MFULL;
3904 h2s->flags |= H2_SF_BLK_MROOM;
3905 goto end;
3906 }
3907
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003908 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003909 }
3910
3911 if (outbuf.size < 9) {
3912 h2c->flags |= H2_CF_MUX_MFULL;
3913 h2s->flags |= H2_SF_BLK_MROOM;
3914 goto end;
3915 }
3916
3917 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003918 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3919 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3920 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003921
3922 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3923 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003924 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003925 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003926 break;
3927 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003928 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003929 if ((long long)size > h1m->curr_len)
3930 size = h1m->curr_len;
3931 break;
3932 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003933 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003934 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003935 if (!ret)
3936 goto end;
3937
3938 if (ret < 0) {
3939 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003940 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003941 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3942 goto end;
3943 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003944 max -= ret;
3945 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003946 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003947 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003948 }
3949
Willy Tarreau801250e2018-09-11 11:45:04 +02003950 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003951 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003952 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003953 if (!ret)
3954 goto end;
3955
3956 if (ret < 0) {
3957 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003958 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003959 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3960 goto end;
3961 }
3962
3963 size = chunk;
3964 h1m->curr_len = chunk;
3965 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003966 max -= ret;
3967 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003968 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003969 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003970 if (!size)
3971 goto send_empty;
3972 }
3973
3974 /* in MSG_DATA state, continue below */
3975 size = h1m->curr_len;
3976 break;
3977 }
3978
3979 /* we have in <size> the exact number of bytes we need to copy from
3980 * the H1 buffer. We need to check this against the connection's and
3981 * the stream's send windows, and to ensure that this fits in the max
3982 * frame size and in the buffer's available space minus 9 bytes (for
3983 * the frame header). The connection's flow control is applied last so
3984 * that we can use a separate list of streams which are immediately
3985 * unblocked on window opening. Note: we don't implement padding.
3986 */
3987
Willy Tarreau5dd17352018-06-14 13:33:30 +02003988 if (size > max)
3989 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003990
3991 if (size > h2s->mws)
3992 size = h2s->mws;
3993
3994 if (size <= 0) {
3995 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003996 if (h2s->send_wait) {
3997 LIST_DEL(&h2s->list);
3998 LIST_INIT(&h2s->list);
3999 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004000 goto end;
4001 }
4002
4003 if (h2c->mfs && size > h2c->mfs)
4004 size = h2c->mfs;
4005
4006 if (size + 9 > outbuf.size) {
4007 /* we have an opportunity for enlarging the too small
4008 * available space, let's try.
4009 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004010 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004011 goto realign_again;
4012 size = outbuf.size - 9;
4013 }
4014
4015 if (size <= 0) {
4016 h2c->flags |= H2_CF_MUX_MFULL;
4017 h2s->flags |= H2_SF_BLK_MROOM;
4018 goto end;
4019 }
4020
4021 if (size > h2c->mws)
4022 size = h2c->mws;
4023
4024 if (size <= 0) {
4025 h2s->flags |= H2_SF_BLK_MFCTL;
4026 goto end;
4027 }
4028
4029 /* copy whatever we can */
4030 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004031 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004032 if (ret == 1)
4033 len2 = 0;
4034
4035 if (!ret || len1 + len2 < size) {
4036 /* FIXME: must normally never happen */
4037 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4038 goto end;
4039 }
4040
4041 /* limit len1/len2 to size */
4042 if (len1 + len2 > size) {
4043 int sub = len1 + len2 - size;
4044
4045 if (len2 > sub)
4046 len2 -= sub;
4047 else {
4048 sub -= len2;
4049 len2 = 0;
4050 len1 -= sub;
4051 }
4052 }
4053
4054 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004055 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004056 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004057 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004058
4059 send_empty:
4060 /* we may need to add END_STREAM */
4061 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4062 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004063 *
4064 * FIXME: what we do here is not correct because we send end_stream
4065 * before knowing if we'll have to send a HEADERS frame for the
4066 * trailers. More importantly we're not consuming the trailing CRLF
4067 * after the end of trailers, so it will be left to the caller to
4068 * eat it. The right way to do it would be to measure trailers here
4069 * and to send ES only if there are no trailers.
4070 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004071 */
4072 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004073 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004074 es_now = 1;
4075
4076 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004077 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004078
4079 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004080 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004081
4082 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004083 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004084
4085 /* consume incoming H1 response */
4086 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004087 max -= size;
4088 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004089 total += size;
4090 h1m->curr_len -= size;
4091 h2s->mws -= size;
4092 h2c->mws -= size;
4093
4094 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004095 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004096 goto new_frame;
4097 }
4098 }
4099
4100 if (es_now) {
4101 if (h2s->st == H2_SS_OPEN)
4102 h2s->st = H2_SS_HLOC;
4103 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004104 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004105
Willy Tarreau35a62702018-02-27 15:37:25 +01004106 if (!(h1m->flags & H1_MF_CHNK)) {
4107 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004108 total += max;
4109 ofs += max;
4110 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004111
Willy Tarreau801250e2018-09-11 11:45:04 +02004112 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004113 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004114
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004115 h2s->flags |= H2_SF_ES_SENT;
4116 }
4117
4118 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004119 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 +02004120 return total;
4121}
4122
Willy Tarreau115e83b2018-12-01 19:17:53 +01004123/* Try to send a HEADERS frame matching HTX response present in HTX message
4124 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4125 * must check the stream's status to detect any error which might have happened
4126 * subsequently to a successful send. The htx blocks are automatically removed
4127 * from the message. The htx message is assumed to be valid since produced from
4128 * the internal code, hence it contains a start line, an optional series of
4129 * header blocks and an end of header, otherwise an invalid frame could be
4130 * emitted and the resulting htx message could be left in an inconsistent state.
4131 */
4132static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4133{
4134 struct http_hdr list[MAX_HTTP_HDR];
4135 struct h2c *h2c = h2s->h2c;
4136 struct htx_blk *blk;
4137 struct htx_blk *blk_end;
4138 struct buffer outbuf;
4139 struct htx_sl *sl;
4140 enum htx_blk_type type;
4141 int es_now = 0;
4142 int ret = 0;
4143 int hdr;
4144 int idx;
4145
4146 if (h2c_mux_busy(h2c, h2s)) {
4147 h2s->flags |= H2_SF_BLK_MBUSY;
4148 return 0;
4149 }
4150
4151 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4152 h2c->flags |= H2_CF_MUX_MALLOC;
4153 h2s->flags |= H2_SF_BLK_MROOM;
4154 return 0;
4155 }
4156
4157 /* determine the first block which must not be deleted, blk_end may
4158 * be NULL if all blocks have to be deleted.
4159 */
4160 idx = htx_get_head(htx);
4161 blk_end = NULL;
4162 while (idx != -1) {
4163 type = htx_get_blk_type(htx_get_blk(htx, idx));
4164 idx = htx_get_next(htx, idx);
4165 if (type == HTX_BLK_EOH) {
4166 if (idx != -1)
4167 blk_end = htx_get_blk(htx, idx);
4168 break;
4169 }
4170 }
4171
4172 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004173 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004174 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004175 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004176 if (h2s->status < 100 || h2s->status > 999)
4177 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004178
4179 /* and the rest of the headers, that we dump starting at header 0 */
4180 hdr = 0;
4181
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004182 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004183 while ((idx = htx_get_next(htx, idx)) != -1) {
4184 blk = htx_get_blk(htx, idx);
4185 type = htx_get_blk_type(blk);
4186
4187 if (type == HTX_BLK_UNUSED)
4188 continue;
4189
4190 if (type != HTX_BLK_HDR)
4191 break;
4192
4193 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4194 goto fail;
4195
4196 list[hdr].n = htx_get_blk_name(htx, blk);
4197 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004198 hdr++;
4199 }
4200
4201 /* marker for end of headers */
4202 list[hdr].n = ist("");
4203
4204 if (h2s->status == 204 || h2s->status == 304) {
4205 /* no contents, claim c-len is present and set to zero */
4206 es_now = 1;
4207 }
4208
4209 chunk_reset(&outbuf);
4210
4211 while (1) {
4212 outbuf.area = b_tail(&h2c->mbuf);
4213 outbuf.size = b_contig_space(&h2c->mbuf);
4214 outbuf.data = 0;
4215
4216 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4217 break;
4218 realign_again:
4219 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4220 }
4221
4222 if (outbuf.size < 9)
4223 goto full;
4224
4225 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4226 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4227 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4228 outbuf.data = 9;
4229
4230 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004231 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004232 if (b_space_wraps(&h2c->mbuf))
4233 goto realign_again;
4234 goto full;
4235 }
4236
4237 /* encode all headers, stop at empty name */
4238 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4239 /* these ones do not exist in H2 and must be dropped. */
4240 if (isteq(list[hdr].n, ist("connection")) ||
4241 isteq(list[hdr].n, ist("proxy-connection")) ||
4242 isteq(list[hdr].n, ist("keep-alive")) ||
4243 isteq(list[hdr].n, ist("upgrade")) ||
4244 isteq(list[hdr].n, ist("transfer-encoding")))
4245 continue;
4246
4247 if (isteq(list[hdr].n, ist("")))
4248 break; // end
4249
4250 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4251 /* output full */
4252 if (b_space_wraps(&h2c->mbuf))
4253 goto realign_again;
4254 goto full;
4255 }
4256 }
4257
4258 /* we may need to add END_STREAM.
4259 * FIXME: we should also set it when we know for sure that the
4260 * content-length is zero as well as on 204/304
4261 */
4262 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4263 es_now = 1;
4264
4265 if (h2s->cs->flags & CS_FL_SHW)
4266 es_now = 1;
4267
4268 /* update the frame's size */
4269 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4270
4271 if (es_now)
4272 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4273
4274 /* commit the H2 response */
4275 b_add(&h2c->mbuf, outbuf.data);
4276 h2s->flags |= H2_SF_HEADERS_SENT;
4277
Willy Tarreau115e83b2018-12-01 19:17:53 +01004278 if (es_now) {
4279 h2s->flags |= H2_SF_ES_SENT;
4280 if (h2s->st == H2_SS_OPEN)
4281 h2s->st = H2_SS_HLOC;
4282 else
4283 h2s_close(h2s);
4284 }
4285
4286 /* OK we could properly deliver the response */
4287
4288 /* remove all header blocks including the EOH and compute the
4289 * corresponding size.
4290 *
4291 * FIXME: We should remove everything when es_now is set.
4292 */
4293 ret = 0;
4294 idx = htx_get_head(htx);
4295 blk = htx_get_blk(htx, idx);
4296 while (blk != blk_end) {
4297 ret += htx_get_blksz(blk);
4298 blk = htx_remove_blk(htx, blk);
4299 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004300
4301 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4302 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004303 end:
4304 return ret;
4305 full:
4306 h2c->flags |= H2_CF_MUX_MFULL;
4307 h2s->flags |= H2_SF_BLK_MROOM;
4308 ret = 0;
4309 goto end;
4310 fail:
4311 /* unparsable HTX messages, too large ones to be produced in the local
4312 * list etc go here (unrecoverable errors).
4313 */
4314 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4315 ret = 0;
4316 goto end;
4317}
4318
Willy Tarreau80739692018-10-05 11:35:57 +02004319/* Try to send a HEADERS frame matching HTX request present in HTX message
4320 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4321 * must check the stream's status to detect any error which might have happened
4322 * subsequently to a successful send. The htx blocks are automatically removed
4323 * from the message. The htx message is assumed to be valid since produced from
4324 * the internal code, hence it contains a start line, an optional series of
4325 * header blocks and an end of header, otherwise an invalid frame could be
4326 * emitted and the resulting htx message could be left in an inconsistent state.
4327 */
4328static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4329{
4330 struct http_hdr list[MAX_HTTP_HDR];
4331 struct h2c *h2c = h2s->h2c;
4332 struct htx_blk *blk;
4333 struct htx_blk *blk_end;
4334 struct buffer outbuf;
4335 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004336 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004337 enum htx_blk_type type;
4338 int es_now = 0;
4339 int ret = 0;
4340 int hdr;
4341 int idx;
4342
4343 if (h2c_mux_busy(h2c, h2s)) {
4344 h2s->flags |= H2_SF_BLK_MBUSY;
4345 return 0;
4346 }
4347
4348 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4349 h2c->flags |= H2_CF_MUX_MALLOC;
4350 h2s->flags |= H2_SF_BLK_MROOM;
4351 return 0;
4352 }
4353
4354 /* determine the first block which must not be deleted, blk_end may
4355 * be NULL if all blocks have to be deleted.
4356 */
4357 idx = htx_get_head(htx);
4358 blk_end = NULL;
4359 while (idx != -1) {
4360 type = htx_get_blk_type(htx_get_blk(htx, idx));
4361 idx = htx_get_next(htx, idx);
4362 if (type == HTX_BLK_EOH) {
4363 if (idx != -1)
4364 blk_end = htx_get_blk(htx, idx);
4365 break;
4366 }
4367 }
4368
4369 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004370 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004371 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004372 meth = htx_sl_req_meth(sl);
4373 path = htx_sl_req_uri(sl);
4374
4375 /* and the rest of the headers, that we dump starting at header 0 */
4376 hdr = 0;
4377
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004378 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004379 while ((idx = htx_get_next(htx, idx)) != -1) {
4380 blk = htx_get_blk(htx, idx);
4381 type = htx_get_blk_type(blk);
4382
4383 if (type == HTX_BLK_UNUSED)
4384 continue;
4385
4386 if (type != HTX_BLK_HDR)
4387 break;
4388
4389 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4390 goto fail;
4391
4392 list[hdr].n = htx_get_blk_name(htx, blk);
4393 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004394 hdr++;
4395 }
4396
4397 /* marker for end of headers */
4398 list[hdr].n = ist("");
4399
4400 chunk_reset(&outbuf);
4401
4402 while (1) {
4403 outbuf.area = b_tail(&h2c->mbuf);
4404 outbuf.size = b_contig_space(&h2c->mbuf);
4405 outbuf.data = 0;
4406
4407 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4408 break;
4409 realign_again:
4410 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4411 }
4412
4413 if (outbuf.size < 9)
4414 goto full;
4415
4416 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4417 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4418 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4419 outbuf.data = 9;
4420
4421 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004422 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004423 if (b_space_wraps(&h2c->mbuf))
4424 goto realign_again;
4425 goto full;
4426 }
4427
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004428 /* RFC7540 #8.3: the CONNECT method must have :
4429 * - :authority set to the URI part (host:port)
4430 * - :method set to CONNECT
4431 * - :scheme and :path omitted
4432 */
4433 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4434 /* encode the scheme which is always "https" (or 0x86 for "http") */
4435 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4436 /* output full */
4437 if (b_space_wraps(&h2c->mbuf))
4438 goto realign_again;
4439 goto full;
4440 }
Willy Tarreau80739692018-10-05 11:35:57 +02004441
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004442 /* encode the path, which necessarily is the second one */
4443 if (!hpack_encode_path(&outbuf, path)) {
4444 /* output full */
4445 if (b_space_wraps(&h2c->mbuf))
4446 goto realign_again;
4447 goto full;
4448 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004449
4450 /* look for the Host header and place it in :authority */
4451 auth = ist2(NULL, 0);
4452 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4453 if (isteq(list[hdr].n, ist("")))
4454 break; // end
4455
4456 if (isteq(list[hdr].n, ist("host"))) {
4457 auth = list[hdr].v;
4458 break;
4459 }
4460 }
4461 }
4462 else {
4463 /* for CONNECT, :authority is taken from the path */
4464 auth = path;
4465 }
4466
4467 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4468 /* output full */
4469 if (b_space_wraps(&h2c->mbuf))
4470 goto realign_again;
4471 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004472 }
4473
4474 /* encode all headers, stop at empty name */
4475 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4476 /* these ones do not exist in H2 and must be dropped. */
4477 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004478 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004479 isteq(list[hdr].n, ist("proxy-connection")) ||
4480 isteq(list[hdr].n, ist("keep-alive")) ||
4481 isteq(list[hdr].n, ist("upgrade")) ||
4482 isteq(list[hdr].n, ist("transfer-encoding")))
4483 continue;
4484
4485 if (isteq(list[hdr].n, ist("")))
4486 break; // end
4487
4488 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4489 /* output full */
4490 if (b_space_wraps(&h2c->mbuf))
4491 goto realign_again;
4492 goto full;
4493 }
4494 }
4495
4496 /* we may need to add END_STREAM if we have no body :
4497 * - request already closed, or :
4498 * - no transfer-encoding, and :
4499 * - no content-length or content-length:0
4500 * Fixme: this doesn't take into account CONNECT requests.
4501 */
4502 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4503 es_now = 1;
4504
4505 if (sl->flags & HTX_SL_F_BODYLESS)
4506 es_now = 1;
4507
4508 if (h2s->cs->flags & CS_FL_SHW)
4509 es_now = 1;
4510
4511 /* update the frame's size */
4512 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4513
4514 if (es_now)
4515 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4516
4517 /* commit the H2 response */
4518 b_add(&h2c->mbuf, outbuf.data);
4519 h2s->flags |= H2_SF_HEADERS_SENT;
4520 h2s->st = H2_SS_OPEN;
4521
Willy Tarreau80739692018-10-05 11:35:57 +02004522 if (es_now) {
4523 // trim any possibly pending data (eg: inconsistent content-length)
4524 h2s->flags |= H2_SF_ES_SENT;
4525 h2s->st = H2_SS_HLOC;
4526 }
4527
4528 /* remove all header blocks including the EOH and compute the
4529 * corresponding size.
4530 *
4531 * FIXME: We should remove everything when es_now is set.
4532 */
4533 ret = 0;
4534 idx = htx_get_head(htx);
4535 blk = htx_get_blk(htx, idx);
4536 while (blk != blk_end) {
4537 ret += htx_get_blksz(blk);
4538 blk = htx_remove_blk(htx, blk);
4539 }
4540
4541 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4542 htx_remove_blk(htx, blk_end);
4543
4544 end:
4545 return ret;
4546 full:
4547 h2c->flags |= H2_CF_MUX_MFULL;
4548 h2s->flags |= H2_SF_BLK_MROOM;
4549 ret = 0;
4550 goto end;
4551 fail:
4552 /* unparsable HTX messages, too large ones to be produced in the local
4553 * list etc go here (unrecoverable errors).
4554 */
4555 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4556 ret = 0;
4557 goto end;
4558}
4559
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004560/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004561 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4562 * caller must check the stream's status to detect any error which might have
4563 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004564 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4565 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004566static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004567{
4568 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004569 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004570 struct buffer outbuf;
4571 size_t total = 0;
4572 int es_now = 0;
4573 int bsize; /* htx block size */
4574 int fsize; /* h2 frame size */
4575 struct htx_blk *blk;
4576 enum htx_blk_type type;
4577 int idx;
4578
4579 if (h2c_mux_busy(h2c, h2s)) {
4580 h2s->flags |= H2_SF_BLK_MBUSY;
4581 goto end;
4582 }
4583
4584 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4585 h2c->flags |= H2_CF_MUX_MALLOC;
4586 h2s->flags |= H2_SF_BLK_MROOM;
4587 goto end;
4588 }
4589
Willy Tarreau98de12a2018-12-12 07:03:00 +01004590 htx = htx_from_buf(buf);
4591
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004592 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4593 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4594 * the caller to handle.
4595 */
4596
4597 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004598 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004599 goto end;
4600
4601 idx = htx_get_head(htx);
4602 blk = htx_get_blk(htx, idx);
4603 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4604 bsize = htx_get_blksz(blk);
4605 fsize = bsize;
4606
4607 if (type == HTX_BLK_EOD) {
4608 /* if we have an EOD, we're dealing with chunked data. We may
4609 * have a set of trailers after us that the caller will want to
4610 * deal with. Let's simply remove the EOD and return.
4611 */
4612 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004613 total++; // EOD counts as one byte
4614 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004615 goto end;
4616 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004617 else if (type == HTX_BLK_EOM) {
4618 if (h2s->flags & H2_SF_ES_SENT) {
4619 /* ES already sent */
4620 htx_remove_blk(htx, blk);
4621 total++; // EOM counts as one byte
4622 count--;
4623 goto end;
4624 }
4625 }
4626 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004627 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004628
4629 /* Perform some optimizations to reduce the number of buffer copies.
4630 * First, if the mux's buffer is empty and the htx area contains
4631 * exactly one data block of the same size as the requested count, and
4632 * this count fits within the frame size, the stream's window size, and
4633 * the connection's window size, then it's possible to simply swap the
4634 * caller's buffer with the mux's output buffer and adjust offsets and
4635 * length to match the entire DATA HTX block in the middle. In this
4636 * case we perform a true zero-copy operation from end-to-end. This is
4637 * the situation that happens all the time with large files. Second, if
4638 * this is not possible, but the mux's output buffer is empty, we still
4639 * have an opportunity to avoid the copy to the intermediary buffer, by
4640 * making the intermediary buffer's area point to the output buffer's
4641 * area. In this case we want to skip the HTX header to make sure that
4642 * copies remain aligned and that this operation remains possible all
4643 * the time. This goes for headers, data blocks and any data extracted
4644 * from the HTX blocks.
4645 */
4646 if (unlikely(fsize == count &&
4647 htx->used == 1 && type == HTX_BLK_DATA &&
4648 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4649 void *old_area = h2c->mbuf.area;
4650
4651 if (b_data(&h2c->mbuf)) {
4652 /* too bad there are data left there. If we have less
4653 * than 1/4 of the mbuf's size and everything fits,
4654 * we'll perform a copy anyway. Otherwise we'll pretend
4655 * the mbuf is full and wait.
4656 */
4657 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4658 goto copy;
4659 h2c->flags |= H2_CF_MUX_MFULL;
4660 h2s->flags |= H2_SF_BLK_MROOM;
4661 goto end;
4662 }
4663
4664 /* map an H2 frame to the HTX block so that we can put the
4665 * frame header there.
4666 */
4667 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004668 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004669 h2c->mbuf.data = fsize + 9;
4670 outbuf.area = b_head(&h2c->mbuf);
4671
4672 /* prepend an H2 DATA frame header just before the DATA block */
4673 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4674 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4675 h2_set_frame_size(outbuf.area, fsize);
4676
4677 /* update windows */
4678 h2s->mws -= fsize;
4679 h2c->mws -= fsize;
4680
4681 /* and exchange with our old area */
4682 buf->area = old_area;
4683 buf->data = buf->head = 0;
4684 total += fsize;
4685 goto end;
4686 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004687
Willy Tarreau98de12a2018-12-12 07:03:00 +01004688 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004689 /* for DATA and EOM we'll have to emit a frame, even if empty */
4690
4691 while (1) {
4692 outbuf.area = b_tail(&h2c->mbuf);
4693 outbuf.size = b_contig_space(&h2c->mbuf);
4694 outbuf.data = 0;
4695
4696 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4697 break;
4698 realign_again:
4699 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4700 }
4701
4702 if (outbuf.size < 9) {
4703 h2c->flags |= H2_CF_MUX_MFULL;
4704 h2s->flags |= H2_SF_BLK_MROOM;
4705 goto end;
4706 }
4707
4708 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4709 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4710 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4711 outbuf.data = 9;
4712
4713 /* we have in <fsize> the exact number of bytes we need to copy from
4714 * the HTX buffer. We need to check this against the connection's and
4715 * the stream's send windows, and to ensure that this fits in the max
4716 * frame size and in the buffer's available space minus 9 bytes (for
4717 * the frame header). The connection's flow control is applied last so
4718 * that we can use a separate list of streams which are immediately
4719 * unblocked on window opening. Note: we don't implement padding.
4720 */
4721
4722 /* EOM is presented with bsize==1 but would lead to the emission of an
4723 * empty frame, thus we force it to zero here.
4724 */
4725 if (type == HTX_BLK_EOM)
4726 bsize = fsize = 0;
4727
4728 if (!fsize)
4729 goto send_empty;
4730
4731 if (h2s->mws <= 0) {
4732 h2s->flags |= H2_SF_BLK_SFCTL;
4733 if (h2s->send_wait) {
4734 LIST_DEL(&h2s->list);
4735 LIST_INIT(&h2s->list);
4736 }
4737 goto end;
4738 }
4739
Willy Tarreauee573762018-12-04 15:25:57 +01004740 if (fsize > count)
4741 fsize = count;
4742
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004743 if (fsize > h2s->mws)
4744 fsize = h2s->mws; // >0
4745
4746 if (h2c->mfs && fsize > h2c->mfs)
4747 fsize = h2c->mfs; // >0
4748
4749 if (fsize + 9 > outbuf.size) {
4750 /* we have an opportunity for enlarging the too small
4751 * available space, let's try.
4752 * FIXME: is this really interesting to do? Maybe we'll
4753 * spend lots of time realigning instead of using two
4754 * frames.
4755 */
4756 if (b_space_wraps(&h2c->mbuf))
4757 goto realign_again;
4758 fsize = outbuf.size - 9;
4759
4760 if (fsize <= 0) {
4761 /* no need to send an empty frame here */
4762 h2c->flags |= H2_CF_MUX_MFULL;
4763 h2s->flags |= H2_SF_BLK_MROOM;
4764 goto end;
4765 }
4766 }
4767
4768 if (h2c->mws <= 0) {
4769 h2s->flags |= H2_SF_BLK_MFCTL;
4770 goto end;
4771 }
4772
4773 if (fsize > h2c->mws)
4774 fsize = h2c->mws;
4775
4776 /* now let's copy this this into the output buffer */
4777 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004778 h2s->mws -= fsize;
4779 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004780 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004781
4782 send_empty:
4783 /* update the frame's size */
4784 h2_set_frame_size(outbuf.area, fsize);
4785
4786 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4787 * meeting EOM. We should optimize this later.
4788 */
4789 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004790 total++; // EOM counts as one byte
4791 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004792 es_now = 1;
4793 }
4794
4795 if (es_now)
4796 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4797
4798 /* commit the H2 response */
4799 b_add(&h2c->mbuf, fsize + 9);
4800
4801 /* consume incoming HTX block, including EOM */
4802 total += fsize;
4803 if (fsize == bsize) {
4804 htx_remove_blk(htx, blk);
4805 if (fsize)
4806 goto new_frame;
4807 } else {
4808 /* we've truncated this block */
4809 htx_cut_data_blk(htx, blk, fsize);
4810 }
4811
4812 if (es_now) {
4813 if (h2s->st == H2_SS_OPEN)
4814 h2s->st = H2_SS_HLOC;
4815 else
4816 h2s_close(h2s);
4817
4818 h2s->flags |= H2_SF_ES_SENT;
4819 }
4820
4821 end:
4822 return total;
4823}
4824
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004825/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4826 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4827 * processed. The caller must check the stream's status to detect any error
4828 * which might have happened subsequently to a successful send. The htx blocks
4829 * are automatically removed from the message. The htx message is assumed to be
4830 * valid since produced from the internal code. Processing stops when meeting
4831 * the EOM, which is also removed. All trailers are processed at once and sent
4832 * as a single frame. The ES flag is always set.
4833 */
4834static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4835{
4836 struct http_hdr list[MAX_HTTP_HDR];
4837 struct h2c *h2c = h2s->h2c;
4838 struct htx_blk *blk;
4839 struct htx_blk *blk_end;
4840 struct buffer outbuf;
4841 struct h1m h1m;
4842 enum htx_blk_type type;
4843 uint32_t size;
4844 int ret = 0;
4845 int hdr;
4846 int idx;
4847 void *start;
4848
4849 if (h2c_mux_busy(h2c, h2s)) {
4850 h2s->flags |= H2_SF_BLK_MBUSY;
4851 goto end;
4852 }
4853
4854 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4855 h2c->flags |= H2_CF_MUX_MALLOC;
4856 h2s->flags |= H2_SF_BLK_MROOM;
4857 goto end;
4858 }
4859
4860 /* The principle is that we parse each and every trailers block using
4861 * the H1 headers parser, and append it to the list. We don't proceed
4862 * until EOM is met. blk_end will point to the EOM block.
4863 */
4864 hdr = 0;
4865 memset(list, 0, sizeof(list));
4866 blk_end = NULL;
4867
4868 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4869 blk = htx_get_blk(htx, idx);
4870 type = htx_get_blk_type(blk);
4871
4872 if (type == HTX_BLK_UNUSED)
4873 continue;
4874
4875 if (type != HTX_BLK_TLR) {
4876 if (type == HTX_BLK_EOM)
4877 blk_end = blk;
4878 break;
4879 }
4880
4881 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4882 goto fail;
4883
4884 size = htx_get_blksz(blk);
4885 start = htx_get_blk_ptr(htx, blk);
4886
4887 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4888 h1m.err_pos = 0;
4889 ret = h1_headers_to_hdr_list(start, start + size,
4890 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4891 &h1m, NULL);
4892 if (ret < 0)
4893 goto fail;
4894
4895 /* ret == 0 if an incomplete trailers block was found (missing
4896 * empty line), or > 0 if it was found. We have to continue on
4897 * incomplete messages because the trailers block might be
4898 * incomplete.
4899 */
4900
4901 /* search the new end */
4902 while (hdr <= sizeof(list)/sizeof(list[0])) {
4903 if (!list[hdr].n.len)
4904 break;
4905 hdr++;
4906 }
4907 }
4908
4909 if (!blk_end)
4910 goto end; // end not found yet
4911
4912 if (!hdr)
4913 goto done;
4914
4915 chunk_reset(&outbuf);
4916
4917 while (1) {
4918 outbuf.area = b_tail(&h2c->mbuf);
4919 outbuf.size = b_contig_space(&h2c->mbuf);
4920 outbuf.data = 0;
4921
4922 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4923 break;
4924 realign_again:
4925 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4926 }
4927
4928 if (outbuf.size < 9)
4929 goto full;
4930
4931 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4932 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4933 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4934 outbuf.data = 9;
4935
4936 /* encode status, which necessarily is the first one */
4937 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
4938 if (b_space_wraps(&h2c->mbuf))
4939 goto realign_again;
4940 goto full;
4941 }
4942
4943 /* encode all headers */
4944 for (idx = 0; idx < hdr; idx++) {
4945 /* these ones do not exist in H2 or must not appear in
4946 * trailers and must be dropped.
4947 */
4948 if (isteq(list[idx].n, ist("host")) ||
4949 isteq(list[idx].n, ist("content-length")) ||
4950 isteq(list[idx].n, ist("connection")) ||
4951 isteq(list[idx].n, ist("proxy-connection")) ||
4952 isteq(list[idx].n, ist("keep-alive")) ||
4953 isteq(list[idx].n, ist("upgrade")) ||
4954 isteq(list[idx].n, ist("te")) ||
4955 isteq(list[idx].n, ist("transfer-encoding")))
4956 continue;
4957
4958 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
4959 /* output full */
4960 if (b_space_wraps(&h2c->mbuf))
4961 goto realign_again;
4962 goto full;
4963 }
4964 }
4965
4966 /* update the frame's size */
4967 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4968
4969 /* commit the H2 response */
4970 b_add(&h2c->mbuf, outbuf.data);
4971 h2s->flags |= H2_SF_ES_SENT;
4972
4973 if (h2s->st == H2_SS_OPEN)
4974 h2s->st = H2_SS_HLOC;
4975 else
4976 h2s_close(h2s);
4977
4978 /* OK we could properly deliver the response */
4979 done:
4980 /* remove all header blocks including EOM and compute the corresponding size. */
4981 ret = 0;
4982 idx = htx_get_head(htx);
4983 blk = htx_get_blk(htx, idx);
4984 while (blk != blk_end) {
4985 ret += htx_get_blksz(blk);
4986 blk = htx_remove_blk(htx, blk);
4987 }
4988 blk = htx_remove_blk(htx, blk);
4989 end:
4990 return ret;
4991 full:
4992 h2c->flags |= H2_CF_MUX_MFULL;
4993 h2s->flags |= H2_SF_BLK_MROOM;
4994 ret = 0;
4995 goto end;
4996 fail:
4997 /* unparsable HTX messages, too large ones to be produced in the local
4998 * list etc go here (unrecoverable errors).
4999 */
5000 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5001 ret = 0;
5002 goto end;
5003}
5004
Olivier Houchard6ff20392018-07-17 18:46:31 +02005005/* Called from the upper layer, to subscribe to events, such as being able to send */
5006static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5007{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005008 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005009 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005010 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005011
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005012 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005013 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005014 if (!(sw->events & SUB_RETRY_RECV)) {
5015 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005016 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005017 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005018 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005019 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005020 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005021 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005022 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005023 if (!(sw->events & SUB_RETRY_SEND)) {
5024 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005025 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005026 h2s->send_wait = sw;
5027 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5028 if (h2s->flags & H2_SF_BLK_MFCTL)
5029 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5030 else
5031 LIST_ADDQ(&h2c->send_list, &h2s->list);
5032 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005033 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005034 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005035 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005036 if (event_type != 0)
5037 return -1;
5038 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005039
5040
5041}
5042
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005043static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5044{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005045 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005046 struct h2s *h2s = cs->ctx;
5047
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005048 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005049 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005050 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005051 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005052 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005053 }
5054 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005055 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005056 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005057 if (h2s->send_wait == sw) {
5058 LIST_DEL(&h2s->list);
5059 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005060 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005061 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005062 }
5063 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005064 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5065 sw = param;
5066 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005067 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005068 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005069 LIST_DEL(&h2s->list);
5070 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005071 }
5072 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005073 return 0;
5074}
5075
5076
Olivier Houchard511efea2018-08-16 15:30:32 +02005077/* Called from the upper layer, to receive data */
5078static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5079{
Olivier Houchard638b7992018-08-16 15:41:52 +02005080 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005081 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005082 struct htx *h2s_htx = NULL;
5083 struct htx *buf_htx = NULL;
5084 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005085 size_t ret = 0;
5086
5087 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005088 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5089 /* in HTX mode we ignore the count argument */
5090 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005091 if (htx_is_empty(h2s_htx)) {
5092 if (cs->flags & CS_FL_REOS)
5093 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005094 if (cs->flags & CS_FL_ERR_PENDING)
5095 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01005096 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005097 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005098
5099 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005100 count = htx_free_data_space(buf_htx);
5101 if (flags & CO_RFL_KEEP_RSV) {
5102 if (count <= global.tune.maxrewrite)
5103 goto end;
5104 count -= global.tune.maxrewrite;
5105 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005106
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005107 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01005108
5109 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005110 htx_to_buf(buf_htx, buf);
5111 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005112 ret = htx_ret.ret;
5113 }
5114 else {
5115 ret = b_xfer(buf, &h2s->rxbuf, count);
5116 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005117
Olivier Houchard638b7992018-08-16 15:41:52 +02005118 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005119 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005120 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005121 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005122 if (cs->flags & CS_FL_REOS)
5123 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005124 if (cs->flags & CS_FL_ERR_PENDING)
5125 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005126 if (b_size(&h2s->rxbuf)) {
5127 b_free(&h2s->rxbuf);
5128 offer_buffers(NULL, tasks_run_queue);
5129 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005130 }
5131
Willy Tarreau082f5592018-11-25 08:03:32 +01005132 if (ret && h2c->dsi == h2s->id) {
5133 /* demux is blocking on this stream's buffer */
5134 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005135 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005136 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005137end:
Olivier Houchard511efea2018-08-16 15:30:32 +02005138 return ret;
5139}
5140
Olivier Houchardd846c262018-10-19 17:24:29 +02005141static void h2_stop_senders(struct h2c *h2c)
5142{
5143 struct h2s *h2s, *h2s_back;
5144
5145 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5146 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5147 if (h2c->msi == h2s_id(h2s))
5148 continue;
5149 LIST_DEL(&h2s->list);
5150 LIST_INIT(&h2s->list);
5151 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005152 h2s->send_wait->events |= SUB_RETRY_SEND;
5153 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005154 LIST_ADD(&h2c->send_list, &h2s->list);
5155 }
5156}
5157
Willy Tarreau62f52692017-10-08 23:01:42 +02005158/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005159static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005160{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005161 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005162 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005163 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005164 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005165 struct htx *htx;
5166 struct htx_blk *blk;
5167 enum htx_blk_type btype;
5168 uint32_t bsize;
5169 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005170
Olivier Houchardd846c262018-10-19 17:24:29 +02005171 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005172 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005173 h2s->send_wait = NULL;
5174 LIST_DEL(&h2s->list);
5175 LIST_INIT(&h2s->list);
5176 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005177 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5178 return 0;
5179
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005180 /* htx will be enough to decide if we're using HTX or legacy */
5181 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5182
Willy Tarreau0bad0432018-06-14 16:54:01 +02005183 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005184 h2s->flags |= H2_SF_OUTGOING_DATA;
5185
Willy Tarreau751f2d02018-10-05 09:35:00 +02005186 if (h2s->id == 0) {
5187 int32_t id = h2c_get_next_sid(h2s->h2c);
5188
5189 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005190 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005191 return 0;
5192 }
5193
5194 eb32_delete(&h2s->by_id);
5195 h2s->by_id.key = h2s->id = id;
5196 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005197 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005198 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5199 }
5200
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005201 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005202 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5203 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005204 idx = htx_get_head(htx);
5205 blk = htx_get_blk(htx, idx);
5206 btype = htx_get_blk_type(blk);
5207 bsize = htx_get_blksz(blk);
5208
5209 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005210 case HTX_BLK_REQ_SL:
5211 /* start-line before headers */
5212 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5213 if (ret > 0) {
5214 total += ret;
5215 count -= ret;
5216 if (ret < bsize)
5217 goto done;
5218 }
5219 break;
5220
Willy Tarreau115e83b2018-12-01 19:17:53 +01005221 case HTX_BLK_RES_SL:
5222 /* start-line before headers */
5223 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5224 if (ret > 0) {
5225 total += ret;
5226 count -= ret;
5227 if (ret < bsize)
5228 goto done;
5229 }
5230 break;
5231
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005232 case HTX_BLK_DATA:
5233 case HTX_BLK_EOD:
5234 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005235 /* all these cause the emission of a DATA frame (possibly empty).
5236 * This EOM necessarily is one before trailers, as the EOM following
5237 * trailers would have been consumed by the trailers parser.
5238 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005239 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005240 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005241 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005242 total += ret;
5243 count -= ret;
5244 if (ret < bsize)
5245 goto done;
5246 }
5247 break;
5248
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005249 case HTX_BLK_TLR:
5250 /* This is the first trailers block, all the subsequent ones AND
5251 * the EOM will be swallowed by the parser.
5252 */
5253 ret = h2s_htx_make_trailers(h2s, htx);
5254 if (ret > 0) {
5255 total += ret;
5256 count -= ret;
5257 if (ret < bsize)
5258 goto done;
5259 }
5260 break;
5261
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005262 default:
5263 htx_remove_blk(htx, blk);
5264 total += bsize;
5265 count -= bsize;
5266 break;
5267 }
5268 }
5269 goto done;
5270 }
5271
5272 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005273 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005274 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005275 if (h2s->h2c->flags & H2_CF_IS_BACK)
5276 ret = -1;
5277 else
5278 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005279 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005280 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005281 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005282 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005283 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005284 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005285 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005286
Willy Tarreau5dd17352018-06-14 13:33:30 +02005287 if (unlikely((int)ret <= 0)) {
5288 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005289 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5290 break;
5291 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005292 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005293 total += count;
5294 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005295 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005296 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005297 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005298 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005299 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005300 break;
5301 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005302
5303 total += ret;
5304 count -= ret;
5305
5306 if (h2s->st >= H2_SS_ERROR)
5307 break;
5308
5309 if (h2s->flags & H2_SF_BLK_ANY)
5310 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005311 }
5312
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005313 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005314 if (h2s->st >= H2_SS_ERROR) {
5315 /* trim any possibly pending data after we close (extra CR-LF,
5316 * unprocessed trailers, abnormal extra data, ...)
5317 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005318 total += count;
5319 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005320 }
5321
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005322 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005323 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005324 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005325 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005326 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005327 }
5328
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005329 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005330 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005331 } else {
5332 b_del(buf, total);
5333 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005334
5335 /* The mux is full, cancel the pending tasks */
5336 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5337 (h2s->flags & H2_SF_BLK_MBUSY))
5338 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005339
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005340 /* If we're running HTX, and we read the whole buffer, then pretend
5341 * we read exactly what the caller specified, as with HTX the caller
5342 * will always give the buffer size, instead of the amount of data
5343 * available.
5344 */
5345 if (htx && !b_data(buf))
5346 total = orig_count;
5347
Olivier Houchard7505f942018-08-21 18:10:44 +02005348 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005349 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005350 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005351
Olivier Houchard7505f942018-08-21 18:10:44 +02005352 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005353 /* If we're waiting for flow control, and we got a shutr on the
5354 * connection, we will never be unlocked, so add an error on
5355 * the conn_stream.
5356 */
5357 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5358 !b_data(&h2s->h2c->dbuf) &&
5359 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5360 if (cs->flags & CS_FL_EOS)
5361 cs->flags |= CS_FL_ERROR;
5362 else
5363 cs->flags |= CS_FL_ERR_PENDING;
5364 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005365 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005366}
5367
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005368/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005369static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005370{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005371 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005372 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005373 struct eb32_node *node;
5374 int fctl_cnt = 0;
5375 int send_cnt = 0;
5376 int tree_cnt = 0;
5377 int orph_cnt = 0;
5378
5379 if (!h2c)
5380 return;
5381
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005382 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005383 fctl_cnt++;
5384
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005385 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005386 send_cnt++;
5387
Willy Tarreau3af37712018-12-18 14:34:41 +01005388 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005389 node = eb32_first(&h2c->streams_by_id);
5390 while (node) {
5391 h2s = container_of(node, struct h2s, by_id);
5392 tree_cnt++;
5393 if (!h2s->cs)
5394 orph_cnt++;
5395 node = eb32_next(node);
5396 }
5397
Willy Tarreau987c0632018-12-18 10:32:05 +01005398 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5399 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5400 " .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 +02005401 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5402 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005403 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005404 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5405 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5406 h2c->msi,
5407 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5408 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5409
5410 if (h2s) {
5411 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5412 h2s, h2s->id, h2s->flags,
5413 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5414 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5415 h2s->cs);
5416 if (h2s->cs)
5417 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5418 h2s->cs->flags, h2s->cs->data);
5419 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005420}
Willy Tarreau62f52692017-10-08 23:01:42 +02005421
5422/*******************************************************/
5423/* functions below are dedicated to the config parsers */
5424/*******************************************************/
5425
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005426/* config parser for global "tune.h2.header-table-size" */
5427static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5428 struct proxy *defpx, const char *file, int line,
5429 char **err)
5430{
5431 if (too_many_args(1, args, err, NULL))
5432 return -1;
5433
5434 h2_settings_header_table_size = atoi(args[1]);
5435 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5436 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5437 return -1;
5438 }
5439 return 0;
5440}
Willy Tarreau62f52692017-10-08 23:01:42 +02005441
Willy Tarreaue6baec02017-07-27 11:45:11 +02005442/* config parser for global "tune.h2.initial-window-size" */
5443static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5444 struct proxy *defpx, const char *file, int line,
5445 char **err)
5446{
5447 if (too_many_args(1, args, err, NULL))
5448 return -1;
5449
5450 h2_settings_initial_window_size = atoi(args[1]);
5451 if (h2_settings_initial_window_size < 0) {
5452 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5453 return -1;
5454 }
5455 return 0;
5456}
5457
Willy Tarreau5242ef82017-07-27 11:47:28 +02005458/* config parser for global "tune.h2.max-concurrent-streams" */
5459static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5460 struct proxy *defpx, const char *file, int line,
5461 char **err)
5462{
5463 if (too_many_args(1, args, err, NULL))
5464 return -1;
5465
5466 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005467 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005468 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5469 return -1;
5470 }
5471 return 0;
5472}
5473
Willy Tarreau62f52692017-10-08 23:01:42 +02005474
5475/****************************************/
5476/* MUX initialization and instanciation */
5477/***************************************/
5478
5479/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005480static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005481 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005482 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005483 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005484 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005485 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005486 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005487 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005488 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005489 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005490 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005491 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005492 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005493 .shutr = h2_shutr,
5494 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005495 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005496 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005497 .name = "H2",
5498};
5499
Christopher Faulet32f61c02018-04-10 14:33:41 +02005500/* PROTO selection : this mux registers PROTO token "h2" */
5501static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005502 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005503
Willy Tarreau0108d902018-11-25 19:14:37 +01005504INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5505
Willy Tarreauf8957272018-10-03 10:25:20 +02005506static struct mux_proto_list mux_proto_h2_htx =
5507 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5508
5509INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5510
Willy Tarreau62f52692017-10-08 23:01:42 +02005511/* config keyword parsers */
5512static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005513 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005514 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005515 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005516 { 0, NULL, NULL }
5517}};
5518
Willy Tarreau0108d902018-11-25 19:14:37 +01005519INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);